Supervisor 管理进程详解

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/windy135/article/details/90204224

Supervisor 是一个用 Python 实现的进程管理工具,可以很方便地启动,关闭,重启,查看,以及监控进程,当进程由于某种原因崩溃或者被误杀后,可以自动重启并发送事件通知。Supervisor 可谓运维利器,使用 Supervisor 来管理进程,可以提高系统的高可用特性。

注意:supervisor管理这些进程是把他们当做自己的子进程来管理,还有supervisor不能管理以daemon形式运行的进程,supervisor只能管理前台运行的进程。
Supervisor 简介:

Supervisor 包括以下四个组件。

supervisord

服务端程序,主要功能是启动 supervisord 服务及其管理的子进程,记录日志,重启崩溃的子进程,等。

supervisorctl

命令行客户端程序,它提供一个类似 shell 的接口,通过 UNIX 域套接字或者 TCP 套接字并使用 XML_RPC 协议与 supervisord 进程进行数据通信。它的主要功能是管理(启动,关闭,重启,查看状态)子进程。

Web Server

实现在界面上管理进程,还能查看进程日志和清除日志。

XML-RPC 接口

可以通过 XML_RPC 协议对 supervisord 进行远程管理,达到和 supervisorctl 以及 Web
Server 一样的管理功能。

下面是一些常见的supervisor status说明:

running:进程处于运行状态
starting:Supervisor 收到启动请求后,进程处于正在启动过程中
stopped:进程处于关闭状态
stopping:Supervisor 收到关闭请求后,进程处于正在关闭过程中
backoff:进程进入 starting 状态后,由于马上就退出导致没能进入 running 状态
fatal:进程没有正常启动
exited:进程从 running 状态退出
一、supervisor的安装

1、查看主机环境:
cat /proc/version

安装supervisor:
yum install python-setuptools
pip install supervisor

测试安装是否成功(用echo_supervisord_conf命令查看有如下配置文件既是成功)

[root@localhost ~]# echo_supervisord_conf
; Sample supervisor config file.
;
; For more information on the config file, please see:
; http://supervisord.org/configuration.html
;
; Notes:
;  - Shell expansion ("~" or "$HOME") is not supported.  Environment
;    variables can be expanded using this syntax: "%(ENV_HOME)s".
;  - Quotes around values are not supported, except in the case of
;    the environment= options as shown below.
;  - Comments must have a leading space: "a=b ;comment" not "a=b;comment".
;  - Command will be truncated if it looks like a config file comment, e.g.
;    "command=bash -c 'foo ; bar'" will truncate to "command=bash -c 'foo ".
[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]         ; inet (TCP) server disabled by default
;port=127.0.0.1:9001        ; ip_address:port specifier, *:port for all iface
;username=user              ; default is no username (open server)
;password=123               ; default is no password (open server)
...

2、创建配置文件
创建supervisor配置文件目录/etc/supervisor/
mkdir -m 755 -p /etc/supervisor/

创建主配文件supervisord.conf
echo_supervisord_conf > /etc/supervisor/supervisord.conf

创建项目配置文件目录
mkdir -m 755 conf.d

在这里我们穿插一下supervisor的配置文件supervisors.conf:

 ~]# cat /etc/supervisord.conf
; Sample supervisor config file.
;
; For more information on the config file, please see:
; http://supervisord.org/configuration.html
;
; Notes:
;  - Shell expansion ("~" or "$HOME") is not supported.  Environment
;    variables can be expanded using this syntax: "%(ENV_HOME)s".
;  - Comments must have a leading space: "a=b ;comment" not "a=b;comment".

[unix_http_server]
file=/tmp/supervisor.sock   ; (socket 文件的路径)
;chmod=0700                 ; socket 文件权限 (default 0700)
;chown=nobody:nogroup       ; socket 文件属主:属组
;username=user              ; (启动http的用户 (open server))
;password=123               ; (默认的密码 (open server))

;[inet_http_server]         ; 默认禁用tcp监听的http 服务
;port=127.0.0.1:9001        ; (指定监听在本机ip地址和端口)
;username=user              ; (默认启动http服务的用户)
;password=123               ; (默认的密码)

