2018-05-18 在docker容器上跑supervisord进程管家

一、mysupervisord文件夹准备
[weis@localhost ~]$ mkdir mysupervisord
[weis@localhost mysupervisord]$ cd mysupervisord
[weis@localhost mysupervisord]$ vim supervisord        #内容见附件
[weis@localhost mysupervisord]$ chmod +x supervisord
[weis@localhost mysupervisord]$ vim backup.sh        #内容见附件
[weis@localhost mysupervisord]$ chmod +x backup.sh
[weis@localhost mysupervisord]$ vim Dockerfile        #内容见附件
[weis@localhost mysupervisord]$ wget https://files.pythonhosted.org/packages/45/a0/317c6422b26c12fe0161e936fc35f36552069ba8e6f7ecbd99bbffe32a5f/meld3-1.0.2.tar.gz
[weis@localhost mysupervisord]$ wget https://files.pythonhosted.org/packages/44/60/698e54b4a4a9b956b2d709b4b7b676119c833d811d53ee2500f1b5e96dc3/supervisor-3.3.4.tar.gz
[weis@localhost mysupervisord]$ wget https://pypi.python.org/packages/6f/10/5398a054e63ce97921913052fde13ebf332a3a4104c50c4d7be9c465930e/setuptools-26.1.1.zip#md5=f81d3cc109b57b715d46d971737336db
[weis@localhost mysupervisord]$ wget https://files.pythonhosted.org/packages/62/af/2370dc19c942c8d0468add39b33c17963af6d49da6f36b325843e76a4f77/importlib-1.0.1.zip
[weis@localhost mysupervisord]$ unzip setuptools-26.1.1.zip    #centos:6.9镜像未带unzip命令,所以先行解压,再拷贝到镜像中
[weis@localhost mysupervisord]$ unzip importlib-1.0.4.zip
[weis@localhost mysupervisord]$ rm setuptools-26.1.1.zip
[weis@localhost mysupervisord]$ rm importlib-1.0.4.zip
[weis@localhost mysupervisord]$ ll
总用量 464
-rwxrwxr-x 1 weis weis    219 5月  18 09:40 backup.sh
-rw-r--r-- 1 weis weis    727 5月  18 15:57 Dockerfile
drwxrwxr-x 3 weis weis     88 5月  18 13:56 importlib-1.0.4
-rw-rw-r-- 1 weis weis  36478 5月  18 09:33 meld3-1.0.2.tar.gz
drwxrwxr-x 7 weis weis   4096 5月  18 14:35 setuptools-26.1.1
-rw-rw-r-- 1 weis weis 419794 5月  18 09:33 supervisor-3.3.4.tar.gz
-rwxrwxr-x 1 weis weis   2836 5月  18 09:41 supervisord

二、mysupervisord容器的生成:
docker build -t mysupervisord:v1 .
docker run --name mysupervisord -d mysupervisord:v1    #不可以加-it交互参数,具体原因见报错3
docker exec -it mysupervisord bash

三、附件
supervisord文件内容:
#!/bin/bash
#
# supervisord  Startup script for the Supervisor process control system
#
# Author:    Mike McGrath <[email protected]> (based off yumupdatesd)
#        Jason Koppe <[email protected]> adjusted to read sysconfig,
#          use supervisord tools to start/stop, conditionally wait
#          for child processes to shutdown, and startup later
#        Erwan Queffelec <[email protected]>
#          make script LSB-compliant
#
# chkconfig:   345 83 04
# description: Supervisor is a client/server system that allows \
#  its users to monitor and control a number of processes on \
#  UNIX-like operating systems.
# processname: supervisord
# config: /etc/supervisord.conf
# config: /etc/sysconfig/supervisord
# pidfile: /var/run/supervisord.pid
#
### BEGIN INIT INFO
# Provides: supervisord
# Required-Start: $all
# Required-Stop: $all
# Short-Description: start and stop Supervisor process control system
# Description: Supervisor is a client/server system that allows
#  its users to monitor and control a number of processes on
#  UNIX-like operating systems.
### END INIT INFO
 
# Source function library
. /etc/rc.d/init.d/functions
 
# Source system settings
if [ -f /etc/sysconfig/supervisord ]; then
   . /etc/sysconfig/supervisord
fi
 
# Path to the supervisorctl script, server binary,
# and short-form for messages.
supervisorctl=/usr/bin/supervisorctl
supervisord=${SUPERVISORD-/usr/bin/supervisord}
prog=supervisord
pidfile=${PIDFILE-/var/run/supervisord.pid}
lockfile=${LOCKFILE-/var/lock/subsys/supervisord}
STOP_TIMEOUT=${STOP_TIMEOUT-60}
OPTIONS="${OPTIONS--c /etc/supervisord.conf}"
RETVAL=0
 
