Docker-Compose部署nginx代理Tomcat集群,实现负载均衡
程序员文章站
2022-06-07 09:09:13
...
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 !$
大体步骤分为以下四步
- 下载所需的文件tomcat,jdk
- 编写dockerfile来布署tomcat与java环境,生成镜像文件
- 编写docker-compose.yml配置文件,启动所有容器服务
- 测试负载均衡
(一)具体配置文件如下
#整个目录结构
[aaa@qq.com 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
#两个测试首页文件
[aaa@qq.com java]# cat webserver/tomcatA/index.jsp
welcome to tomcat-A server
[aaa@qq.com java]# cat webserver/tomcatB/index.jsp
welcome to tomcat-B server
#配置文件
[aaa@qq.com 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环境
[aaa@qq.com 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配置文档
[aaa@qq.com 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;
}
}
}
(二)启动所有容器服务
[aaa@qq.com java]# docker-compose up -d
(三)查看启动情况
[aaa@qq.com java]# docker-compose ps
(四)检测负载均衡
curl localhost
(五)查看日志输出信息
tail /usr/local/nginx/log/access.log
下一篇: 【JVM入门系列】一、虚拟机栈