一、簡介
簡介部分參考博文
https://www.cnblogs.com/hanyifeng/p/6728151.html
supervisor與launchd,daemontools,runit等程序有着相同的功能,與某些程序不同的是,它並不作為“id 為 1的進程”而替代init。相反,它用於控制應用程序,像啟動其它程序一樣,通俗理解就是,把Supervisor服務管理的進程程序,它們作為supervisor的子進程來運行,而supervisor是父進程。supervisor來監控管理子進程的啟動關閉和異常退出后的自動啟動。
1、Supervisor與系統自帶init 進程管理比較
方便:有些編譯運行的程序,在安裝完成后,需要為他們編寫啟動停止管理腳本,寫入和維護可能很痛苦,而且進程在異常崩潰結束時,許多程序都不會正確重新啟動的。Supervisord啟動管理的程序進程是作為其子進程來運行的,並且可以配置為在進程崩潰停止時自動重新啟動它們。
准確:在Unix上的進程通常很難獲得准確的up/down狀態。Pidfiles經常說謊。Supervisord將進程作為子進程啟動,所以它總是知道其子進程的正確的up/down狀態,可以方便的對這些數據進行查詢
進程分組:進程支持分組啟動和停止,也支持啟動順序,即‘優先級’,supervisor允許為進程分配優先級,並允許用戶通過supervisorctl客戶端發出命令,如“全部啟動”和”重新啟動所有“,它們以預先分配的優先級順序啟動。還可以將進程分為”進程組“,一組邏輯關聯的進程可以作為一個單元停止或啟動。
2、Supervisor的特點
簡單:supervisor通過簡單的INI風格的配置文件進行配置管理,易於學習,並提供了許多每個進程選項,如重新啟動失敗的進程和日志的自動切割。
集中:supervisor提供一個start、stop和監控進程的地方,進程可以單獨或分組進行控制。可以通過supervisor的本地或遠程命令行管理和web管理(一般為了安全,web通常需要禁用)
高效:supervisor通過fork/exec啟動子進程,子進程需要前台運行,操作系統進程終止時去通知supervisor,而不像一些我們需要寫腳本去定期輪詢PID文件來重新啟動失敗的進程。
可擴展:supervisor有一個簡單的事件(event)通知協議,還有一個用於控制的XML-RPC接口,可以用Python開發人員來擴展構建。
兼容:supervisor由Python編寫,在除Windows操作系統以外基本都支持,如linux,Mac OS x,solaris,FreeBSD系統
3、Supervisor的組建構成
Supervisord:supervisor服務器的進程名是supervisord。它主要負責在自己的調用中啟動子程序,響應客戶端的命令,重新啟動崩潰或退出的進程,記錄其子進程stdout和stderr的輸出,以及生成和處理對應於子進程生命周期中的”event“
服務器進程使用的配置文件,通常路徑存放在/etc/supervisord.confa中。此配置文件是INI格式的配置文件。
supervisorctl:supervisor命令行的客戶端名稱是supervisorctl。它為supervisord提供了一個類似於shell的交互界面。使用supervisorctl,用戶可以查看不同的supervisord進程列表,獲取控制子進程的狀態,如停止和啟動子進程
web服務器:一個可以通過Web界面來查看和控制進程的狀態,默認監聽在9001上。
二、安裝和配置
本次測試環境
主機名 | ip | 系統 |
---|---|---|
auto | 10.0.0.6/24 | Centos 7.5 |
1、安裝
[root@auto ~]# yum install supervisor -y
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
epel/x86_64/metalink | 8.4 kB 00:00:00
* base: mirrors.aliyun.com
* epel: mirrors.aliyun.com
* extras: mirrors.aliyun.com
* updates: mirrors.aliyun.com
base | 3.6 kB 00:00:00
docker-ce-edge | 3.5 kB 00:00:00
docker-ce-stable | 3.5 kB 00:00:00
epel | 3.2 kB 00:00:00
extras | 3.4 kB 00:00:00
nginx | 2.9 kB 00:00:00
updates | 3.4 kB 00:00:00
Resolving Dependencies
--> Running transaction check
---> Package supervisor.noarch 0:3.1.4-1.el7 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
=============================================================================================================================================================================================
Package Arch Version Repository Size
=============================================================================================================================================================================================
Installing:
supervisor noarch 3.1.4-1.el7 epel 446 k
Transaction Summary
=============================================================================================================================================================================================
Install 1 Package
Total download size: 446 k
Installed size: 2.2 M
Downloading packages:
supervisor-3.1.4-1.el7.noarch.rpm | 446 kB 00:00:01
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : supervisor-3.1.4-1.el7.noarch 1/1
Verifying : supervisor-3.1.4-1.el7.noarch 1/1
Installed:
supervisor.noarch 0:3.1.4-1.el7
Complete!
#生成配置文件
[root@auto ~]# echo_supervisord_conf>/etc/supervisord.conf
[root@auto ~]# cat /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".
; - Comments must have a leading space: "a=b ;comment" not "a=b;comment".
[unix_http_server]
file=/tmp/supervisor.sock ; (socket 文件的路徑)
;chmod=0700 ; socket 文件權限 (default 0700)
;chown=nobody:nogroup ; socket 文件屬主:屬組
;username=user ; (啟動http的用戶 (open server))
;password=123 ; (默認的密碼 (open server))
;[inet_http_server] ; 默認禁用tcp監聽的http 服務
;port=127.0.0.1:9001 ; (指定監聽在本機ip地址和端口)
;username=user ; (默認啟動http服務的用戶)
;password=123 ; (默認的密碼)
[supervisord]
logfile=/tmp/supervisord.log ; (主日志文件的存放位置,默認在程序的工作啟動目錄)
logfile_maxbytes=50MB ; (主日志文件的最大值,之后進行切割;默認 50MB)
logfile_backups=10 ; (主日志文件備份的數目;默認 10)
loglevel=info ; (日志級別;默認是info; 其它: debug,warn,trace)
pidfile=/tmp/supervisord.pid ; (supervisord 運行時的pidfile路徑;默認 supervisord.pid)
nodaemon=false ; (如果為true,程序就以前台運行;默認是 false)
minfds=1024 ; (min. 啟動有效的文件描述符數目;默認 1024)
minprocs=200 ; (min. 有效進程描述符;默認 200)
;umask=022 ; (進程文件創建的默認權限;默認 022)
;user=chrism ; (默認是當前啟動的用戶)
;identifier=supervisor ; (supervisord 標識符, 默認是'supervisor')
;directory=/tmp ; (默認啟動時間不會切換)
;nocleanup=true ; (在啟動時不清理臨時文件;默認值為false)
;childlogdir=/tmp ; ('AUTO' 子進程日志目錄, 默認 $TEMP)
;environment=KEY="value" ; (增加一個環境變量鍵值對:key=”value“)
;strip_ansi=false ; (在log日志里去掉ansi轉義編碼; 默認是 false)
; 下面的部分選項必須保留在RPC的配置文件中
; (supervisorctl/web 接口) 使用以下配置來管理
; added by defining them in separate rpcinterface: sections
[rpcinterface:supervisor]
supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface
[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 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
; 以下是被管理的示例程序顯示所有可能用到的配置。
; 創建一個或“多個”程序: 要遵循以下的鍵值對規則。
; supervisor.
;[program:theprogramname]
;command=/bin/cat ; 程序的啟動命令 (使用絕對路徑)
;process_name=%(program_name)s ; process_name 表示 (默認是 %(program_name)s)
;numprocs=1 ; 啟動時的進程數 (默認 1)
;directory=/tmp ; 執行時切換到的目錄 (def no cwd)
;umask=022 ; umask for process (default None)
;priority=999 ; 相對啟動優先級(default 999)
;autostart=true ; 是否跟隨supervisord程序啟動該監控程序 (default: true)
;startsecs=1 ; # 在設定時間內,程序必須保持運行 (def. 1)
;startretries=3 ; 當啟動失敗時嘗試的最大次數(default 3)
;autorestart=unexpected ; 如果退出后,什么狀態退出的去重啟,默認非意外的(def: unexpected)
;exitcodes=0,2 ; 'expected' 符合退出代碼之后去重啟 (default 0,2)
;stopsignal=QUIT ; 用於殺死進程的信號 (default TERM)
;stopwaitsecs=10 ; 最大等待秒數 SIGKILL (default 10)
;stopasgroup=false ; 發送停止信號到Unix進程組 (default false)
;killasgroup=false ; SIGKILL UNIX進程組 (def false)
;user=chrism ; setuid to this UNIX account to run the program
;redirect_stderr=true ; 是否開啟程序標准錯誤輸出的重定向 (default false)
;stdout_logfile=/a/path ; 標准輸出路徑; default AUTO
;stdout_logfile_maxbytes=1MB ; 文件最大大小 # 日志文件進行切割 (default 50MB)
;stdout_logfile_backups=10 ; # 日志文件備份數目 (default 10)
;stdout_capture_maxbytes=1MB ; ‘捕獲模式’中的字節數 (default 0)
;stdout_events_enabled=false ; 在標准輸出寫入文件時發出事件 (default false)
;stderr_logfile=/a/path ; 標准錯誤輸出, NONE for none; default AUTO
;stderr_logfile_maxbytes=1MB ; 文件最大大小 # 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" ; 添加進程環境變量 (def no adds)
;serverurl=AUTO ; 覆蓋serverurl計算 (childutils)
;下面是event事件部分所有可能設置的值,大部分同上面一樣。
; 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 ; 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,2 ; 'expected' exit codes used with autorestart (default 0,2)
;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 (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=10 ; # 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 ; 這里的progname1,progname2就是定義的監控管理程序的名字,如[program:x]這里就是x
;priority=999 ; the relative start priority (default 999)
; 下面的 [include] 選項只能包含一個files 設置,功能是定義supervisor管理程序的配置文件,可以單獨的移除去,和主配置文件分開,方便。
; 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 ;定義管理監控程序的配置文件的路徑
2、管理supervisor
1)supervisord -c /etc/supervisor.conf 服務端啟動
上面命令會讓supervisor在后台運行,后續管理命令直接在bash界面執行
supervisorctl status #查看狀態
supervisorctl stop usercenter #停止子進程
supervisorctl start usercenter #開啟子進程
supervisorctl restart usercenter #重啟子進程
supervisorctl reread #讀取有更新(增加)的配置文件,不會啟動新添加的程序
supervisorctl update #重啟配置文件修改過的程序
2)supervisorctl -c /etc/supervisor.conf 客戶端啟動
這個命令會進入 supervisorctl 的 shell 界面,然后可以執行不同的命令了:
> status # 查看程序狀態
> stop tomcat # 關閉 tomcat 程序
> start tomcat # 啟動 tomcat 程序
> restart tomcat # 重啟 tomcat 程序
> reread # 讀取有更新(增加)的配置文件,不會啟動新添加的程序
> update # 重啟配置文件修改過的程序
> exit # 退出
3)web界面
后續演示
2、配置supervisor管理nginx進程
#修改配置文件
[root@auto supervisord.d]# vim supervisor.conf
[include]
files = /etc/supervisord.d/*.conf
#(此處類似nginx的include conf.d/*.conf)
注意!!!supervisor不能管理daemon進程,nginx 是daemon進程,如果要管理,需要在nginx.conf配置文件的http標簽外增加 daemon off; 此處只是測試適用。
# 修改nginx配置
[root@auto super]# vim /usr/local/nginx/conf/nginx.conf
user nginx;
worker_processes 1;
daemon off;
events {
worker_connections 1024;
}
http {
.......
配置supervisor管理nginx的配置文件
[root@auto supervisord.d]# pwd
/etc/supervisord.d
[root@auto supervisord.d]# cat nginx_test.conf
[program:nginx]
command=/usr/local/nginx/sbin/nginx
stdout_logfile=/tmp/nginx.log
stderr_logfile=/tmp/nginx_err.log
autostart=true
autorestart=true
startsecs=10
stopwaitsecs=50 #這里最好配置
priority=1
運行supervisor並查看狀態
#運行前查看nginx進程
[root@auto etc]# ps -ef|grep nginx
root 3311 1991 0 17:21 pts/1 00:00:00 grep --color=auto nginx
#運行supervisor
[root@auto supervisord.d]# supervisord -c /etc/supervisord.conf
[root@auto supervisord.d]# supervisorctl status
nginx STARTING
[root@auto supervisord.d]# supervisorctl status
nginx RUNNING pid 3342, uptime 0:01:36
#驗證nginx進程
[root@auto supervisord.d]# !ps
ps -ef|grep nginx
root 3342 3302 0 17:23 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx 3343 3342 0 17:23 ? 00:00:00 nginx: worker process
root 3360 1991 0 17:25 pts/1 00:00:00 grep --color=auto nginx
#nginx開啟成功
3、配置supervisor管理mysql進程
配置文件無需修改 只需要在/etc/supervisord.d/內增加管理mysq的配置文件。與nginx的類似
[root@auto supervisord.d]# vim mysql.conf
[program:mysql]
command=/usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=/usr/local/mysql/data/auto.err --pid-file=/usr/local/mysql/data/auto.pid --port=3306
stdout_logfile=/tmp/mysql.log
stderr_logfile=/tmp/mysql_err.log
autostart=true
autorestart=true
startsecs=10
stopwaitsecs=50
priority=3
適用supervisorctl客戶端命令平滑增加mysql管理
[root@auto supervisord.d]# supervisorctl reread
mysql: available
[root@auto supervisord.d]# supervisorctl update
mysql: added process group
#驗證
[root@auto supervisord.d]# supervisorctl status
mysql RUNNING pid 3512, uptime 0:00:15
nginx RUNNING pid 3342, uptime 0:07:55
4、配置supervisor管理php進程
配置文件無需修改 只需要在/etc/supervisord.d/內增加管理php的配置文件。與nginx的類似,由於php的daemon進程,需要在配置文件php-fpm.conf內進行修改
[root@auto super]# vim /usr/local/nginx/conf/nginx.conf
;daemonize = yes ;默認配置
daemonize = no
#配置supervisor管理php配置文件
[root@auto supervisord.d]# vim php.conf
[program:php]
command=/usr/local/php/sbin/php-fpm
stdout_logfile=/tmp/php.log
stderr_logfile=/tmp/php_err.log
autostart=true
autorestart=true
startsecs=20
stopwaitsecs=30
priority=2
適用supervisorctl客戶端命令平滑增加php管理
[root@auto supervisord.d]# supervisorctl reread
php: available
[root@auto supervisord.d]# supervisorctl update
php: added process group
#驗證
[root@auto supervisord.d]# supervisorctl status
mysql RUNNING pid 3512, uptime 0:04:41
nginx RUNNING pid 3342, uptime 0:12:21
php RUNNING pid 3658, uptime 0:00:21
5、web界面管理supervisor進程
修改配置文件
[root@auto supervisord.d]# vim /etc/supervisor.conf
#下面是修改部分
[inet_http_server] ; inet (TCP) server disabled by default
port=10.0.0.6:9001 ; (ip_address:port specifier, *:port for all iface)
username=admin ; (default is no username (open server))
password=admin123 ; (default is no password (open server))
保存后平滑啟動
[root@auto supervisord.d]# supervisorctl update
打開瀏覽器 登陸http://10.0.0.6:9001,根據配置文件內的username和password登陸管理
該界面可以對supervisor管理的所有進程進行管理 關閉 重啟 等 ,也能查看相應的運行時間和啟動失敗日志等信息。
6、測試
1)測試1
嘗試web關閉nginx 返回auto主機進行查看
[root@auto supervisord.d]# ps -ef|grep nginx
root 3679 1991 0 17:39 pts/1 00:00:00 grep --color=auto nginx
#發現nginx已經關閉
2)測試2
嘗試手動停止nginx進程,觀察supervisor是否會對nginx進程重啟
測試結果可知,無論是正常關閉nginx還是pkill強制關閉nginx,supervisor都會重新啟動,驗證成功
三、總結
supervisor在生產中還是比較實用的,可以管理很大一部分服務,對於本身就是daemon運行的程序,實際意義不是很大(個人理解),對於服務啟動速度,測試結果是和配置文件內的startsecs有關,從手動執行 supervisorctl start nginx命令開始,大約過了startsecs配置的時間,才會真正的更新是否開啟狀態,其實真正的服務啟動情況和具體服務有關,這個時間可以根據實際情況處理。至於stopwaitsecs的適用我還沒真正掌握,測試時候如果不配置該時間,服務會無法啟動,個人暫時理解為執行start的時候會在stopwaitsecs時間內對服務進行檢測,如果在該時間內還沒啟動,則視為失敗,后續繼續測試驗證。