天天看點

3.Consul 安裝配置(Proxysql+mgr)

1.環境準備

1.MGR 三節點提前安裝好

2.Proxy 參考上一章節準備好

3.Consul 配置資訊如下

機器分布如下:

10.85.10.51 Mgr + ProxySQL + Consul client

10.85.10.52 Mgr + ProxySQL + Consul client

10.85.10.53 Mgr + ProxySQL + Consul client

10.85.10.54 Consul server

10.85.10.55 Consul server

10.85.10.56 Consul server

10.85.10.57 dnsmaq

2.Consul 安裝

2.1 軟體安裝 在51-53 proxysql 54-56節點

建立目錄 
mkdir -p /consul/log
mkdir -p /consul/consul.d
mkdir -p /etc/consul.d
sudo touch /consul/log/consul.log


解壓
cd /soft 
unzip consul_1.9.5_linux_amd64.zip  -d /consul
--建立軟連結
ln -s /consul/consul /usr/bin/consul
--檢視版本
consul --version           

2.2Consul Server 叢集配置

#54節點
nohup consul agent -server -bootstrap-expect 3 -bind=10.85.10.54 -client=0.0.0.0 -data-dir=/consul/data -node=n54 -ui >> /consul/log/consul.log 2>&1 &
#55節點
nohup consul agent -server -bootstrap-expect 3 -bind=10.85.10.55 -client=0.0.0.0 -data-dir=/consul/data -node=n55 -ui >> /consul/log/consul.log 2>&1 &
#56節點
nohup consul agent -server -bootstrap-expect 3 -bind=10.85.10.56 -client=0.0.0.0 -data-dir=/consul/data -node=n56 -ui >> /consul/log/consul.log 2>&1 &

#在55,56,将節點中加入到54叢集中
consul join 10.85.10.54

#查詢叢集狀态
consul members
consul operator raft list-peers

[root@huyidb06 data]# consul members
Node          Address           Status  Type    Build  Protocol  DC   Segment
n54           10.85.10.54:8301  alive   server  1.9.5  2         dc1  <all>
n55           10.85.10.55:8301  alive   server  1.9.5  2         dc1  <all>
n56           10.85.10.56:8301  alive   server  1.9.5  2         dc1  <all>

[root@huyidb06 soft]# consul operator raft list-peers
Node  ID                                    Address           State     Voter  RaftProtocol
n54   059044be-ea1a-4444-ebb8-6f4ca4fcce10  10.85.10.54:8300  leader    true   3
n55   b262526b-c5ab-5a61-cb57-067d036ca993  10.85.10.55:8300  follower  true   3
n56   48a4b36c-51dc-edfa-3aa8-103e29636248  10.85.10.56:8300  follower  true   3           
#54節點,其它節點(55-56)修改node_name、advertise_addr和bind_addr即可:
mkdir -p /etc/consul.d/
pkill -9 consul

cat > /etc/consul.d/server.json  <<EOF
{ 
  "data_dir": "/consul/data",
  "datacenter": "dc1",
  "node_name": "n54", 
  "enable_syslog": true,
  "log_level": "INFO", 
  "server": true, 
  "advertise_addr":"10.85.10.54", 
  "bootstrap_expect": 3, 
  "bind_addr": "10.85.10.54", 
  "client_addr": "0.0.0.0", 
  "retry_join": ["10.85.10.54","10.85.10.55","10.85.10.56"],
  "retry_interval": "10s",
  "rejoin_after_leave": true,
  "start_join": ["10.85.10.54","10.85.10.55","10.85.10.56"] ,
  "ui": true
}
EOF 

nohup consul agent -config-dir=/etc/consul.d > /consul/log/consul.log &

#55節點
mkdir -p /etc/consul.d/
pkill -9 consul

cat > /etc/consul.d/server.json <<EOF 
{ 
  "data_dir": "/consul/data",
  "datacenter": "dc1",
  "node_name": "n55", 
  "enable_syslog": true,
  "log_level": "INFO", 
  "server": true, 
  "advertise_addr":"10.85.10.55", 
  "bootstrap_expect": 3, 
  "bind_addr": "10.85.10.55", 
  "client_addr": "0.0.0.0", 
  "retry_join": ["10.85.10.54","10.85.10.55","10.85.10.56"],
  "retry_interval": "10s",
  "rejoin_after_leave": true,
  "start_join": ["10.85.10.54","10.85.10.55","10.85.10.56"] ,
  "ui": true
}

EOF

nohup consul agent -config-dir=/etc/consul.d > /consul/log/consul.log &

#56節點
mkdir -p /etc/consul.d/
pkill -9 consul

