docker之使用supervisor管理多个进程

docker题外话:

    centos7安装supervisor:

    源码编译安装:

下载源码文件:supervisor-3.3.1.tar.gz
下载地址:https://pypi.python.org/pypi/supervisor
1
2
3
4
5
6
7
8
9
10
11
12
13
14
安装:
[root@cenots7 src] # tar -zxf supervisor-3.3.1.tar.gz
[root@cenots7 src] # cd supervisor-3.3.1/
[root@cenots7 supervisor-3.3.1] # python setup.py install
 
 
检查是否安装成功:
登陆python控制台输入 import  supervisor 查看是否能成功加载
[root@cenots7 supervisor-3.3.1] # python
Python 2.7.5 (default, Sep 15 2016, 22:37:39) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-4)] on linux2
Type  "help" "copyright" "credits"  or  "license"  for  more  information.
>>>  import  supervisor
>>>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
生成配置文件:
[root@cenots7 supervisor-3.3.1] # mkdir /etc/supervisor
[root@cenots7 supervisor-3.3.1] # echo_supervisord_conf > /etc/supervisor/supervisord.conf
  
[root@cenots7 supervisor-3.3.1] # grep -E -v '^;|^$' /etc/supervisor/supervisord.conf
[unix_http_server]
file = /tmp/supervisor .sock   ; (the path to the socket  file )
[supervisord]
logfile= /tmp/supervisord .log ; (main log  file ;default $CWD /supervisord .log)
logfile_maxbytes=50MB        ; (max main logfile bytes b4 rotation;default 50MB)
logfile_backups=10           ; (num of main logfile rotation backups;default 10)
loglevel=info                ; (log level;default info; others: debug,warn,trace)
pidfile= /tmp/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                 ; (min. avail process descriptors;default 200)
[rpcinterface:supervisor]
supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface
[supervisorctl]
serverurl=unix: ///tmp/supervisor .sock ; use a unix: //  URL   for  a unix socket


1
2
3
4
5
6
7
8
9
10
11
12
启动服务:
[root@cenots7 tmp] # supervisord -c /etc/supervisor/supervisord.conf
  
关闭服务:
[root@cenots7 tmp] # supervisorctl shutdown
Shut down
  
查看状态:
[root@cenots7 tmp] # supervisorctl status
 
启动或停止某一个服务:
supervisorctl stop|start program_name


    yum直接安装:

1
2
3
4
5
rpm -Uvh https: //mirrors .ustc.edu.cn /fedora/epel/7/x86_64/e/epel-release-7-8 .noarch.rpm
yum update;yum -y  install  supervisor
cat  /etc/supervisord .conf
 
supervisord -c  /etc/supervisord .conf



说明:

    Docker 容器在启动的时候开启单个进程,比如,一个 ssh 或者 apache 的 daemon 服务。但我们经常需要在一个机器上开启多个服务,这可以有很多方法,最简单的就是把多个启动命令放到一个启动脚本里面,启动的时候直接启动这个脚本,另外就是安装进程管理工具

    使用进程管理工具 supervisor 来管理容器中的多个进程。使用 Supervisor 可以更好的控制、管理、重启我们希望运行的进程

扫描二维码关注公众号,回复: 1433534 查看本文章

    在这里我们演示一下容器中如何同时使用 ssh 和 tomcat 服务



配置:

    首先创建Dockerfile文件:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
[root@cenots7 shencj] # vim Dockerfile
  
# centos:ssh
#
# VERSION               0.0.1
  
FROM centos
MAINTAINER shencj  "[email protected]"
  
#ssh
RUN yum  install  -y openssh openssh-server openssh-clients
RUN  mkdir  /var/run/sshd
RUN  ssh -keygen -t rsa -f  /etc/ssh/ssh_host_rsa_key
RUN  ssh -keygen -t dsa -f  /etc/ssh/ssh_host_dsa_key
RUN  sed  -i  's/#UseDNS yes/UseDNS no/g'  /etc/ssh/sshd_config
RUN  sed  -i  's/GSSAPIAuthentication yes/GSSAPIAuthentication no/g'  /etc/ssh/sshd_config
RUN  /bin/echo  'root:123456'  |chpasswd
RUN  /bin/sed  -i  's/.*session.*required.*pam_loginuid.so.*/session optional pam_loginuid.so/g'  /etc/pam .d /sshd
RUN  /bin/echo  -e  "LANG=\"en_US.UTF-8\""  /etc/default/local
  
#tomcat
ADD apache-tomcat-7.0.41. tar .gz  /usr/local/src/
COPY jdk-7u80-linux-x64.rpm  /usr/local/src/
WORKDIR  /usr/local/src/
RUN rpm -ivh jdk-7u80-linux-x64.rpm
ENV JAVA_HOME  /usr/java/jdk1 .7.0_80
ENV PATH $PATH:$JAVA_HOME /bin
ENV CLASSPATH .:$JAVA_HOME /jre/lib/rt .jar:$JAVA_HOME /lib/dt .jar:$JAVA_HOME /lib/tools .jar
#RUN java -version
RUN  mkdir  -p  /usr/local/tools
RUN  cp  -r apache-tomcat-7.0.41  /usr/local/tools/tomcat7_8080
  
