supervisor模塊學習使用


supervisor組件

  1. supervisord

    supervisord是supervisor的服務端程序。

    啟動supervisor程序自身,啟動supervisor管理的子進程,響應來自clients的請求,重啟閃退或異常退出的子進程,把子進程的stderr或stdout記錄到日志文件中,生成和處理Event

  2. supervisorctl

    客戶端的命令行工具,提供一個類似shell的操作接口,通過它你可以連接到不同的supervisord進程上來管理它們各自的子程序,最牛逼的一點是,supervisorctl不僅可以連接到本機上的supervisord,還可以連接到遠程的supervisord,當然在本機上面是通過UNIX socket連接的,遠程是通過TCP socket連接的。supervisorctl和supervisord之間的通信,是通過xml_rpc完成的。服務端也可以要求客戶端提供身份驗證之后才能進行操作   相應的配置在[supervisorctl]

  3. Web Server

    Web Server主要可以在界面上管理進程,Web Server其實是通過XML_RPC來實現的,可以向supervisor請求數據,也可以控制supervisor及子進程。配置在[inet_http_server]

  4. XML_RPC接口

            供遠程調用,supervisorctl和Web Server需要使用

 

  • 安裝

  supervisor安裝完成后會生成三個執行程序:supervisortd、supervisorctl、echo_supervisord_conf,分別是supervisor的守護進程服務(用於接收進程管理命令)、客戶端(用於和守護進程通信,發送管理進程的指令)、生成初始配置文件程序。

pip install supervisor
  • 生成配置文件

安裝好supervisor之后,默認是沒有生成配置文件的。可以通過以下命令生成配置文件(我們通常是把配置文件放到/etc/下面,當然也可以放到任意路徑下面)

echo_supervisord_conf > /etc/supervisord.conf

配置文件里每一行開頭都是分號;這個符號用來表示注釋,去掉需要配置項的分號並作修改即可

 

[unix_http_server] 

file=/home/supervisor.sock ;socket文件的路徑,supervisorctl用XML_RPC和supervisord通信就是通過它進行
的。如果不設置的話,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]          ;偵聽在TCP上的socket,Web Server和遠程的supervisorctl都要用到他(提供web管理界面),非必須

port=xx.xx.xx.xxx:9001 ;Web管理后台運行的IP和端口,如果開放到公網,需要注意安全性,非必須設置
;username=user ; default is no username (open server)
;password=123 ; default is no password (open server)
 [supervisord]  ; 主要是定義supervisord這個服務端進程的一些參數的,必須設置

logfile=/home/supervisord.log ; supervisor服務的日志文件 main log file; default $CWD/supervisord.log,$CWD為當前目錄非必須設置
logfile_maxbytes=50MB ; 日志文件大小當超過50M的時候,會生成一個新的日志文件。當設置為0時,表示不限制文件大小,默認值為50M,非必須設置
logfile_backups=10 ;日志文件保持的數量,上面的日志文件大於50M時,就會生成一個新文件。文件數量大於10時,最初的老文件被新文件覆蓋,
文件數量將保持為10當設置為0時,表示不限制文件的數量默認情況下為10。。。非必須設置
loglevel=warn ; 日志級別,默認是info log level; default info; others: debug,warn,trace。程序穩定的情況下,維護用warn就夠了
pidfile=/tmp/supervisord.pid ; supervisord pidfile; default supervisord.pid
nodaemon=false ; 如果是true,supervisord進程將在前台運行默認為false,也就是后台以守護進程運行。。。非必須設置
minfds=1024 ;這個是最少系統空閑的文件描述符,低於這個值supervisor將不會啟動。系統的文件描述符在這里設置
cat /proc/sys/fs/file-max默認情況下為1024。。。非必須設置
minprocs=200 ;最小可用的進程描述符,低於這個值supervisor也將不會正常啟動。ulimit  -u這個命令,可以查看
linux下面用戶的最大進程數默認為200。。。非必須設置
;umask=022 ; process file creation umask; default 022
;user=chrism ; default is current user, required 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] ;給XML_RPC用的,如果想使用supervisord或者web server 都必須設置

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];supervisorctl的一些配置
serverurl=unix:///home/supervisor.sock   ;use a unix:// URL  for a unix socket這個是supervisorctl本地連接supervisord的時候,本地UNIX socket
路徑,注意這個是和前面的[unix_http_server]對應的默認值就是unix:///tmp/supervisor.sock。。非必須設置
serverurl=http://xx.21.21.xxx:9001 ;use an http:// url to specify an inet socket這個是supervisorctl遠程連接supervisord的時候,
用到的TCP socket路徑注意這個和前面的[inet_http_server]對應默認就是http://127.0.0.1:9001。非必須項
;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:f5_manage] command=nohup java -jar /home/soft/jarpackage/sr_f5_manage-0.0.1-SNAPSHOT.jar & ; the program (relative uses PATH, can take args) 
;process_name=%(program_name)s ; 這個是進程名,如果我們下面的numprocs參數為1的話,就不用管這個參數
                                 了,它默認值%(program_name)s也就是上面的那個program冒號后面的名字,
                                 但是如果numprocs為多個的話,那就不能這么干了。想想也知道,不可能每個
                                 進程都用同一個進程名吧。                                
;numprocs=1                    ; 啟動進程的數目。當不為1時,就是進程池的概念,注意process_name的設置
                                 默認為1    。。非必須設置
;directory=/tmp                ; 進程運行前,會前切換到這個目錄
                                 默認不設置。。。非必須設置
;umask=022                     ; 進程掩碼,默認none,非必須
;priority=999                  ; 子進程啟動關閉優先級,優先級低的,最先啟動,關閉的時候最后關閉
                                 默認值為999 。。非必須設置
;autostart=true                ; 如果是true的話,子進程將在supervisord啟動后被自動啟動
                                 默認就是true   。。非必須設置
;autorestart=unexpected        ; 這個是設置子進程掛掉后自動重啟的情況,有三個選項,false,unexpected
                                 和true。如果為false的時候,無論什么情況下,都不會被重新啟動,
                                 如果為unexpected,只有當進程的退出碼不在下面的exitcodes里面定義的退 
                                 出碼的時候,才會被自動重啟。當為true的時候,只要子進程掛掉,將會被無
                                 條件的重啟
;startsecs=1                   ; 這個選項是子進程啟動多少秒之后,此時狀態如果是running,則我們認為啟
                                 動成功了
                                 默認值為1 。。非必須設置
;startretries=3                ; 當進程啟動失敗后,最大嘗試啟動的次數。。當超過3次后,supervisor將把
                                 此進程的狀態置為FAIL
                                 默認值為3 。。非必須設置
;exitcodes=0,2                 ; 注意和上面的的autorestart=unexpected對應。。exitcodes里面的定義的
                                 退出碼是expected的。
;stopsignal=QUIT               ; 進程停止信號,可以為TERM, HUP, INT, QUIT, KILL, USR1, or USR2等信號
                                  默認為TERM 。。當用設定的信號去干掉進程,退出碼會被認為是expected
                                  非必須設置
;stopwaitsecs=10               ; 這個是當我們向子進程發送stopsignal信號后,到系統返回信息
                                 給supervisord,所等待的最大時間。 超過這個時間,supervisord會向該
                                 子進程發送一個強制kill的信號。
                                 默認為10秒。。非必須設置
;stopasgroup=false             ; 這個東西主要用於,supervisord管理的子進程,這個子進程本身還有
                                 子進程。那么我們如果僅僅干掉supervisord的子進程的話,子進程的子進程
                                 有可能會變成孤兒進程。所以咱們可以設置可個選項,把整個該子進程的
                                 整個進程組都干掉。 設置為true的話,一般killasgroup也會被設置為true。
                                 需要注意的是,該選項發送的是stop信號
                                 默認為false。。非必須設置。。
;killasgroup=false             ; 這個和上面的stopasgroup類似,不過發送的是kill信號
;user=chrism                   ; 如果supervisord是root啟動,我們在這里設置這個非root用戶,可以用來
                                 管理該program
                                 默認不設置。。。非必須設置項
;redirect_stderr=true          ; 如果為true,則stderr的日志會被寫入stdout日志文件中
                                 默認為false,非必須設置
;stdout_logfile=/a/path        ; 子進程的stdout的日志路徑,可以指定路徑,AUTO,none等三個選項。
                                 設置為none的話,將沒有日志產生。設置為AUTO的話,將隨機找一個地方
                                 生成日志文件,而且當supervisord重新啟動的時候,以前的日志文件會被
                                 清空。當 redirect_stderr=true的時候,sterr也會寫進這個日志文件
;stdout_logfile_maxbytes=1MB   ; 日志文件最大大小,和[supervisord]中定義的一樣。默認為50
;stdout_logfile_backups=10     ; 和[supervisord]定義的一樣。默認10
;stdout_capture_maxbytes=1MB   ; 這個東西是設定capture管道的大小,當值不為0的時候,子進程可以從stdout
                                 發送信息,而supervisor可以根據信息,發送相應的event。
                                 默認為0,為0的時候表達關閉管道。。。非必須項
;stdout_events_enabled=false   ; 當設置為ture的時候,當子進程由stdout向文件描述符中寫日志的時候,將
                                 觸發supervisord發送PROCESS_LOG_STDOUT類型的event
                                 默認為false。。。非必須設置
;stderr_logfile=/a/path        ; 這個東西是設置stderr寫的日志路徑,當redirect_stderr=true。這個就不用
                                 設置了,設置了也是白搭。因為它會被寫入stdout_logfile的同一個文件中
                                 默認為AUTO,也就是隨便找個地存,supervisord重啟被清空。。非必須設置
;stderr_logfile_maxbytes=1MB   ; 這個出現好幾次了,就不重復了
;stderr_logfile_backups=10     ; 這個也是
;stderr_capture_maxbytes=1MB   ; 這個一樣,和stdout_capture一樣。 默認為0,關閉狀態
;stderr_events_enabled=false   ; 這個也是一樣,默認為false
;environment=A="1",B="2"       ; 這個是該子進程的環境變量,和別的子進程是不共享的
;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,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 (0 means none, 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 (0 means none, 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 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

 

  • 啟動:
1.supervisord  #直接啟動
2.supervisord -c /etc/supervisord.conf #指定配置文件啟動服務
[root@localhost tmp]# supervisord
/usr/lib/python2.7/site-packages/supervisor/options.py:461: UserWarning: Supervisord is running as root and it is searching for its configuration file in default locations (including its current working directory); you probably want to specify a "-c" argument specifying an absolute path to a configuration file for improved security.
  'Supervisord is running as root and it is searching '
Error: Another program is already listening on a port that one of our HTTP servers is configured to use.  Shut this program down first before starting supervisord.
For help, use /usr/bin/supervisord -h

 

①【啟動錯誤】如果報這個錯是因為supervisord已經在運行了,

ps -ef | grep supervisord 
[root@localhost tmp]# ps -ef | grep supervisord 
root      2886     1  0 16:07 ?        00:00:02 /usr/bin/python /usr/bin/supervisord
#下面這行的意思是用戶root在執行grep --color=auto
supervisord的命令,不用管
root 3076 2588 0 19:53 pts/0 00:00:00 grep --color=auto supervisord
#殺掉supervisord服務
kill -9 2886
#再次啟動服務
root@localhost tmp]# supervisord
#下面的提示是希望啟動服務的時候指定具體的配置文件而非默認的,
#當服務有幾個不同的配置文件的時候,每次啟動服務的時候可以根據業務需求選擇使用的配置文件
/usr/lib/python2.7/site-packages/supervisor/options.py:461: UserWarning: Supervisord is running as root and it is searching for its configuration file in default locations (including its current working directory); you probably want to specify a "-c" argument specifying an absolute path to a configuration file for improved security.
'Supervisord is running as root and it is searching '
#這個提示,每次重啟都會提示這個,沒有搞明白為什么,有什么影響
Unlinking stale socket /tmp/supervisor.sock
#可以執行下面的命令解決,
sudo unlink /tmp/supervisor.sock

②【使用supervisorctl命令行報錯】一開始supervisorctl沒有配置好就啟動服務了,更改配置后要重新啟動supervisord服務才可以使用命令行

error: <class 'socket.error'>, [Errno 111] Connection refused: file: /usr/lib64/python2.7/socket.py line: 571

③【直接kill掉supervisord之后又重啟,子進程會出現異常】在關閉supervisord服務之前需要先手工關閉被supervisor啟動的子進程。如果直接關掉supervisord服務,那么被管理的子進程變為孤兒進程,還存在,之后再重啟supervisord,它會一直嘗試啟動子進程。supervisorctl status查到的pid一直在動態變化,其實就是在嘗試啟動被管理的服務。手工殺掉之前的孤兒進程才會正常

[root@localhost ~]# netstat -nlp | grep 8007
tcp6       0      0 :::8007                 :::*                    LISTEN      711/java            
[root@localhost ~]# supervisorctl status
f5_manage                        RUNNING   pid 791, uptime 0:00:04
[root@localhost ~]# supervisorctl status
f5_manage                        RUNNING   pid 861, uptime 0:00:05
[root@localhost ~]# supervisorctl status
f5_manage                        RUNNING   pid 885, uptime 0:00:02
[root@localhost ~]# supervisorctl status
f5_manage                        RUNNING   pid 885, uptime 0:00:05
[root@localhost ~]# supervisorctl status
f5_manage                        RUNNING   pid 885, uptime 0:00:08
[root@localhost ~]# supervisorctl status
f5_manage                        STARTING  
[root@localhost ~]# supervisorctl status
f5_manage RUNNING pid
935, uptime 0:00:03 [root@localhost ~]# supervisorctl status f5_manage RUNNING pid 1006, uptime 0:00:03 [root@localhost ~]# netstat -nlp | grep 8007 tcp6 0 0 :::8007 :::* LISTEN 711/java

 [root@localhost ~]# kill -9 711
 [root@localhost ~]# netstat -nlp | grep 8007
 tcp6 0 0 :::8007 :::* LISTEN 2551/java
 [root@localhost ~]# netstat -nlp | grep 8007
 tcp6 0 0 :::8007 :::* LISTEN 2551/java
 [root@localhost ~]# netstat -nlp | grep 8007
 tcp6 0 0 :::8007 :::* LISTEN 2551/java
 [root@localhost ~]# supervisorctl status f5_manage
 f5_manage RUNNING pid 2551, uptime 0:00:30

ps:正確的操作

①通過supervisorctl加載配置,重啟

# 載入最新的配置文件,停止原有進程並按新的配置啟動、管理所有進程
supervisorctl reload
# 根據最新的配置文件,啟動新配置或有改動的進程,配置沒有改動的進程不會受影響而重啟
supervisorctl update

②通過supervisorctl先停掉所有的子進程再kill  supervisord以及重啟 

# 停止全部進程,注:start、restart、stop 都不會載入最新的配置文件
supervisorctl stop all

 

supervisord啟動成功后,可以通過supervisorctl客戶端控制進程,啟動、停止、重啟。運行supervisorctl命令,不加參數,會進入supervisor客戶端的交互終端,並會列出當前所管理的所有進程

  • supervisorctl常用命令
# 停止某一個進程,program_name 為 [program:x] 里的 x
supervisorctl stop program_name
# 啟動某個進程
supervisorctl start program_name
# 重啟某個進程
supervisorctl restart program_name
# 結束所有屬於名為 groupworker 這個分組的進程 (start,restart 同理)
supervisorctl stop groupworker:
# 結束 groupworker:name1 這個進程 (start,restart 同理)
supervisorctl stop groupworker:name1
# 停止全部進程,注:start、restart、stop 都不會載入最新的配置文件
supervisorctl stop all
# 載入最新的配置文件,停止原有進程並按新的配置啟動、管理所有進程
supervisorctl reload
# 根據最新的配置文件,啟動新配置或有改動的進程,配置沒有改動的進程不會受影響而重啟
supervisorctl update

 

 

 

 

 

參考文章:

                  https://www.cnblogs.com/zhaoding/p/6257363.html

                  https://www.cnblogs.com/wswang/p/5795766.html

                  http://blog.51cto.com/lixcto/1539136

                  https://blog.csdn.net/xyang81/article/details/51555473

                  https://www.cnblogs.com/zhoujinyi/p/6073705.html

官方文檔:

                  http://supervisord.org/


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM