0%

[DevOps] 使用 supervisor 管理 process

前言

Supervisor 是一個使用 Python 開發的 process 管理工具,主要用在 process 後台維護、即時監控 process 狀態、自動重啟服務…等。

安裝

使用 pip 來安裝:

1
$ pip install supervisor

設定

安裝完成後,可以使用 echo_supervisord_conf 來取得預設的 config file:

1
$ echo_supervisord_conf > supervisord.conf

以下是預設的 config file 中的主要設定內容:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
[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=user ; default is no username (open server)
;password=123 ; default is no password (open server)

[inet_http_server] ; Web 管理介面
port=127.0.0.1:9001 ; Web 管理介面的 IP 和 port, 如果設為 public 須注意安全性問題
username=user ; 登入管理介面的 user
password=123 ; 登入管理介面的密碼

[supervisord]
logfile=/tmp/supervisord.log ; 日誌文件,默認是 $CWD/supervisord.log
logfile_maxbytes=50MB ; 日誌文件大小,超出會rotate,默認50MB
logfile_backups=10 ; 日誌文件保留備份數量, 默認10
loglevel=info ; 日誌級別,默認info,其它: debug,warn,trace
pidfile=/tmp/supervisord.pid ; pid 文件
nodaemon= false ; 是否在前台啟動,默認是false,即以daemon的方式啟動
silent=false ; no logs to stdout if true; 預設是 false
minfds=1024 ; 可以打開的文件描述符的最小值,默認1024
minprocs=200 ; 可以打開的進程數的最小值,默認200

; 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

; 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]
serverurl=unix:///tmp/supervisor.sock ; use a unix:// URL for a unix socket, 透過 unix socket 連接 supervisord, 路徑和 unix_http_server 的 file 一致
serverurl=http://127.0.0.1:9001 ; use an http:// url to specify an inet socket, 透過 HTTP 的方式連接 supervisord
username=admin ; should be same as in [*_http_server] if set
password=admin2020@dwave ; 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 [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/supervisord/service-enabled/*.conf ; 可以是 *.conf 或 *.ini

如果有很多個服務,可以將各個服務的 config 寫在不同檔案,最後在 supervisord.conf 中 include 即可,以下範例為 program config file:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
[program:program_name]                                               ; program_name 須為唯一值, 不可重複
directory=<project_dir> ; directory to cwd to before exec (def no cwd)
environment=PATH="/home/<user>/anaconda3/envs/api/bin" ; process environment additions (def no adds)
command=/home/<user>/anaconda3/envs/api/bin/uwsgi --ini <project_dir>/conf/uwsgi.ini ; the program (relative uses PATH, can take args)
autostart=true ; start at supervisord start (default: true)
autorestart=true ; when to restart if exited after running (def: unexpected)
stdout_logfile=/stdout/logfile/path ; stdout log path, NONE for none; default AUTO
stderr_logfile=/stderr/logfile/path ; stderr log path, NONE for none; default AUTO

;process_name=%(program_name)s ; process_name expr (default %(program_name)s)
;numprocs=1 ; number of processes copies to start (def 1)
;umask=022 ; umask for process (default None)
;priority=999 ; the relative start priority (default 999)
;startsecs=1 ; # of secs prog must stay up to be running (def. 1)
;startretries=3 ; max # of serial start failures when starting (default 3)
;exitcodes=0 ; 'expected' exit codes used with autorestart (default 0)
;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=true ; redirect proc stderr to stdout (default false)
;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)
;stdout_syslog=false ; send stdout to syslog with process name (default false)
;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)
;stderr_syslog=false ; send stderr to syslog with process name (default false)
;serverurl=AUTO ; override serverurl computation (childutils)

使用

啟動 supervisord

撰寫好 config file 之後,使用以下指令來啟動 supervisord:

1
2
# -c: config file
$ supervisord -c supervisord.conf

啟動之後可以搭配 supervisorctl 監控狀態, 或是可以連線至 http://127.0.0.1:9001, 登入管理者帳號密碼之後,就可以看到目前各個服務的狀態。

supervisorctl 指令

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# -c: config file
$ supervisorctl -c supervisord.conf

# 檢查 process 狀態
$ supervisorctl status

# 啟動 process
$ supervisorctl start <process_name>

# 關閉 process
$ supervisorctl stop <process_name>

# Restart process
$ supervisorctl restart <process_name>

# 重新載入配置檔案
$ upervisordctl update

# 關閉 supervisord
$ supervisorctl shutdown

# 清空 log file
$ supervisorctl clear <process_name>

# 進入互動模式, 可使用 help 查看所有指令
$ supervisorctl

參考資料