Supervisor

该板块内容来自 Kev 的贡献。

Supervisor is a client/server system that allows its users to monitor and control a number of processes on UNIX-like operating systems.


由于docker运行container时,只能启动一个主进程。如果我们需要同时启动多个进程,就可以借助supervisor启动/管理它们。

另外supervisor还自带一个WEB客户端,让其管理的进程一目了然。


安装

sudo apt-get install supervisor


配置

tree /etc/supervisor/
  ├── conf.d
  │   ├── haproxy.conf
  │   ├── renderjs.conf
  │   └── restart.conf
  └── supervisord.conf
supervisord.conf
; supervisor config file
 
[unix_http_server]
file=/var/run/supervisor.sock
chmod=0700
 
[inet_http_server]
port = 0.0.0.0:9001
 
[supervisord]
logfile=/var/log/supervisor/supervisord.log
pidfile=/var/run/supervisord.pid
childlogdir=/var/log/supervisor
 
[rpcinterface:supervisor]
supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface
 
[supervisorctl]
serverurl=http://127.0.0.1:9001
 
[include]
files = /etc/supervisor/conf.d/*.conf
haproxy.conf
[program:haproxy]
command = /usr/sbin/haproxy -f /etc/haproxy/haproxy.cfg -p /var/run/haproxy.pid
renderjs.conf
[program:renderjs]
command = /usr/local/bin/phantomjs /usr/local/bin/render.js %(process_num)d
numprocs_start = 8080
numprocs = 10
process_name = %(program_name)s-%(process_num)d
restart.conf
[program:restart]
command = /usr/local/bin/restart.sh


测试

⚠ 在本机测试

# 服务端
sudo service supervisor start
 
# 客户端(CLI)
supervisorctl
 
# 客户端(WEB)
firefox http://localhost:9001


Dockerfile

Dockerfile
#
# Dockerfile for renderjs
#
 
FROM ubuntu:14.04
MAINTAINER kev
 
RUN apt-get update && apt-get install -y supervisor haproxy libfontconfig1 libfreetype6
 
ADD ./phantomjs /usr/local/bin/
ADD ./render.js /usr/local/bin/
ADD ./restart.sh /usr/local/bin/
ADD ./haproxy.cfg /etc/haproxy/
ADD ./supervisor/supervisord.conf /etc/supervisor/
ADD ./supervisor/conf.d/haproxy.conf /etc/supervisor/conf.d/
ADD ./supervisor/conf.d/renderjs.conf /etc/supervisor/conf.d/
ADD ./supervisor/conf.d/restart.conf /etc/supervisor/conf.d/
 
EXPOSE 1024 9001
 
CMD /usr/bin/supervisord -n -c /etc/supervisor/supervisord.conf


注意

  • [program:xxx]进程不可在后台运行 (例如: haproxy -D不可取)
  • 通过配置[inet_http_server]允许HTTP访问
  • xxx.conf不能递归include其他conf
  • Dockerfile中使用CMD定义入口程序, 使用-nsupervisord在前台运行

FIXME

Jovi Meng 2017/09/06 10:25