<p>Consul 實戰</p>
阿新 • • 發佈:2021-07-01
上兩小節我們介紹了 MGR 和 ProxySQL 的部署,接下來我們繼續從實戰角度,學習這套高可用架構的最後部分:Consul 實戰。
1. Consul 介紹
Consul 是 HashiCorp 公司推出的一個用於實現分散式系統的服務發現與配置工具。Consul 使用 Go 語言編寫,具有天然可移植性,支援多平臺部署,安裝包僅僅是一個可執行檔案,部署非常簡單。
Consul 內建了服務註冊與發現、分佈一致性協議實現、dns 解析、健康檢查、Key/Value 儲存、多資料中心方案。
Consul 官方網址:https://www.consul.io;
2. Consul 部署
下面從實戰的角度一步步搭建 Consul 環境。
2.1 基本環境
Consul-1 | Consul-2 | Consul-3 | |
---|---|---|---|
MySQL版本 | Consul_1.8.4 | Consul_1.8.4 | Consul_1.8.4 |
作業系統 | CentOS 7.8 | CentOS 7.8 | CentOS 7.8 |
伺服器IP | 192.168.0.1 | 192.168.0.2 | 192.168.0.3 |
埠 | 8600 | 8600 | 8600 |
伺服器配置 | 2c4g | 2c4g | 2c4g |
2.2 安裝配置
建立日誌檔案和配置檔案:
--consul日誌
sudo touch /consul/log/consul.log
--Consul配置
sudo touch /consul/consul.d/consul_config.json
--服務註冊
sudo touch /consul/consul.d/proxysql.json
安裝 Consul:
--解壓縮即可
cd /consul
unzip consul_1.8.4_linux_amd64.zip
--建立軟連結
ln -s /consul/consul /usr/bin/consul
--檢視版本
consul --version
Consul v1.8.4
2.3 Consul配置-Server端
全域性配置-consul_config.json:
vi /consul/consul.d/consul_config.json
{
"datacenter":"datacenter-1",
"data_dir" :"/consul/data",
"log_level": "INFO",
"node_name": "consul-server-01",
"bootstrap_expect": 2,
"server": true,
// ui介面在一臺server設定為true,其它設定為false
"ui":true,
// 如果繫結具體IP,會導致consul叢集之間tcp8301埠失敗
"bind_addr":"0.0.0.0",
// 客戶端允許訪問ip
"client_addr":"0.0.0.0",
"enable_script_checks":true,
// 加入叢集
"start_join":
["192.168.0.1", "192.168.0.2", "192.168.0.3"],
"retry_join":
["192.168.0.1", "192.168.0.2", "192.168.0.3"],
"ports":
{"dns": 53}
}
2.4 Consul 配置-Client 端
全域性配置-consul_config.json:
/consul/consul.d/consul_config.json
{
"datacenter":"datacenter-1",
"data_dir":"/consul/data",
"log_level": "INFO",
"node_name": "consul-app-proxysql-01",
"server":false,
//ui介面在一臺server設定為true,其它設定為false
"ui":false,
// 如果繫結具體IP,會導致consul叢集之間tcp8301埠失敗
"bind_addr":"0.0.0.0",
// 客戶端允許訪問ip
"client_addr":"0.0.0.0",
"enable_script_checks":true,
// 加入叢集
"start_join":
["192.168.0.1", "192.168.0.2", "192.168.0.3"]],
"retry_join":
["192.168.0.1", "192.168.0.2", "192.168.0.3"]],
"ports":
{"dns": 53}
}
服務註冊-proxysql.json:
--採用mysqladmin檢查
vi /consul/consul.d/proxysql.json
{
"service": {
"id": "proxysql-01",
"name": "proxysql",
"tags": ["6033-rw-app"],
"address": "192.168.0.1",
"port": 6033,
"check": {
"script": "mysqladmin ping --host=localhost --port=6033 --user=root --password=123456",
"interval": "3s"
}
}
}
--採用telnet檢查
vi /consul/consul.d/proxysql.json
{
"service": {
"id": "proxysql1",
"name": "proxysql",
"tags": ["6033-rw-app"],
"address": "192.168.0.1",
"port": 6033,
"check": {
"interval": "3s",
"tcp": "127.0.0.1:6033",
"timeout": "1s"
}
}
}
2.5 DNS 解析配置
--在應用端配置nameserver,指向consul叢集
vi /etc/resolv.conf
#指向本地consul 53埠,正常由本地做dns解析
nameserver 127.0.0.1
#指向consul叢集 53埠,備用
nameserver 192.168.0.1
nameserver 192.168.0.2
nameserver 192.168.0.3
3. 基礎維護
Server 端啟動:
consul agent -config-dir=/consul/consul.d/ >> /software/consul/log/consul.log &
Client 端啟動:
consul agent -config-dir=/consul/consul.d/ >> /software/consul/log/consul.log &
域名測試:
dig @127.0.0.1 -p 53 proxysql.service.consul
dig 6033-rw-app.proxysql.service.consul
退出 Consul:
--consul命令
consul leave
檢視 Consul 叢集資訊:
--檢視consul叢集資訊
consul members
4. 小結
本小節主要從實戰角度介紹如何搭建 Consul 環境。
對一個企業級的核心繫統來說,資料庫架構在設計時必須考慮足夠的高可用,這樣才能最大程度的確保業務的連續性。MGR+ProxySQL+Consul 的架構三個層面各司其職,確保 MySQL 的高可用:
- Consul:dns 解析、服務發現、健康檢查;
- ProxySQL:負載均衡、讀寫分離、故障發現;
- MGR:單主模式、故障轉移、強一致性。