基于Docker部署Tomcat集群、 Nginx负载均衡的问题小结
程序员文章站
2022-04-06 09:16:08
目录一,ngixn 镜像制作二,java web(tomcat)应用镜像构建三,运行容器 nginx镜像写在前面看完dokcer相关的书籍,正好有个项目要这样搞,所以自己练习一下。当作一百世一样。这里...
写在前面
看完dokcer相关的书籍,正好有个项目要这样搞,所以自己练习一下。
当作一百世一样。这里的道理很明白:我思故我在,既然我存在,就不能装作不存在。无论如何,我要为自己负起责任。——王小波《三十而立》
结构图:
这里仅作为一种学习,一般这种负载的话,nginx
是放到主机侧
的, javaweb(tomcat)
应用放到容器里。
效果
新建文件夹。
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 文件内容
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 .
二,java web(tomcat)应用镜像构建
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 .
三,运行容器 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
查看运行的容器
浏览器访问
查看负载方式:新进程的方式
查看负载方式:–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负载均衡的文章就介绍到这了,更多相关docker部署tomcat nginx负载均衡内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!