---
title: "使用supervisor + cesi管理/守护进程"
author: "tinyhema"
date: "2018-09-19T00:35:46Z"
tags: []
categories: ["开源软件"]
slug: "supervisor-cesi"
draft: false
lastmod: "2018-09-22T16:42:14Z"
id: 2430879
url: "https://tinyhema.iteye.com/blog/2430879"
---

背景
有多台服务器，每台服务器需要管理/守护多个进程（进程crash后立即重启）

服务器  CentOS 7.3 x64
supervisor版本  3.1.4
cesi版本  1.0


选择方案
守护进程的方案选择
1. crontab方案
优点：比较简单，不需要安装额外的软件
缺点：全手工管理，易出错，服务器、进程多了以后管理成本高

2. supervisor方案
优点：可以集中管理所有的进程，配合cesi可管理集群
缺点：需要额外安装软件，引入新的依赖，supervisor进程挂掉的情况下被守护的进程也会挂掉

考虑到维护管理成本，选择使用supervisor方案，为了方便的管理多台，引入cesi来管理supervisor集群

安装supervisor

[supervisor介绍](https://github.com/Supervisor/supervisor)

安装过程比较简单
[参考](https://blog.csdn.net/DongGeGe214/article/details/80264811)


```shell

# 安装epel源
$ yum install epel-release
# 安装supervisor
$ yum install -y supervisor

$ systemctl enable supervisord   # 开机自启动
# systemctl start supervisord     # 启动supervisord服务

```


常用命令

```shell

$ systemctl stop supervisord   # 停止服务
$ systemctl start supervisord  # 启动服务
$ systemctl status supervisord # 查看服务状态
$ systemctl reload supervisord   # 重新加载配置文件
$ systemctl restart supervisord   # 重启服务

```


supervisorctl是用来管理supervisord进程的管理工具

```shell

supervisorctl  reload   # 重启supervisord进程，会导致所有被守护的进程重启
supervisorctl  update  # 更新配置文件，如果配置文件有变更（新增、修改、减少被守护进程），可使用此命令重新加载配置文件

```


问题记录
如果使用pip安装，在设置启动脚本的时候，需要注意一下，systemctl start supervisord后会报错，实际上应用已经正常启动，原因是教程中的systemctl配置文件有一处错误，需要去掉PIDFile项，supervisord有自己的pid存放位置，[参考官方的启动脚本](https://github.com/Supervisor/initscripts/blob/master/centos-systemd-etcs)

配置supervisor

```shell

# vim /etc/supervisord.conf

# 打开http服务
# 修改 [inet_http_server] 节点，去掉注释，修改ip、端口、用户名、密码
[inet_http_server]         ; inet (TCP) server disabled by default
port=127.0.0.1:9001        ; ip_address:port specifier, *:port for all iface
;username=user              ; default is no username (open server)
;password=123               ; default is no password (open server)

...

# 打开配置文件夹
[include]
files = /etc/supervisord.d/*.ini


```


reload进程即可

```shell

supervisorctl reload

```


至此，supervisor配置完成

配置nginx反向代理
由于服务商的安全策略，9001端口不能提供http服务，使用nginx配置一个反向代理

```shell

# vi /path/to/nginx/vhost/supervisor.conf

server{ 
    charset utf-8;
    
    listen 80;
    server_name supervisor.domain.com;

    access_log  $log_root/supervisor.log;
    error_log $log_root/supervisor_error.log;

    location / {
        proxy_pass http://127.0.0.1:9001;
    }
}

```


通过supervisor.domain.com即可访问supervisor的管理页面


cesi安装与配置
supervisor自带的管理界面略有一点小问题，在nginx反向代理后，点击页面上的功能链接，还是会自动跳到127.0.0.1:9001路径下，导致功能不可用，因此引入cesi来作supervisor的管理界面，同时也可以管理集群，一举多得

[cesi介绍](https://github.com/gamegos/cesi)

安装cesi
[参考文档](https://blog.csdn.net/qianghaohao/article/details/81146199)

```shell

yum install git python-flask
git clone https://github.com/gamegos/cesi /opt/cesi
sqlite3 /opt/cesi/userinfo.db < /opt/cesi/userinfo.sql
ln -s  /opt/cesi/defaults/cesi.conf /etc/cesi.conf  # 配置文件软链接

# 作为服务运行
cp /opt/cesi/defaults/cesi.service /etc/systemd/system/cesi.service
systemctl daemon-reload
systemctl start cesi


```


配置cesi

```shell

# vi /etc/cesi.conf
# 配置节点，与上一步配置的用户、端口一致
[node:first]
username = abc
password = 123
host = 127.0.0.1
port = 9001

# 配置cesi自身
[cesi]
database = /opt/cesi/userinfo.db
activity_log = /opt/cesi/activity.log
host = 127.0.0.1  # 仅允许本地的连接


```


重启cesi

```shell

systemctl restart cesi

```


配置nginx反向代理

```shell

# vi /path/to/nginx/vhost/cesi.conf

server{ 
    charset utf-8;
    
    listen 80;
    server_name cesi.domain.com;

    access_log  $log_root/cesi.log;
    error_log $log_root/cesi_error.log;

    location / {
        proxy_pass http://127.0.0.1:5000;  # cesi默认端口
    }
}

```


使用cesi.domain.com即可访问cesi，配置完成。