ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

​ 05-Supervisor的使用

2022-05-03 01:00:20  阅读:185  来源: 互联网

标签:supervisor supervisord 05 supervisorctl etc program conf 使用 Supervisor


Supervisor 的使用

一 Supervisor介绍

Supervisor (http://supervisord.org) 是一个用 [Python] 写的进程管理工具,可以很方便的用来启动、重启、关闭进程(不仅仅是 Python 进程)。除了对单个进程的控制,还可以同时启动、关闭多个进程,比如很不幸的服务器出问题导致所有应用程序都被杀死,此时可以用 supervisor 同时启动所有应用程序而不是一个一个地敲命令启动

二 安装

2.1 安装方式

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# 可以通过apt-get、yum安装,既然Supervisor是基于python编写的,那我们就用pip安装好了

# 1 配置好yum源后,可以直接安装
yum install supervisor

#2 Debian/Ubuntu可通过apt安装
apt-get install supervisor

# 3 pip安装
sudo yum install python-setuptools-devel
pip install supervisor

# 4 easy_install安装
sudo yum install python-setuptools-devel
easy_install supervisor

2.2 验证

1
2
3
4
5
6
7
8
9
10
11
12
# 1 安装完成后,会在 /usr/bin 下加入三个命令:
[root@lqz bin]# ls |grep super
echo_supervisord_conf  # 生成一个配置文件示例
supervisorctl          # 服务端
supervisord            # 客户端
# 2 安装完成后,会在 /etc 下创建一个 supervisord.d目录用于存放supervisor的配置文件,
# 还有一个supervisord.conf配置文件(如果没有使用命令: echo_supervisord_conf > /etc/supervisord.conf 生成)
[root@lqz etc]# ls |grep super
supervisord.conf
supervisord.d

# 3 方便起见,把supervisor服务器相关的配置写入supervisord.conf中,把监控各个进程的配置,按照进程名存在 supervisord.conf 目录下。(这个可以在supervisord.conf中的[include]部分下配置)

2.3 配置

1
2
3
4
5
6
7
8
9
10
11
12
# 1 修改配置文件
vim supervisord.conf 
# 2 最后一行改为(;表示注释),这样配置文件可以写到supervisord.d目录下一xx.ini命名
[include]
files = supervisord.d/*.ini

# 3 简单说明:
    [unix_http_server] 配置socket连接部分
    [supervisord] 配置supervisor服务器部分
    [supervisorctl] 配置supervisor客户端部分
    [inet_http_server] 配置web管理界面
    [include] 配置需要引入的其他配置

2.4 配置详情(了解)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
[unix_http_server]
file=/tmp/supervisor.sock   ; UNIX socket 文件,supervisorctl 会使用
;chmod=0700                 ; socket 文件的 mode,默认是 0700
;chown=nobody:nogroup       ; socket 文件的 owner,格式: uid:gid

;[inet_http_server]         ; HTTP 服务器,提供 web 管理界面
;port=127.0.0.1:9001        ; Web 管理后台运行的 IP 和端口,如果开放到公网,需要注意安全性
;username=user              ; 登录管理后台的用户名
;password=123               ; 登录管理后台的密码

[supervisord]
logfile=/tmp/supervisord.log ; 日志文件,默认是 $CWD/supervisord.log
logfile_maxbytes=50MB        ; 日志文件大小,超出会 rotate,默认 50MB
logfile_backups=10           ; 日志文件保留备份数量默认 10
loglevel=info                ; 日志级别,默认 info,其它: debug,warn,trace
pidfile=/tmp/supervisord.pid ; pid 文件
nodaemon=false               ; 是否在前台启动,默认是 false,即以 daemon 的方式启动
minfds=1024                  ; 可以打开的文件描述符的最小值,默认 1024
minprocs=200                 ; 可以打开的进程数的最小值,默认 200

; the below section must remain in the config file for RPC
; (supervisorctl/web interface) to work, additional interfaces may be
; added by defining them in separate rpcinterface: sections
[rpcinterface:supervisor]
supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface

[supervisorctl]
serverurl=unix:///tmp/supervisor.sock ; 通过 UNIX socket 连接 supervisord,路径与 unix_http_server 部分的 file 一致
;serverurl=http://127.0.0.1:9001 ; 通过 HTTP 的方式连接 supervisord

; 包含其他的配置文件
[include]
files = relative/directory/*.ini    ; 可以是 *.conf 或 *.ini

2.5 启动、停止、重启

1
2
3
4
5
6
7
8
9
# 1 启动supervisord
supervisord -c /etc/supervisord.conf   或  supervisord 
# 2 停止supervisord
supervisorctl shutdown
# 3 重新加载配置文件
supervisorctl reload

# 4 注意:如果配置了密码(使用如下命令)
supervisorctl -u user -p 123 reload

三 program 配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# 只要在/etc/supervisord.d/目录下所有 *.ini都会被管理
vi /etc/supervisord.d/redis.ini
[program:redis-server]
command=/usr/bin/redis-server /etc/redis/6379.conf
priority=999                ; 优先级(越小越优先)
autostart=true              ; supervisord启动时,该程序也启动
autorestart=true            ; 异常退出时,自动启动
startsecs=10                ; 启动后持续10s后未发生异常,才表示启动成功
startretries=3              ; 异常后,自动重启次数
exitcodes=0,2               ; exit异常抛出的是0、2时才认为是异常
stopsignal=QUIT             ; 杀进程的信号

; 在程序发送stopignal后,等待操作系统将SIGCHLD返回给supervisord的秒数。
; 如果在supervisord从进程接收到SIGCHLD之前经过了这个秒数,
; supervisord将尝试用最终的SIGKILL杀死它
stopwaitsecs=1
user=root                   ; 设置启动该程序的用户
log_stdout=true             ; 如果为True,则记录程序日志
log_stderr=false            ; 如果为True,则记录程序错误日志
logfile=/var/log/redis-server.log    ; 程序日志路径
logfile_maxbytes=1MB        ; 日志文件最大大小
logfile_backups=10          ; 日志文件最大数量

四 进程管理

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# 1启动supervisord管理的所有进程
supervisorctl start all
#2 停止supervisord管理的所有进程
supervisorctl stop all
#3 启动supervisord管理的某一个特定进程
supervisorctl start program-name   # program-name为[program:xx]中的xx
# 4 停止supervisord管理的某一个特定进程
supervisorctl stop program-name  // program-name为[program:xx]中的xx

# 5 重启所有进程或所有进程
supervisorctl restart all 
# 重启所有supervisorctl reatart program-name 
# 重启某一进程,program-name为[program:xx]中的xx
# 6 查看supervisord当前管理的所有进程的状态
supervisorctl status

五 使用 supervisorctl 客户端命令

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#Supervisorctl 是 supervisord 的一个命令行客户端工具,启动时需要指定与 supervisord 使用同一份配置文件,否则与 supervisord 一样按照顺序查找配置文件
supervisorctl -c /etc/supervisord.conf
# 上面这个命令会进入 supervisorctl 的 shell 界面,然后可以执行不同的命令了:

supervisorctl # 或者启动后,直接使用 (进入shell界面)

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

上面这些命令都有相应的输出,除了进入 supervisorctl 的 shell 界面,等同于直接在 bash 终端运行:
$ supervisorctl status
$ supervisorctl stop usercenter
$ supervisorctl start usercenter
$ supervisorctl restart usercenter
$ supervisorctl reread
$ supervisorctl update

六 使用web管理

1
2
3
4
5
6
7
8
9
10
11
12
# 1 修改配置文件
vim /etc/supervisord.conf 
# 2 修改内容如下
[inet_http_server]         ; inet (TCP) server disabled by default
port=0.0.0.0: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))

# 3 重启
supervisorctl reload
# 4 在浏览器打开:http://101.133.225.166:8080/
可以看到

image-20200518012520200

七 Supervisor配置systemctl服务,开机自动启

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# 1 新建配置文件
vim  /usr/lib/systemd/system/supervisor.service
# 2 内容如下
[Unit]
Description=supervisor
After=network.target

[Service]
Type=forking
ExecStart=/usr/bin/supervisord -c /usr/local/supervisord.conf
ExecStop=/usr/bin/supervisorctl $OPTIONS shutdown
ExecReload=/usr/bin/supervisorctl $OPTIONS reload
KillMode=process
Restart=on-failure
RestartSec=42s

[Install]
WantedBy=multi-user.target

# 3 干掉原先的supervisor进程
ps -ef|grep super
root     14465     1  0 00:58 ?        00:00:00 /usr/bin/python /usr/bin/supervisord -c /etc/supervisord.conf
kill 14465
#4 使用systemctl启动
systemctl start supervisor.service
systemctl status supervisor.service
#5 开机自启
systemctl enable supervisor.service

八 Supervisor管理redis和nginx

8.1 配置nginx

1
2
3
4
5
6
7
8
#1 vim /etc/vim /etc/supervisord.d/nginx.ini
# 2 写入
[program:nginx]
command=/usr/sbin/nginx  -g 'daemon off;' 
autostart=true ; 自动启动
autorestart=true ; 自动重启
user=root ; 以哪个用户执行
stdout_logfile=/tmp/supervisor_nginx.log ; 日志路径

8.2 配置redis

1
2
3
4
5
6
7
8
#1 vim /etc/vim /etc/supervisord.d/redis.ini
# 2 写入
[program:redis]
command=redis-server 
autostart=true ; 自动启动
autorestart=true ; 自动重启
user=root ; 以哪个用户执行
stdout_logfile=/tmp/supervisor_redis.log ; 日志路径

8.3 重启

1
2
3
4
supervisorctl shutdown
supervisord -c /etc/supervisord.conf
# 或者
supervisorctl reload

8.4 测试

1
2
3
4
5
# 杀死nginx进程
# 杀死redis进程
redis-cli
输入:shutwodn
# 发现服务又自动重启了

九 常见问题及解决

9.1 问题一

1
2
3
4
# 报错
BACKOFF   Exited too quickly (process log may have details)
# 原因
supervisor 比较适合监控业务应用,且只能监控前台程序,实现的daemon【后台启动】的程序不能用它监控,否则supervisor> status 会提示:BACKOFF  Exited too quickly (process log may have details)

9.2 问题二

1
2
3
4
5
6
7
8
9
# 报错
FATAL     Exited too quickly (process log may have details
# 原因
错误FATAL产生的原因可能是你的python命令的环境配置有问题,如果你是虚拟环境配置的话,必须使用虚拟环境的路径的python或gunicorn命令否则会失败!
# 解决
[program:gunicorn]
command=/root/.local/share/virtualenvs/blog/bin/gunicorn -c other_config/gunicorn.py main:app 
#; 这里的gunicorn必须是你运行python环境对应的环境【如果是虚拟环境就必须配置虚拟环境的路径下面的命令】
autostart = true     ; 在 supervisord 启动的时候也自动启动

9.3 问题三

1
2
3
4
5
# 报错
启动了多个supervisord服务,导致无法正常关闭服务
在运行supervisord -c /etc/supervisord.conf之前,直接运行过supervisord -c /etc/supervisord.d/xx.conf导致有些进程被多个superviord管理,无法正常关闭进程。
# 解决
使用ps -fe | grep supervisord查看所有启动过的supervisord服务,kill相关的进程。

9.4 问题四

1
2
3
4
5
# 报错
unix:///var/run/supervisor/supervisor.sock no such file
# 解决
sudo chmod 777 /run
sudo chmod 777 /var/log

9.5 问题五

1
2
3
4
5
# 报错
Unlinking stale socket /var/run/supervisor/supervisor.sock

# 解决(或者直接删除)
unlink /var/run/supervisor/supervisor.sock

9.6 问题六

1
2
3
4
5
6

#报错
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 aux | grep supervisord
kill - 9 进程ID

标签:supervisor,supervisord,05,supervisorctl,etc,program,conf,使用,Supervisor
来源: https://www.cnblogs.com/kid21/p/16217489.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有