#supervisor
RUN rpm -Uvh https: //mirrors .ustc.edu.cn /fedora/epel/7/x86_64/e/epel-release-7-8 .noarch.rpm
RUN yum update;yum -y  install  supervisor
RUN  mkdir  -p  /etc/supervisor/
COPY supervisord.conf  /etc/supervisor/
  
EXPOSE 22 8080
CMD supervisord -c  /etc/supervisor/supervisord .conf
#CMD ["supervisord","-c","/etc/supervisor/supervisord.conf"]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
[root@cenots7 shencj] # vim supervisord.conf 
  
[unix_http_server]
file = /var/run/supervisor/supervisor .sock   ; (the path to the socket  file )
  
[supervisord]
logfile= /var/run/supervisor/supervisord .log ; (main log  file ;default $CWD /supervisord .log)
logfile_maxbytes=50MB        ; (max main logfile bytes b4 rotation;default 50MB)
logfile_backups=10           ; (num of main logfile rotation backups;default 10)
loglevel=info                ; (log level;default info; others: debug,warn,trace)
pidfile= /var/run/supervisor/supervisord .pid ; (supervisord pidfile;default supervisord.pid)
nodaemon= true                ; (start  in  foreground  if  true ;default  false )
minfds=1024                  ; (min. avail startup  file  descriptors;default 1024)
minprocs=200                 ; (min. avail process descriptors;default 200)
  
[rpcinterface:supervisor]
supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface
  
[supervisorctl]
serverurl=unix: ///var/run/supervisor/supervisor .sock ; use a unix: //  URL   for  a unix socket
  
[program:sshd]
command = /usr/sbin/sshd  -D
  
[program:tomcat]
command = /usr/local/tools/tomcat7_8080/bin/catalina .sh run

    注意

        serverurl=unix:///var/run/supervisor/supervisor.sock : 这个建议不要修改

        nodaemon=true : 设置为true

        command=/usr/local/tools/tomcat7_8080/bin/catalina.sh run :这个必须这样写(supervisor管理tomcat必须这样启动,其他方式好像都有问题),参考:http://serverfault.com/questions/425132/controlling-tomcat-with-supervisor

 

    创建镜像:

[root@cenots7 shencj]# docker build -t shencj/centos-ssh-tomcat:v1 .


    运行容器:

1
2
3
[root@cenots7 shencj] # docker run -d --name ssh-tomcat --restart=always -p 4426:22 -p 82:8080 shencj/centos-ssh-tomcat:v1
cc8006c07f6c703c476c69ecc3699f0c9a1e5f456949e65a8e92b55dafa8be1e
[root@cenots7 shencj] #


   查看容器:    

1
2
3
[root@cenots7 shencj] # docker ps -a
CONTAINER ID        IMAGE                         COMMAND                  CREATED             STATUS              PORTS                                        NAMES
cc8006c07f6c        shencj /centos-ssh-tomcat :v1    "/bin/sh -c 'supervis"    56 seconds ago      Up 54 seconds       0.0.0.0:4426->22 /tcp , 0.0.0.0:82->8080 /tcp    ssh -tomcat


   登录容器:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
[root@cenots7 shencj] # ssh localhost -p 4426
root@localhost's password: 
[root@cc8006c07f6c ~] # ps -ef 
UID         PID   PPID  C STIME TTY          TIME CMD
root          1      0  0 03:56 ?        00:00:00  /usr/bin/python  /usr/bin/supervisord  -c  /etc/supervisor/supervisord .conf
root          7      1  0 03:56 ?        00:00:00  /usr/sbin/sshd  -D
root          8      1  3 03:56 ?        00:00:03  /usr/java/jdk1 .7.0_80 /bin/java  -Djava.util.logging.config. file = /usr/local/tools/tomcat7_8080/conf/logging .properties -Djava.util.logging.manager=org.apa
root         30      7  0 03:58 ?        00:00:00 sshd: root@pts /0
root         32     30  0 03:58 pts /0     00:00:00 - bash
root         45     32  0 03:58 pts /0     00:00:00  ps  -ef
 
[root@cc8006c07f6c ~] # supervisorctl status
sshd                             RUNNING   pid 7, uptime 0:02:58
tomcat                           RUNNING   pid 8, uptime 0:02:58


    访问tomcat:

        http://ip:82

spacer.gifwKiom1f9vTSQ647GAAFbt8Ui09M497.png-wh_50

猜你喜欢

转载自blog.csdn.net/lemontree1945/article/details/80508443