cat > /etc/consul.d/server.json <<EOF 
{ 
  "data_dir": "/consul/data",
  "datacenter": "dc1",
  "node_name": "n56", 
  "enable_syslog": true,
  "log_level": "INFO", 
  "server": true, 
  "advertise_addr":"10.85.10.56", 
  "bootstrap_expect": 3, 
  "bind_addr": "10.85.10.56", 
  "client_addr": "0.0.0.0", 
  "retry_join": ["10.85.10.54","10.85.10.55","10.85.10.56"],
  "retry_interval": "10s",
  "rejoin_after_leave": true,
  "start_join": ["10.85.10.54","10.85.10.55","10.85.10.56"] ,
  "ui": true
}

EOF


nohup consul agent -config-dir=/etc/consul.d > /consul/log/consul.log &

#查詢叢集狀态
consul members
consul operator raft list-peers           

2.3Consul Client 配置

#proxysql1
mkdir -p /etc/consul.d/

cat > /etc/consul.d/client.json <<EOF
{
  "data_dir": "/data/consul",
  "enable_script_checks": true,
  "bind_addr": "10.85.10.51",
  "retry_join": ["10.85.10.54","10.85.10.55","10.85.10.56"],
  "retry_interval": "10s",
  "rejoin_after_leave": true,
  "start_join": ["10.85.10.54","10.85.10.55","10.85.10.56"] ,
  "node_name": "mgr-client51"
}
EOF

nohup consul agent -config-dir=/etc/consul.d > /consul/log/consul.log  &


#proxysql2
mkdir -p /etc/consul.d/
cat > /etc/consul.d/client.json <<EOF
{
  "data_dir": "/data/consul",
  "enable_script_checks": true,
  "bind_addr": "10.85.10.52",
  "retry_join": ["10.85.10.54","10.85.10.55","10.85.10.56"],
  "retry_interval": "10s",
  "rejoin_after_leave": true,
  "start_join": ["10.85.10.54","10.85.10.55","10.85.10.56"] ,
  "node_name": "mgr-client52"
}
EOF

nohup consul agent -config-dir=/etc/consul.d > /consul/log/consul.log  &



#proxysql3
mkdir -p /etc/consul.d/
cat > /etc/consul.d/client.json <<EOF
{
  "data_dir": "/data/consul",
  "enable_script_checks": true,
  "bind_addr": "10.85.10.53",
  "retry_join": ["10.85.10.54","10.85.10.55","10.85.10.56"],
  "retry_interval": "10s",
  "rejoin_after_leave": true,
  "start_join": ["10.85.10.54","10.85.10.55","10.85.10.56"] ,
  "node_name": "mgr-client53"
}
EOF

nohup consul agent -config-dir=/etc/consul.d  -enable-script-checks > /consul/log/consul.log  &

#狀态檢查
[root@huyidb02 soft]# consul members
Node          Address           Status  Type    Build  Protocol  DC   Segment
n54           10.85.10.54:8301  alive   server  1.9.5  2         dc1  <all>
n55           10.85.10.55:8301  alive   server  1.9.5  2         dc1  <all>
n56           10.85.10.56:8301  alive   server  1.9.5  2         dc1  <all>
mgr-client51  10.85.10.51:8301  alive   client  1.9.5  2         dc1  <default>
mgr-client52  10.85.10.52:8301  alive   client  1.9.5  2         dc1  <default>
mgr-client53  10.85.10.53:8301  alive   client  1.9.5  2         dc1  <default>           

4.Consul 服務配置

vi /etc/consul.d/proxysql.json
{
"service": {
"id": "proxysql1",
"name": "proxysql",
"tags": ["6033-rw-app"],
"address": "10.85.10.51",
"port": 6033,
        "check": {
            "interval": "3s", 
            "tcp": "127.0.0.1:6033", 
            "timeout": "1s"
        }
}
}           

5.Consul 狀态檢查

5.1 服務檢查

#檢查配置檔案是否正常
consul validate /etc/consul.d/

# 重新加載配置檔案
consul reload

#檢查配置結果 
consul catalog services
[root@huyidb03 consul.d]# consul catalog services
consul
proxysql           

5.2 服務檢查

#安裝工具包
yum install -y bind-utils bind bind-chroot dnsmasq

#服務檢查 dig @10.85.10.54  -p 8600  proxysql.service.consul a

[root@huyidb04 soft]# dig @10.85.10.54  -p 8600 proxysql.service.consul a

; <<>> DiG 9.9.4-RedHat-9.9.4-72.el7 <<>> @10.85.10.54 -p 8600 proxysql.service.consul a
; (1 server found)
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 44905
;; flags: qr aa rd; QUERY: 1, ANSWER: 3, AUTHORITY: 0, ADDITIONAL: 1
;; WARNING: recursion requested but not available

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 4096
;; QUESTION SECTION:
;proxysql.service.consul.       IN      A