start() {
   echo -n $"Starting $prog: "
   daemon --pidfile=${pidfile} $supervisord $OPTIONS
   RETVAL=$?
   echo
   if [ $RETVAL -eq 0 ]; then
     touch ${lockfile}
     $supervisorctl $OPTIONS status
   fi
   return $RETVAL
}
 
stop() {
   echo -n $"Stopping $prog: "
   killproc -p ${pidfile} -d ${STOP_TIMEOUT} $supervisord
   RETVAL=$?
   echo
   [ $RETVAL -eq 0 ] && rm -rf ${lockfile} ${pidfile}
}
 
reload() {
   echo -n $"Reloading $prog: "
   LSB=1 killproc -p $pidfile $supervisord -HUP
   RETVAL=$?
   echo
   if [ $RETVAL -eq 7 ]; then
     failure $"$prog reload"
   else
     $supervisorctl $OPTIONS status
   fi
}
 
restart() {
   stop
   start
}
 
case "$1" in
   start)
     start
     ;;
   stop)
     stop
     ;;
   status)
     status -p ${pidfile} $supervisord
     RETVAL=$?
     [ $RETVAL -eq 0 ] && $supervisorctl $OPTIONS status
     ;;
   restart)
     restart
     ;;
   condrestart|try-restart)
     if status -p ${pidfile} $supervisord >&/dev/null; then
      stop
      start
     fi
     ;;
   force-reload|reload)
     reload
     ;;
   *)
     echo $"Usage: $prog {start|stop|restart|condrestart|try-restart|force-reload|reload}"
     RETVAL=2
esac
 
exit $RETVAL


backup.sh文件内容:
#!/bin/bash
source="/etc /home /root /var/lib /var/spool/{cron,at,mail}"
target="/backups/backup-system-$(date +%Y-%m-%d).tar.gz"
[ ! -d /backups ] && mkdir /backups
tar -zcvf ${target} ${source} &> /backups/backup.log


Dockerfile文件内容:
FROM centos:6.9
ADD meld3-1.0.2.tar.gz /
ADD supervisor-3.3.4.tar.gz /
COPY importlib-1.0.4 /importlib
COPY setuptools-26.1.1 /setuptools
COPY supervisord /etc/init.d/
COPY backup.sh /backups/
RUN cd /importlib \
    && python setup.py install --record log \
    && cd /setuptools \
    && python setup.py install --record log \
    && cd /meld3-1.0.2 \
    && python setup.py install --record log \
    && cd /supervisor-3.3.4 \
    && python setup.py install --record log \
    && echo_supervisord_conf > /etc/supervisord.conf \
    && mkdir -p /var/log/supervisor/ \
    && chkconfig --add supervisord \
    && echo "[program:backup]" >> /etc/supervisord.conf \
    && echo "command=/backups/backup.sh" >> /etc/supervisord.conf
CMD ["supervisord","-n"]



四、报错及解决办法
报错1:
[root@defccb488a15 ~]# python /supervisor-3.3.4/setup.py install --record log
Traceback (most recent call last):
  File "/supervisor-3.3.4/setup.py", line 32, in <module>
    from setuptools import setup, find_packages
ImportError: No module named setuptools
解决办法,下载组件setuptools、importlib并解压。

报错2:
unix:///tmp/supervisor.sock no such file
解决办法:
Dockerfile文件中添加容器启动命令:CMD ["supervisord"]。
注:尝试了将supervisord、supervisorctl放到RUN中执行是无效的。["supervisorctl","status、start、reloed等参数"]放到CMD中执行也不会起作用,因为supervisord服务未启动,supervisord只能再CMD命令中启动。
建立容器的命令中:docker run -it --name mysupervisord -d mysupervisord:v1 最后不要加bash参数,添加的话会将Dockerfile文件中的CMD覆盖。

报错3:
[weis@localhost mysupervisord]$ docker ps -a    #容器退出了(Exited)
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                      PORTS               NAMES
75a2d6509a53        mysupervisord:v1    "/bin/sh -c supervis…"   14 minutes ago      Exited (0) 14 minutes ago                       a6
解决办法:
镜像前台必须有东西再跑。
Dockerfile文件中将supervisord放到前台运行;
docker run容器生成时不加-it交互参数。

猜你喜欢

转载自www.cnblogs.com/shoubituling/p/9058219.html