一、部署环境
序号
hostname
ip
备注
1
manager107
10.0.3.107
centos7;3.10.0-957.1.3.el7.x86_64
2
worker68
10.0.3.68
centos7;3.10.0-957.1.3.el7.x86_64
3
worker80
10.0.3.80
centos7;3.10.0-957.1.3.el7.x86_64
docker版本
docker version
Client:
Version:18.09.0API version:1.39Go version: go1.10.4Git commit: 4d60db4
Built: Wed Nov7 00:48:22 2018OS/Arch: linux/amd64
Experimental:falseServer: Docker Engine-Community
Engine:
Version:18.09.0API version:1.39 (minimum version 1.12)
Go version: go1.10.4Git commit: 4d60db4
Built: Wed Nov7 00:19:08 2018OS/Arch: linux/amd64
Experimental:false
二、首先搭建基于swarm的percona-xtradb-cluster集群,参见上一篇
三、三台服务器安装haproxy
1、编写haproxy-Dockerfile
1 cd /home/workspace/haproxy2 vim haproxy-Dockerfile
haproxy-Dockerfile内容:
1 FROM haproxy2 COPY ./haproxy.cfg /usr/local/etc/haproxy/
3 EXPOSE 4567 3306
4 CMD ["/usr/local/sbin/haproxy", "-db", "-f", "/usr/local/etc/haproxy/haproxy.cfg"]
2、编辑haproxy配置文件
vim haproxy.cfg
输入haproxy.cfg内容:
global
#工作目录chroot /usr/local/etc/haproxy
#日志文件,使用rsyslog服务中local5日志设备(/var/log/local5),等级info
log127.0.0.1 local5 info#守护进程运行
daemon
defaults
log global
mode http
#日志格式
option httplog
#日志中不记录负载均衡的心跳检测记录
option dontlognull
#连接超时(毫秒)
timeout connect5000#客户端超时(毫秒)
timeout client50000#服务器超时(毫秒)
timeout server50000#监控界面
listen admin_stats
#监控界面的访问的IP和端口
bind0.0.0.0:4567#访问协议
mode http
#URI相对地址
stats uri/haproxy
#统计报告格式
stats realm Global\ statistics
#登陆帐户信息
stats auth admin:abc123456
#数据库负载均衡
listen proxy-mysql
#访问的IP和端口(前面ip=0代表任何ip都可访问)
bind0.0.0.0:3306#网络协议
mode tcp
#负载均衡算法(轮询算法)
#轮询算法:roundrobin
#权重算法:static-rr
#最少连接算法:leastconn
#请求源IP算法:source
balance roundrobin
#日志格式
option tcplog
#在MySQL中创建一个没有权限的haproxy用户,密码为空。Haproxy使用这个账户对MySQL数据库心跳检测
option mysql-check user haproxy
server node1 node1:3306 check weight 1 maxconn 2000server node2 node2:3306 check weight 1 maxconn 2000server node3 node3:3306 check weight 1 maxconn 2000#使用keepalive检测死链
option tcpka
注:node1、node2、node3分别为三台服务器上的pxc集群的容器名字。
3、构建haproxy镜像
docker build -t haproxy01 .
4、三台服务器启动haproxy容器服务
1 docker run -it -d -p 3106:3306 -p 7010:4567 --name c_haproxy01 --net=pxc-net --privileged haproxy012 docker run -it -d -p 3106:3306 -p 7010:4567 --name c_haproxy02 --net=pxc-net --privileged haproxy013 docker run -it -d -p 3106:3306 -p 7010:4567 --name c_haproxy03 --net=pxc-net --privileged haproxy01
这时访问http://10.0.3.107:7010/haproxy,应该可以看到统计页面了。
四、三台服务器安装keepalived
1、三台服务器yum安装keepalived
2、选择107作为master,两外两台作为backup
在107配置keepalived:
1 cd /etc/keepalived/
2 vim keepalived.conf
keepalived.conf内容:
1 ! Configuration File forkeepalived2
3 vrrp_instance VI_1 {4 state MASTER5 interface enp0s31f66 virtual_router_id 51
7 priority 100
8 advert_int 1
9 authentication {10 auth_type PASS11 auth_pass 1111
12 }13 virtual_ipaddress {14 10.0.3.63
15 }16 }17
18 virtual_server 10.0.3.63 7010{19 delay_loop 6
20 lb_algo rr21 lb_kind NAT22 persistence_timeout 50
23 protocol TCP24
25 real_server 10.0.3.107 7010{26 weight 1
27 }28
29 real_server 10.0.3.68 7010{30 weight 1
31 }32
33 real_server 10.0.3.80 7010{34 weight 1
35 }36
37 }38
39 virtual_server 10.0.3.63 3106{40 delay_loop 6
41 lb_algo rr42 lb_kind NAT43 persistence_timeout 50
44 protocol TCP45
46 real_server 10.0.3.107 3106{47 weight 1
48 }49
50 real_server 10.0.3.68 3106{51 weight 1
52 }53
54 real_server 10.0.3.80 3106{55 weight 1
56 }57
58 }
启动keepalived
systemctl start keepalived
同样,在68和80上配置keepalived:
1 ! Configuration File forkeepalived2
3 vrrp_instance VI_1 {4 state BACKUP5 interface enp3s06 virtual_router_id 51
7 priority 100
8 advert_int 1
9 authentication {10 auth_type PASS11 auth_pass 1111
12 }13 virtual_ipaddress {14 10.0.3.63
15 }16 }17
18
19 virtual_server 10.0.3.63 7010{20 delay_loop 6
21 lb_algo rr22 lb_kind NAT23 persistence_timeout 50
24 protocol TCP25
26 real_server 10.0.3.107 7010{27 weight 1
28 }29
30 real_server 10.0.3.68 7010{31 weight 1
32 }33
34 real_server 10.0.3.80 7010{35 weight 1
36 }37
38 }39
40 virtual_server 10.0.3.63 3106{41 delay_loop 6
42 lb_algo rr43 lb_kind NAT44 persistence_timeout 50
45 protocol TCP46
47 real_server 10.0.3.107 3106{48 weight 1
49 }50
51 real_server 10.0.3.68 3106{52 weight 1
53 }54
55 real_server 10.0.3.80 3106{56 weight 1
57 }58
59 }
同样启动keepalived。
五、测试
现在可以用vip来访问统计页面(http://10.0.3.63:7010/haproxy)和连接数据库集群(10.0.3.63:3106)了。
模拟keepalived挂掉:
ip a
可以看到现在63正挂在107下面,现在在107上停掉keepalived
systemctl stop keepalived
可以看到63已经漂移到了68下面了。
当然还可以配置服务器检查策略。