您好,登錄后才能下訂單哦!
怎么使用supervisor管理nginx+tomcat容器?很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細講解,有這方面需求的人可以來學習下,希望你能有所收獲。
需求:
使用docker來啟動nginx + tomcat 雙進程,實際應用中,多進程還是比較常見的。
1: 創建dockerfile目錄
mkdir -p /docker/web
2: 編寫dockerfile: /docker/web/Dockerfile
FROM centos7 MAINTAINER lin test@163.com COPY CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo COPY nginx_install.sh /tmp/nginx_install.sh RUN sh /tmp/nginx_install.sh; \rm -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.gz COPY tomcat_install.sh /tmp/tomcat_install.sh RUN sh /tmp/tomcat_install.sh; \rm -rf /usr/local/src/* COPY supervisor_install.sh /tmp/supervisor_install.sh COPY supervisord.conf /etc/supervisord.conf COPY start_tomcat.sh /usr/local/tomcat/bin/mystart.sh RUN sh /tmp/supervisor_install.sh; \rm -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 - Base baseurl=http://mirrors.163.com/centos/$releasever/os/$basearch/ gpgcheck=1 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7 #released updates [updates] name=CentOS-$releasever - Updates baseurl=http://mirrors.163.com/centos/$releasever/updates/$basearch/ gpgcheck=1 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7 #additional packages that may be useful [extras] name=CentOS-$releasever - Extras baseurl=http://mirrors.163.com/centos/$releasever/extras/$basearch/ gpgcheck=1 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7 #additional packages that extend functionality of existing packages [centosplus] name=CentOS-$releasever - Plus baseurl=http://mirrors.163.com/centos/$releasever/centosplus/$basearch/ gpgcheck=1 enabled=0 gpgkey=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/src wget 'http://nginx.org/download/nginx-1.12.2.tar.gz' tar -zxvf nginx-1.12.2.tar.gz cd nginx-1.12.2 ./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-stream --with-stream_ssl_module make make install exit 0
3.3tomcat安裝腳本
[root@docker web]# cat tomcat_install.sh yum install -y wget tar cd /usr/local/src/ tar -zxvf jdk-8u162-linux-x64.tar.gz mv 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/profile echo 'PATH=$PATH:$JAVA_HOME/bin' >>/etc/profile echo 'CLASSPATH=.:$JAVA_HOME/lib/tools.jar:$JAVA_HOME/lib/dt.jar:$CLASSPATH' >>/etc/profile source /etc/profile wget https://mirrors.tuna.tsinghua.edu.cn/apache/tomcat/tomcat-8/v8.5.38/bin/apache-tomcat-8.5.38.tar.gz tar -zxvf apache-tomcat-8.5.38.tar.gz mv apache-tomcat-8.5.38 /usr/local/tomcat
3.4dockerfile文件中涉及到的配置文件、腳本、安裝包如下
[root@docker web]# ll total 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-release yum -y install python2-pip pip 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 ;pid nodaemon=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 ; 前臺啟動nginx autostart=true ; 隨著supervisor自動啟動 startsecs=10 ; 啟動10s后算正常啟動 autorestart=true ; 程序退出后自動重啟 startretries=3 ; 啟動失敗自動重試次數 stdout_logfile_maxbytes=20MB ;stdout 日志文件大小最大20Mb stdout_logfile=/usr/local/nginx/logs/out.log [program:tomcat] command=sh /usr/local/tomcat/bin/mystart.sh ; 前臺啟動tomcat autostart=true ; 隨著supervisor自動啟動 startsecs=10 ; 啟動10s后算正常啟動 autorestart=true ; 程序退出后自動重啟 startretries=3 ; 啟動失敗自動重試次數 stdout_logfile_maxbytes=20MB ;stdout 日志文件大小最大20Mb stdout_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/web docker build -t shijiange_web . [root@docker web]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE shijiange_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 ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 76782ab71c3b shijiange_web "container-entrypo..." 12 seconds ago Up 12 seconds happy_jones
9.測試驗證:
[root@docker web]# docker exec -it 76782ab /bin/bash bash-4.2# ifconfig
10.容器驗證:curl nginx
11.容器驗證:curl tomcat
看完上述內容是否對您有幫助呢?如果還想對相關知識有進一步的了解或閱讀更多相關文章,請關注億速云行業資訊頻道,感謝您對億速云的支持。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。