• Mysql MGR + Consul + Consul-template + Haproxy 搭建mysql 高可用集群 (四)


    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工作正常,则会出现问题,需要思考解决。。

  • 相关阅读:
    2020/3/21 简单的学习
    2020/3/7 A-B
    2020/3/6 旋转骰子
    2020/3/6 美丽数组
    面向对象程序设计寒假作业2
    自我介绍
    深度优先搜索-迷宫问题(走迷宫题解)
    开机方案题解
    好吃的巧克力题解
    数楼梯题解
  • 原文地址:https://www.cnblogs.com/qixingyi/p/10310298.html
Copyright © 2020-2023  润新知