Consul集群搭建 2Server+ 3Client

环境说明:
192.168.202.177 consul-server01
192.168.202.177 consul-server02
192.168.202.174 mysql server node1、consul client
192.168.202.175 mysql server node2、consul client
192.168.202.176 mysql server node3、consul client
系统版本:centos 7.5
Mysql version:5.7.25
consul version:1.4.4

安装

在官网: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/shell

配置部署

在consul server 192.168.202.177上 编写配置文件

[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上 编写配置文件

[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.174、192.168.202.175、192.168.202.176上编写配置文件,三台服务器的上bind_addr 修改为响应IP即可

[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.174、192.168.202.175、192.168.202.176上编写检测primay 脚本 和检测slave 脚本 

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.177、192.168.202.178上 
nohup consul agent -config-dir=/etc/consul.d > /data/consul/consul.log & 

启动consul client 在192.168.202.174、192.168.202.175、192.168.202.176
nohup consul agent -config-dir=/etc/consul.d > /data/consul/consul.log & 

观察consul server的log日志3个client自动注册到了consul上了 

查看consul成员

[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>
访问consulserver的web页面 http://192.168.202.177:8500/ui/ 

到此为止consul 集群已经搭建成功了 

构建bind域名解析

请参考 https://www.cnblogs.com/EikiXu/p/10683490.html

参考来源:

https://www.hi-linux.com/posts/28048.html

原文地址:https://www.cnblogs.com/EikiXu/p/10684769.html