Consul 實戰
上兩小節我們介紹了 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:單主模式、故障轉移、強一致性。