[root@localhost ~]# tree -L 3 pes 文件目录结构
pes
├── docker-compose.yml
├── haproxy
│ └── haproxy.cfg
├── java
│ ├── Dockerfile
│ ├── jdk
│ │ ├── bin
│ │ ├── conf
│ │ ├── include
│ │ ├── jmods
│ │ ├── legal
│ │ ├── lib
│ │ ├── LICENSE -> legal/java.base/LICENSE
│ │ ├── man
│ │ ├── README
│ │ └── release
│ ├── src
│ │ ├── application.properties
│ │ └── Project_ExamSystem-V1.0.0.war
│ └── start.sh
├── mysql
│ ├── data
│ │ ├── auto.cnf
│ │ ├── ca-key.pem
│ │ ├── ca.pem
│ │ ├── client-cert.pem
│ │ ├── client-key.pem
│ │ ├── docker01.yuanyu.zhangmin.err
│ │ ├── ib_buffer_pool
│ │ ├── ibdata1
│ │ ├── ib_logfile0
│ │ ├── ib_logfile1
│ │ ├── ibtmp1
│ │ ├── mysql
│ │ ├── mysql.sock -> /var/run/mysqld/mysqld.sock
│ │ ├── performance_schema
│ │ ├── private_key.pem
│ │ ├── project_exam_system
│ │ ├── public_key.pem
│ │ ├── server-cert.pem
│ │ ├── server-key.pem
│ │ └── sys
│ └── project_exam_system.sql
└── web
└── src
└── dist
记得做hosts劫持:
192.168.1.81 bu.yuanyu.zhangmin
[root@localhost ~]# cd pes/
[root@localhost pes]# vim docker-compose.yml
version: "3"
services:
web0:
container_name: web0
image: nginx:latest
#ports:
#- "80:80"
volumes:
- /root/pes/web/src/dist:/usr/share/nginx/html
hostname: web01.yuanyu.zhangmin
expose:
- 80
web1:
container_name: web1
image: nginx:latest
#ports:
#- "81:80"
volumes:
- /root/pes/web/src/dist:/usr/share/nginx/html
hostname: web01.yuanyu.zhangmin
expose:
- 80
web2:
container_name: web2
image: nginx:latest
#ports:
#- "82:80"
volumes:
- /root/pes/web/src/dist:/usr/share/nginx/html
hostname: web01.yuanyu.zhangmin
expose:
- 80
java0:
container_name: java0
build: ./java
#ports:
#- "8080:8080"
volumes:
- /root/pes/java/src/:/java/src/
hostname: java01.yuanyu.zhangmin
java1:
container_name: java1
image: pes_java:latest
#ports:
#- "8080:8080"
volumes:
- /root/pes/java/src/:/java/src/
hostname: java01.yuanyu.zhangmin
java2:
container_name: java2
image: pes_java:latest
#ports:
#- "8080:8080"
volumes:
- /root/pes/java/src/:/java/src/
hostname: java01.yuanyu.zhangmin
mysql:
container_name: mysql01
image: mysql:5.7.44
ports:
- "3306:3306"
volumes:
- /root/pes/mysql/data:/var/lib/mysql/
haproxy:
container_name: haproxy01
image: haproxy:latest
volumes:
- ./haproxy/:/haproxy-override
- ./haproxy/haproxy.cfg:/usr/local/etc/haproxy/haproxy.cfg
ports:
- "5000:5000"
- "8888:8888"
- "8080:8080"
expose:
- 5000
- 8888
- 8080
links:
- web0
- web1
- web2
- java0
- java1
- java2
[root@localhost pes]# vim haproxy/haproxy.cfg //haproxy配置文件
global
daemon
# nbproc 1
# pidfile /var/run/haproxy.pid
# 工作目录
# chroot /usr/local/etc/haproxy
defaults
log 127.0.0.1 local0 err #[err warning info debug]
mode http #默认的模式mode { tcp|http|health },tcp是4层,http是7层,health只会返回OK
retries 2 #两次连接失败就认为是服务器不可用,也可以通过后面设置
option redispatch #当serverId对应的服务器挂掉后,强制定向到其他健康的服务器
option abortonclose #当服务器负载很高的时候,自动结束掉当前队列处理比较久的链接
option dontlognull #日志中不记录负载均衡的心跳检测记录
maxconn 4096 #默认的最大连接数
timeout connect 5000ms #连接超时
timeout client 30000ms #客户端超时
timeout server 30000ms #服务器超时
#timeout check 2000 #=心跳检测超时
######## 监控界面配置 #################
listen admin_status
# 监控界面访问信息
bind 0.0.0.0:8888
mode http
# URI相对地址
stats uri /dbs
# 统计报告格式
stats realm Global\ statistics
# 登录账户信息
stats auth admin:123456
########frontend配置##############
######## mysql负载均衡配置 ###############
listen proxy-mysql
bind 0.0.0.0:3306
mode tcp
# 负载均衡算法
# static-rr 权重, leastconn 最少连接, source 请求IP, 轮询 roundrobin
balance roundrobin
# 日志格式
option tcplog
# 在 mysql 创建一个没有权限的haproxy用户,密码为空。 haproxy用户
# create user 'haproxy'@'%' identified by ''; FLUSH PRIVILEGES;
option mysql-check user haproxy
# 这里是容器中的IP地址,由于配置的是轮询roundrobin,weight 权重其实没有生效
server MYSQL_1 192.168.130.100:3306 check weight 1 maxconn 2000
server MYSQL_2 192.168.130.101:3306 check weight 1 maxconn 2000
#server MYSQL_3 192.168.130.102:3306 check weight 1 maxconn 2000
# 使用keepalive检测死链
# option tcpka
#########################################
######## java负载均衡配置 ###############
listen proxy-java
bind 0.0.0.0:8080
mode tcp
# 负载均衡算法
# static-rr 权重, leastconn 最少连接, source 请求IP, 轮询 roundrobin
balance roundrobin
# 日志格式
option tcplog
# 在 mysql 创建一个没有权限的haproxy用户,密码为空。 haproxy用户
# create user 'haproxy'@'%' identified by ''; FLUSH PRIVILEGES;
#option mysql-check user haproxy
# 这里是容器中的IP地址,由于配置的是轮询roundrobin,weight 权重其实没有生效
server java0 java0:8080 check weight 1 maxconn 2000
server java1 java1:8080 check weight 1 maxconn 2000
server java2 java2:8080 check weight 1 maxconn 2000
#server MYSQL_3 192.168.130.102:3306 check weight 1 maxconn 2000
# 使用keepalive检测死链
# option tcpka
#########################################
######## web负载均衡配置 ###############
listen proxy-web
bind 0.0.0.0:5000
mode http
# 负载均衡算法
# static-rr 权重, leastconn 最少连接, source 请求IP, 轮询 roundrobin
balance roundrobin
# 日志格式
option tcplog
# 在 mysql 创建一个没有权限的haproxy用户,密码为空。 haproxy用户
# create user 'haproxy'@'%' identified by ''; FLUSH PRIVILEGES;
#option mysql-check user haproxy
# 这里是容器中的IP地址,由于配置的是轮询roundrobin,weight 权重其实没有生效
server web0 web0:80 check weight 1 maxconn 2000
server web1 web1:80 check weight 1 maxconn 2000
server web2 web2:80 check weight 1 maxconn 2000
#server MYSQL_3 192.168.130.102:3306 check weight 1 maxconn 2000
# 使用keepalive检测死链
# option tcpka
#########################################
[root@localhost pes]# docker-compose up -d //运行
[root@localhost pes]# docker-compose ps