使用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
s2 172.16.0.76:8301 alive server 1.4.0 2 dc1
s3 172.16.0.175:8301 alive server 1.4.0 2 dc1
参数含义:
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/bashif [ ! -n "$1" ] ;then
echo "you have not input a agrs! eg: sh check_mysql_status.sh [master|slave]"exit2
fiROLE_VALUE=$1comm="/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$valueecho$server_uuidecho$MEMBER_ROLEecho$MEMBER_STATEecho "check mysql"
if [ $value -ne 1 ];then
echo "mysql is down!!"exit2
fi
echo "check online"
if [ $MEMBER_STATE != "ONLINE" ];then
echo "MGR if not ONLINE!!!"exit2
fi
if [ $ROLE_VALUE == "master" ];then
if [ $MEMBER_ROLE == "PRIMARY" ] ;then
echo "I am master!"exit0
else
echo "I am not master!"exit2
fi
elif [ $ROLE_VALUE == "slave" ];then
if [ $MEMBER_ROLE != "PRIMARY" ] ;then
echo "I am slave!"exit0
else
echo "I am not slave!"exit2
fi
else
echo "your arg is not master or slave!"exit2
fi
重载consul配置,使服务发现生效:
consul reload
登录http://58.49.63.150:8500/ui ,确认rw、ro均已注册服务,一个rw,两个ro: