環境說明:
192.168.202.177 consul-server01
192.168.202.177 consul-server02
192.168.202.174 mysql server node一、consul client
192.168.202.175 mysql server node二、consul client
192.168.202.176 mysql server node三、consul client
系統版本:centos 7.5
Mysql version:5.7.25
consul version:1.4.4 html
在官網:https://www.consul.io/downloads.html下載對應的版本,解壓後copy consul 到/usr/local/bin/下便可
分別在5臺機器上安裝而後運行
mkdir -pv /etc/consul.d/ && mkdir -pv /data/consul/ && mkdir -pv /data/consul/shellnode
配置部署mysql
在consul server 192.168.202.177上 編寫配置文件linux
[root@consul-server consul]# cat /etc/consul.d/server.json { "datacenter": "dc1", "data_dir": "/data/consul", "log_level": "INFO", "node_name": "consul-server01", "server": true, "bootstrap_expect": 1, "bind_addr": "192.168.202.177", "client_addr": "192.168.202.177", "ui":true, "retry_join": ["192.168.202.177","192.168.202.178"], "retry_interval": "10s", "enable_debug": false, "rejoin_after_leave": true, "start_join": ["192.168.202.177","192.168.202.178"], "enable_syslog": true, "syslog_facility": "local0" }
在consul server 192.168.202.178上 編寫配置文件web
[root@consul-server02 consul]# cat /etc/consul.d/server.json { "datacenter": "dc1", "data_dir": "/data/consul", "log_level": "INFO", "node_name": "consul-server02", "server": true, "bootstrap_expect": 1, "bind_addr": "192.168.202.178", "client_addr": "192.168.202.178", "ui":true, "retry_join": ["192.168.202.177","192.168.202.178"], "retry_interval": "10s", "enable_debug": false, "rejoin_after_leave": true, "start_join": ["192.168.202.177","192.168.202.178"], "enable_syslog": true, "syslog_facility": "local0" }
在consul client 192.168.202.17四、192.168.202.17五、192.168.202.176上編寫配置文件,三臺服務器的上bind_addr 修改成響應IP便可sql
[root@node1 consul]# cat /etc/consul.d/client.json { "data_dir": "/data/consul", "enable_script_checks": true, "bind_addr": "192.168.202.174", "retry_join": ["192.168.202.177"], "retry_interval": "30s", "rejoin_after_leave": true, "start_join": ["192.168.202.177"] , "node_name": "node1" }
在consul client 192.168.202.17四、192.168.202.17五、192.168.202.176上編寫檢測primay 腳本 和檢測slave 腳本 shell
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
[root@node1 consul]# cat /data/consul/shell/check_mysql_mgr_master.sh
#!/bin/bash
port=3306
user=
"root"
passwod=
"iforgot"
comm=
"/usr/local/mysql/bin/mysql -u$user -hlocalhost -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
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
[root@node1 consul]# cat /data/consul/shell/check_mysql_mgr_slave.sh
#!/bin/bash
port=3306
user=
"root"
passwod=
"iforgot"
comm=
"/usr/local/mysql/bin/mysql -u$user -hlocalhost -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
|
啓動consul server 在192.168.202.17七、192.168.202.178上
nohup consul agent -config-dir=/etc/consul.d > /data/consul/consul.log &
啓動consul client 在192.168.202.17四、192.168.202.17五、192.168.202.176
nohup consul agent -config-dir=/etc/consul.d > /data/consul/consul.log & json
觀察consul server的log日誌3個client自動註冊到了consul上了
查看consul成員bootstrap
[root@consul-server consul]# consul members -http-addr='192.168.202.177:8500' Node Address Status Type Build Protocol DC Segment consul-server01 192.168.202.177:8301 alive server 1.4.4 2 dc1 <all> consul-server02 192.168.202.178:8301 alive server 1.4.4 2 dc1 <all> node1 192.168.202.174:8301 alive client 1.4.4 2 dc1 <default> node2 192.168.202.175:8301 alive client 1.4.4 2 dc1 <default> node3 192.168.202.176:8301 alive client 1.4.4 2 dc1 <default>
到此爲止consul 集羣已經搭建成功了 centos
請參考 http://www.javashuo.com/article/p-bxmpukca-bc.html
參考來源: