0×00 Supervisor简介
————————————————————
Supervisor是用Python实现的一款非常实用的进程管理工具,可以方便的通过命令开启、关闭、重启等操作,而且它管理的进程一旦崩溃会自动重启,但是supervisor管理的进程必须由supervisord来启动,并且管理的程序必要是非Daemon程序,Supervisor会帮你把它转化为Daemon程序,比如想要使用Supervisor来管理Nginx进程,就必须在Nginx配置文件中加入 daemon off让Nginx以非Daemon方式运行
0×01 Supervisor安装
————————————————————
Supervisor是基于Python编写,所以可以使用easy_install或 pip来安装,这里使用pip
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
[root@localhost ~]# yum install python-pip -y
[root@localhost ~]# pip install supervisor
Downloading/unpacking supervisor
Downloading supervisor-3.0.tar.gz (459kB): 459kB downloaded
Running setup.py egg_info for package supervisor
Requirement already satisfied (use --upgrade to upgrade): distribute in /usr/lib/python2.6/site-packages (from supervisor)
Downloading/unpacking meld3>=0.6.5 (from supervisor)
Downloading meld3-0.6.10.tar.gz (41kB): 41kB downloaded
Running setup.py egg_info for package meld3
Installing collected packages: supervisor, meld3
Running setup.py install for supervisor
Skipping installation of /usr/lib/python2.6/site-packages/supervisor/__init__.py (namespace package)
Installing /usr/lib/python2.6/site-packages/supervisor-3.0-py2.6-nspkg.pth
Installing echo_supervisord_conf script to /usr/bin
Installing pidproxy script to /usr/bin
Installing supervisorctl script to /usr/bin
Installing supervisord script to /usr/bin
Running setup.py install for meld3
Successfully installed supervisor meld3
Cleaning up...
|
安装完成后,supervisor会提供两个命令:
supervisord: 启动supervisor守护进程
supervisorctl: supervisor控制脚本,类似与 service 命令
0×02 Supervisor配置
————————————————————
生成配置文件:
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
|
[root@localhost ~]# echo_supervisord_conf > /etc/supervisord.conf
[root@localhost ~]# vim /etc/supervisord.conf
[unix_http_server]
; 定义Socket文件位置
file=/tmp/supervisor.sock
; WEB管理界面,默认关闭
[inet_http_server]
port=9100 ; 默认 127.0.0.1:9001
username=tux
password=123
[supervisord]
logfile=/var/log/supervisord.log
logfile_maxbytes=50MB ; 单个日志大小, 默认 50M, 超过指定大小则进行日志轮询
logfile_backups=10 ; 保留日志数据
loglevel=info ; 可选 debug,warn,trace
pidfile=/var/run/supervisord.pid
nodaemon=false
minfds=1024
minprocs=200
[rpcinterface:supervisor]
supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface
[supervisorctl]
; Socket 文件路径,必须与 unix_http_server中 file 位置一致
serverurl=unix:///tmp/supervisor.sock ; use a unix:// URL for a unix socket
; 管理单个进程配置, program 后面是服务名称
[program:flask]
command=/opt/verystar/flask/bin/python runserver.py
process_name=%(program_name)s ; 程序名称
directory=/opt/verystar/ ; 程序目录,执行命令前会先进行到此目录
autostart=true ; supervisord启动时是否自动启动此进程
autorestart=true ; 发生故障时自动重启
startsecs=5 ; 5秒内程序必须启动, 默认 1秒
startretries=3 ; 最多 3 次启动失败
stopsignal=QUIT ; 使用QUIT信号来结束进程,默认: TERM
redirect_stderr=true ; 将 stderr 重定向到 stdout
stdout_logfile=/var/log/flask-access.log
|
0×03 简单flask测试脚本
————————————————————
[root@localhost verystar]# vim /opt/verystar/runserver.py
runserver.py
Python
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
#!/opt/verystar/flask/bin/python
# -*- coding: utf-8 -*-
from flask import Flask
app = Flask(__name__)
@app.route('/')
@app.route('/<name>')
def index(name=None):
if not name:
name = 'World'
return "Hello,%s" % name
if __name__ == "__main__":
app.run(host='0.0.0.0')
|
0×04 Supervisor启动及进程管理
————————————————————
启动supervisord守护进程
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
[root@localhost verystar]# supervisord -c /etc/supervisord.conf
[root@localhost verystar]# supervisorctl status flask
flask RUNNING pid 13306, uptime 0:00:20
[root@localhost verystar]# supervisorctl stop flask
flask: stopped
[root@localhost verystar]# supervisorctl status
flask STOPPED Nov 27 01:04 PM
[root@localhost verystar]# supervisorctl start flask
flask: started
[root@localhost verystar]# supervisorctl reload flask
Restarted supervisord
[root@localhost verystar]# curl http://localhost:5000/
Hello,World
[root@localhost verystar]# curl http://localhost:5000/tux
Hello,tux
测试: 将flask进程Kill掉,看supervisor是否能将其重启
[root@localhost verystar]# kill 13306
[root@localhost verystar]# supervisorctl status flask
flask STARTING
[root@localhost verystar]# supervisorctl status flask
flask RUNNING pid 13331, uptime 0:00:08
|