1.介绍
1 2 3 4 5 6 7 |
Supervisor是一个进程管理工具,就是有一个进程需要每时每刻不断的运行,但是这个进程又有可能由于各种原因有可能停止运行。当进程停止运行的时候我们希望能自动重新启动,Supervisor就可以帮我们实现。Supervisor是用Python开发的,能将一个普通的命令行进程变为后台daemon,并监控进程状态,异常退出时能自动重启。它是通过fork/exec的方式把这些被管理的进程当作supervisor的子进程来启动,这样只要在supervisor的配置文件中,把要管理的进程的可执行文件的路径写进去即可。也实现当子进程挂掉的时候,父进程可以准确获取子进程挂掉的信息的,可以选择是否自己启动和报警。supervisor还提供了一个功能,可以为supervisord或者每个子进程,设置一个非root的user,这个user就可以管理它对应的进程。 supervisor的命令主要有: supervisord : supervisor的服务器端部分,启动supervisor就是运行这个命令 supervisorctl:启动supervisor的命令行窗口。 |
2.安装
2.1创建用户
1 2 |
useradd -s /sbin/nologin -M supervisor chown -R supervisor:supervisor /opt/supervisor/ |
1 2 3 4 |
wget https://pypi.python.org/packages/source/s/supervisor/supervisor-3.1.3.tar.gz tar zxvf supervisor-3.1.3.tar.gz cd supervisor python setup.py install |
3.测试
1 2 3 4 5 |
#测试是否安装成功 echo_supervisord_conf #创建配置文件 echo_supervisord_conf > /etc/supervisor/supervisord.conf |
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 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
[root@centos-011 ~ 07:50:00]#cat /etc/supervisord.conf.bak ; Sample supervisor config file. [unix_http_server] file=/var/run/supervisor/supervisor.sock ; socket 路径 ;chmod=0700 ; socket 文件的权限 ;chown=nobody:nogroup ; socket 所属用户及组 ;username=user ; 用户名 ;password=123 ; 密码 ;[inet_http_server] ; 是否启用服务,默认是关闭的(启用的话可以看到supervisor 管理的服务状态) ;port=127.0.0.1:9001 ; 监听的IP及端口 ;username=user ; 用户名 ;password=123 ; 密码 [supervisord] ; supervisord 全局配置 logfile=/var/log/supervisor/supervisord.log ; supervisor 日志路径 logfile_maxbytes=50MB ; 单个日志文件最大数 logfile_backups=10 ; 保留多少个日志文件(默认10个) loglevel=info ; (log level;default info; others: debug,warn,trace) pidfile=/var/run/supervisord.pid ; pid 文件路径 nodaemon=false ; 启动是否丢到前台,设置为false ,表示以daemon 的方式启动 minfds=1024 ; 最小文件打开数,对应系统limit.conf 中的nofile ,默认最小为1024,最大为4096 minprocs=200 ; 最小的进程打开数,对应系统的limit.conf 中的nproc,默认为200 ;umask=022 ; (process file creation umask;default 022) ;user=chrism ; 启动supervisord 服务的用户,默认为root ;identifier=supervisor ; (supervisord identifier, default is 'supervisor') ;directory=/tmp ; 这里的目录指的是服务的工作目录 ;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 ; use a unix:// URL for a unix socket ;serverurl=http://127.0.0.1:9001 ; use an http:// url to specify an inet socket ;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] ; 定义一个守护进程 ,比如下面的elasticsearch ;command=/bin/cat ; 启动程序使用的命令,可以是绝对路径或者相对路径 ;process_name=%(program_name)s ; 一个python字符串表达式,用来表示supervisor进程启动的这个的名称,默认值是%(program_name)s ;numprocs=1 ; Supervisor启动这个程序的多个实例,如果numprocs>1,则process_name的表达式必须包含%(process_num)s,默认是1 ;directory=/tmp ; supervisord在生成子进程的时候会切换到该目录 ;umask=022 ; umask for process (default None) ;priority=999 ; 权重,可以控制程序启动和关闭时的顺序,权重越低:越早启动,越晚关闭。默认值是999 ;autostart=true ; 如果设置为true,当supervisord启动的时候,进程会自动启动 ;autorestart=true ; 设置为随 supervisord 重启而重启,值可以是false、true、unexpected。false:进程不会自动重启 ;startsecs=10 ; 程序启动后等待多长时间后才认为程序启动成功,默认是10秒 ;startretries=3 ; supervisord尝试启动一个程序时尝试的次数。默认是3 ;exitcodes=0,2 ; 一个预期的退出返回码,默认是0,2。 ;stopsignal=QUIT ; 当收到stop请求的时候,发送信号给程序,默认是TERM信号,也可以是 HUP, INT, QUIT, KILL, USR1, or USR2 ;stopwaitsecs=10 ; 在操作系统给supervisord发送SIGCHILD信号时等待的时间 ;user=chrism ; 如果supervisord以root运行,则会使用这个设置用户启动子程序 ;redirect_stderr=true ; 如果设置为true,进程则会把标准错误输出到supervisord后台的标准输出文件描述符 ;stdout_logfile=/a/path ; 把进程的标准输出写入文件中,如果stdout_logfile没有设置或者设置为AUTO,则supervisor会自动选择一个文件位置 ;stdout_logfile_maxbytes=1MB ; 标准输出log文件达到多少后自动进行轮转,单位是KB、MB、GB。如果设置为0则表示不限制日志文件大小 ;stdout_logfile_backups=10 ; 标准输出日志轮转备份的数量,默认是10,如果设置为0,则不备份 ;stdout_capture_maxbytes=1MB ; 当进程处于stderr capture mode模式的时候,写入FIFO队列的最大bytes值,单位可以是KB、MB、GB ;stdout_events_enabled=false ; 如果设置为true,当进程在写它的stderr ;stderr_logfile=/a/path ; 把进程的错误日志输出一个文件中,除非redirect_stderr参数被设置为true ;stderr_logfile_maxbytes=1MB ; 错误log文件达到多少后自动进行轮转,单位是KB、MB、GB。如果设置为0则表示不限制日志文件大小 ;stderr_logfile_backups=10 ; 错误日志轮转备份的数量,默认是10,如果设置为0,则不备份 ;stderr_capture_maxbytes=1MB ; 当进程处于stderr capture mode模式的时候,写入FIFO队列的最大bytes值,单位可以是KB、MB、GB ;stderr_events_enabled=false ; 如果设置为true,当进程在写它的stderr到文件描述符的时候,PROCESS_LOG_STDERR事件会被触发 ;environment=A=1,B=2 ; 一个k/v对的list列表 ;serverurl=AUTO ; 是否允许子进程和内部的HTTP服务通讯,如果设置为AUTO,supervisor会自动的构造一个url ; 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. #这个地方是自定义一个守护进程 [program:elasticsearch] ; 定义一个守护进程 elasticsearch environment=ES_HOME=/usr/local/elasticsearch ; 设置ES_HOME 环境变量 user=elk ; 启动elasticsearch 的用户 directory=/usr/local/elasticsearch ; 进入到这个目录中 command=/usr/local/elasticsearch/bin/elasticsearch ; 执行启动命令 numprocs=1 ; Supervisor启动这个程序的多个实例,如果numprocs>1,则process_name的表达式必须包含%(process_num)s,默认是1 autostart=true ; 设置为随 supervisord 启动而启动 autorestart=true ; 设置为随 supervisord 重启而重启 startretries=3 ; 设置elasticsearch 重启的重试次数 priority=1 ; 权重,可以控制程序启动和关闭时的顺序,权重越低:越早启动,越晚关闭。默认值是999 ;[eventlistener:theeventlistenername] ;command=/bin/eventlistener ; 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) ;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 ; start at supervisord start (default: true) ;autorestart=unexpected ; restart at unexpected quit (default: unexpected) ;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_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 ; 上面配置好的服务名,比如elasticsearch,kibana,logstash ;priority=999 ; the relative start priority (default 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 |
1 2 3 4 5 6 7 8 9 10 |
[program:grafana] ; 定义一个守护进程 grafana environment=GF_HOME=/opt/grafana/ ; 设置GF_HOME 环境变量 user=grafana ; 启动grafana 的用户 directory=/opt/grafana/ ; 进入到这个目录中 command=/opt/grafana/bin/grafana-server ; 执行启动命令 numprocs=1 ; Supervisor启动这个程序的多个实例,如果numprocs>1,则process_name的表达式必须包含%(process_num)s,默认是1 autostart=true ; 设置为随 supervisord 启动而启动 autorestart=true ; 设置为随 supervisord 重启而重启 startretries=3 ; 设置elasticsearch 重启的重试次数 priority=999 ; 权重,可以控制程序启动和关闭时的顺序,权重越低:越早启动,越晚关闭。默认值是999 |
5.web界面配置
1 2 3 4 5 6 |
需要在supervisor的配置文件里添加[inet_http_server]选项组:之后可以通过http://ip:9001来访问控制子线程的管理。 [inet_http_server] port=192.168.66.180:9001 username=user password=12345 |
6.配置管理进程
1 2 3 |
进程管理配置参数,不建议全都写在supervisord.conf文件中,应该每个进程写一个配置文件放在include指定的目录下包含进supervisord.conf文件中。 (类似于apache 或者nginx的vhost) 1> 创建/etc/supervisor/config.d目录,用于存放进程管理的配置文件 2> 修改/etc/supervisor/supervisord.conf中的include参数,将/etc/supervisor/conf.d目录添加到include中 |
1 2 |
[include] files = /etc/supervisor/config.d/*.ini |
下面是配置 Prometheus 进程的一个例子:
1 2 3 4 5 6 7 8 9 10 |
[program:prometheus] user=prometheus command=/opt/prometheus/prometheus --config.file=/opt/prometheus/prometheus.yml --storage.tsdb.path=/opt/prometheus/data stdout_logfile=/opt/prometheus/logs/catalina.out ;把进程的标准输出写入文件中 autostart=true autorestart=true startsecs=5 priority=1 stopasgroup=true killasgroup=true |
7.启动Supervisor服务
1 |
supervisord -c /etc/supervisor/supervisord.conf |
8.控制进程
8.1 交互终端
supervisord启动成功后,可以通过supervisorctl客户端控制进程,启动、停止、重启。运行supervisorctl命令,不加参数,会进入supervisor客户端的交互终端,并会列出当前所管理的所有进程。
1 2 3 4 5 |
[root@minio3 config.d]# supervisorctl grafana FATAL can't find command '/opt/grafana/bin/grafana-server' prometheus FATAL Exited too quickly (process log may have details) supervisor> |
8.2 bash终端
1 2 3 4 5 6 |
supervisorctl status supervisorctl stop tomcat supervisorctl start tomcat supervisorctl restart tomcat supervisorctl reread supervisorctl update |
8.3 Web管理界面
1 |
1.浏览器输入http://ip:9001访问 |
9.开机启动Supervisor服务
9.1 配置systemctl服务
1 |
vim /etc/systemd/system/supervisord.service |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
[Unit] Description=supervisor After=network.target [Service] Type=forking ExecStart=/usr/bin/supervisord -c /etc/supervisor/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 |
设置开机启动
1 2 3 4 5 6 |
systemctl daemon-reload systemctl enable supervisord.service systemctl start supervisord.service chmod 766 /etc/systemd/system/supervisord.service |
9.2 配置service类型服务
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 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
#!/bin/bash # # supervisord This scripts turns supervisord on # # Author: Mike McGrath <mmcgrath@redhat.com> (based off yumupdatesd) # # chkconfig: - 95 04 # # description: supervisor is a process control utility. It has a web based # xmlrpc interface as well as a few other nifty features. # processname: supervisord # config: /etc/supervisor/supervisord.conf # pidfile: /var/run/supervisord.pid # # source function library . /etc/rc.d/init.d/functions RETVAL=0 start() { echo -n $"Starting supervisord: " daemon "supervisord -c /etc/supervisor/supervisord.conf " RETVAL=$? echo [ $RETVAL -eq 0 ] && touch /var/lock/subsys/supervisord } stop() { echo -n $"Stopping supervisord: " killproc supervisord echo [ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/supervisord } restart() { stop start } case "$1" in start) start ;; stop) stop ;; restart|force-reload|reload) restart ;; condrestart) [ -f /var/lock/subsys/supervisord ] && restart ;; status) status supervisord RETVAL=$? ;; *) echo $"Usage: $0 {start|stop|status|restart|reload|force-reload|condrestart}" exit 1 esac exit $RETVAL |
1 2 3 4 5 |
将上述脚本内容保存到/etc/rc.d/init.d/supervisor文件中,修改文件权限为755,并设置开机启动 chmod 755 /etc/rc.d/init.d/supervisord chkconfig supervisor on |
注意:
Supervisor只能管理非daemon的进程,也就是说Supervisor不能管理守护进程。否则提示Exited too quickly (process log may have details)异常。例子中的Tomcat默认是以守护进程启动的,所以我们改成了catalina.sh run,以前台进程的方式运行。
1 2 3 4 5 6 7 8 9 10 11 12 |
常见问题 unix:///var/run/supervisor.sock no such file 问题描述:安装好supervisor没有开启服务直接使用supervisorctl报的错 解决办法:supervisord -c /etc/supervisord.conf command中指定的进程已经起来,但supervisor还不断重启 问题描述:command中启动方式为后台启动,导致识别不到pid,然后不断重启,这里使用的是elasticsearch,command指定的是$path/bin/elasticsearch -d 解决办法:supervisor无法检测后台启动进程的pid,而supervisor本身就是后台启动守护进程,因此不用担心这个 启动了多个supervisord服务,导致无法正常关闭服务 问题描述:在运行supervisord -c /etc/supervisord.conf之前,直接运行过supervisord -c /etc/supervisord.d/xx.conf导致有些进程被多个superviord管理,无法正常关闭进程。 解决办法:使用ps -fe | grep supervisord查看所有启动过的supervisord服务,kill相关的进程。 |
来源:https://www.cnblogs.com/heyongboke/p/9188125.html
- 本文固定链接: https://www.yoyoask.com/?p=4458
- 转载请注明: shooter 于 SHOOTER 发表