supervisor简介
supervisor是一个c/s架构进程管理工具,通过它可以监控和控制其他的进程,同时它自身提供了一个webui,可以在webui进行start、stop、restart操作。由supervisor管理的进程,都是它的子进程。
supervisor的服务器端称为supervisord,主要负责在启动自身时启动管理的子进程,响应客户端的命令,重启崩溃或退出的子进程,记录子进程stdout和stderr输出,生成和处理子进程生命周期中的事件。可以在一个配置文件中配置相关参数,包括supervisord自身的状态,其管理的各个子进程的相关属性。
supervisor的客户端称为supervisorctl,它提供了一个类shell的接口(即命令行)来使用supervisord服务端提供的功能。通过supervisorctl,用户可以连接到supervisord服务器进程,获得服务器进程控制的子进程的状态,启动和停止子进程,获得正在运行的进程列表。客户端通过unix域套接字或者tcp套接字与服务端进行通信,服务器端具有身份凭证认证机制,可以有效提升安全性。
当客户端和服务器位于同一台机器上时,客户端与服务器共用同一个配置文件,通过不同标签来区分两者的配置。
部署
安装supervisor
supervisor由python开发,所以除了yum方式外也可以通过pip进行安装。
yum安装
yum install -y epel-release && yum install -y supervisor
pip安装
pip install supervisor
安装jdk
由于是springboot服务所以还需要安装jdk。
yum install java-1.8.0-openjdk
supervisor配置文件
在etc目录下生成supervisor配置文件。
echo_supervisord_conf > /etc/supervisord.conf
完整配置文件如下,也可以根据以下内容直接创建配置文件。
; sample supervisor config file.
;
; for more information on the config file, please see:
; http://supervisord.org/configuration.html
;
; notes:
; - shell expansion ("~" or "$home") is not supported. environment
; variables can be expanded using this syntax: "%(env_home)s".
; - quotes around values are not supported, except in the case of
; the environment= options as shown below.
; - comments must have a leading space: "a=b ;comment" not "a=b;comment".
; - command will be truncated if it looks like a config file comment, e.g.
; "command=bash -c 'foo ; bar'" will truncate to "command=bash -c 'foo ".
;
; warning:
; paths throughout this example file use /tmp because it is available on most
; systems. you will likely need to change these to locations more appropriate
; for your system. some systems periodically delete older files in /tmp.
; notably, if the socket file defined in the [unix_http_server] section below
; is deleted, supervisorctl will be unable to connect to supervisord.
[unix_http_server]
file=/tmp/supervisor.sock ; the path to the socket file
;chmod=0700 ; socket file mode (default 0700)
;chown=nobody:nogroup ; socket file uid:gid owner
;username=user ; default is no username (open server)
;password=123 ; default is no password (open server)
; security warning:
; the inet http server is not enabled by default. the inet http server is
; enabled by uncommenting the [inet_http_server] section below. the inet
; http server is intended for use within a trusted environment only. it
; should only be bound to localhost or only accessible from within an
; isolated, trusted network. the inet http server does not support any
; form of encryption. the inet http server does not use authentication
; by default (see the username= and password= options to add authentication).
; never expose the inet http server to the public internet.
;[inet_http_server] ; inet (tcp) server disabled by default
;port=127.0.0.1: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)
[supervisord]
logfile=/tmp/supervisord.log ; main log file; default $cwd/supervisord.log
logfile_maxbytes=50mb ; max main logfile bytes b4 rotation; default 50mb
logfile_backups=10 ; # of main logfile backups; 0 means none, default 10
loglevel=info ; log level; default info; others: debug,warn,trace
pidfile=/tmp/supervisord.pid ; supervisord pidfile; default supervisord.pid
nodaemon=false ; start in foreground if true; default false
silent=false ; no logs to stdout if true; default false
minfds=1024 ; min. avail startup file descriptors; default 1024
minprocs=200 ; min. avail process descriptors;default 200
;umask=022 ; process file creation umask; default 022
;user=supervisord ; setuid to this unix account at startup; recommended 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 rpcinterface:supervisor 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:x] sections.
[rpcinterface:supervisor]
supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface
; the supervisorctl section configures how supervisorctl will connect to
; supervisord. configure it match the settings in either the unix_http_server
; or inet_http_server section.
[supervisorctl]
serverurl=unix:///tmp/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 in [*_http_server] if set
;password=123 ; should be same as in [*_http_server] if set
;prompt=mysupervisor ; cmd line prompt (default "supervisor")
;history_file=~/.sc_history ; use readline history if available
; the sample program section below 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)
;startsecs=1 ; # of secs prog must stay up to be running (def. 1)
;startretries=3 ; max # of serial start failures when starting (default 3)
;autorestart=unexpected ; when to restart if exited after running (def: unexpected)
;exitcodes=0 ; 'expected' exit codes used with autorestart (default 0)
;stopsignal=quit ; signal used to kill process (default term)
;stopwaitsecs=10 ; max num secs to wait b4 sigkill (default 10)
;stopasgroup=false ; send stop signal to the unix process group (default false)
;killasgroup=false ; sigkill the unix process group (def false)
;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 (0 means none, default 10)
;stdout_capture_maxbytes=1mb ; number of bytes in 'capturemode' (default 0)
;stdout_events_enabled=false ; emit events on stdout writes (default false)
;stdout_syslog=false ; send stdout to syslog with process name (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 (0 means none, default 10)
;stderr_capture_maxbytes=1mb ; number of bytes in 'capturemode' (default 0)
;stderr_events_enabled=false ; emit events on stderr writes (default false)
;stderr_syslog=false ; send stderr to syslog with process name (default false)
;environment=a="1",b="2" ; process environment additions (def no adds)
;serverurl=auto ; override serverurl computation (childutils)
; the sample eventlistener section below shows all possible eventlistener
; subsection values. create one or more 'real' eventlistener: sections to be
; able to handle event notifications sent by supervisord.
;[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)
;startsecs=1 ; # of secs prog must stay up to be running (def. 1)
;startretries=3 ; max # of serial start failures when starting (default 3)
;autorestart=unexpected ; autorestart if exited after running (def: unexpected)
;exitcodes=0 ; 'expected' exit codes used with autorestart (default 0)
;stopsignal=quit ; signal used to kill process (default term)
;stopwaitsecs=10 ; max num secs to wait b4 sigkill (default 10)
;stopasgroup=false ; send stop signal to the unix process group (default false)
;killasgroup=false ; sigkill the unix process group (def false)
;user=chrism ; setuid to this unix account to run the program
;redirect_stderr=false ; redirect_stderr=true is not allowed for eventlisteners
;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 (0 means none, default 10)
;stdout_events_enabled=false ; emit events on stdout writes (default false)
;stdout_syslog=false ; send stdout to syslog with process name (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 (0 means none, default 10)
;stderr_events_enabled=false ; emit events on stderr writes (default false)
;stderr_syslog=false ; send stderr to syslog with process name (default false)
;environment=a="1",b="2" ; process environment additions
;serverurl=auto ; override serverurl computation (childutils)
; the sample group section below 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 ; 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 = relative/directory/*.ini
生成的配置文件模板由多段配置组成,使用英文分号“;”标识注释。
[unix_http_server]
这个配置段,配置http服务监听的 socket,它的作用是为了让supervisorctl 能连接supervisord http server获取进程状态信息,包括管理子进程。
[unix_http_server] file=/tmp/supervisor.sock ; the path to the socket file ;chmod=0700 ; socket file mode (default 0700) ;chown=nobody:nogroup ; socket file uid:gid owner ;username=user ; default is no username (open server) ;password=123 ; default is no password (open server)
[inet_http_server]
supervisor提供了一个webui的管理界面,如果需要打开的话,就在这个配置段设置,port指定监听ip和端口,username 和password指定访问webui需要的认证信息。
[supervisord]
supervisor服务的配置项。
[supervisord] logfile=/tmp/supervisord.log ; main log file; default $cwd/supervisord.log logfile_maxbytes=50mb ; max main logfile bytes b4 rotation; default 50mb logfile_backups=10 ; # of main logfile backups; 0 means none, default 10 loglevel=info ; log level; default info; others: debug,warn,trace pidfile=/tmp/supervisord.pid ; supervisord pidfile; default supervisord.pid nodaemon=false ; start in foreground if true; default false silent=false ; no logs to stdout if true; default false minfds=1024 ; min. avail startup file descriptors; default 1024 minprocs=200 ; min. avail process descriptors;default 200 ;umask=022 ; process file creation umask; default 022 ;user=supervisord ; setuid to this unix account at startup; recommended 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
[supervisorctl]
supervisor的客户端程序supervisorctl的相关配置,注意serverurl对应的sock就是[unix_http_server]中file的配置项,如果上面配置了用户名密码,此处也需要配置。
[supervisorctl] serverurl=unix:///tmp/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 in [*_http_server] if set ;password=123 ; should be same as in [*_http_server] if set ;prompt=mysupervisor ; cmd line prompt (default "supervisor") ;history_file=~/.sc_history ; use readline history if available
[include]
指定包含的配置文件的路径,通常被监控的程序配置文件,一个程序一个,更加清晰方便管理。可以将示例中的.ini后缀改为.conf。
;[include] ;files = relative/directory/*.ini
[program:theprogramname]
具体需要监控的程序配置项,program冒号后面的内容就是程序名称。
;[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) ;startsecs=1 ; # of secs prog must stay up to be running (def. 1) ;startretries=3 ; max # of serial start failures when starting (default 3) ;autorestart=unexpected ; when to restart if exited after running (def: unexpected) ;exitcodes=0 ; 'expected' exit codes used with autorestart (default 0) ;stopsignal=quit ; signal used to kill process (default term) ;stopwaitsecs=10 ; max num secs to wait b4 sigkill (default 10) ;stopasgroup=false ; send stop signal to the unix process group (default false) ;killasgroup=false ; sigkill the unix process group (def false) ;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 (0 means none, default 10) ;stdout_capture_maxbytes=1mb ; number of bytes in 'capturemode' (default 0) ;stdout_events_enabled=false ; emit events on stdout writes (default false) ;stdout_syslog=false ; send stdout to syslog with process name (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 (0 means none, default 10) ;stderr_capture_maxbytes=1mb ; number of bytes in 'capturemode' (default 0) ;stderr_events_enabled=false ; emit events on stderr writes (default false) ;stderr_syslog=false ; send stderr to syslog with process name (default false) ;environment=a="1",b="2" ; process environment additions (def no adds) ;serverurl=auto ; override serverurl computation (childutils)
准备springboot程序
准备两个springboot程序demo01.jar、demo02.jar用于测试。程序内容没什么好说的,主要是提供一个接口来确认程序是否正常运行,并将两个jar包上传至/usr/app目录下。。
package com.zzp;
import org.springframework.boot.springapplication;
import org.springframework.boot.autoconfigure.springbootapplication;
import org.springframework.web.bind.annotation.getmapping;
import org.springframework.web.bind.annotation.restcontroller;
@restcontroller
@springbootapplication
public class demo01application {
@getmapping("/hello")
public string hello(){
return "hello,demo01";
}
public static void main(string[] args) {
springapplication.run(demo01application.class, args);
}
}
package com.example;
import org.springframework.boot.springapplication;
import org.springframework.boot.autoconfigure.springbootapplication;
import org.springframework.web.bind.annotation.getmapping;
import org.springframework.web.bind.annotation.restcontroller;
@restcontroller
@springbootapplication
public class demo02application {
@getmapping("/hello")
public string hello(){
return "hello demo02";
}
public static void main(string[] args) {
springapplication.run(demo02application.class, args);
}
}
springboot supervisor程序配置
在/etc/supervisor目录下准备两个springboot对应的supervisor程序配置文件demo01.conf、demo02.conf,具体的配置文件含义可以参考注释。
[program:demo01] directory = /usr/app ; 程序的启动目录 command = java -jar /usr/app/demo01.jar ; 启动命令,可以看出与手动在命令行启动的命令是一样的 autostart = true ; 在 supervisord 启动的时候也自动启动 startsecs = 30 ; 启动 30 秒后没有异常退出,就当作已经正常启动了 autorestart = true ; 程序异常退出后自动重启 startretries = 3 ; 启动失败自动重试次数,默认是 3 redirect_stderr = true ; 把 stderr 重定向到 stdout,默认 false stdout_logfile_maxbytes = 20mb ; stdout 日志文件大小,默认 50mb stdout_logfile_backups = 20 ; stdout 日志文件备份数 ; stdout 日志文件,需要注意当指定目录不存在时无法正常启动,所以需要手动创建目录(supervisord 会自动创建日志文件) stdout_logfile = /usr/app/demo01.log ;应用日志目录
[program:demo02] directory = /usr/app ; 程序的启动目录 command = java -jar /usr/app/demo02.jar ; 启动命令,可以看出与手动在命令行启动的命令是一样的 autostart = true ; 在 supervisord 启动的时候也自动启动 startsecs = 30 ; 启动 30 秒后没有异常退出,就当作已经正常启动了 autorestart = true ; 程序异常退出后自动重启 startretries = 3 ; 启动失败自动重试次数,默认是 3 redirect_stderr = true ; 把 stderr 重定向到 stdout,默认 false stdout_logfile_maxbytes = 20mb ; stdout 日志文件大小,默认 50mb stdout_logfile_backups = 20 ; stdout 日志文件备份数 ; stdout 日志文件,需要注意当指定目录不存在时无法正常启动,所以需要手动创建目录(supervisord 会自动创建日志文件) stdout_logfile = /usr/app/demo02.log ;应用日志目录
修改supervisor配置文件
修改/etc/supervisord.conf文件中的[include]这段配置,将注释放开并把上面两个程序配置文件加入进去。
[include] files = /etc/supervisor/*.conf
启动supervisord
通过“-c”指定配置文件启动supervisord,否则会默认到当前目录下去寻找。
supervisord -c /etc/supervisord.conf
验证程序
supervisord服务启动成功后,我们就可以通过supervisorctl命令进行交互了。
# 查看管理进程状态 supervisorctl status # 停止指定进程,如果是all则是操作全部管理的进程 supervisorctl stop [进程名称] # 启动指定进程,如果是all则是操作全部管理的进程 supervisorctl start [进程名称] # 重启指定进程,如果是all则是操作全部管理的进程 supervisorctl restart [进程名称] # 配置文件修改后使用该命令加载新的配置 supervisorctl update # 重新启动配置中的所有程序 supervisorctl reload
输入supervisorctl status如果看到以下内容则表示两个springboot服务启动成功。
[root@supervisor etc]# supervisorctl status demo01 running pid 3853, uptime 0:01:50 demo02 running pid 3852, uptime 0:01:50
同样通过ps -ef也能看到对应进程信息。
[root@supervisor etc]# ps -ef| grep java root 3852 3851 0 16:36 ? 00:00:07 java -jar /usr/app/demo02.jar root 3912 3851 1 16:40 ? 00:00:07 java -jar /usr/app/demo01.jar root 3945 1643 0 16:49 pts/0 00:00:00 grep --color=auto java
访问程序中的接口验证程序正常启动。
[root@supervisor ~]# curl http://127.0.0.1:8001/hello hello,demo01 [root@supervisor ~]# curl http://127.0.0.1:8002/hello hello,demo02
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持代码网。
发表评论