一、前言python
Supervisor(http://supervisord.org/)是用Python開發的一個client/server服務,是UNIX-like系統下的一個進程管理工具,不支持Windows系統。它能夠很方便的監聽、啓動、中止、重啓一個或多個進程。web
提供的高可用場景,當你的程序出現異常,例如core/內存溢出等,致使服務進程被殺死,這個時候supervisort監聽到進程終止後,會自動將它從新拉起。vim
二、安裝架構
supervisor是基於python開發的,因此安裝時首先要保證有python環境,固然最好已經安裝了以下工具。socket
pip:python的包管理工具工具
virtualenv:虛擬沙盒環境ui
當安裝方式很簡單。url
pip install supervisor
supervisor是一個C/S架構的工具。安裝完成後,會生成三個執行程序:spa
1)supervisortd:守護進程服務(用於接收進程管理命令)命令行
2)supervisorctl:客戶端(用於和守護進程通訊,發送管理進程的指令)
3)echo_supervisord_conf:生成初始配置文件程序
三、配置
運行supervisor服務的時候,須要指定supervisor的配置文件,有兩種方式:
1)顯式的指定
能夠經過-c參數顯式的指定supervior配置文件,靈活性高
$SUPERVISOR/bin/supervisorctl -c $SUPERVISOR/conf/supervisord.conf start CIServer
2)非顯式的指定
若是沒有顯示指定的話,supervisor會在以下路徑下找配置文件:
$CWD/supervisord.conf $CWD/etc/supervisord.conf /etc/supervisord.conf /etc/supervisor/supervisord.conf (since Supervisor 3.3.0) ../etc/supervisord.conf (Relative to the executable) ../supervisord.conf (Relative to the executable)
初始配置文件的生成,使用以下命令
echo_supervisord_conf > /etc/supervisor/supervisord.conf
四、配置文件參數說明
通常來講,配置管理咱們採用supervisor+program的方式。
將supervisor的配置文件,和管理進程的配置文件區分開,這樣的好處是,當有多個進程須要管理的時候,不會互相干擾。
下面分開說明一下。
1)配置supervisor
先粘上我使用的supervisor配置,後面再詳細講解。
[unix_http_server]
file=/home/kangaroo/supervisor/run/supervisor.sock ;必須,UNIX socket文件,supervisorctl 會使用
;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] ; 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=/home/kangaroo/supervisor/log/supervisord.log ;日誌文件,默認是 $CWD/supervisord.log
logfile_maxbytes=50MB ;日誌文件大小,超出會rotate,默認 50MB,若是設成0,表示不限制大小
logfile_backups=10 ;日誌文件保留備份數量默認10,設爲0表示不備份
loglevel=info ;日誌級別,默認info,其它: debug,warn,trace
pidfile=/home/kangaroo/supervisor/run/supervisord.pid ;進程pid文件 supervisord.pid
nodaemon=false ;是否在前臺啓動,默認是false,即以 daemon 的方式啓動
minfds=1024 ;能夠打開的文件描述符的最小值,默認 1024
minprocs=200 ;能夠打開的進程數的最小值,默認 200
; 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:///home/kangaroo/supervisor/run/supervisor.sock ;經過UNIX socket鏈接supervisord,路徑與unix_http_server部分的file一致
[include]
files = /home/kangaroo/supervisor/supervisor.d/*.conf
2)配置program進程
能夠看到上面文件中有[include]項,這個項指定了在/home/kangaroo/supervisor/supervisor.d/*.conf路徑下的配置文件也會被加載進來。
通常來講咱們會在這些配置文件裏管理進程的配置。
vim /home/kangaroo/supervisor/supervisor.d/CIServer.conf
配置內容
[program:CIServer] ; 程序名稱,能夠經過ctl指定名稱進行控制 directory = /home/kangaroo/build/CIServer ; 程序的啓動目錄 command = python manage.py runserver --noreload 0.0.0.0:8200 ; 啓動命令,能夠看出與手動在命令行啓動的命令是同樣的 autostart = true ; 在 supervisord 啓動的時候也自動啓動 startsecs = 5 ; 啓動 5 秒後沒有異常退出,就看成已經正常啓動了 autorestart = true ; 程序異常退出後自動重啓 startretries = 3 ; 啓動失敗自動重試次數,默認是 3 user = xiaoju ; 用哪一個用戶啓動 redirect_stderr = true ; 把 stderr 重定向到 stdout,默認 false stdout_logfile_maxbytes = 20MB ; stdout 日誌文件大小,默認 50MB stdout_logfile_backups = 20 ; stdout 日誌文件備份數 ; stdout 日誌文件,須要注意當指定目錄不存在時沒法正常啓動,因此須要手動建立目錄(supervisord 會自動建立日誌文件) stdout_logfile = /home/kangaroo/supervisor/log/supervior.log
這樣就配置完成了。
五、啓動supervisor管理服務
supervisord -c /etc/supervisor/supervisord.conf
這樣,進程就被啓動而且管理起來了。
六、client的命令
最後補充上一些supervisorctl的命令供參考
supervisorctl start CIServer # 啓動,配置文件中要配置program:CIServer supervisorctl stop CIServer # 關閉 supervisorctl restart CIServer # 重啓 supervisorctl status # 查看管理進程的狀態