;; ANSWER SECTION:
proxysql.service.consul. 0      IN      A       10.85.10.51
proxysql.service.consul. 0      IN      A       10.85.10.52
proxysql.service.consul. 0      IN      A       10.85.10.53

;; Query time: 0 msec
;; SERVER: 10.85.10.54#8600(10.85.10.54)
;; WHEN: Tue May 25 23:10:23 CST 2021
;; MSG SIZE  rcvd: 100


#在client 端測試
dig @127.0.0.1 -p 8600 mgr-client51.node.consul
dig @127.0.0.1 -p 8600 mgr-client52.node.consul
dig @127.0.0.1 -p 8600 mgr-client53.node.consul

[root@huyidb01 consul.d]# dig @127.0.0.1 -p 8600 mgr-client51.node.consul

; <<>> DiG 9.9.4-RedHat-9.9.4-72.el7 <<>> @127.0.0.1 -p 8600 mgr-client51.node.consul
; (1 server found)
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 31142
;; flags: qr aa rd; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 2
;; WARNING: recursion requested but not available

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 4096
;; QUESTION SECTION:
;mgr-client51.node.consul.      IN      A

;; ANSWER SECTION:
mgr-client51.node.consul. 0     IN      A       10.85.10.51

;; ADDITIONAL SECTION:
mgr-client51.node.consul. 0     IN      TXT     "consul-network-segment="

;; Query time: 220 msec
;; SERVER: 127.0.0.1#8600(127.0.0.1)
;; WHEN: Tue May 25 15:11:11 CST 2021
;; MSG SIZE  rcvd: 105           

6.DSN 配置

6.1 dnsmaq 安裝

#這裡用57節點進行安裝 dnsmaq
echo "
 server=/consul/10.85.10.54#8600
 server=/consul/10.85.10.55#8600
 server=/consul/10.85.10.56#8600
 " > /etc/dnsmasq.d/10-consul

 echo "
 server=114.114.114.114
 server=8.8.8.8
 server=223.5.5.5
 " >> /etc/dnsmasq.conf


#添加到所有機器,包括本機、3個client、3個server端等
 echo "nameserver 10.85.10.57" > /etc/resolv.conf

# 在57節點上 啟動 dnsmasq
 systemctl enable dnsmasq
 systemctl restart dnsmasq
 systemctl status dnsmasq           

6.2 dnsmaq 狀态檢查

dig @10.85.10.57  -p 53 proxysql.service.consul a
 dig @10.85.10.57  -p 53 6033-rw-app.proxysql.service.consul a


 nslookup  proxysql.service.consul
 nslookup  6033-rw-app.proxysql.service.consul

 ping proxysql.service.consul -c 4
 ping 6033-rw-app.proxysql.service.consul -c 4

#示例
#dig 測試
[root@huyidb04 soft]#  dig @10.85.10.57  -p 53 proxysql.service.consul a
; <<>> DiG 9.9.4-RedHat-9.9.4-72.el7 <<>> @10.85.10.57 -p 53 proxysql.service.consul a
; (1 server found)
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 8702
;; flags: qr aa rd ra; QUERY: 1, ANSWER: 3, AUTHORITY: 0, ADDITIONAL: 1

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 4096
;; QUESTION SECTION:
;proxysql.service.consul.       IN      A

;; ANSWER SECTION:
proxysql.service.consul. 0      IN      A       10.85.10.53
proxysql.service.consul. 0      IN      A       10.85.10.52
proxysql.service.consul. 0      IN      A       10.85.10.51

;; Query time: 1 msec
;; SERVER: 10.85.10.56#53(10.85.10.56)
;; WHEN: Tue May 25 23:20:23 CST 2021
;; MSG SIZE  rcvd: 100


#nslookup 測試
[root@huyidb04 soft]# nslookup  proxysql.service.consul
Server:         10.85.10.57
Address:        10.85.10.57#53
Name:   proxysql.service.consul
Address: 10.85.10.51
Name:   proxysql.service.consul
Address: 10.85.10.52
Name:   proxysql.service.consul
Address: 10.85.10.53

#ping 測試
[root@huyidb04 soft]#  ping proxysql.service.consul -c 4
PING proxysql.service.consul (10.85.10.52) 57(84) bytes of data.
64 bytes from huyidb02 (10.85.10.52): icmp_seq=1 ttl=64 time=0.511 ms
64 bytes from huyidb02 (10.85.10.52): icmp_seq=2 ttl=64 time=0.362 ms
64 bytes from huyidb02 (10.85.10.52): icmp_seq=3 ttl=64 time=0.312 ms           

7.連接配接測試

[root@huyidb01 consul.d]#  mysql -uitpux -pitpux123  -hproxysql.service.consul            
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 19704
Server version: 8.0.21 MySQL Community Server - GPL

Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>