0
点赞
收藏
分享

微信扫一扫

docker-compose 通过NGINX快速搭建负载均衡的Tomcat集群


                         docker-compose 通过NGINX快速搭建负载均衡的Tomcat集群

从标题也可以看出,需要三个软件,docker-compose ,docker-ce(docker的运行环境),Tomcat的镜像。docker-compose 和docker的安装就不用说了,都可以离线安装,安装方法见博客:

(docker-compose安装方法)

(docker离线安装方法以及本地化配置以提高pull效率)

(nginx编译安装脚本)

本次实验环境介绍:

centos7虚拟机环境,IP地址192.168.0.16,docker的版本为docker-19.3.09,docker-compose的版本为1.25.1版本,NGINX版本为1.8.1(在宿主机上安装,安装方式为编译),tomcat镜像使用的为官方tomcat:latest。

tomcat集群的搭建步骤:

一,从官网pull镜像

[root@centos6 conf]# docker pull tomcat
[root@centos6 conf]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
tomcat              latest              e0bd8b34b4ea        13 days ago         649MB

二,编写docker-compose所使用的编排文件,启动tomcat集群,计划tomcat容器端口为8081,8082,8083,分别映射到宿主机的8080端口,文件内容如下(本实验我定义的名字为tomcat_cluster.yaml )。

version: '3'
services:
  tomcat1:
    image: tomcat:latest
    container_name: tomcat1
    volumes:
      - /opt/tomcat1:/usr/local/tomcat/webapps/
    restart: always
    ports:
      - 8081:8080
  tomcat2:
    image: tomcat:latest
    container_name: tomcat2
    volumes:
      - /opt/tomcat2:/usr/local/tomcat/webapps/
    restart: always
    ports:
      - 8082:8080
  tomcat3:
    image: tomcat:latest
    container_name: tomcat3
    volumes:
      - /opt/tomcat3:/usr/local/tomcat/webapps/
    restart: always
    ports:
      - 8083:8080

三,建立容器和宿主机映射的路径,并写入测试页面以区分三个容器。

 

容器名称在文件内固定了,分别为tomcat1 ,tomcat2,tomcat3,在宿主机也就是192.168.0.16这个虚拟机内,要建立映射目录,命令如下:

mkdir -p /opt/{tomcat1,tomcat2,tomcat3}/ROOT/ 

在/opt/tomcat1/ROOT目录下生成一个index.html 文件,文件内容为  tomcat1  ,命令为: echo "tomcat1" > /opt/tomat1/ROOT/index.html

在/opt/tomcat2/ROOT目录下生成一个index.html 文件,文件内容为  tomcat2  ,命令为: echo "tomcat2" > /opt/tomat2/ROOT/index.html

在/opt/tomcat3/ROOT目录下生成一个index.html 文件,文件内容为  tomcat3 ,命令为: echo "tomcat3" > /opt/tomat3/ROOT/index.html

四,NGINX的负载均衡+反向代理配置(简单实验就简单的来了,因此配置文件写在主配置文件 nginx.conf 了,需要添加的内容如下:

server {
        listen       80;
        server_name  192.168.0.16;

        location / {
            root   html;
            index  index.html index.htm;
            proxy_pass http://tomcat;
        }
 
upstream tomcat{
    server 192.168.0.16:8081;
    server 192.168.0.16:8082;
    server 192.168.0.16:8083;
                  }

完整的nginx.conf文件内容如下:(nginx如果是使用上面我提到的博客编译安装,那么主配置文件路径为:/usr/local/nginx/conf/nginx.conf)

worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    server {
        listen       80;
        server_name  192.168.0.17;
        location / {
            root   html;
            index  index.html index.htm;
            proxy_pass http://tomcat;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
    upstream tomcat{
    server 192.168.0.17:8081;
    server 192.168.0.17:8082;
    server 192.168.0.17:8083;
                  }
}

 

五,重启nginx服务,启动tomcat集群。

nginx -s reload

docker-compose -f ~/tomcat_cluster.yaml up -d

[root@centos6 conf]# docker-compose -f ~/tomcat_cluster.yaml up -d
Creating tomcat1 ... done
Creating tomcat2 ... done
Creating tomcat3 ... done

六,验证阶段

执行docker-compose -f ~/tomcat_cluster.yaml up -d这个命令启动成功集群后,打开任意浏览器,输入以下网址:

http://192.168.0.16:8081   http://192.168.0.16:8082  http://192.168.0.16:8083    以及最后一个最为关键的http://192.168.0.16

本次实验完美完成,特别需要注意的是,在实际生产中,nginx的配置文件可能需要配置负载均衡算法为ip_hash 以保持session。这些都是后话,暂不讨论了。

举报

相关推荐

0 条评论