zoukankan      html  css  js  c++  java
  • 同主机下Docker+nginx+tomcat负载均衡集群搭建

    想用Docker模拟一下nginx+tomcat集群部署,今天折腾了一天,遇坑无数,终于在午夜即将到来之际将整个流程走通,借本文希望给同样遇到类似问题的小伙伴们留点线索。

    主机环境是CentOS 7,nginx容器作为负载均衡,两个tomcat容器模拟Java应用服务器,这三个容器部署在同一个主机上。

    一、创建tomcat Docker镜像(如果你有其他的镜像,该步可略过,下同)

    Dockerfile:

    FROM centos
    LABEL author=Aldwin
    
    ENV JDKFile="server-jre-8u152-linux-x64.tar.gz" 
        TomcatFile="apache-tomcat-9.0.2.tar.gz" 
        TomcatConfigFile="server.xml" 
        WebsitePath=/home/website 
        JAVA_HOME=/usr/local/java 
        CLASSPATH=$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar 
        CATALINA_HOME=/usr/local/tomcat 
        PATH=$PATH:$JAVA_HOME/bin:$CATALINA_HOME/bin
    
    COPY $JDKFile $TomcatFile $TomcatConfigFile ./
    
    RUN mkdir -p $JAVA_HOME 
        && tar -xzf $JDKFile -C $JAVA_HOME --strip-components=1 
        && rm -rf $JDKFile 
        && mkdir -p $CATALINA_HOME 
        && tar -xzf $TomcatFile -C $CATALINA_HOME --strip-components=1 
        && rm -rf $TomcatFile 
        && mkdir -p $WebsitePath 
        && cp -f $TomcatConfigFile $CATALINA_HOME/conf 
        && rm -rf $TomcatConfigFile
    
    EXPOSE 80
    
    CMD ["/usr/local/tomcat/bin/catalina.sh", "run"] && tail -f /usr/local/tomcat/logs/catalina.out

    1. 创建一个空目录,将Dockerfile文件、war包文件和tomcat的配置文件server.xml放入其中。

    2. 将上面涉及的安装文件下载到Dockerfile同目录下。

    3. cd到1所述的目录中,生成website镜像:

    $ docker build -t website .

    二、创建nginx Docker镜像

    Dockerfile:

    FROM nginx
    LABEL author=Aldwin
    
    COPY nginx.conf ./
    
    RUN mv -f nginx.conf /etc/nginx/

    nginx.conf:

    worker_processes  2;
    
    events {
        worker_connections  1024;
    }
    
    http {
        include       mime.types;
        default_type  application/octet-stream;
        sendfile        on;
        keepalive_timeout  650;
    
        upstream tomcats {
            server website1 weight=5 max_fails=3 fail_timeout=20s;
            server website2 weight=5 max_fails=3 fail_timeout=20s;
        }
    
        server {
            listen       80;
            server_name  localhost;
    
            location / {
                proxy_pass http://tomcats;
                proxy_http_version 1.1;
                proxy_set_header Connection "";
                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;
            }
        }
    }

    红色部分是需要特别注意的。

    1. 创建一个空目录,将Dockerfile文件、war包文件和tomcat的配置文件server.xml放入其中。

    2. 将上面涉及的安装文件下载到Dockerfile同目录下。

    3. cd到1所述的目录中,生成mynginx镜像:

    $ docker build -t mynginx .

    三、创建网络(User-Defined Networks)

    同主机下的容器互联需要建立网络。

    1. 首先,我们可以先查看现有的网络:

    $ docker network ls

    2. 创建名为mynet的网络

    $ docker network create mynet

    四、启动容器

    1. 用website镜像启动容器website1:

    $ docker run -d --name website1 -p 9201:80 --net mynet website

    注意--net mynet,该参数将website1容器加入到了mynet网络中。

    2. 用website镜像启动容器website2:

    $ docker run -d --name website2 -p 9202:80 --net mynet website

    3. 用mynginx镜像启动容器nginx:

    $ docker run -d --name nginx -p 80:80 --net mynet mynginx

    这时访问localhost,应该就可以访问到tomcat下你war包内的默认主页了。

    注意,nginx.conf里的upstream server一定是容器的别名,而不能是docker分配给容器的ip地址,否则会报错。

  • 相关阅读:
    java_IO流之 NIO
    No enclosing instance of type Outer is accessible. Must qualify the allocation with an enclosing instance of type Outer (e.g. x.new A() where x is an instance of Outer)
    JAVA I/O流 之入门
    10年老司机经验总结--程序员兼职的那些事
    python 去除html 超链接href 如何实现?
    《模式分类(原书第二版)》pdf格式下载电子书免费下载
    通知-招财猫问题通知专用
    Centos6.5 安装 python3.5 虚拟环境 virtualenvwrapper
    5.区块链平台以太坊从入门到精通之 以太网区块链网络
    4.区块链平台以太坊从入门到精通之 以太币
  • 原文地址:https://www.cnblogs.com/eagle6688/p/8245567.html
Copyright © 2011-2022 走看看