zoukankan      html  css  js  c++  java
  • Mysql MGR + Consul + Consul-template + Haproxy 搭建mysql 高可用集群 (三)

    使用consul做服务发现

    Consul是一款用Go开发的分布式服务协调管理的工具,它提供了服务发现,健康检查,Key/Value存储等功能,并且支持跨数据中心的功能。
    这里用consul来做mysql主从的服务发现。

    Consul-template是Consul的一个扩展工具,通过监听Consul中的数据可以动态修改一些配置文件。
    这里用来动态生成haproxy的配置文件。

    从官网下载consul和consul-template:

    wget https://releases.hashicorp.com/consul/1.4.0/consul_1.4.0_linux_amd64.zip
    wget https://releases.hashicorp.com/consul-template/0.19.5/consul-template_0.19.5_linux_amd64.tgz
    #解压后每个程序就一个单独的执行文件,将其复制到/usr/bin下:
    cp consul  /usr/bin
    cp consul-template /usr/bin 

     配置consul集群,三个节点都服务端工作模式,同时也做客户端使用:

    mkdir /root/consul
    cd  /root/consul
    #swarm1:
    nohup consul agent -server -bootstrap-expect 2 -data-dir=./data/ -config-dir=./config/  -node=s1 
    -enable-script-checks=true -bind=172.16.0.90 -ui -client=0.0.0.0 -rejoin 2>&1 1>./consul.log & #swarm2: nohup consul agent -server -bootstrap-expect 2 -data-dir=./data/ -config-dir=./config/ -node=s2
    -enable-script-checks=true -bind=172.16.0.76 -ui -client=0.0.0.0 -rejoin 2>&1 1>./consul.log & join 172.16.0.90 #swarm3: nohup consul agent -server -bootstrap-expect 2 -data-dir=./data/ -config-dir=./config/ -node=s3
    -enable-script-checks=true -bind=172.16.0.175 -ui -client=0.0.0.0 -rejoin 2>&1 1>./consul.log & join 172.16.0.90 consul members Node Address Status Type Build Protocol DC Segment s1 172.16.0.90:8301 alive server 1.4.0 2 dc1 <all> s2 172.16.0.76:8301 alive server 1.4.0 2 dc1 <all> s3 172.16.0.175:8301 alive server 1.4.0 2 dc1 <all>

     参数含义:

    server	已服务端模式工作
    bootstrap-expect N	至少N个节点集群正常工作
    data-dir	数据目录
    config-dir	配置目录
    node=s1	节点名称
    enable-script-checks	允许服务发现使用script
    bind	用于集群通信的绑定IP地址
    ui	启动页面ui服务
    client	客户端可见IP地址
    rejoin	自动重新加入集群

    配置mysql的服务发现,定义两个服务“mysql_rw”、“ mysql_ro”:(每台主机上分别修改address、tags)

    cd /root/consul/config
    vi mysql_rw.json 
    {
        "service": {
            "name": "mysql_rw",
            "port": 40010,
            "address": "172.16.0.90",
            "tags": ["swarm1_rw"],
            "checks": [
                {
                    "interval": "10s",
                    "timeout": "1s",
                    "args": ["sh","-C","/root/consul/check_mysql_status.sh","master"]
                }
            ]
        }
    }
    vi mysql_ro.json 
    {
        "service": {
            "name": "mysql_ro",
            "port": 40010,
            "address": "172.16.0.90",
            "tags": ["swarm1_ro"],
            "checks": [
                {
                    "interval": "10s",
                    "timeout": "1s",
                    "args": ["sh","-C","/root/consul/check_mysql_status.sh","slave"]
                }
            ]
        }
    }
    

      

    脚本内容如下,用来判断当前mysql是主节点or从节点:

    [root@swarm1 config]# cat /root/consul/check_mysql_status.sh
    #!/bin/bash
    if [ ! -n "$1" ] ;then
            echo "you have not input a agrs! eg: sh check_mysql_status.sh [master|slave]"
            exit 2
    fi
    ROLE_VALUE=$1
    
    comm="/data/mysql_root/base_phony/40010/bin/mysql -uroot -h127.0.0.1 -P40010"
    value=`$comm -Nse "select 1"`
    server_uuid=`$comm -Nse "select VARIABLE_VALUE from performance_schema.global_variables where VARIABLE_NAME='server_uuid';"`
    MEMBER_ROLE=`$comm -Nse "SELECT MEMBER_ROLE from performance_schema.replication_group_members where MEMBER_ID='$server_uuid'"`
    MEMBER_STATE=`$comm -Nse "SELECT MEMBER_STATE from performance_schema.replication_group_members where MEMBER_ID='$server_uuid'"`
    
    echo $value
    echo $server_uuid
    echo $MEMBER_ROLE
    echo $MEMBER_STATE
    
    echo "check mysql"
    if [ $value -ne 1 ];then
            echo "mysql is down!!"
            exit 2
    fi
    
    echo "check online"
    if [ $MEMBER_STATE != "ONLINE" ];then
            echo "MGR if not ONLINE!!!"
            exit 2
    fi
    
    if [ $ROLE_VALUE == "master" ];then
            if [ $MEMBER_ROLE == "PRIMARY" ] ;then
                    echo "I am master!"
                    exit 0
            else
                    echo "I am not master!"
                    exit 2
            fi
    elif [ $ROLE_VALUE == "slave" ];then
            if [ $MEMBER_ROLE != "PRIMARY" ] ;then
                    echo "I am slave!"
                    exit 0
            else
                    echo "I am not slave!"
                    exit 2
            fi
    else
            echo "your arg is not master or slave!"
            exit 2
    fi

    重载consul配置,使服务发现生效:

    consul reload

    登录http://58.49.63.150:8500/ui ,确认rw、ro均已注册服务,一个rw,两个ro:

  • 相关阅读:
    Servlet3.0-使用注解定义Servlet
    poj 1256 Anagram—next_permutation的神奇应用
    poj 1664 放苹果 (划分数)
    poj 1011 Sticks
    poj和hdu部分基础算法分类及难度排序
    Notepad++支持jQuery、html5、css3
    Codeforces Round #395 (Div. 2) C. Timofey and a tree
    Codeforces Round #390 (Div. 2) D. Fedor and coupons
    bazel-编译动态库
    bazel-编译多目标
  • 原文地址:https://www.cnblogs.com/qixingyi/p/10309597.html
Copyright © 2011-2022 走看看