[supervisord]
logfile=/tmp/supervisord.log ; (主日志文件的存放位置,默认在程序的工作启动目录)
logfile_maxbytes=50MB        ; (主日志文件的最大值,之后进行切割;默认 50MB)
logfile_backups=10           ; (主日志文件备份的数目;默认 10)
loglevel=info                ; (日志级别;默认是info; 其它: debug,warn,trace)
pidfile=/tmp/supervisord.pid ; (supervisord 运行时的pidfile路径;默认 supervisord.pid)
nodaemon=false               ; (如果为true,程序就以前台运行;默认是 false)
minfds=1024                  ; (min. 启动有效的文件描述符数目;默认 1024)
minprocs=200                 ; (min. 有效进程描述符;默认 200)
;umask=022                   ; (进程文件创建的默认权限;默认 022)
;user=chrism                 ; (默认是当前启动的用户)
;identifier=supervisor       ; (supervisord 标识符, 默认是'supervisor')
;directory=/tmp              ; (默认启动时间不会切换)
;nocleanup=true              ; (在启动时不清理临时文件;默认值为false)
;childlogdir=/tmp            ; ('AUTO' 子进程日志目录, 默认 $TEMP)
;environment=KEY="value"     ; (增加一个环境变量键值对:key=”value“)
;strip_ansi=false            ; (在log日志里去掉ansi转义编码; 默认是 false)

; 下面的部分选项必须保留在RPC的配置文件中
; (supervisorctl/web 接口) 使用以下配置来管理
; 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              ; should be same as http_username if set
;password=123                ; should be same as http_password if set
;prompt=mysupervisor         ; cmd line prompt (default "supervisor")
;history_file=~/.sc_history  ; use readline history if available

; 以下是被管理的示例程序显示所有可能用到的配置。
; 创建一个或“多个”程序: 要遵循以下的键值对规则。
; supervisor.

;[program:theprogramname]
;command=/bin/cat              ; 程序的启动命令 (使用绝对路径)
;process_name=%(program_name)s ; process_name 表示 (默认是 %(program_name)s)
;numprocs=1                    ; 启动时的进程数 (默认 1)
;directory=/tmp                ; 执行时切换到的目录 (def no cwd)
;umask=022                     ; umask for process (default None)
;priority=999                  ; 相对启动优先级(default 999)
;autostart=true                ; 是否跟随supervisord程序启动该监控程序 (default: true)
;startsecs=1                   ; # 在设定时间内,程序必须保持运行 (def. 1)
;startretries=3                ; 当启动失败时尝试的最大次数(default 3)
;autorestart=unexpected        ; 如果退出后,什么状态退出的去重启,默认非意外的(def: unexpected)
;exitcodes=0,2                 ; 'expected' 符合退出代码之后去重启 (default 0,2)
;stopsignal=QUIT               ; 用于杀死进程的信号 (default TERM)
;stopwaitsecs=10               ; 最大等待秒数 SIGKILL (default 10)
;stopasgroup=false             ; 发送停止信号到Unix进程组 (default false)
;killasgroup=false             ; SIGKILL UNIX进程组 (def false)
;user=chrism                   ; setuid to this UNIX account to run the program
;redirect_stderr=true          ; 是否开启程序标准错误输出的重定向 (default false)
;stdout_logfile=/a/path        ; 标准输出路径; default AUTO
;stdout_logfile_maxbytes=1MB   ; 文件最大大小 # 日志文件进行切割 (default 50MB)
;stdout_logfile_backups=10     ; # 日志文件备份数目 (default 10)
;stdout_capture_maxbytes=1MB   ; ‘捕获模式’中的字节数 (default 0)
;stdout_events_enabled=false   ; 在标准输出写入文件时发出事件 (default false)
;stderr_logfile=/a/path        ; 标准错误输出, NONE for none; default AUTO
;stderr_logfile_maxbytes=1MB   ; 文件最大大小 # 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"       ; 添加进程环境变量 (def no adds)
;serverurl=AUTO                ; 覆盖serverurl计算 (childutils)

