Supervisor使用手冊

Supervisor介紹:

Supervisor (http://supervisord.org) 是一個用Python寫的進程管理工具,可以很方便的用來啓動、重啓、關閉進程(不僅僅是Python進程)。除了對單個進程的控制,還可以同時啓動、關閉多個進程,比如很不幸的服務器出問題導致所有應用程序都被殺死,此時可以用 supervisor同時啓動所有應用程序而不是一個一個地敲命令啓動。Supervisor是一款Linux下的進程管理軟件,最主要的兩個功能是:
1)將非daemon程序變成deamon方式運行,對於daemon程序則不能監控。
2)對程序進行監控,當程序退出時,可以自動拉起程序。

安裝supervisor:

supervisor是基於python開發的,安裝supervisor前,需要檢查系統是否已經安裝python環境;
在ubuntu上安裝可以使用apt-get方式直接安裝:
#apt-get install supervisor
或者可以使用pip安裝:
#pip install supervisor

supervisor配置示例:

; supervisor config file
[unix_http_server]
file=/var/run/supervisor.sock   ; (the path to the socket file)
chmod=0700                       ; sockef file mode (default 0700)
[supervisord]
logfile=/var/log/supervisor/supervisord.log ; (main log file;default $CWD/supervisord.log)
pidfile=/var/run/supervisord.pid ; (supervisord pidfile;default supervisord.pid)
childlogdir=/var/log/supervisor            ; ('AUTO' child log dir, default $TEMP)
; 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.sock ; use a unix:// URL  for a unix socket
; 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 = /etc/supervisor/conf.d/*.conf

Supervisor的基本使用

Supervisor相當強大,提供了很豐富的功能,不過我們可能只需要用到其中一小部分。
安裝完成之後,可以編寫配置文件,來滿足自己的需求。
爲了方便,我們把配置分成兩部分:supervisord(supervisor是一個C/S模型的程序,這是server端,對應的有client端:supervisorctl)和應用程序(即我們要管理的程序)。
首先來看 supervisor 的配置文件,安裝完 supervisor 之後,可以運行 echo_supervisord_conf 命令輸出默認的配置項,也可以重定向到一個配置文件裏:
#echo_supervisord_conf > /etc/supervisor/supervisord.conf

上面生成的是supervisord的初始配置文件,我們可以在裏面配置要管理的進程,但通常並不應該這樣做,而是通過配置文件中的[include]的方式把不同的程序(組)寫到不同的配置文件裏。
如有個進程叫:tomcat,那麼我們可以在/etc/supervisor/conf.d下創建一個新的配置文件,tomcat.conf,在裏面配置tomcat的進程監控。
要確保你的supervisord.conf配置文件中include包含了你自定義的配置文件包含進來,如下:

[include]
files = /etc/supervisor/conf.d/*.conf

下面我們來具體的配置一個進程監控的配置文件,來以daemon的方式啓動tomcat:
#vim /etc/supervisor/conf.d/tomcat.conf

[program:tomcat]
command = /home/tomcat/bin/startup.sh
#啓動程序的命令;
autostart = true
#在supervisord啓動的時候也自動啓動;
autorestart = true
#程序異常退出後自動重啓;
startsecs = 5
#啓動5秒後沒有異常退出,就當作已經正常啓動了;
startretries = 3
#啓動失敗自動重試次數,默認是3;
#user = tomcat
#開啓進程使用哪個用戶和組啓動(這裏tomcat啓動時指定了nobody用戶所以就不用再指定了);
redirect_stderr = true
#把stderr重定向到stdout,默認false;
stdout_logfile=/data/log/tomcat/out-memcache.log
#標準日誌輸出;
stderr_logfile=/data/log/tomcat/err-memcache.log
#錯誤日誌輸出;
stdout_logfile_maxbytes = 20MB
#標準日誌文件大小,默認50MB;
stdout_logfile_backups = 20
#標準日誌文件備份數;

一份配置文件至少需要一個 [program:x] 部分的配置,來告訴 supervisord 需要管理那個進程。
[program:x]語法中的 x 表示 program name,會在客戶端(supervisorctl 或 web 界面)顯示,在 supervisorctl 中通過這個值來對程序進行start、restart、stop等操作。
日誌文件要存放的文件夾要創建好。
使用supervisor還有一個更大的好處就是,可以快速開啓多個進程,配置參數如下:

process_name=%(process_num)s
numprocs=3

表示對同一配置開啓3個線程。
啓動supervisor
#service supervisor start
或者:
#/etc/init.d/supervisor start
也可以指定配置文件進行啓動:
#supervisord -c /etc/supervisor/supervisord.conf
Supervisor啓動後在/tmp目錄會產生supervisord.log 、supervisord.pid 、supervisor.sock這三個文件,如果有問題可以查看日誌。
查看啓動狀態:
# ps -ef| grep supervisor
root 10232 1 0 15:19 ? 00:00:00 /usr/bin/python /usr/bin/supervisord -c /etc/supervisor/supervisord.conf
查看進程啓動狀態:
#supervisorctl status
tomcat RUNNING pid 1230, uptime 0:01:12
對進程進行start、stop、restart管理:
#supervisorctl stop|start|restart tomcat

Supervisorctl命令使用:

Supervisor可通過維護命令supervisorctl管理或通過web管理界面管理。維護命令supervisorctl有兩種用法。一種是命令式,一種是交互式。
命令式:
1.查詢所有進程狀態:
#supervisorctl status
2.啓、停、重啓業務進程,tomcat爲進程名,即[program:tomcat]裏配置的值:
supervisorctl start tomcat
supervisorctl stop tomcat
supervisorctl restart tomcat
3.管理全部進程:
supervisorctl start all
supervisorctl stop all
supervisorctl restart all
4.重新加載配置文件,停止原有進程並按新的配置啓動所有進程(注意:所有進程會停止並重啓,線上操作慎重)
supervisorctl reload
5.根據最新的配置文件,啓動新配置或有改動的進程,配置沒有改動的進程不會受影響而被重啓(注意:這纔是線上可以操作的命令,不會重啓原有進程)
supervisorctl update

交互式:

# supervisorctl
tomcat                       RUNNING   pid 1256, uptime 0:01:47
supervisor> stop tomcat
tomcat: stopped
supervisor> start tomcat
tomcat: started
supervisor> status
tomcat                       RUNNING   pid 1258, uptime 0:00:04
supervisor> restart tomcat
tomcat: stopped
tomcat: started
supervisor> status
tomcat                       RUNNING   pid 1259, uptime 0:00:02
supervisor>

總結:

1)可以自己編寫腳本將Supervisor加入chkconfig中,隨系統自動啓動。 或者可以使用現成的腳本: Supervisor initscripts。
2)除了supervisorctl 之外,還可以配置supervisrod啓動web管理界面,這個web後臺使用Basic Auth的方式進行身份認證。
3)除了單個進程的控制,還可以配置group,進行分組管理。經常查看日誌文件,包括 supervisord的日誌和各個 pragram 的日誌文件,程序crash 或拋出異常的信息一半會輸出到stderr,可以查看相應的日誌文件來查找問題。
4)Supervisor有很豐富的功能,還有其他很多項配置,可以在官方文檔獲取更多信息:http://supervisord.org/index.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章