Haproxy + Consul-template 对外提供服务:
安装haproxy:
yum install haproxy
按照consul-template要求修改frontend和backend,制作配置文件模板:
cd /etc/haproxy/ mv haproxy.cfg haproxy.cfg.tpl vi haproxy.cfg.tpl frontend proxy_rw mode tcp bind *:22014 maxconn 66666 option clitcpka default_backend mysql_servers_rw backend mysql_servers_rw mode tcp option tcp-check {{range service "mysql_rw"}} server {{.Address}} {{.Address}}:{{.Port}} check inter 10s maxconn 66666 {{end}} frontend proxy_ro mode tcp bind *:22015 maxconn 66666 option clitcpka default_backend mysql_servers_ro backend mysql_servers_ro mode tcp option tcp-check {{range service "mysql_ro"}} server {{.Address}} {{.Address}}:{{.Port}} check inter 10s maxconn 66666 {{end}}
启动consul-template:
cd /root/consul #swarm1: nohup consul-template -consul-addr 172.16.0.90:8500 -template /etc/haproxy/haproxy.cfg.tpl:/etc/haproxy/haproxy.cfg:"systemctl restart haproxy.service" >> consul-template.log & #swarm2: nohup consul-template -consul-addr 172.16.0.76:8500 -template /etc/haproxy/haproxy.cfg.tpl:/etc/haproxy/haproxy.cfg:"systemctl restart haproxy.service" >> consul-template.log & #swarm3: nohup consul-template -consul-addr 172.16.0.175:8500 -template /etc/haproxy/haproxy.cfg.tpl:/etc/haproxy/haproxy.cfg:"systemctl restart haproxy.service" >> consul-template.log &
检查haproxy配置文件及启动情况:
systemctl status haproxy.service cat /etc/haproxy/haproxy.cfg frontend proxy_rw mode tcp bind *:22014 maxconn 66666 option clitcpka default_backend mysql_servers_rw backend mysql_servers_rw mode tcp option tcp-check server 172.16.0.90 172.16.0.90:40010 check inter 10s maxconn 66666 frontend proxy_ro mode tcp bind *:22015 maxconn 66666 option clitcpka default_backend mysql_servers_ro backend mysql_servers_ro mode tcp option tcp-check server 172.16.0.76 172.16.0.76:40010 check inter 10s maxconn 66666 server 172.16.0.175 172.16.0.175:40010 check inter 10s maxconn 66666
验证连接情况,使用haproxy配置的新端口:
[root@swarm1 ~]# mysql -uqxy -pqxy -hswarm1 -P22014 -e "show variables like '%hostname%';" mysql: [Warning] Using a password on the command line interface can be insecure. +---------------+--------+ | Variable_name | Value | +---------------+--------+ | hostname | swarm1 | +---------------+--------+ [root@swarm1 ~]# mysql -uqxy -pqxy -hswarm1 -P22015 -e "show variables like '%hostname%';" mysql: [Warning] Using a password on the command line interface can be insecure. +---------------+--------+ | Variable_name | Value | +---------------+--------+ | hostname | swarm2 | +---------------+--------+ [root@swarm1 ~]# mysql -uqxy -pqxy -hswarm1 -P22015 -e "show variables like '%hostname%';" mysql: [Warning] Using a password on the command line interface can be insecure. +---------------+--------+ | Variable_name | Value | +---------------+--------+ | hostname | swarm3 | +---------------+--------+
至此一个3节点MGR集群部署完毕,三个集群模式haproxy对外提供服务,前面加一层网络负载均衡就彻底OK了。。。
备注:
这种部署模式中,consul成为了高可用关键点,如果同一主机上,consul 客户端发生异常,而mysql工作正常,则会出现问题,需要思考解决。。