;下面是event事件部分所有可能设置的值,大部分同上面一样。
; 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    ; 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 (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 (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  ; 这里的progname1,progname2就是定义的监控管理程序的名字,如[program:x]这里就是x
;priority=999                  ; the relative start priority (default 999)

;  下面的 [include] 选项只能包含一个files 设置,功能是定义supervisor管理程序的配置文件,可以单独的移除去,和主配置文件分开,方便。
; 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         ;定义管理监控程序的配置文件的路径
二、下面我们来举几个具体的实例

为了然我们管理的每一个进程(服务)都好便于管理,我们可以创建一个单独的目录存放我们需要管理的进程(服务),在这个目录下可以创建以.conf结尾的文件,我们要管理的进程的命令和配置都放在里面,最好给每一个我们要管理的进程创建一个以.conf结尾的文件。
创建我们需要管理各种进程的目录:

mkdir -p /etc/supervisor/conf.d

然后我们需要把supervisors.conf中的最后两行修改一下:

;[include]
;files = relative/directory/*.ini         ;定义管理监控程序的配置文件的路径

修改为:
[include]
files = /etc/supervisor/conf.d/*.conf

3、启动supervisord及supervisorctl一些常用命令:

supervisord -c /etc/supervisor/supervisord.conf

supervisorctl一些常用命令:

supervisorctl -c /etc/supervisor/supervisord.conf reload // 加载配置,重新启动supervisord

supervisorctl start all //启动所有进程
supervisorctl start redis //启动某一个进程
supervisorctl stop all //停止所有进程
supervisorctl stop redis //停止某一个进程

4、配置redis,MySQL,NGINX:

redis

/etc/supervisor/conf.d/redis.conf:

[program:redis]
command=/usr/local/bin/redis-server /opt/redis-4.0.9/redis.conf
autorestart=true
stopwaitsecs=1
startsecs=3
user=root
stopasgroup=true
stdout_logfile=/var/log/redis/redis.log
stderr_logfile=/var/log/redis/redis.log

由于 Supervisor 管理的进程不能设置为 daemon 模式,故如果 Redis 无法正常启动,可以查看一下 Redis 的配置,并将daemonize选项设置为 no。

daemonize no

mysql

/etc/supervisor/conf.d/mysql.conf

[program:mysql]
command=/usr/sbin/mysqld --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib64/mysql/plugin --user=mysql --log-error=/var/log/mysqld.log --pid-file=/var/run/mysqld/mysqld.pid --socket=/var/lib/mysql/mysql.sock
autorestart=true
stopwaitsecs=1
stopasgroup=true
user=mysql
stderr_logfile=/tmp/mysql_err.log
std_logfile=/tmp/mysql_out.log
nginx

/etc/supervisor/conf.d/nginx.conf:

[program:nginx]

command=/usr/sbin/nginx -c /etc/nginx/nginx.conf -g 'daemon off;'
stdout_logfile=/var/log/nginx/nginx_stdout.log
stdout_logfile_maxbytes=10MB
stderr_logfile=/var/log/nginx/nginx_stderr.log
stderr_logfile_maxbytes=10MB
user=root
autorestart=true

需要将NGINX设置为前台启动,两种方式:
1、/usr/sbin/nginx -c /etc/nginx/nginx.conf -g ‘daemon off;’
2、nginx默认以deamon的方式运行,
daemon off 注意配置文件的位置,在events上面,属于main。

user root;
worker_processes auto;
# 增加daemon off 配置
daemon off;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

# Load dynamic modules. See /usr/share/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}
Supervisord 开机启动

1、新建/usr/lib/systemd/system/supervisord.service文件:

# dservice for systemd (CentOS 7.0+)
# by ET-CS (https://github.com/ET-CS)
[Unit]
Description=Supervisor daemon
[Service]
Type=forking
ExecStart=/usr/bin/supervisord -c /etc/supervisor/supervisord.conf
ExecStop=/usr/bin/supervisorctl shutdown
ExecReload=/usr/bin/supervisorctl reload
KillMode=process
Restart=on-failure
RestartSec=3s
[Install]
WantedBy=multi-user.target

2、设置开机启动: systemctl enable supervisord
3、一些常用的systemctl命令:

启动supervisord服务
systemctl start supervisord.service

停止supervisord服务
systemctl stop supervisord.service

重启supervisord服务
systemctl restart supervisord.service

查看supervisord服务当前状态
systemctl status supervisord.service

设置supervisord服务开机自启动
systemctl enable supervisord.service

停止supervisord服务开机自启动
systemctl disable supervisord.service
Supervisor Web 管理界面

如果需要开启 Web 管理界面功能,需要在supervisord.conf配置中添加以下配置:

[inet_http_server]
port=:9001
username=admin
password=admin

然后,打开浏览器,输入地址 http://127.0.0.1:9001 后者公网ip:9001,便可以进入 Supervisor 提供的进程管理界面。

注意:
echo_supervisord_conf > /etc/supervisor/supervisord.conf 生成的conf文件的原内容是:

;[inet_http_server]         ; inet (TCP) server disabled by default
;port=127.0.0.1:9001        ; ip_address:port specifier, *:port for all iface
;username=admin             ; default is no username (open server)
;password=admin            ; default is no password (open server)

打开配置后,还需要修改port为port=:9001,不然会报如下错误:
Error: Cannot open an HTTP server: socket.error reported errno.EADDRNOTAVAIL (99)
详情见:http://supervisord.org/configuration.html#inet-http-server-section-settings
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/windy135/article/details/90204224