【Python系列-01学习路线-04自动化运维】Supervisor使用详解

Supervisor简介

Supervisor是用Python开发的一套通用的进程管理程序,能将一个普通的命令行进程变为后台daemon,并监控进程状态,异常退出时能自动重启。它是通过fork/exec的方式把这些被管理的进程当作supervisor的子进程来启动,这样只要在supervisor的配置文件中,把要管理的进程的可执行文件的路径写进去即可。也实现当子进程挂掉的时候,父进程可以准确获取子进程挂掉的信息的,可以选择是否自己启动和报警。supervisor还提供了一个功能,可以为supervisord或者每个子进程,设置一个非root的user,这个user就可以管理它对应的进程。

注:本文以centos7为例,supervisor版本3.4.0。

Supervisor安装

Centos安装

yum install supervisor

Debian/Ubuntu可通过apt安装

apt-get install supervisor

pip安装

pip install supervisor

easy_install安装

easy_install supervisor

Supervisor使用

supervisor配置文件:/etc/supervisord.conf

注:supervisor的配置文件默认是不全的,不过在大部分默认的情况下,上面说的基本功能已经满足。

子进程配置文件路径:/etc/supervisord.d/

注:默认子进程配置文件为ini格式,可在supervisor主配置文件中修改。

配置文件说明

Supervisor.conf配置文件

[unix_http_server]
file=/var/run/supervisor/supervisor.sock   ; UNIX socket 文件,supervisorctl 会使用 
;chmod=0700                 ; socket文件的mode,默认是0700
;chown=nobody:nogroup       ; socket文件的owner,格式:uid:gid
;username=user              ; (default is no username (open server))
;password=123               ; (default is no password (open server))

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

