Docker可以在启动容器的时候通过设置Dockerfile中的CMD条目启动一个进程,但如果要在容器中同时启动多个进程,就需要使用进程管理工具了。是一个非常优秀的进程管理工具,使用Python开发。它可以在类UNIX系统的方式让用户来准确地监视和控制后台一定数量的服务进程。并作为一个天使进程让后台进程在当发生内部错误退出、或者进程被意外杀死时自动重启。除此之外,supervisord可以监控TCP端口,让其他主机通过客户端了命令supervisorctl通过HTTP协议直接对Server端进程进行启停,避免让进程/服务器管理者直接接触Shell或root用户。进程之间也有一个优先级和进程组关系,让管理员使用start all和stop all的关系来启动.
解决问题
应用程序进程的控制
多应用进程的管理
应用中断后的快速恢复
Centos下安装
1.yum -y install python-setuptools
2.Wget --no-check-certificate https://pypi.python.org/packages/source/s/supervisor/supervisor-3.0.tar.gz
3.tar -zxvf supervisor-3.0.tar.gz
4.cd supervisor-3.0
5.python setup.py install
6.echo_supervisord_conf >/etc/supervisord.conf
7.Python进入python
输入import supervisor
编辑配置文件
Vim /etc/supervisord.conf
[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服务配置
; Web管理界面设定
port=127.0.0.1:9001 ; tcp端口
username=user ; tcp登陆用户
password=123 ; tcp登陆密码
[supervisord] ; 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文件
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)
[supervisorctl]
serverurl=unix:///tmp/supervisor.sock ; use a unix:// URL for a unix socket
;username=chris ; 如果设置应该与http_username相同
;password=123 ; 如果设置应该与http_password相同
;prompt=mysupervisor ; 命令行提示符,默认"supervisor"
;history_file=~/.sc_history ; 命令行历史纪录
; 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
; 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)
[group:appgroup]
programs=wapp,wfapp ; 任何在[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
可以在配置文件中添加你想要管理的进程,模块如下
[program:cobar]
; 被监控程序指定的运行脚本
; 如果command的栏运行的是shell脚本,那么在shell 脚本启动被监控程序时要用exec修饰。例如:
; echo $RUN_CMD
; eval exec $RUN_CMD
; 否则,supervisord停止不了启动的进程.除此之外,shell脚本里不能出现&之类的后台运行符号
command=/bin/sh /home/jfy/soft/cobar-server-1.2.7/bin/startup2.sh
;process_name=%(program_name)s ; 进程名表达式,默认为%(program_name)s
;numprocs=1 ; 默认启动的进程数目,默认为1
directory=/home/jfy/soft/cobar-server-1.2.7/bin ; 在运行前cwd到指定的目录,默认不执行cmd
;umask=022 ; 进程umask,默认None
priority=1 ; 程序运行的优先级,默认999
autostart=true ; 默认随supervisord自动启动,默认true
autorestart=true ; 被监控程序异常中断是否自动重启
startsecs=5 ; 被监控程序启动时持续时间
startretries=5 ; 被监控程序启动失败重试的次数
;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=root ; 为运行程序的unix帐号设置setuid
redirect_stderr=true ; 将标准错误重定向到标准输出,默认false
stdout_logfile=/home/jfy/soft/cobar-server-1.2.7/logs/console.log ; stdou 重定向输出文件
;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)
实例:
[program:nginx]
command=/usr/sbin/nginx
process_name=%(program_name)s
stdout_logfile=/root/httpd.log
autostart=true
autorestart=true
priority=1
启动supervisor:
Supervisord -c /etc/supervisord.conf
查看状态
Supervisorctl status
Docker容器中监控进程
1.创建一个目录
mkdir web
cd web
Vim Dockerfile
FROM centos
RUN yum -y install python-setuptools openssh-server httpd wget
RUN easy_install supervisor
RUN mkdir -p /var/run/sshd
RUN mkdir -p /var/log/supervisor
RUN echo_supervisord_conf >/etc/supervisord.conf
RUN echo "[program:ssh]" >>/etc/supervisord.conf
RUN echo "command=/usr/sbin/sshd -D" >>/etc/supervisord.conf
RUN echo "[program:apache]" >>/etc/supervisord.conf
RUN echo "command=httpd -DFOREGROUND" >>/etc/supervisord.conf
EXPOSE 22 80
CMD ["/usr/bin/supervisord -c /etc/supervisord.conf"]
执行
docker build -t="jam/web" .
-t 是为新镜像设置仓库和名称
docker run -p 22 -p 80 -t -i jam/web /bin/bash
原文:http://zengxh.blog.51cto.com/10650604/1713838