Docker配置1台Nginx+3台Tomcat做负载均衡

简介: Docker配置1台Nginx+3台Tomcat做负载均衡

环境&结构

1台Nginx最新版本的Docker容器,3台Tomcat8的Docker容器

准备Docker环境

如果不是最新版的Docker的话,如果版本差别太大的话,可能会出问题,所以大家要把Docker升级到最新版,或者直接安装最新版。

可以参考:Centos7-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然后打包成

测试结果

相关实践学习
部署高可用架构
本场景主要介绍如何使用云服务器ECS、负载均衡SLB、云数据库RDS和数据传输服务产品来部署多可用区高可用架构。
负载均衡入门与产品使用指南
负载均衡(Server Load Balancer)是对多台云服务器进行流量分发的负载均衡服务,可以通过流量分发扩展应用系统对外的服务能力,通过消除单点故障提升应用系统的可用性。 本课程主要介绍负载均衡的相关技术以及阿里云负载均衡产品的使用方法。
目录
相关文章
|
7天前
|
前端开发 应用服务中间件 Linux
nginx解决springcloud前后端跨域问题,同时配置ssl
nginx解决springcloud前后端跨域问题,同时配置ssl
|
1天前
|
缓存 前端开发 JavaScript
tomcat核心技术+Nginx性能调优技术
而Tomcat的基本配置,每个配置项也基本上对应了Tomcat的组件结构,如果要用一张图来形象展现一下Tomcat组成的话
14 1
|
2天前
|
存储 文件存储 数据安全/隐私保护
Docker环境下Minio文件存储的安装与配置
Docker环境下Minio文件存储的安装与配置
21 4
|
2天前
|
Docker 容器
Docker Compose的安装与配置
Docker Compose的安装与配置
24 2
Docker Compose的安装与配置
|
5天前
|
负载均衡 Ubuntu 应用服务中间件
|
7天前
|
Java 应用服务中间件 网络安全
Nginx配置静态页面+springboot应用+swagger+SSL的实现
Nginx配置静态页面+springboot应用+swagger+SSL的实现
|
7天前
|
存储 网络安全 API
|
9天前
|
运维 应用服务中间件 Shell
Nginx安装与虚拟主机配置shell脚本
Nginx安装与虚拟主机配置shell脚本
17 0
|
Web App开发 负载均衡 应用服务中间件
|
负载均衡 应用服务中间件 nginx
nginx+tomcat 反向代理 负载均衡配置
1、nginx的安装和配置见:http://www.cnblogs.com/ll409546297/p/6795362.html 2、tomcat部署项目到对应的服务器上面并启动,不详解 3、在nginx中配置nginx.
857 0