Supervisor安装实践

http://supervisord.org/installing.html

脱机安装

setuptools(最新)来自https://pypi.python.org/pypi/setuptools

meld3(最新)来自https://pypi.python.org/pypi/meld3

Supervisor(http://supervisord.org/)是用Python开发的一个client/server服务,是Linux/Unix系统下的一个进程管理工具,不支持Windows系统。它可以很方便的监听、启动、停止、重启一个或多个进程。用Supervisor管理的进程,当一个进程意外被杀死,supervisort监听到进程死后,会自动将它重新拉起,很方便的做到进程自动恢复的功能,不再需要自己写shell脚本来控制。

 

因为Supervisor是Python开发的,安装前先检查一下系统否安装了Python2.4以上版本。下面以CentOS7,Python2.7版本环境下,介绍Supervisor的安装与配置步聚:

 

root

一、安装

1、tar -zvxf meld3-1.0.2.tar.gz    unzip setuptools-39.0.1.zip

2、分别到俩个目录里 执行 python setup.py install

3、esay_install supervisor

Supervisor安装实践

 

supervisor安装完成后 /usr/bin下 会生成三个执行程序:supervisord、supervisorctl、echo_supervisord_conf,分别是supervisor的守护进程服务(用于接收进程管理命令)、客户端(用于和守护进程通信,发送管理进程的指令)、生成初始配置文件程序。

Supervisor安装实践

 

二、配置

Supervisor配置文件通常命名为 supervisord.conf。它由supervisord 和supervisorctl使用。如果任一应用程序在没有-c选项的情况下启动(用于显式指示配置文件名的选项),应用程序将按照指定的顺序在以下位置查找名为supervisord.conf的文件。它将使用它找到的第一个文件。

最好在运行supervisord服务的时候,需要指定supervisor配置文件

1、$CWD/supervisord.conf

2、$CWD/etc/supervisord.conf

3、/etc/supervisord.conf

4、/etc/supervisor/supervisord.conf (since Supervisor 3.3.0)

5、../etc/supervisord.conf (Relative to the executable)

6、../supervisord.conf (Relative to the executable)

 

$CWD表示运行supervisord程序的目录。

可以通过运行echo_supervisord_conf程序生成supervisor的初始化配置文件,如下所示:

 

mkdir /etc/supervisor

echo_supervisord_conf > /etc/supervisor/supervisord.conf

 

配置文件项详解

http://www.supervisord.org/configuration.html

默认配置如下,及相关说明:

; Sample supervisor config file.

; For more information on the config file, please see:

; http://supervisord.org/configuration.html

; Note: shell expansion ("~" or "$HOME") is not supported.  Environment

; variables can be expanded using this syntax: "%(ENV_HOME)s".

 

[unix_http_server]          ; supervisord的unix socket服务配置

file=/tmp/supervisor.sock   ; socket文件的保存目录

;chmod=0700                 ; socket的文件权限 (default 0700)

;chown=nobody:nogroup       ; socket的拥有者和组名

;username=user              ; 默认不需要登陆用户 (open server) 

;password=123               ; 默认不需要登陆密码 (open server) 

 

;[inet_http_server]         ; supervisord的tcp服务配置;HTTP服务器,提供web管理界面

;port=127.0.0.1:9001        ; tcp端口 web管理后台运行的IP和端口,

;username=user              ; tcp登陆用户 即 登录管理后台的用户名

;password=123               ; tcp登陆密码 即 登录管理后台的密码

 

[supervisord]                ; supervisord的主进程配置

logfile=/tmp/supervisord.log ; 主要的进程日志配置

logfile_maxbytes=50MB        ; 最大日志体积,默认50MB,设置为0 表示不限制大小

logfile_backups=10           ; 日志文件备份数目,默认10;设置为0 表示不备份

loglevel=info                ; 日志级别,默认info; 还有:debug,warn,trace

pidfile=/tmp/supervisord.pid ; supervisord的pidfile文件

nodaemon=false               ; 是否以守护进程的方式启动

minfds=1024                  ; 最小的有效文件描述符,默认1024

minprocs=200                 ; 最小的有效进程描述符,默认200

;umask=022                   ; 进程文件的umask,默认200

;user=chrism                 ; 默认为当前用户,如果为root则必填

;identifier=supervisor       ; supervisord的表示符, 默认时'supervisor'

;directory=/tmp              ; 默认不cd到当前目录

;nocleanup=true              ; 不在启动的时候清除临时文件,默认false

;childlogdir=/tmp            ; ('AUTO' child log dir, default $TEMP)

;environment=KEY=value       ; 初始键值对传递给进程

;strip_ansi=false            ; (strip ansi escape codes in logs; def. false)

; 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:///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=chris              ; 如果设置应该与http_username相同

;password=123                ; 如果设置应该与http_password相同

;prompt=mysupervisor         ; 命令行提示符,默认"supervisor"

;history_file=~/.sc_history  ; 命令行历史纪录

; The below sample program section shows all possible program subsection values,

; create one or more 'real' program: sections to be able to control them under

; supervisor.

 

;[program:theprogramname]

;command=/bin/cat              ; 运行的程序 (相对使用PATH路径, 可以使用参数)

;process_name=%(program_name)s ; 进程名表达式,默认为%(program_name)s

;numprocs=1                    ; 默认启动的进程数目,默认为1

;directory=/tmp                ; 在运行前cwd到指定的目录,默认不执行cmd

;umask=022                     ; 进程umask,默认None

;priority=999                  ; 程序运行的优先级,默认999

;autostart=true                ; 默认随supervisord自动启动,默认true

;autorestart=unexpected        ; 被监控托管的程序异常中断的处理方式whether/when to restart (default: unexpected 表示进程意外杀死后才重启);

;startsecs=1                   ; 被监控托管的程序启动时持续时长 即 程序启动多少秒后没有异常退出,则表示进程正常启动了,默认是1秒

;startretries=3                ; 被监控托管的程序启动失败重试的次数

;exitcodes=0,2                 ; 期望的退出码,默认0,2

;stopsignal=QUIT               ; 杀死进程的信号,默认TERM

;stopwaitsecs=10               ; max num secs to wait b4 SIGKILL (default 10)

;stopasgroup=false             ; 向unix进程组发送停止信号,默认false

;killasgroup=false             ; 向unix进程组发送SIGKILL信号,默认false

;user=chrism                   ; 为运行程序的unix帐号设置setuid,即用哪个用户启动进程

;redirect_stderr=true          ; 将标准错误重定向到标准输出,默认false。

;stdout_logfile=/a/path        ; 标准输出的文件路径NONE=none;默认AUTO

;stdout_logfile_maxbytes=1MB   ; max # logfile bytes b4 rotation (default 50MB)

;stdout_logfile_backups=10     ; # of stdout logfile backups (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 (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)

; The below sample eventlistener section shows all possible

; eventlistener subsection values, create one or more 'real'

; eventlistener: sections to be able to handle event notifications

; sent by supervisor.

 

;[eventlistener:theeventlistenername]

;command=/bin/eventlistener    ; 运行的程序 (相对使用PATH路径, 可以使用参数)

;process_name=%(program_name)s ; 进程名表达式,默认为%(program_name)s

;numprocs=1                    ; 默认启动的进程数目,默认为1

;events=EVENT                  ; event notif. types to subscribe to (req'd)

;buffer_size=10                ; 事件缓冲区队列大小,默认10

;directory=/tmp                ; 在运行前cwd到指定的目录,默认不执行cmd

;umask=022                     ; 进程umask,默认None

;priority=-1                   ; 程序运行的优先级,默认-1

;autostart=true                ; 默认随supervisord自动启动,默认true

;autorestart=unexpected        ; whether/when to restart (default: unexpected)

;startsecs=1                   ; number of secs prog must stay running (def. 1)

;startretries=3                ; max # of serial start failures (default 3)

;exitcodes=0,2                 ; 期望的退出码,默认0,2

;stopsignal=QUIT               ; 杀死进程的信号,默认TERM

;stopwaitsecs=10               ; max num secs to wait b4 SIGKILL (default 10)

;stopasgroup=false             ; 向unix进程组发送停止信号,默认false

;killasgroup=false             ; 向unix进程组发送SIGKILL信号,默认false

;user=chrism                   ; 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 (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        ; # of stderr logfile backups (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 below sample group section shows all possible group values,

; create one or more 'real' group: sections to create "heterogeneous"

; process groups.

;[group:thegroupname]

;programs=progname1,progname2  ; 任何在[program:x]中定义的x

;priority=999                  ; 程序运行的优先级,默认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

如果command的栏运行的是shell脚本,那么在shell 脚本启动被监控程序时要用exec修饰。例如:

#!/bin/bash

exec /home/scott/install/storm/storm-0.9.0.1/bin/stormnimbus

否则,supervisord停止不了启动的进程.除此之外,shell脚本里不能出现&之类的后台运行符号。

 

启动supervisord 进程

 /usr/bin/supervisord -c /etc/supervisor/supervisord.conf

 

 

三、supervisorctl 命令

当修改了 supervisord.conf时,要想让配置立即神效,则执行

supervisorctl reload 即可;

start

启动被托管的程序

supervisorctl start xxx   ;[program:xxx]

也可以批处理一批程序

supervisorctl start  xx1  xx2 xx3 ...

被托管监控的程序的启动顺序和 star参数的顺序要一致

stop

停止一个托管已监控的程序

supervisorctl stop xxx

同样可以和 start 一样做批处理

restart

重启一个被托管监听的程序

supervisorctl restart  xxx

同样可以和 start 一样做批处理

status

查看 supervisord 都接受托管监控了哪些程序或查看其状态

Supervisorctl  status

 

实践 supervisor 自动拉Mongod

 

可以通过运行echo_supervisord_conf程序生成supervisor的初始化配置文件,如下所示:

mkdir /etc/supervisor

/usr/bin/echo_supervisord_conf > /etc/supervisor/supervisord.conf

Supervisor安装实践

 

配置

[unix_http_server]

file=/etc/supervisor/supervisor.sock

[supervisord]

logfile=/etc/supervisor/supervisord.log ; main log file; default $CWD/supervisord.log

logfile_maxbytes=10MB        ; max main logfile bytes b4 rotation; default 50MB

logfile_backups=0           ; # of main logfile backups; 0 means none, default 10

loglevel=info                ; log level; default info; others: debug,warn,trace

pidfile=/etc/supervisor/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

 

[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:///etc/supervisor/supervisor.sock ; use a unix:// URL  for a unix socket

 

[program:mongod]

command=/usr/local/mongo/bin/mongod -f /opt/mongo/conf/mongo.conf

process_name=%(program_name)s

numprocs=1

autostart=true

autorestart=true

stopasgroup=true

killasgroup=true

stdout_logfile=/etc/supervisor/%(program_name)s-out.log

stdout_logfile_maxbytes=10MB

stdout_logfile_backups=0

stdout_capture_maxbytes=1MB

stderr_logfile=/etc/supervisor/%(program_name)s-err.log

stderr_logfile_maxbytes=10MB

stderr_logfile_backups=0

stderr_capture_maxbytes=1MB

user=root  ;

 

启动supervisord 进程

 /usr/bin/supervisord -c /etc/supervisor/supervisord.conf

Supervisor安装实践