环境&结构
1台Nginx最新版本的Docker容器,3台Tomcat8的Docker容器
准备Docker环境
如果不是最新版的Docker的话,如果版本差别太大的话,可能会出问题,所以大家要把Docker升级到最新版,或者直接安装最新版。
拉取相应的镜像
准备镜像:
1、tomcat 2、nginx
拉取相应的对象:
[root@zh ~]# docker pull tomcat:jdk8-adoptopenjdk-hotspot [root@zh ~]# docker pull nginx:latest
[root@zh ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE mysql 5.7.29 d5cea958d330 40 hours ago 440MB tomcat jdk8-adoptopenjdk-hotspot ae9ec2a81228 10 days ago 324MB nginx latest 2073e0bcb60e 2 weeks ago 127MB [root@zh ~]#
启动容器
启动Tomcat:
[root@zh ~]# docker run -d -p 8081:8080 --name myt1 --privileged=true ae9ec2a81228 [root@zh ~]# docker run -d -p 8082:8080 --name myt2 --privileged=true ae9ec2a81228 [root@zh ~]# docker run -d -p 8083:8080 --name myt3 --privileged=true ae9ec2a81228
正在运行的结果:
[root@zh ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES c33dde60b4ac ae9ec2a81228 "catalina.sh run" 7 hours ago Up 7 hours 0.0.0.0:8083->8080/tcp myt3 9397416d13b0 ae9ec2a81228 "catalina.sh run" 7 hours ago Up 7 hours 0.0.0.0:8082->8080/tcp myt2 bf6b880e1398 ae9ec2a81228 "catalina.sh run" 7 hours ago Up 7 hours 0.0.0.0:8081->8080/tcp myt1 [root@zh ~]#
启动Nginx:
在启动之前呢有必要跟大家说一下:
因为后来咱们需要编辑nginx的配置文件,也为了后来咱们好看日志文件,咱们可以做一个配置文件的映射
首先呢,准备文件:
[root@zh ~]# mkdir /data/nginx/ [root@zh ~]# mkdir /data/nginx/conf/ [root@zh ~]# mkdir /data/nginx/logs/ [root@zh ~]# touch /data/nginx/conf/nginx.conf
然后我们先把配置文件nginx.conf配置好:
# For more information on configuration, see: # * Official English Documentation: http://nginx.org/en/docs/ # * Official Russian Documentation: http://nginx.org/ru/docs/ user nginx; worker_processes auto; error_log /var/log/nginx/error.log; pid /run/nginx.pid; # Load dynamic modules. See /usr/share/nginx/README.dynamic. include /usr/share/nginx/modules/*.conf; events { worker_connections 1024; } http { log_format main '$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; "/data/nginx/conf/nginx.conf" 68L, 1836C # For more information on configuration, see: # * Official English Documentation: http://nginx.org/en/docs/ # * Official Russian Documentation: http://nginx.org/ru/docs/ user nginx; worker_processes auto; error_log /var/log/nginx/error.log; pid /run/nginx.pid; # Load dynamic modules. See /usr/share/nginx/README.dynamic. include /usr/share/nginx/modules/*.conf; events { worker_connections 1024; } http { log_format main '$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; tcp_nodelay on; keepalive_timeout 65; types_hash_max_size 2048; include /etc/nginx/mime.types; default_type application/octet-stream; # Load modular configuration files from the /etc/nginx/conf.d directory. # See http://nginx.org/en/docs/ngx_core_module.html#include # for more information. include /etc/nginx/conf.d/*.conf; server { listen 80 default_server; listen [::]:80 default_server; server_name 182.254.161.54; root /usr/share/nginx/html; # Load configuration files for the default server block. include /etc/nginx/default.d/*.conf; location / { proxy_pass http://pic; } error_page 404 /404.html; location = /40x.html { } error_page 500 502 503 504 /50x.html; location = /50x.html { } } upstream pic{ server sdgzs.net:8081 weight=5; server sdgzs.net:8082 weight=5; server sdgzs.net:8083 weight=5; } }
重点是修改:
upstream pic{ server ip:port weight=5; server ip:port weight=5; server ip:port weight=5; }
如果不知道ip的话,可以利用下面的命令查看:
查看所有的ip地址
[root@zh ~]# docker inspect $(docker ps -qa) | grep IPAddress "SecondaryIPAddresses": null, "IPAddress": "172.17.0.5", "IPAddress": "172.17.0.5", "SecondaryIPAddresses": null, "IPAddress": "", "IPAddress": "", "SecondaryIPAddresses": null, "IPAddress": "172.17.0.4", "IPAddress": "172.17.0.4", "SecondaryIPAddresses": null, "IPAddress": "172.17.0.3", "IPAddress": "172.17.0.3", "SecondaryIPAddresses": null, "IPAddress": "172.17.0.2", "IPAddress": "172.17.0.2", "SecondaryIPAddresses": null, "IPAddress": "", "IPAddress": "", You have mail in /var/spool/mail/root [root@zh ~]# docker inspect $(docker ps -qa) | grep Name "Name": "/myng3", "Name": "no", "Name": "overlay2" "Name": "/myng1", "Name": "no", "Name": "overlay2" "Name": "/myt3", "Name": "no", "Name": "overlay2" "Name": "/myt2", "Name": "no", "Name": "overlay2" "Name": "/myt1", "Name": "no", "Name": "overlay2" "Name": "/vigilant_lamarr", "Name": "no", "Name": "overlay2" [root@zh ~]#
启动nginx
[root@zh ~]# docker run --name myng3 -d -p 8889:80 -v /data/nginx/conf/nginx.conf:/etc/nginx/nginx.conf -v /data/nginx/logs:/var/log/nginx -d 2073e0bcb60e
把准备好的war包拷贝到tomcat容器下的webapps目录下:
[root@zh ~]# docker cp ~/test.war myt1:/usr/local/tomcat/webapps/ [root@zh ~]# docker cp ~/test.war myt2:/usr/local/tomcat/webapps/ [root@zh ~]# docker cp ~/test.war myt3:/usr/local/tomcat/webapps/
注意:test.war是我创建的一个javaweb的项目,利用maven然后打包成
测试结果