使用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: