supervisor: supervisor安装和gunicorn部署


安装supervisor和gunicorn

  1. 如果用的是阿里云的CentOS7会提示找不到supervisor,则yum install epel-release先安装EPEL源
    yum install -y supervisor

  2. 安装gunicorn
    /data/anaconda37/bin/pip install gunicorn
    模仿一个写法其中Python37路径是:
    /data/anaconda3/bin/python,
    /data/anaconda3/bin/pip install gunicorn
    Successfully installed gunicorn-20.0.4
    目录应该在/data/anaconda3/bin/gunicorn, 后面配置文件会用到命令的目录

一般使用配置文件目录以及其解释

  1. supervisord
    负责管理进程的server端,配置文件: /etc/supervisor/supervisord.conf或者在/etc/supervisord.conf
    修改/etc/supervisord.conf的最后一行.ini为.conf

  2. supervisorctl
    client端的命令行工具,管理子进程,配置文件: /etc/supervisor/supervisord.d/目录下
    这目录下又建立了supervisor.conf的配置文件, 把程序配置写入

注意以上文件是 supervisord和supervisor的区别

设置开机自启动

systemctl enable supervisord

系统启动supervisor

  1. systemctl start supervisord
  2. supervisord -c /etc/supervisord.conf

查看启动状态

systemctl status supervisord
● supervisord.service - Process Monitoring and Control Daemon
   Loaded: loaded (/usr/lib/systemd/system/supervisord.service; enabled; vendor preset: disabled)
   Active: active (running) since Thu 2020-05-07 18:22:50 CST; 14s ago
  Process: 49802 ExecStart=/usr/bin/supervisord -c /etc/supervisord.conf (code=exited, status=0/SUCCESS)
 Main PID: 49805 (supervisord)
   CGroup: /system.slice/supervisord.service
           └─49805 /usr/bin/python /usr/bin/supervisord -c /etc/supervisord.conf

May 07 18:22:50 yzsjhl-evdc1-31.opi.com systemd[1]: Starting Process Monitoring and Control Daemon...
May 07 18:22:50 yzsjhl-evdc1-31.opi.com systemd[1]: Started Process Monitoring and Control Daemon.

修改配置文件开启web界面访问监控程序

vim /etc/supervisord.conf
把inet_http_server模块的注释去并修改IP、用户名与密码,如下

[inet_http_server]
port=0.0.0.0:9001
username=root 
password=root

重新载入配置文件supervisorctl reload

开启Supervisor默认的9001端口

firewall-cmd --zone=public --add-port=9001/tcp --permanent
firewall-cmd --reload 

访问web页面(我的机器ip是10.4.1.31):http://10.4.1.31:9001, , 可以看到你部署东西的状态

查看配置文件最后一个模块

还是上面那个文件
cat /etc/supervisord.conf
配置需要管理的进程也可修改为files = supervisord.d/*.conf后缀,目录在/etc/supervisord.d/下面

[include]
files = supervisord.d/*.conf

编辑supervisor.conf配置文件

gunicorn文件gunicorn_config.py

bind = '10.16.1.31:18888'
log_level = "debug"
worker_class = "sanic.worker.GunicornWorker"
workers = 4
worker_connections = 1000
spew = False
daemon = False

Python案例

[program:rrw_idfa]
command=/data/anaconda3/envs/sanic/bin/gunicorn -c config/gunicorn_config.py index:app
process_name=%(program_name)s-8888
directory=/home/tornado/rrw_idfa/src                  ; 运行前cd到此目录
autostart=true                                          ; supervisord守护程序启动时自动启动sanic
autorestart=true                                        ; supervisord守护程序重启时自动重启sanic
user=tornado                                            ; 运行程序前su到此用户
redirect_stderr=true                                    ; 将stderr重定向到stdout
stdout_logfile=/home/tornado/rrw_idfa/logs/sanic_tornado_stdout.log        ; 记录控制台输出的日志位置

Java案例

[program:xxx]
directory = /root/xxx                           ;启动目录
command =  java -jar xxx-4.0.jar       ;启动命令
autostart = true                                               ;在supervisord启动的时候也启动
startsecs = 5                                                   ;启动5秒后没有异常退出,就当作已经正常启动了
autorestart = true                                            ;程序异常退出后自动重启
startretries = 3                                                ;启动失败自动重试次数,默认是3
user = root                                                      ;哪个用户启动
redirect_stderr = true                                      ;把stderr重定向到stdout,默认false
stdout_logfile_maxbytes = 20MB                    ;stdout日志文件大小,默认50MB
stdout_logfile_backups = 20                           ;stdout日志文件备份数
stdout_logfile = /root/xxx/logs/xxx.log
;stdout日志文件,需要手动创建/root/xxx/logs目录
[program:rrw_operational_uid_api]
directory=/data/rrw_tmp/rrw_operational_uid_api/src                  ; 运行前cd到此目录
command=/data/anaconda3/bin/gunicorn -c config/gunicorn_config.py index:app
process_name=%(program_name)s-8888
autostart=true                                          ; supervisord守护程序启动时自动启动sanic
autorestart=true                                        ; supervisord守护程序重启时自动重启sanic
user=tornado                                            ; 运行程序前su到此用户
redirect_stderr=true                                    ; 将stderr重定向到stdout
stdout_logfile=/data/rrw_tmp/rrw_operational_uid_api/logs/supervisor_operational_stdout.log        ; 记录控制台输出的日志位置


单独启动rrw_operational_uid_api进程

supervisorctl start rrw_operational_uid_api

supervisorctl常用命令

> status           #查看程序状态
> stop name    #关闭name程序
> start name    #启动name程序
> restart name # 重启name程序
> reread          #读取有更新的配置文件,不会启动新添加的程序
> update          #重启配置文件修改过的程序
     

nginx配置

修改nginx配置文件把80端口映射到9001端口添加


# 文件位置
vim /usr/local/nginx/conf/nginx.conf
# 文件内容

location /supervisor/ {
        proxy_pass http://127.0.0.1:9001/;
}
# 改完以后执行命令
nginxsystemctl restart nginx

访问http://192.168.1.108/supervisor/
或者继续访问带端口的地址

查看supervisord.conf文件是否正常配置

supervisord -c /etc/supervisord.conf

出现报错信息即错误要改掉

常见问题

问题1
Error: Another program is already listening on a port that one of our HTTP servers is configured to use. Shut this program down first before starting supervisord.
ps -ef | grep supervisord
kill -9 xxxx

问题2
Unlinking stale socket /var/run/supervisor/supervisor.sock
unlink /var/run/supervisor/supervisor.sock
stale是不新鲜的意思

问题3
unix:///var/run/supervisor/supervisor.sock no such file
sudo chmod 777 /run
sudo chmod 777 /var/log

查看所有状态

查看所有状态
supervisorctl -c /etc/supervisord.conf status
查看单个状态:
supervisorctl -c /etc/supervisord.conf status xxx:xxx-17777
修改配置文件后重新启动: 
supervisorctl -c /etc/supervisord.conf restart xxx:xxx-17777
更新代码后一般重新启动:
supervisorctl restart xxx:xxx-17777

出现问题: xxx:xxx-17777 FATAL Exited too quickly (process log may have details)
查看你部署服务的日志: tail -f -n 100 /data/www/ope_test/xxx/logs/xxx.log
日志显示: Error: 'config/gunicorn_config_test.py' doesn't exist
mv gunicorn_confit_test.py gunicorn_config_test.py
原来文件名写错了

出现问题: Failed to find application object 'app' in 'index
将app初始化的拿到全局来, 不放在函数中

杀死进程等

yum install psmisc
pstree -ap|grep gunicorn
kill -9 端口杀不死
先停下supervisorctl stop all 再杀, 不然永远杀不死进程


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM