supervisor 使用實踐

注意:Supervisor要求Python 2.4或更高版本,但不支持Python 3的任何版本。

1. supervisor 介紹

Supervisor是一個客戶端/服務器系統,允許用戶在類UNIX操作系統上控制大量進程。

Supervisor組件:

  Supervisord: 服務器部分爲Supervisord,負責啓動子程序,相應客戶端的命令,記錄stdout,stderr。

          配置文件位於/etc/supervisord.conf中,ini風格的配置文件。

  Supervisorctl: 客戶端部分爲Supervisorctl,它爲Supervisord服務提供一個類似shell的界面,從Supervisorctl,用戶可以連接到不同的Supervisord進程,獲得控制子進程狀態,對子進程進行命令控制。

  Web Server: 一個功能與Supervisorctl相媲美的用戶界面,可以通過瀏覽器進行訪問。激活配置文件的[inet_http_server]部分後,可以通過訪問web界面查看和控制進程狀態。

平臺要求:大多數Unix系統上,python2.4以上版本。(注:任何windows,pyhton3環境都不能使用)

2.安裝supervisor並使用

  我是通過使用pip進行安裝的,直接執行 pip install supervisor進行安裝。也可以下載supervisor包進行安裝,解壓後調用 python setup.py install 進行安裝也是可以的,安裝簡單。

  Supervisord安裝完成後,運行echo_supervisord_conf,這將會在你的終端打印一個supervisor配置文件的樣本。

需要把配置文件追加到一個文件,然後利用supervisord命令的-c 選項來指定改文件問配置文件,進行supervisord服務器的啓動(可根據需要對配置文件進行修改)。

啓動服務:  使用supervisord 可以直接以守護進程的方式啓動進程。

  一般常用的參數(其他參數可以使用-h,--help進行查看):

      -c FILE, --configuration=FILE: 指定supervisord的配置文件。

      -n, --nodaemon: 在前臺啓動supervisord。

      -u USER, --user=USER: 設置啓動supervisord的用戶。

      -d PATH, --directory=PATH: 設置supervisord的工作目錄。

      -l FILE, --logfile=FILE:  設置supervisord的日誌文件路徑。

      -y BYTES, --logfile_maxbytes=BYTES: 日誌文件最大大小,到達大小後會進行日誌分割。

      -y NUM, --logfile_backups=NUM:  保留的supervisord日誌的副本數。

      -j FILE, --pidfile=FILE: pid文件的路徑。

啓動supervisord示例

  supervisord -c supervisord.conf

  supervisord -n 

使用supervisorctl:

  第一種: 執行supervisorctl命令,將顯示一個shell,使用該控制檯控制當前由supervisord管理的進程,輸入help 獲取相關支持的命令。

  第二種: 執行“一次性”命令,例如: supervisorctl stop all / supervisorctl status all  (會返回執行狀態)

supervctl命令行選項:

      -c, --configuration: 配置文件路徑(默認是/etc/supervisord.conf)

      -i, --interactive: 執行命令後啓動交互式shell

      -s, --serverurl URL: supervisord服務器正在監聽的URL

      -u, --username: 用於與服務器驗證的用戶名

      -p, --password: 用於與服務器驗證的密碼

      -r, --history-file: 保持readline歷史記錄

3. 配置文件

Supervisor配置文件通常命名爲supervisord.conf。它由supervisord和supervisorctl使用。如果任一應用程序沒用使用 -c 指定配置文件情況下啓動,應用程序將按照指定的順序在一下位置查找名爲supervisord.conf的文件。它將使用它找到的第一個文件。

  1. $CWD/supervisord.conf

  2. $CWD/etc/supervisord.conf

  3. /etc/supervisord.conf

  4. /etc/supervisor/supervisord.conf(supervisor 3.3.0以上版本)

  5. ../etc/supervisord.conf(相對於可執行文件)

  6. ../supervisord.conf(相對於可執行文件)

文件格式:

  supervisord.conf是一個Windows-INI風格的(python Configparser)文件,它包含段(內個段都用[header])和段內的鍵值對。

環境變量:

  使用python字符串表達式語法 %(ENV_X)s ,可以在配置文件中使用在supervisord啓動時環境中存在的環境變量: 

[program: example]
command = /usr/bin/example --loglevel=%(ENV_LOGLEVEL)s

 

在上慄中,表達式 %(ENV_LOGLEVEL)將被擴展爲環境變量 LOGLEVEL 的值。

注意:  在supervisor3.2 及更高版本中,所有選項都支持%(ENV_X)s表達式。

配置文件大同小異,具體的配置最好是看官網來的快, 這裏給一個我的樣例,如下。

配置樣例:  

[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=helei              ; default is no username (open server)
password=123456             ; default is no password (open server)

[inet_http_server]         ; inet (TCP) server disabled by default
port=:9001        ; ip_address:port specifier, *:port for all iface
username=helei              ; default is no username (open server)
password={SHA}21950a827d7e7b4e023795ffd31d5a2f47369148             ; 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=blather               ; 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
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=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]
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=helei              ; should be same as in [*_http_server] if set
password=123456                ; should be same as in [*_http_server] if set
prompt=mysupervisor         ; cmd line prompt (default "supervisor")
history_file=~/.supervisorcrl_history  ; use readline history if available


[program:filebeat]
command=/elk/filebeat-6.2.4-linux-x86_64/filebeat -c filebeat.yml --strict.perms=false            ; 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)
numprocs_start=7
directory=/elk/filebeat-6.2.4-linux-x86_64                ; 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,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=root                  ; 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)
;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)
;environment=A="1",B="2"       ; process environment additions (def no adds)
;serverurl=AUTO                ; override serverurl computation (childutils)

;[group:thegroupname]
;programs=progname1,progname2  ; each refers to 'x' in [program:x] definitions
;priority=999                  ; the relative start priority (default 999)

[include]
files = relative/directory/*.ini
[program:foo]
command = /bin/cat
包含兩個項目foo和filebeat

 

具體的設置選項可以看官網:http://www.supervisord.org/configuration.html#unix-http-server-section-settings

3. 子進程

fasdfasd

 

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