一.下载
https://www.haproxy.org/download/2.0/src/haproxy-2.0.5.tar.gz
或者在这里下载(下面需要的各个配置文件都有):
https://download.csdn.net/download/cyw8998/89170129
二.编写文件,制作docker镜像
1.Dockerfile:
FROM centos:7
#将同级目录下的文件haproxy-2.0.5.tar.gz拷贝到该目录下
ADD haproxy-2.0.5.tar.gz /usr/local/etc/
## 安装编译工具,开始安装编译haproxy
RUN yum install -y gcc gcc-c++ glibc glibc-devel pcre \pcre-devel openssl openssl-devel systemd-devel \net-tools vim iotop bc zip unzip zlib-devel lrzsz \tree screen lsof tcpdump wget ntpdate –y \&& cd /usr/local/etc/haproxy-2.0.5 \&& make ARCH=x86_64 TARGET=linux-glibc \USE_PCRE=1 USE_OPENSSL=1 USE_ZLIB=1 \USE_SYSTEMD=1 USE_CPU_AFFINITY=1 \PREFIX=/usr/local/haproxy \&& make install PREFIX=/usr/local/haproxy \&& cp haproxy /usr/sbin/ \&& mkdir /usr/local/haproxy/run
# 将Dockerfile同级目录下的haproxy.cfg配置文件拷贝到镜像的该目录下
ADD haproxy.cfg /etc/haproxy/
ADD haproxy.cfg /usr/local/etc/haproxy-2.0.5/
# 将 run_haproxy.sh 启动脚本文件拷贝到镜像的该目录下
ADD run_haproxy.sh /usr/bin
# 赋予 run_haproxy.sh 运行权限
RUN chmod +x /usr/bin/run_haproxy.sh
# 向外暴露 80 和 8888 端口
EXPOSE 80 8888
#执行 run_haproxy.sh 脚本
CMD ["/usr/bin/run_haproxy.sh"]
2.haproxy.cfg:
global#工作目录chroot /usr/local/etc/haproxy-2.0.5#日志文件,使用rsyslog服务中local5日志设备(/var/log/local5),等级infolog 127.0.0.1 local5 info#守护进程运行daemondefaultslog globalmode http#日志格式option httplog#日志中不记录负载均衡的心跳检测记录option dontlognull#连接超时(毫秒)timeout connect 5000#客户端超时(毫秒)timeout client 50000#服务器超时(毫秒)timeout server 50000#监控界面
listen admin_stats#监控界面的访问的IP和端口bind 0.0.0.0:8888#访问协议mode http#URI相对地址stats uri /dbs#统计报告格式stats realm Global\ statistics#登陆帐户信息stats auth admin:admin
#数据库负载均衡
listen proxy-mysql#访问的IP和端口bind 0.0.0.0:3306 #网络协议mode tcp#负载均衡算法(轮询算法)#轮询算法:roundrobin#权重算法:static-rr#最少连接算法:leastconn#请求源IP算法:source balance roundrobin#日志格式option tcplog#在MySQL中创建一个没有权限的haproxy用户,密码为空。Haproxy使用这个账户对MySQL数据库心跳检测option mysql-check user haproxyserver pxc_node1 170.19.0.11:3306 check weight 1 maxconn 2000 server pxc_node2 170.19.0.12:3306 check weight 1 maxconn 2000server pxc_node3 170.19.0.13:3306 check weight 1 maxconn 2000#使用keepalive检测死链option tcpka
3.run_haproxy.sh
#!/bin/bash
haproxy -f /etc/haproxy/haproxy.cfg
tail -f /etc/hosts
4.制作镜像
docker build -t mars/haproxy .
三.启动pxc和harproxy
1.dock-compose制作镜像并启动pxc,见上一篇文章,pxc8证书制作也略
version : '3.7'
services:db1:container_name: db1image: percona/percona-xtradb-cluster:8.0privileged: truenetworks:my-pxc:ipv4_address: 170.19.0.11environment:- "CLUSTER_NAME=JWSPXC"- "XTRABACKUP_PASSWORD=123456"- "MYSQL_ROOT_PASSWORD=123456"- "TZ=Asia/Shanghai"ports:- "13306:3306"volumes:- v301:/var/lib/mysql- ~/pxc-docker-test/cert:/cert- ~/pxc-docker-test/config:/etc/percona-xtradb-cluster.conf.ddb2:container_name: db2image: percona/percona-xtradb-cluster:8.0privileged: truenetworks:my-pxc:ipv4_address: 170.19.0.12environment:- "CLUSTER_NAME=JWSPXC"- "XTRABACKUP_PASSWORD=123456"- "TZ=Asia/Shanghai"- "CLUSTER_JOIN=db1"ports:- "23306:3306"volumes:- v302:/var/lib/mysql- ~/pxc-docker-test/cert:/cert- ~/pxc-docker-test/config:/etc/percona-xtradb-cluster.conf.drestart: always #on-failuredepends_on:- db1db3:container_name: db2image: percona/percona-xtradb-cluster:8.0privileged: truenetworks:my-pxc:ipv4_address: 170.19.0.12environment:- "CLUSTER_NAME=JWSPXC"- "XTRABACKUP_PASSWORD=123456"- "TZ=Asia/Shanghai"- "CLUSTER_JOIN=db1"ports:- "33306:3306"volumes:- v303:/var/lib/mysql- ~/pxc-docker-test/cert:/cert- ~/pxc-docker-test/config:/etc/percona-xtradb-cluster.conf.drestart: always #on-failuredepends_on:- db1volumes:v301:v302:v303:
networks:my-pxc:driver: bridgeipam:driver: defaultconfig:- subnet: 170.19.0.0/24
#docker-compose up -d db1 先检测第一个主点,成功后再执行下一句
#docker-compose up -d db2 检测第2个点是否成功,成功后再执行下一句
#docker-compose up -d db3
注意:docker-compose定义的卷标名和network名,在生成docker后,查看network和volume名都带着前缀d-compose_取决于文件夹名_
5.启动一下haproxy
docker run -d -p 80:80 -p 8888:8888 mars/haproxy
因为已经配置了haproxy.cfg,所以容器启动后会自动代理pxc集群
6.访问一下haproxy2.0控制台
192.168.xx.xxx:8888/dbs
发现负载的两台目标机器是DOWN状态
在 pxc集群中创建一个用户给 haproxy使用
CREATE USER 'haproxy'@'%' IDENTIFIED BY '';
运行
docker run -d -p 80:80 -p 8888:8888 -p 13301:3306 --name haproxy --privileged --net=d-compose_my-pxc --ip 170.19.0.10 mars/haproxy
再访问控制台
192.168.xx.xxx:8888/dbs
发现负载的两台目标机器是up状态