您的位置:首页 > 技术中心 > 运维 >

怎么使用supervisor管理nginx和tomcat容器

时间:2023-05-12 23:12

需求:

使用docker来启动nginx + tomcat 双进程,实际应用中,多进程还是比较常见的。

1: 创建dockerfile目录

mkdir -p /docker/web

2: 编写dockerfile: /docker/web/dockerfile

from centos7maintainer lin test@163.comcopy centos-base.repo /etc/yum.repos.d/centos-base.repocopy nginx_install.sh /tmp/nginx_install.shrun sh /tmp/nginx_install.sh; 
m -rf /usr/local/src/*run sed -i -e '/worker_processes/a daemon off;' /usr/local/nginx/conf/nginx.conf; copy jdk-8u162-linux-x64.tar.gz /usr/local/src/jdk-8u162-linux-x64.tar.gzcopy tomcat_install.sh /tmp/tomcat_install.shrun sh /tmp/tomcat_install.sh; 
m -rf /usr/local/src/* copy supervisor_install.sh /tmp/supervisor_install.shcopy supervisord.conf /etc/supervisord.confcopy start_tomcat.sh /usr/local/tomcat/bin/mystart.shrun sh /tmp/supervisor_install.sh; 
m -rf /tmp/*.sh

3: dockerfile集成的配置文件及安装文件

3.1 默认源下载较慢,更换yum源,以下centos-base.repo配置文件拷贝到容器里更换

copy centos-base.repo /etc/yum.repos.d/centos-base.repo[root@docker web]# cat centos-base.repo [base]name=centos-$releasever - basebaseurl=http://mirrors.163.com/centos/$releasever/os/$basearch/gpgcheck=1gpgkey=file:///etc/pki/rpm-gpg/rpm-gpg-key-centos-7 #released updates[updates]name=centos-$releasever - updatesbaseurl=http://mirrors.163.com/centos/$releasever/updates/$basearch/gpgcheck=1gpgkey=file:///etc/pki/rpm-gpg/rpm-gpg-key-centos-7 #additional packages that may be useful[extras]name=centos-$releasever - extrasbaseurl=http://mirrors.163.com/centos/$releasever/extras/$basearch/gpgcheck=1gpgkey=file:///etc/pki/rpm-gpg/rpm-gpg-key-centos-7 #additional packages that extend functionality of existing packages[centosplus]name=centos-$releasever - plusbaseurl=http://mirrors.163.com/centos/$releasever/centosplus/$basearch/gpgcheck=1enabled=0gpgkey=file:///etc/pki/rpm-gpg/rpm-gpg-key-centos-7

3.2nginx安装脚本

[root@docker web]# cat nginx_install.sh yum install -y wget tar gcc gcc-c++ make pcre pcre-devel zlib zlib-devel openssl openssl-devel cd /usr/local/srcwget 'http://nginx.org/download/nginx-1.12.2.tar.gz'tar -zxvf nginx-1.12.2.tar.gzcd nginx-1.12.2./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-stream --with-stream_ssl_modulemakemake installexit 0

3.3tomcat安装脚本

[root@docker web]# cat tomcat_install.sh yum install -y wget tarcd /usr/local/src/tar -zxvf jdk-8u162-linux-x64.tar.gzmv jdk1.8.0_162 /usr/local/#/usr/local/jdk1.8.0_162/bin/java -version #配置java环境变量echo 'java_home=/usr/local/jdk1.8.0_162/' >>/etc/profileecho 'path=$path:$java_home/bin' >>/etc/profileecho 'classpath=.:$java_home/lib/tools.jar:$java_home/lib/dt.jar:$classpath' >>/etc/profilesource /etc/profile wget https://mirrors.tuna.tsinghua.edu.cn/apache/tomcat/tomcat-8/v8.5.38/bin/apache-tomcat-8.5.38.tar.gztar -zxvf apache-tomcat-8.5.38.tar.gzmv apache-tomcat-8.5.38 /usr/local/tomcat

3.4dockerfile文件中涉及到的配置文件、脚本、安装包如下

[root@docker web]# lltotal 185384-rw-r--r-- 1 root root 835 mar 9 01:12 centos-base.repo-rw-r--r-- 1 root root 669 mar 9 01:11 dockerfile-rw-r--r-- 1 root root 189815615 mar 9 01:15 jdk-8u162-linux-x64.tar.gz-rw-r--r-- 1 root root 340 mar 9 01:13 nginx_install.sh-rw-r--r-- 1 root root 581 mar 9 01:17 tomcat_install.sh

4: 一键安装supervisor: /docker/web/supervisor_install.sh

yum -y install epel-releaseyum -y install python2-pippip install supervisor

5: supervisor配置文件: /docker/web/supervisord.conf

[unix_http_server]file=/tmp/supervisor.sock ; the path to the socket file [supervisord]logfile=/tmp/supervisord.log ; 日志logfile_maxbytes=50mb ; 最大50m日志logfile_backups=10 ; 轮循日志备份10个loglevel=info ; 日志等级记录info的pidfile=/tmp/supervisord.pid ;pidnodaemon=true ;在前台启动minfds=102400 ; 文件描述符限制minprocs=2000 ; 进程数 [rpcinterface:supervisor]supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface [supervisorctl]serverurl=unix:///tmp/supervisor.sock ; use a unix:// url for a unix socket [program:nginx]command=/usr/local/nginx/sbin/nginx ; 前台启动nginxautostart=true ; 随着supervisor自动启动startsecs=10 ; 启动10s后算正常启动autorestart=true ; 程序退出后自动重启startretries=3 ; 启动失败自动重试次数stdout_logfile_maxbytes=20mb ;stdout 日志文件大小最大20mbstdout_logfile=/usr/local/nginx/logs/out.log [program:tomcat]command=sh /usr/local/tomcat/bin/mystart.sh ; 前台启动tomcatautostart=true ; 随着supervisor自动启动startsecs=10 ; 启动10s后算正常启动autorestart=true ; 程序退出后自动重启startretries=3 ; 启动失败自动重试次数stdout_logfile_maxbytes=20mb ;stdout 日志文件大小最大20mbstdout_logfile=/usr/local/tomcat/logs/catalina.out

6: tomcat的启动脚本/docker/web/start_tomcat.sh

#由于supervisor无法使用source,需要编写个脚本来启动source /etc/profile/usr/local/tomcat/bin/catalina.sh run

7: 构建镜像

cd /docker/webdocker build -t shijiange_web .[root@docker web]# docker imagesrepository tag image id created sizeshijiange_web latest bc06a9974252 7 seconds ago 1.33 gb

8: 启动容器测试

[root@docker web]# docker run -d shijiange_web /bin/bash -c 'supervisord -c /etc/supervisord.conf'76782ab71c3b1d2f818ad76214d6336ae11a524eeb9d211f154fe4ad5226015d[root@docker web]# [root@docker web]# docker pscontainer id image command created status ports names76782ab71c3b shijiange_web "container-entrypo..." 12 seconds ago up 12 seconds happy_jones

9.测试验证:

[root@docker web]# docker exec -it 76782ab /bin/bashbash-4.2# ifconfig

怎么使用supervisor管理nginx和tomcat容器

10.容器验证:curl nginx

怎么使用supervisor管理nginx和tomcat容器

11.容器验证:curl tomcat

怎么使用supervisor管理nginx和tomcat容器

以上就是怎么使用supervisor管理nginx和tomcat容器的详细内容,更多请关注Gxl网其它相关文章!

热门排行

今日推荐

热门手游