[supervisord]
logfile=/var/log/supervisor/supervisord.log  ; 日志文件,默认是 $CWD/supervisord.log
logfile_maxbytes=50MB       ; 日志文件大小,超出会rotate,默认 50MB,如果设成0,表示不限制大小
logfile_backups=10          ; 日志文件保留备份数量默认10,设为0表示不备份
loglevel=info               ; 日志级别,默认info,其它: debug,warn,trace
pidfile=/var/run/supervisord.pid ; pid 文件
nodaemon=false              ; 是否在前台启动,默认是false,即以 daemon 的方式启动
minfds=1024                 ; 可以打开的文件描述符的最小值,默认 1024
minprocs=200                ; 可以打开的进程数的最小值,默认 200
;umask=022                  ; (process file creation umask;default 022)
;user=chrism                 ; (default is current user, required if root)
;identifier=supervisor       ; (supervisord identifier, default is 'supervisor')
;directory=/tmp              ; (default is not to cd during start)
;nocleanup=true              ; (don't clean up tempfiles at start;default false)
;childlogdir=/tmp            ; ('AUTO' child log dir, default $TEMP)
;environment=KEY=value       ; (key value pairs to add to environment)
;strip_ansi=false            ; (strip ansi escape codes in logs; def. false)

; 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:///var/run/supervisor/supervisor.sock ; 通过UNIX socket连接supervisord,路径与unix_http_server部分的file一致
;serverurl=http://127.0.0.1:9001 ; 通过HTTP的方式连接supervisord
;username=chris              ; should be same as http_username if set
;password=123                ; should be same as http_password if set
;prompt=mysupervisor         ; cmd line prompt (default "supervisor")
;history_file=~/.sc_history  ; use readline history if available

; The below sample program section shows all possible program subsection values,
; create one or more 'real' program: sections to be able to control them under
; supervisor.

;[program:theprogramname]
;command=/bin/cat              ; the program (relative uses PATH, can take args)
;process_name=%(program_name)s ; process_name expr (default %(program_name)s)
;numprocs=1                    ; number of processes copies to start (def 1)
;directory=/tmp                ; directory to cwd to before exec (def no cwd)
;umask=022                     ; umask for process (default None)
;priority=999                  ; the relative start priority (default 999)
;autostart=true                ; start at supervisord start (default: true)
;autorestart=true              ; retstart at unexpected quit (default: true)
;startsecs=10                  ; number of secs prog must stay running (def. 1)
;startretries=3                ; max # of serial start failures (default 3)
;exitcodes=0,2                 ; 'expected' exit codes for process (default 0,2)
;stopsignal=QUIT               ; signal used to kill process (default TERM)
;stopwaitsecs=10               ; max num secs to wait b4 SIGKILL (default 10)
;user=chrism                   ; setuid to this UNIX account to run the program
;redirect_stderr=true          ; redirect proc stderr to stdout (default false)
;stdout_logfile=/a/path        ; stdout log path, NONE for none; default AUTO
;stdout_logfile_maxbytes=1MB   ; max # logfile bytes b4 rotation (default 50MB)
;stdout_logfile_backups=10     ; # of stdout logfile backups (default 10)
;stdout_capture_maxbytes=1MB   ; number of bytes in 'capturemode' (default 0)
;stdout_events_enabled=false   ; emit events on stdout writes (default false)
;stderr_logfile=/a/path        ; stderr log path, NONE for none; default AUTO
;stderr_logfile_maxbytes=1MB   ; max # logfile bytes b4 rotation (default 50MB)
;stderr_logfile_backups=10     ; # of stderr logfile backups (default 10)
;stderr_capture_maxbytes=1MB   ; number of bytes in 'capturemode' (default 0)
;stderr_events_enabled=false   ; emit events on stderr writes (default false)
;environment=A=1,B=2           ; process environment additions (def no adds)
;serverurl=AUTO                ; override serverurl computation (childutils)

; The below sample eventlistener section shows all possible
; eventlistener subsection values, create one or more 'real'
; eventlistener: sections to be able to handle event notifications
; sent by supervisor.

;[eventlistener:theeventlistenername]
;command=/bin/eventlistener    ; 程序启动命令,例如:/opt/apache-tomcat-8.0.35/bin/catalina.sh run
;process_name=%(program_name)s ; 在supervisord启动的时候也自动启动
;numprocs=1                    ; 启动1秒后没有异常退出,就表示进程正常启动了,默认为1秒
;events=EVENT                  ; event notif. types to subscribe to (req'd)
;buffer_size=10                ; event buffer queue size (default 10)
;directory=/tmp                ; directory to cwd to before exec (def no cwd)
;umask=022                     ; umask for process (default None)
;priority=-1                   ; the relative start priority (default -1)
;autostart=true                ; 程序退出后自动重启,可选值:[unexpected,true,false],默认为unexpected,表示进程意外杀死后才重启
;autorestart=unexpected        ; restart at unexpected quit (default: unexpected)
;startsecs=10                  ; number of secs prog must stay running (def. 1)
;startretries=3                ; 启动失败自动重试次数,默认是3
;exitcodes=0,2                 ; 'expected' exit codes for process (default 0,2)
;stopsignal=QUIT               ; signal used to kill process (default TERM)
;stopwaitsecs=10               ; max num secs to wait b4 SIGKILL (default 10)
;user=chrism                   ; 用哪个用户启动进程,默认是root
;redirect_stderr=true          ; 把stderr重定向到stdout,默认false
;stdout 日志文件,需要注意当指定目录不存在时无法正常启动,所以需要手动创建目录(supervisord 会自动创建日志文件)
;stdout_logfile=/a/path        ; stdout log path, NONE for none; default AUTO
;stdout_logfile_maxbytes=1MB   ; stdout 日志文件大小,默认50MB
;stdout_logfile_backups=10     ; stdout 日志文件备份数,默认是10
;stdout_events_enabled=false   ; emit events on stdout writes (default false)
;stderr_logfile=/a/path        ; stderr log path, NONE for none; default AUTO
;stderr_logfile_maxbytes=1MB   ; max # logfile bytes b4 rotation (default 50MB)
;stderr_logfile_backups        ; # of stderr logfile backups (default 10)
;stderr_events_enabled=false   ; emit events on stderr writes (default false)
;environment=A=1,B=2           ; process environment additions
;serverurl=AUTO                ; override serverurl computation (childutils)

; The below sample group section shows all possible group values,
; create one or more 'real' group: sections to create "heterogeneous"
; process groups.

;[group:thegroupname]
;programs=progname1,progname2  ; each refers to 'x' in [program:x] definitions
;priority=999                  ; 进程启动优先级,默认999,值小的优先启动

; The [include] section can just contain the "files" setting.  This
; setting can list multiple files (separated by whitespace or
; newlines).  It can also contain wildcards.  The filenames are
; interpreted as relative to this file.  Included files *cannot*
; include files themselves.

[include]
files = supervisord.d/*.ini    ;可以指定一个或多个以.ini结束的配置文件

子进程配置文件

给需要管理的子进程(程序)编写一个配置文件,放在/etc/supervisor.d/目录下,以.ini作为扩展名(每个进程的配置文件都可以单独分拆也可以把相关的脚本放一起)。如任意定义一个和脚本相关的项目名称的选项组(/etc/supervisord.d/xxx.ini):

xxx为app名称
此例子为flask的例子

# supervisor的程序名字
[program:xxx]
# supervisor执行的命令
command=uwsgi --ini /opt/pysite/xxx/xxx_uwsgi.ini
# 项目的目录
directory = /opt/pysite/xxx
# 开始的时候等待多少秒
startsecs=10
# 停止的时候等待多少秒
stopwaitsecs=5
# 自动开始
autostart=true
# 程序挂了后自动重启
autorestart=true
# 输出的log文件
stdout_logfile=/opt/logs/xxx/supervisord.log
# 输出的错误文件
stderr_logfile=/opt/logs/xxx/supervisord.err
stopasgroup=true
killasgroup=true

Supervisor命令说明

常用命令

supervisorctl status        //查看所有进程的状态
supervisorctl stop xxx      //停止xxx应用
supervisorctl start xxx     //启动xxx应用
supervisorctl restart xxx   //重启xxx应用
supervisorctl update        //配置文件修改后使用该命令加载新的配置
supervisorctl reload        //重新启动配置中的所有程序

注:把es换成all可以管理配置中的所有进程。直接输入supervisorctl进入supervisorctl的shell交互界面,此时上面的命令不带supervisorctl可直接使用。

启动命令

systemctl start supervisord.service     //启动supervisor并加载默认配置文件
systemctl enable supervisord.service    //将supervisor加入开机启动项

注意事项

常见问题

  1. 问题描述:安装好supervisor没有开启服务直接使用supervisorctl报的错
    1. 问题描述:unix:///var/run/supervisor.sock no such file
    2. 解决办法:supervisord -c /etc/supervisord.conf
  2. command中指定的进程已经起来,但supervisor还不断重启
    1. 问题描述:command中启动方式为后台启动,导致识别不到pid,然后不断重启,这里使用的是elasticsearch,command指定的是$path/bin/elasticsearch -d
    2. 解决办法:supervisor无法检测后台启动进程的pid,而supervisor本身就是后台启动守护进程,因此不用担心这个
  3. 启动了多个supervisord服务,导致无法正常关闭服务
    1. 问题描述:在运行supervisord -c /etc/supervisord.conf之前,直接运行过supervisord -c /etc/supervisord.d/xx.conf导致有些进程被多个superviord管理,无法正常关闭进程。
    2. 解决办法:使用ps -fe | grep supervisord查看所有启动过的supervisord服务,kill相关的进程。
  • 27
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值