Docker Nginx容器和Tomcat容器实现负载均衡与动静分离操作
这篇文章主要介绍了Docker Nginx容器和Tomcat容器实现负载均衡与动静分离操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧下载Tomcat8镜像12345[root@localhost ~]# docker search tomcat8NAME DESCRIPTION STARS OFFICIAL AUTOMATEDashince/tomcat8 Tomcat GUI Manager pre-configured docker ima… 5podbox/tomcat8 2 [OK]这个tomcat包含了jdk而且启动了可以直接访问,自己启动了8080端口参考文献:http://www.cncsto.com/article/5761234567891011121314151617181920[root@localhost ~]# docker pull ashince/tomcat8Using default tag: latestlatest: Pulling from ashince/tomcat806b22ddb1913: Pulling fs layer336c28b408ed: Pull complete1f3e6b8d80c3: Pull complete[root@localhost ~]# docker imagesREPOSITORY TAG IMAGE ID CREATED SIZEnginx latest 2622e6cca7eb 2 weeks ago 132MBashince/tomcat8 latest 02aedead27dd 22 months ago 314MB启动一个Nginx容器和两个Tomcat容器[root@localhost ~]# docker run -itd -p 8080:8080 ashince/tomcat83e3f2aabe67de7ee3f4b6d62176e21aaa9d2302922845cb08ad37af7146b13c5[root@localhost ~]# docker run -itd -p 8081:8080 ashince/tomcat8644d59711c805a626b7c1c219aa018f744098a14dd41e54744d6b13e7ba66a2f[root@localhost ~]# docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMEScca55c4ad919 ashince/tomcat8 "catalina.sh run" About a minute ago Up About a minute 0.0.0.0:8081->8080/tcp unruffled_lalande08b58d2f41d6 ashince/tomcat8 "catalina.sh run" 7 minutes ago Up 7 minutes 0.0.0.0:8080->8080/tcp relaxed_williamsonaeebcb0b40a2 nginx "/docker-entrypoint.…" 2 hours ago Up 2 hours 0.0.0.0:80->80/tcp priceless_ardinghelli
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192#将Nginx容器当中的配置拷贝到本地修改,因为容器当中没有vi vim命令[root@localhost ~]# docker cp 68d2bdf336ed:/etc/nginx/conf.d/default.conf .[root@localhost ~]# lsanaconda-ks.cfg default.conf index.html[root@localhost ~]# grep -vE "#|^$" default.confserver {listen 80;listen [::]:80;server_name localhost;location / {root /usr/share/nginx/html;index index.html index.htm;}error_page 500 502 503 504 /50x.html;location = /50x.html {root /usr/share/nginx/html;}}#两台tomcat的ip地址[root@localhost ~]# for i in {72e174adc77d,080068dae40a};do docker inspect $i| grep -i ipaddr |tail -n 1;done"IPAddress": "172.17.0.4","IPAddress": "172.17.0.3",拷贝静态资源到Nginx目录下做动静分离 ,同时修改配置文件#将其中一台Tomcat的ROOT目录拷贝到本地,因为做动静分离Nginx需要访问静态资源要在本地[root@localhost ~]# docker cp 72e174adc77d:/usr/local/tomcat/webapps/ROOT .#拷贝到Nginx发布目录下面[root@localhost ~]# docker cp ROOT 68d2bdf336ed:/usr/share/nginx/html/[root@localhost WEB-INF]# docker exec 68d2bdf336ed ls -l /usr/share/nginx/html/ROOTtotal 184-rwxrwxrwx 1 root root 7064 Jun 21 2017 RELEASE-NOTES.txtdrwxrwxrwx 2 root root 21 Jul 27 2017 WEB-INF-rwxrwxrwx 1 root root 26447 Jun 21 2017 asf-logo-wide.svg-rwxrwxrwx 1 root root 713 Jun 21 2017 bg-button.png-rwxrwxrwx 1 root root 1918 Jun 21 2017 bg-middle.png-rwxrwxrwx 1 root root 1392 Jun 21 2017 bg-nav-item.png-rwxrwxrwx 1 root root 1401 Jun 21 2017 bg-nav.png-rwxrwxrwx 1 root root 3103 Jun 21 2017 bg-upper.png-rwxrwxrwx 1 root root 21630 Jun 21 2017 favicon.ico-rwxrwxrwx 1 root root 12279 Jun 21 2017 index.jsp-rwxrwxrwx 1 root root 2376 Jun 21 2017 tomcat-power.gif-rwxrwxrwx 1 root root 5581 Jun 21 2017 tomcat.css-rwxrwxrwx 1 root root 2066 Jun 21 2017 tomcat.gif-rwxrwxrwx 1 root root 5103 Jun 21 2017 tomcat.png-rwxrwxrwx 1 root root 67795 Jun 21 2017 tomcat.svg#修改后的Nginx配置文件如下,修改完后拷贝会容器当中[root@localhost ~]# docker cp default.conf 68d2bdf336ed:/etc/nginx/conf.d/default.conf[root@localhost ~]# docker exec 68d2bdf336ed cat /etc/nginx/conf.d/default.confupstream tomcat_web{server 172.17.0.3:8080 weight=100 max_fails=2 fail_timeout=15;server 172.17.0.4:8080 weight=100 max_fails=2 fail_timeout=15;}server {listen 80;listen [::]:80;server_name localhost;error_page 500 502 503 504 /50x.html;root /usr/share/nginx/html;location /{proxy_pass http://tomcat_web;proxy_set_header host $host;}location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|mp4|ico)$ {root /usr/share/nginx/html/ROOT;expires 30d;access_log off;}location ~ .*\.(eot|ttf|otf|woff|svg)$ {root /usr/share/nginx/html/ROOT;expires 30d;access_log off;}location ~ .*\.(js|css)$ {root /usr/share/nginx/html/ROOT;expires 30d;access_log off;}location = /50x.html {root /usr/share/nginx/html;}}#加载新的配置项[root@localhost ~]# docker exec -it 68d2bdf336ed /bin/bashroot@68d2bdf336ed:/# /usr/sbin/nginx -s reload2020/06/29 07:12:05 [notice] 79#79: signal process started再去访问Nginx 80端口如图所示:
补充知识:Docker 基础操作 容器自启动 删除镜像和删除容器Docker容器自动重启设置重启reboot操作系统后,发现docker 服务未启动,容器也未启动,怎么才能重启后自动启动呢1、docker服务自动重启设置[root@localhost ~]# systemctl enable docker.service2、docker容器自动启动设置1234567891011121314[root@localhost ~]# docker imagesREPOSITORY TAG IMAGE ID CREATED SIZEnginx latest 5a3221f0137b 10 months ago 126MB[root@localhost ~]# docker run -itd -p 80:80 nginx3e28c4b5c6256c0ba04666751e426987d848b7afeb9c59774d5e9831dc78e5ee[root@localhost ~]# docker run -itd -p 81:80 nginxf0597c725fd6b7f4229aa9ab5de4a3cb29d09097a81dc8f64d1a60d469001379[root@localhost ~]# docker port f0597c725fd680/tcp -> 0.0.0.0:81[root@localhost ~]# docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMESf0597c725fd6 nginx "nginx -g 'daemon of?? 30 seconds ago Up 29 seconds 0.0.0.0:81->80/tcp elastic_allen3e28c4b5c625 nginx "nginx -g 'daemon of?? 35 seconds ago Up 33 seconds 0.0.0.0:80->80/tcp tender_volhard3、 docker容器自动启动设置1234567891011[root@localhost ~]# docker update --restart=always f0597c725fd6 3e28c4b5c625f0597c725fd63e28c4b5c625[root@localhost ~]# reboot -h nowConnection closed by foreign host.[root@localhost ~]# docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMESf0597c725fd6 nginx "nginx -g 'daemon of?? 13 minutes ago Up 2 minutes 0.0.0.0:81->80/tcp elastic_allen3e28c4b5c625 nginx "nginx -g 'daemon of?? 13 minutes ago Up 2 minutes 0.0.0.0:80->80/tcp tender_volhard当一个host中镜像和容器较多,需要重置时可选择删除其中部分或全部的镜像和容器。那么你就需要下面的操作了。1、删除容器1234567[root@localhost ~]# docker ps -aCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES27c5c4d3cf86 nginx "nginx -g 'daemon of?? 2 minutes ago Up 2 minutes 0.0.0.0:80->80/tcp gracious_nash61cccfe238a8 nginx "nginx -g 'daemon of?? 2 minutes ago Up 2 minutes 0.0.0.0:81->80/tcp distracted_grothendieck[root@localhost ~]# docker ps -aq27c5c4d3cf8661cccfe238a81)首先需要停止所有的容器12345678910111213141516171819[root@localhost ~]# docker ps -aqf0597c725fd63e28c4b5c6258855c7777f83466d2efe3dd920ca589b1a10e5457b41cae6314d1d01c941[root@localhost ~]# docker stop $(docker ps -aq)f0597c725fd63e28c4b5c6258855c7777f83466d2efe3dd920ca589b1a10e5457b41cae6314d1d01c941[root@localhost ~]# docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES2)删除所有的容器(只删除单个时把后面的变量改为image id即可)12345678910[root@localhost ~]# docker rm -f $(docker ps -aq)f0597c725fd63e28c4b5c6258855c7777f83466d2efe3dd920ca589b1a10e5457b41cae6314d1d01c941[root@localhost ~]# docker ps -aq[root@localhost ~]#2、删除镜像1)查看host中的镜像docker images2)删除指定id的镜像docker rmi <image id>3)删除全部的imagesdocker rmi $(docker images -q)3、当要删除的iamges和其他的镜像有关联而无法删除时可通过 -f 参数强制删除docker rmi -f $(docker images -q)以上这篇Docker Nginx容器和Tomcat容器实现负载均衡与动静分离操作就是小编分享给大家的全部内容了,希望能给大家一个参考