使用Docker-Compose部署nginx代理Tomcat集群,实现负载均衡

回复 收藏

1、安装docker-compose

curl -L https://github.com/docker/compose/releases/download/1.17.0-rc1/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose

chmod 755 !$

大体步骤分为以下四步

1、下载所需的文件tomcat,jdk

2、编写dockerfile来布署tomcat与java环境,生成镜像文件

3、编写docker-compose.yml配置文件,启动所有容器服务

4、测试负载均衡

(一)具体配置文件如下

#整个目录结构

[root@master java]# tree ./

./

├── docker-compose.yml

├── etc

│   └── localtime

├── nginx

│   └── nginx.conf

├── tomcat

│   ├── apache-tomcat-8.5.31.tar.gz

│   ├── Dockerfile

│   └── jdk-8u144-linux-x64.tar.gz

└── webserver

    ├── tomcatA

    │   └── index.jsp

    └── tomcatB

        └── index.jsp

6 directories, 8 files

#两个测试首页文件

[root@master java]# cat webserver/tomcatA/index.jsp 

welcome to tomcat-A server

[root@master java]# cat webserver/tomcatB/index.jsp 

welcome to tomcat-B server

#配置文件

[root@master java]# cat docker-compose.yml 

version: "3"

services:

  nginx:

    image: nginx:1.14

    restart: always

    ports:

      - 80:80

    links:

      - tomcat1:tomcat1

      - tomcat2:tomcat2

    volumes:

      - ./webserver:/webserver

      - ./nginx/nginx.conf:/etc/nginx/nginx.conf

      - ./etc/localtime:/etc/localtime

    depends_on:

      - tomcat1

      - tomcat2

  tomcat1:

    hostname: tomcat1

    build: ./tomcat

    volumes:

      - ./webserver/tomcatA:/usr/local/apache-tomcat-8.5.31/webapps/ROOT

      - ./etc/localtime:/etc/localtime

  tomcat2:

    hostname: tomcat2

    build: ./tomcat

    volumes:

      - ./webserver/tomcatB:/usr/local/apache-tomcat-8.5.31/webapps/ROOT

      - ./etc/localtime:/etc/localtime

#安装JAVA环境

[root@master java]# cat tomcat/Dockerfile 

FROM centos

ADD jdk-8u144-linux-x64.tar.gz /usr/local

ENV JAVA_HOME /usr/local/jdk1.8.0_144

ADD apache-tomcat-8.5.31.tar.gz /usr/local

EXPOSE 8080

ENTRYPOINT ["/usr/local/apache-tomcat-8.5.31/bin/catalina.sh", "run"]

#nginx.conf配置文档

[root@slave nginx]# cat nginx.conf

user  nginx;

worker_processes  1;

error_log  /var/log/nginx/error.log warn;

pid        /var/run/nginx.pid;

events {

    worker_connections  1024;

}

http {

    include       /etc/nginx/mime.types;

    default_type  application/octet-stream;

    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;

    client_max_body_size 10m;

    sendfile        on;

    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

upstream tomcat_client {

         server tomcat1:8080 weight=1;

         server tomcat2:8080 weight=1;

    }

    server {

        server_name "";

        listen 80 default_server;

        listen [::]:80 default_server ipv6only=on;

        location / {

            proxy_pass http://tomcat_client;

            proxy_redirect default;

            proxy_set_header Host $host;

            proxy_set_header X-Real-IP $remote_addr;

        }

    }

}

(二)启动所有容器服务

[root@master java]# docker-compose up -d

Exported_from_Notepad++.png

(三)查看启动情况

[root@master java]# docker-compose ps    

 QQ图片20190521154445.png

(四)检测负载均衡

QQ图片20190521154558.png

(五)查看日志输出信息

C83GC4G}3~M4LL6)2OL2Y[J.png

2019-05-21 15:39 举报
已邀请:

回复帖子,请先登录注册

退出全屏模式 全屏模式 回复
评分
可选评分理由: