一、supervisor简介
Supervisor是用Python开发的一套通用的进程管理程序,能将一个普通的命令行进程变为后台daemon,并监控进程状态,异常退出时能自动重启。它是通过fork/exec的方式把这些被管理的进程当作supervisor的子进程来启动,这样只要在supervisor的配置文件中,把要管理的进程的可执行文件的路径写进去即可。也实现当子进程挂掉的时候,父进程可以准确获取子进程挂掉的信息的,可以选择是否自己启动和报警。supervisor还提供了一个功能,可以为supervisord或者每个子进程,设置一个非root的user,这个user就可以管理它对应的进程。
二、supervisor安装
2.1 部署
Centos
yum install supervisor
Ubuntu
apt-get install supervisor
pip安装
pip install supervisor
配置开机启动
systemctl enable supervisor.service
2.2 配置
优化supervisor参数
sed -i '/\[Service\]/a\TasksMax=infinity' /lib/systemd/system/supervisor.servicesed -i '/\[Service\]/a\LimitCORE=infinity' /lib/systemd/system/supervisor.servicesed -i '/\[Service\]/a\LimitNOFILE=infinity' /lib/systemd/system/supervisor.servicesed -i '/\[Service\]/a\LimitNPROC=infinity' /lib/systemd/system/supervisor.service
修改后配置如下
#以上省略[Service]LimitNPROC=infinity #新增内容,实际无此注释LimitNOFILE=infinity #新增内容,实际无此注释LimitCORE=infinity #新增内容,实际无此注释TasksMax=infinity #新增内容,实际无此注释ExecStart=/usr/bin/supervisord -n -c /etc/supervisor/supervisord.confExecStop=/usr/bin/supervisorctl $OPTIONS shutdownExecReload=/usr/bin/supervisorctl -c /etc/supervisor/supervisord.conf $OPTIONS reloadKillMode=processRestart=on-failureRestartSec=50s#以下省略
备份原来配置
mv /etc/supervisor/supervisord.conf /etc/supervisor/supervisord.conf.bak
使用新配置
vi /etc/supervisor/supervisord.conf
[unix_http_server]file=/var/run/supervisor.sock ;UNIX socket 文件,supervisorctl 会使用;chmod=0700 ;socket文件的mode,默认是0700;chown=nobody:nogroup ;socket文件的owner,格式:uid:gid[inet_http_server] ;HTTP服务器,提供web管理界面port=0.0.0.0:9001 ;Web管理后台运行的IP和端口,如果开放到公网,需要注意安全性username=super ;登录管理后台的用户名password=super8! ;登录管理后台的密码[supervisord]logfile=/var/log/supervisor/supervisord.log ;日志文件,默认是 $CWD/supervisord.loglogfile_maxbytes=50MB ;日志文件大小,超出会rotate,默认 50MB,如果设成0,表示不限制大小logfile_backups=10 ;日志文件保留备份数量默认10,设为0表示不备份loglevel=info ;日志级别,默认info,其它: debug,warn,tracepidfile=/var/run/supervisord.pid ;pid 文件nodaemon=false ;是否在前台启动,默认是false,即以 daemon 的方式启动minfds=1024 ;可以打开的文件描述符的最小值,默认 1024minprocs=200 ;可以打开的进程数的最小值,默认 200[rpcinterface:supervisor] ;这个选项是给XML_RPC用的,当然你如果想使用supervisord或者web server 这个选项必须要开启的supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface[supervisorctl]serverurl=unix:///var/run/supervisor.sock ;通过UNIX socket连接supervisord,路径与unix_http_server部分的file一致;serverurl=http://127.0.0.1:9001 ; 通过HTTP的方式连接supervisord;包含其它配置文件[include]files = /etc/supervisor/conf.d/*.conf ;可以指定一个或多个以.conf结束的配置文件
自定义启动服务配置(想使用supervisord管理我们自己的服务先要定义supervisord配置)
vi /etc/supervisor/conf.d/test.conf #test修改为自己对应的服务名
; [program:test]是被管理的进程配置参数,test是进程的名称[program:test]command=/opt/apache-tomcat-8.0.35/bin/catalina.sh run ; 程序启动命令autostart=true ; 在supervisord启动的时候也自动启动startsecs=10 ; 启动10秒后没有异常退出,就表示进程正常启动了,默认为1秒autorestart=true ; 程序退出后自动重启,可选值:[unexpected,true,false],默认为unexpected,表示进程意外杀死后才重启startretries=3 ; 启动失败自动重试次数,默认是3user=tomcat ; 用哪个用户启动进程,默认是rootpriority=999 ; 进程启动优先级,默认999,值小的优先启动redirect_stderr=true ; 把stderr重定向到stdout,默认falsestdout_logfile_maxbytes=100MB ; stdout 日志文件大小,默认50MBstdout_logfile_backups = 10 ; stdout 日志文件备份数,默认是10stdout_logfile=/var/log/test.log ; stdout 日志文件,需要注意当指定目录不存在时无法正常启动,所以需要手动创建目录(supervisord 会自动创建日志文件)
[program:test]command=/opt/apache-tomcat-8.0.35/bin/catalina.sh runautostart=truestartsecs=10autorestart=trueredirect_stderr=truestdout_logfile_maxbytes=100MBstdout_logfile=/var/log/test.log
更新配置
supervisorctl update #让supervisor读取自定义的配置,新加的配置都要执行下
三、supervisor使用
3.1 常用命令
supervisorctl status #查看所有进程的状态supervisorctl start es #启动es服务supervisorctl stop es #停止es服务supervisorctl restart #重启es服务supervisorctl update #配置文件修改后使用该命令加载新的配置supervisorctl reload #重新启动配置中的所有程序
查看supervisor管理
supervisorctl status
输出
服务名 运行状态 PID号,运行时间test RUNNING pid 3177635, uptime 0:00:13
参数详解
; 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,最大为4096minprocs=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] ; 定义一个守护进程 elasticsearchenvironment=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,默认是1autostart=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将需要守护的进程写入配置 保存目录与supevisor配置文件里面的
- command:启动程序使用的命令,可以是绝对路径或者相对路径- process_name:一个python字符串表达式,用来表示supervisor进程启动的这个的名称,默认值是%(program_name)s- numprocs:Supervisor启动这个程序的多个实例,如果numprocs>1,则process_name的表达式必须包含%(process_num)s,默认是1- numprocs_start:一个int偏移值,当启动实例的时候用来计算numprocs的值- priority:权重,可以控制程序启动和关闭时的顺序,权重越低:越早启动,越晚关闭。默认值是999- autostart:如果设置为true,当supervisord启动的时候,进程会自动重启。- autorestart:值可以是false、true、unexpected。false:进程不会自动重启,unexpected:当程序退出时的退出码不是exitcodes中定义的时,进程会重启,true:进程会无条件重启当退出的时候。- startsecs:程序启动后等待多长时间后才认为程序启动成功- startretries:supervisord尝试启动一个程序时尝试的次数。默认是3- exitcodes:一个预期的退出返回码,默认是0,2。- stopsignal:当收到stop请求的时候,发送信号给程序,默认是TERM信号,也可以是 HUP, INT, QUIT, KILL, USR1, or USR2。- stopwaitsecs:在操作系统给supervisord发送SIGCHILD信号时等待的时间- stopasgroup:如果设置为true,则会使supervisor发送停止信号到整个进程组- killasgroup:如果设置为true,则在给程序发送SIGKILL信号的时候,会发送到整个进程组,它的子进程也会受到影响。- user:如果supervisord以root运行,则会使用这个设置用户启动子程序- redirect_stderr:如果设置为true,进程则会把标准错误输出到supervisord后台的标准输出文件描述符。- stdout_logfile:把进程的标准输出写入文件中,如果stdout_logfile没有设置或者设置为AUTO,则supervisor会自动选择一个文件位置。- stdout_logfile_maxbytes:标准输出log文件达到多少后自动进行轮转,单位是KB、MB、GB。如果设置为0则表示不限制日志文件大小- stdout_logfile_backups:标准输出日志轮转备份的数量,默认是10,如果设置为0,则不备份- stdout_capture_maxbytes:当进程处于stderr capture mode模式的时候,写入FIFO队列的最大bytes值,单位可以是KB、MB、GB- stdout_events_enabled:如果设置为true,当进程在写它的stderr到文件描述符的时候,PROCESS_LOG_STDERR事件会被触发- stderr_logfile:把进程的错误日志输出一个文件中,除非redirect_stderr参数被设置为true- stderr_logfile_maxbytes:错误log文件达到多少后自动进行轮转,单位是KB、MB、GB。如果设置为0则表示不限制日志文件大小- stderr_logfile_backups:错误日志轮转备份的数量,默认是10,如果设置为0,则不备份- stderr_capture_maxbytes:当进程处于stderr capture mode模式的时候,写入FIFO队列的最大bytes值,单位可以是KB、MB、GB- stderr_events_enabled:如果设置为true,当进程在写它的stderr到文件描述符的时候,PROCESS_LOG_STDERR事件会被触发- environment:一个k/v对的list列表- directory:supervisord在生成子进程的时候会切换到该目录- umask:设置进程的umask- serverurl:是否允许子进程和内部的HTTP服务通讯,如果设置为AUTO,supervisor会自动的构造一个url
