欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

docker-compose部署nginx代理tomcat群集

程序员文章站 2022-06-07 09:09:07
...

一、在hos主机先验证下

mkdir myconsete
 上传一份jdk
cd myconsete/
wget https://mirrors.aliyun.com/apache/tomcat/tomcat-8/v8.5.56/bin/apache-tomcat-8.5.56.tar.gz 下载软件包
tar  zxf jdk-8u211-linux-x64.tar.gz 
tar zxf apache-tomcat-8.5.56.tar.gz
mv jdk1.8.0_211/ /usr/local/java
mv apache-tomcat-8.5.56 /usr/local/tomcat8
 
vim /etc/profile

export JAVA_HOME=/usr/local/java
export PATH=$JAVA_HOME/bin/:$PATH

./etc/profile   (刷新)
java -version (查看版本)

/usr/local/tomcat8/bin/catalina.sh run (以前台方式启动)

二、编写dockerfile

cd tomcat/
vim Dockerfile

FROM centos-7
ADD jdk-8u211-linux-x64.tar.gz  /usr/local/
ENV JAVA_HOME /usr/local/jdk1.8.0_211
ADD apache-tomcat-8.5.56.tar.gz /usr/local/
EXPOSE 8080
ENTRYPOINT ["/usr/local/apache-tomcat-8.5.56/bin/catalina.sh", "run"]

三、编写tomcat网页文件

cd ..
mkdir webserver
cd webserver/
mkdir {tomcat1,tomcat2}

vim tomcat1/index.jsp

<%
out.println("this is tomcat1");
%>

cp tomcat1/index.jsp tomcat2/ (更改下里面内容)

四、编写nginx代理

cd..
mkdir nginx
cd nginx/
vim default.conf

upstream webserver {     (添加负载均衡主机)
    server tomcat1:8080;
    server tomcat2:8080;
}
server {
    listen       80;
    listen  [::]:80;
    server_name  localhost;

    #charset koi8-r;
    #access_log  /var/log/nginx/host.access.log  main;

    location / {
        proxy_pass http://webserver;   (修改为负载均衡地址)
    }
}

五、同步时间规划

cd ..
mkdir etc
cp /usr/share/zoneinfo/Asia/Shanghai  etc/localtime
tree

    docker-compose部署nginx代理tomcat群集

六、编写docker-compose

cd ..
vim docker-compose.yml

version: "3"
services:
  nginx:
    image: nginx
    restart: always
    ports:
       - 80:80
    links:
       - tomcat1:tomcat1
       - tomcat2:tomcat2
    volumes:
       - ./etc/localtime:/etc/localtime
       - ./nginx/default.conf:/etc/nginx/conf.d/default.conf
    depends_on:
       - tomcat1
       - tomcat2
  tomcat1:
      hostname: tomcat1
      build: ./tomcat
      volumes:
       - ./etc/localtime:/etc/localtime
       - ./webserver/tomcat1:/usr/local/apache-tomcat-8.5.56/webapps/ROOT
  tomcat2:
      hostname: tomcat2
      build: ./tomcat
      volumes:
       - ./etc/localtime:/etc/localtime
       - ./webserver/tomcat2:/usr/local/apache-tomcat-8.5.56/webapps/ROOT

    depends_on:  这个选项用作 先启动tomcat 在启动nginx

七、启动查看验证

docker-compose up -d

docker-compose部署nginx代理tomcat群集

查看镜像:

docker-compose部署nginx代理tomcat群集

网页访问

docker-compose部署nginx代理tomcat群集

 

 

 

 

 

 

 

 

 

 

相关标签: docker