您好,登錄后才能下訂單哦!
這篇文章主要介紹了基于Docker如何部署Tomcat集群、 Nginx負載均衡,具有一定借鑒價值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。
結構圖:
這里僅作為一種學習,一般這種負載的話,Nginx
是放到主機側
的, JavaWeb(Tomcat)
應用放到容器里。
效果
新建文件夾。
D=uag;mkdir $D;cd $D;mkdir uag_nginx uag_tomcat8; ls uag_nginx uag_tomcat8
cd uag_nginx/ # 用于存放配置文件 mkdir nginx vim Dockerfile
Dockerfile 文件內容
FROM nginx LABEL maintainer="uag" ENV REFRESHED_AT 2021-08-27 EXPOSE 8099
構建nginx配置文件內容
這個的配置文件,在容器運行的時候通過 -v
參數與 容器內部共享。方便后期參數更改
cd ./nginx vim nginx.conf
nginx.conf 配置文件內容
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; } } # 這里配置負載 upstream backend { server 172.23.231.190:8069; server 172.23.231.190:8079; server 172.23.231.190:8089; } }
配置負載:172.23.231.190
為宿主機IP,8069,8079,8089為對應的Java Web 暴露的應用端口。
# 這里配置負載 upstream backend { server 172.23.231.190:8069; server 172.23.231.190:8079; server 172.23.231.190:8089; }
構建Nginx鏡像
docker build -t uag/uag_nginx .
cd uag_tomcat8/ vim Dockerfile
Dockerfile 文件內容
FROM dordoka/tomcat MAINTAINER LIRUILONG COPY UAWeb.war /opt/tomcat/webapps/UAWeb.war EXPOSE 8080 ENTRYPOINT [ "/opt/tomcat/bin/catalina.sh", "run" ]
上傳對應的War包
ls Dockerfile UAWeb.war
構建鏡像
docker build -t uag/uag_tomcat .
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
查看運行的容器
瀏覽器訪問
查看負載方式:新進程的方式
查看負載方式:–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
構建好鏡像上傳倉庫:
嗯,需要注冊一個Docker Hub賬號
,然后登錄,需要鏡像前面加 賬戶名/
┌──(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 # 拉去鏡像
感謝你能夠認真閱讀完這篇文章,希望小編分享的“基于Docker如何部署Tomcat集群、 Nginx負載均衡”這篇文章對大家有幫助,同時也希望大家多多支持億速云,關注億速云行業資訊頻道,更多相關知識等著你來學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。