Consul 部署
下载链接
wget https://releases.hashicorp.com/consul/1.7.1/consul_1.7.1_linux_amd64.zip
unzip consul_1.7.1_linux_amd64.zip
cp consul /usr/bin
vim /usr/lib/systemd/system/consul.service
systemctl 管理
[Unit]
Description=consul
Documentation=https://www.consul.io/docs/index.html
Wants=network-online.target
After=network-online.target
[Service]
PrivateTmp=true
WorkingDirectory=/usr/bin/
User=consul
Group=consul
ExecStart=/usr/bin/consul agent -config-dir=/etc/consul.d
ExecStop=/usr/bin/consul leave
ExecReload=/usr/bin/consul reload
StandardOutput=journal
StandardError=inherit
[Install]
WantedBy=multi-user.target
配置文件
mkdir /etc/consul.d
mkdir /data/consul_data/script -p
cd /etc/consul.d
vim server.json
vim r_fat.json
vim w_fat.json
cd /data/consul_data/script
vim check_mgr_r.sh
server.json
{
"server": true,
"datacenter": "fat",
"client_addr": "0.0.0.0",
"advertise_addr":"10.10.10.64"
"node_name": "fatmysql3",
"bootstrap_expect": 2,
"enable_syslog": false,
"domain": "consul",
"log_file": "/data/consul_data/consul_server.log",
"enable_local_script_checks": true,
"data_dir": "/data/consul_data",
"recursors" : ["8.8.8.8"],
"ports" : {
"dns" : 53,
"http": 8500,
"server": 8300
},
"retry_join": [ "10.10.10.35","10.10.10.36","10.10.10.64" ]
}
r_fat.json
{
"services":[
{
"ID": "fat_check_r1",
"Name": "r_fat",
"Tags": ["fat_read_3306"],
"Address": "10.4.22.35",
"Port": 3306,
"check":
{
"args": ["/data/consul_data/script/check_mgr_r.sh","10.4.22.35","3306"],
"interval": "1s",
"timeout": "1s"
}
},
{
"ID": "fat_check_r2",
"Name": "r_fat",
"Tags": ["fat_read_3306"],
"Address": "10.4.22.36",
"Port": 3306,
"check":
{
"args": ["/data/consul_data/script/check_mgr_r.sh","10.4.22.36","3306"],
"interval": "1s",
"timeout": "1s"
}
},
{
"ID": "fat_check_r3",
"Name": "r_fat",
"Tags": ["fat_read_3306"],
"Address": "10.4.22.64",
"Port": 3306,
"check":
{
"args": ["/data/consul_data/script/check_mgr_r.sh","10.4.22.64","3306"],
"interval": "1s",
"timeout": "1s"
}
}
]
}
w_fat.json
{
"services":[
{
"ID": "fat_check_w1",
"Name": "w_fat",
"Tags": ["fat_write_3306"],
"Address": "10.4.22.35",
"Port": 3306,
"check":
{
"args": ["/data/consul_data/script/check_mgr_w.sh","10.4.22.35","3306" ],
"interval": "1s",
"timeout": "1s"
}
},
{
"ID": "fat_check_w2",
"Name": "w_fat",
"Tags": ["fat_write_3306"],
"Address": "10.4.22.36",
"Port": 3306,
"check":
{
"args": ["/data/consul_data/script/check_mgr_w.sh","10.4.22.36","3306" ],
"interval": "1s",
"timeout": "1s"
}
},
{
"ID": "fat_check_w3",
"Name": "w_fat",
"Tags": ["fat_write_3306"],
"Address": "10.10.10.64",
"Port": 3306,
"check":
{
"args": ["/data/consul_data/script/check_mgr_w.sh","10.4.22.64","3306" ],
"interval": "1s",
"timeout": "1s"
}
}
]
}
check_mgr_r.sh
#!/bin/bash
host=$1
port=$2
user="monitor_status"
passwod="monitor_status"
comm="/usr/local/mysql/bin/mysql -u$user -h $host -P $port -p$passwod"
value=`$comm -Nse "select 1"`
primary_member=`$comm -Nse "select variable_value from performance_schema.global_status WHERE VARIABLE_NAME= 'group_replication_primary_member'"`
server_uuid=`$comm -Nse "select variable_value from performance_schema.global_variables where VARIABLE_NAME='server_uuid';"`
# 判断mysql是否存活
if [ -z $value ]
then
echo "mysql $port is down....."
exit 2
fi
# 判断节点状态
node_state=`$comm -Nse "select MEMBER_STATE from performance_schema.replication_group_members where MEMBER_ID='$server_uuid'"`
if [[ $node_state != "ONLINE" ]]
then
echo "MySQL $port state is not online...."
exit 2
fi
# 判断是不是主节点
if [[ $server_uuid != $primary_member ]]
then
echo "MySQL $port Instance is slave ........"
exit 0
else
node_num=`$comm -Nse "select count(*) from performance_schema.replication_group_members"`
# 判断如果没有任何从节点,主节点也注册从角色服务。
if [ $node_num -eq 1 ]
then
echo "MySQL $port Instance is slave ........"
exit 0
else
echo "MySQL $port Instance is master ........"
exit 2
fi
fi
check_mgr_w.sh
#!/bin/bash
host=$1
port=$2
user="monitor_status"
passwod="monitor_status"
comm="/usr/local/mysql/bin/mysql -u$user -h $host -P $port -p$passwod"
value=`$comm -Nse "select 1"`
primary_member=`$comm -Nse "select variable_value from performance_schema.global_status WHERE VARIABLE_NAME= 'group_replication_primary_member'"`
server_uuid=`$comm -Nse "select variable_value from performance_schema.global_variables where VARIABLE_NAME='server_uuid';"`
# 判断mysql是否存活
if [ -z $value ]
then
echo "mysql $port is down....."
exit 2
fi
判断节点状态
node_state=`$comm -Nse "select MEMBER_STATE from performance_schema.replication_group_members where MEMBER_ID='$server_uuid'"`
if [ $node_state != "ONLINE" ]
then
echo "MySQL $port state is not online...."
exit 2
fi
# 判断是不是主节点
if [[ $server_uuid == $primary_member ]]
then
echo "MySQL $port Instance is master ........"
exit 0
else
echo "MySQL $port Instance is slave ........"
exit 2
fi
授权 否则启动报错:
setcap 'CAP_NET_BIND_SERVICE=+ep' /usr/bin/consul
测试
dig @127.0.0.1 -p 53 r_fat.service.consul