基于Docker部署Tomcat集群、 Nginx負(fù)載均衡的問題小結(jié)
寫在前面
看完Dokcer相關(guān)的書籍,正好有個(gè)項(xiàng)目要這樣搞,所以自己練習(xí)一下。
當(dāng)作一百世一樣。這里的道理很明白:我思故我在,既然我存在,就不能裝作不存在。無論如何,我要為自己負(fù)起責(zé)任?!跣〔ā度ⅰ?
結(jié)構(gòu)圖:
這里僅作為一種學(xué)習(xí),一般這種負(fù)載的話,Nginx
是放到主機(jī)側(cè)
的, JavaWeb(Tomcat)
應(yīng)用放到容器里。
效果
新建文件夾。
D=uag;mkdir $D;cd $D;mkdir uag_nginx uag_tomcat8; ls uag_nginx uag_tomcat8
一,Ngixn 鏡像制作
cd uag_nginx/ # 用于存放配置文件 mkdir nginx vim Dockerfile
Dockerfile 文件內(nèi)容
FROM nginx LABEL maintainer="uag" ENV REFRESHED_AT 2021-08-27 EXPOSE 8099
構(gòu)建nginx配置文件內(nèi)容
這個(gè)的配置文件,在容器運(yùn)行的時(shí)候通過 -v
參數(shù)與 容器內(nèi)部共享。方便后期參數(shù)更改
cd ./nginx vim nginx.conf
nginx.conf 配置文件內(nèi)容
user nginx; worker_processes auto; error_log /var/log/nginx/error.log notice; pid /var/run/nginx.pid; daemon off; events { worker_connections 1024; } http { include /etc/nginx/mime.types; default_type application/octet-stream; log_format main '$upstream_addr - $remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log /var/log/nginx/access.log main; sendfile on; #tcp_nopush on; keepalive_timeout 65; #gzip on; include /etc/nginx/conf.d/*.conf; server { listen 8099; server_name localhost; root /var/www/html/; index index.html index.htm; access_log /var/log/nginx/default_access.log main; error_log /var/log/nginx/default_error.log; location / { proxy_pass http://backend; } location ~ .* { proxy_pass http://backend; proxy_set_header Host $http_host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } } # 這里配置負(fù)載 upstream backend { server 172.23.231.190:8069; server 172.23.231.190:8079; server 172.23.231.190:8089; } }
配置負(fù)載:172.23.231.190
為宿主機(jī)IP,8069,8079,8089為對(duì)應(yīng)的Java Web 暴露的應(yīng)用端口。
# 這里配置負(fù)載 upstream backend { server 172.23.231.190:8069; server 172.23.231.190:8079; server 172.23.231.190:8089; }
構(gòu)建Nginx鏡像
docker build -t uag/uag_nginx .
二,java Web(Tomcat)應(yīng)用鏡像構(gòu)建
cd uag_tomcat8/ vim Dockerfile
Dockerfile 文件內(nèi)容
FROM dordoka/tomcat MAINTAINER LIRUILONG COPY UAWeb.war /opt/tomcat/webapps/UAWeb.war EXPOSE 8080 ENTRYPOINT [ "/opt/tomcat/bin/catalina.sh", "run" ]
上傳對(duì)應(yīng)的War包
ls Dockerfile UAWeb.war
構(gòu)建鏡像
docker build -t uag/uag_tomcat .
三,運(yùn)行容器 Nginx鏡像
docker run -d -p 8099:8099 --name uag_nginx -v $PWD/nginx/nginx.conf:/etc/nginx/nginx.conf uag/uag_nginx nginx
java Web(Tomcat)鏡像
docker run -it -d -p 8089:8080 --name uag_app_1 uag/uag_tomcat docker run -it -d -p 8079:8080 --name uag_app_2 uag/uag_tomcat docker run -it -d -p 8069:8080 --name uag_app_3 uag/uag_tomcat
查看運(yùn)行的容器
瀏覽器訪問
查看負(fù)載方式:新進(jìn)程的方式
查看負(fù)載方式:–volumes-from 方式
Dockerfile文件
FROM nginx LABEL maintainer="uag" ENV REFRESHED_AT 2021-08-27 VOLUME /var/log/nginx/ EXPOSE 80
┌──(liruilong㉿Liruilong)-[/mnt/e/docker/uag/uag_nginx] └─$ docker run -it --rm --volumes-from nginx_log centos cat /var/log/nginx/default_access.log 172.23.231.190:8069 - 172.17.0.1 - - [30/Aug/2021:12:55:02 +0000] "GET /UAWeb/services/listServices HTTP/1.1" 200 12660 "http://127.0.0.1:8099/UAWeb/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.159 Safari/537.36" "-" 172.23.231.190:8079 - 172.17.0.1 - - [30/Aug/2021:12:55:02 +0000] "GET /UAWeb/axis2-web/css/axis-style.css HTTP/1.1" 200 1587 "http://127.0.0.1:8099/UAWeb/services/listServices" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.159 Safari/537.36" "-" 172.23.231.190:8069 - 172.17.0.1 - - [30/Aug/2021:12:55:02 +0000] "GET /UAWeb/axis2-web/images/asf-logo.gif HTTP/1.1" 200 5866 "http://127.0.0.1:8099/UAWeb/services/listServices" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.159 Safari/537.36" "-" 172.23.231.190:8079 - 172.17.0.1 - - [30/Aug/2021:12:55:02 +0000] "GET /UAWeb/axis2-web/images/axis_l.jpg HTTP/1.1" 200 12340 "http://127.0.0.1:8099/UAWeb/services/listServices" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.159 Safari/537.36" "-" 172.23.231.190:8089 - 172.17.0.1 - - [30/Aug/2021:12:55:03 +0000] "GET /UAWeb/services/listServices HTTP/1.1" 200 12660 "http://127.0.0.1:8099/UAWeb/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.159 Safari/537.36" "-" 172.23.231.190:8069 - 172.17.0.1 - - [30/Aug/2021:12:55:03 +0000] "GET /UAWeb/axis2-web/images/asf-logo.gif HTTP/1.1" 200 5866 "http://127.0.0.1:8099/UAWeb/services/listServices" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92
構(gòu)建好鏡像上傳倉庫:
嗯,需要注冊(cè)一個(gè)Docker Hub賬號(hào)
,然后登錄,需要鏡像前面加 賬戶名/
┌──(liruilong㉿Liruilong)-[/mnt/e/docker/uag/uag_nginx] └─$ docker push liruilong/nginx_log The push refers to repository [docker.io/liruilong/nginx_log] An image does not exist locally with the tag: liruilong/nginx_log ┌──(liruilong㉿Liruilong)-[/mnt/e/docker/uag/uag_nginx] └─$ docker tag 9c9af0362eb9 liruilong/nginx_log ┌──(liruilong㉿Liruilong)-[/mnt/e/docker/uag/uag_nginx] └─$ docker push liruilong/nginx_log The push refers to repository [docker.io/liruilong/nginx_log] fb04ab8effa8: Pushed 8f736d52032f: Pushed 009f1d338b57: Pushed 678bbd796838: Pushed d1279c519351: Pushed f68ef921efae: Pushed latest: digest: sha256:2af7e8aeab84e8a816caf6b0342e1a45f95c7089ff52578040ea3a4c28a943c7 size: 1570 ┌──(liruilong㉿Liruilong)-[/mnt/e/docker/uag/uag_nginx] └─$ docker push liruilong/nginx_log:tagname # 拉去鏡像
到此這篇關(guān)于基于Docker部署 Tomcat集群、 Nginx負(fù)載均衡的文章就介紹到這了,更多相關(guān)Docker部署Tomcat Nginx負(fù)載均衡內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- docker安裝nginx實(shí)現(xiàn)對(duì)springboot項(xiàng)目的負(fù)載均衡的操作方法
- docker swam集群如何實(shí)現(xiàn)負(fù)載均衡
- Docker安裝Nacos容器并根據(jù)Nginx實(shí)現(xiàn)負(fù)載均衡
- docker搭建nginx實(shí)現(xiàn)負(fù)載均衡的示例代碼
- docker swarm外部驗(yàn)證負(fù)載均衡時(shí)不生效的解決方案
- Docker Nginx容器和Tomcat容器實(shí)現(xiàn)負(fù)載均衡與動(dòng)靜分離操作
- 使用Docker Compose 實(shí)現(xiàn)nginx負(fù)載均衡的方法步驟
- 詳解Docker Swarm服務(wù)發(fā)現(xiàn)和負(fù)載均衡原理
- 詳解利用nginx和docker實(shí)現(xiàn)一個(gè)簡易的負(fù)載均衡
- Docker部署tenine實(shí)現(xiàn)后端應(yīng)用的高可用與負(fù)載均衡(推薦)
相關(guān)文章
Linux/Docker 中使用 System.Drawing.Common 踩坑記錄分享
這篇文章主要介紹了Linux/Docker 中使用 System.Drawing.Common 踩坑記錄,本文通過兩種方案給大家詳細(xì)介紹,對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-07-07Docker跨主機(jī)容器通信overlay實(shí)現(xiàn)過程詳解
這篇文章主要介紹了Docker跨主機(jī)容器通信overlay實(shí)現(xiàn)過程詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-05-05docker中nginx的location轉(zhuǎn)發(fā)不生效的解決
這篇文章主要介紹了docker中nginx的location轉(zhuǎn)發(fā)不生效的解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-06-06docker compose入門helloworld的詳細(xì)過程
docker-compose是基于docker的,所以我們需要先安裝docker才能使用docker-compose,接下來通過本文給大家介紹docker compose入門helloworld的過程,一起看看吧2021-09-09Docker容器無法被stop or kill問題的解決方法
這篇文章主要介紹了Docker容器無法被stop or kill問題的解決方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09docker安裝RabbitMQ及安裝延遲插件的詳細(xì)過程
MQ(message queue)字面意思上來說消息隊(duì)列,是一種跨進(jìn)程的通信機(jī)制,用于上下游傳遞消息,本文給大家詳細(xì)介紹docker安裝RabbitMQ及安裝延遲插件的過程,感興趣的朋友一起看看吧2022-06-06