一、背景故事:上周四聽了美的MySQL數據庫利用consul實現高可用,還有redis 集群模式,讓我對consul產生了濃厚的興趣,特花了三四天的時間研究consul集群,consul的特點是什么呢?
1、 使用 Raft 算法來保證一致性, 比復雜的 Paxos 算法更直接. 相比較而言, zookeeper 采用的是Paxos, 而 etcd 使用的則是 Raft.
2、支持多數據中心,內外網的服務采用不同的端口進行監聽。 多數據中心集群可以避免單數據中心的單點故障,而其部署則需要考慮網絡延遲, 分片等情況等. zookeeper和 etcd 均不提供多數據中心功能的支持. 滿足多數據中心架構
3、支持健康檢查. etcd 不提供此功能.
4、支持 http 和 dns 協議接口. zookeeper 的集成較為復雜,etcd 只支持 http 協議. 有DNS功能,支持REST API
5、官方提供web管理界面, etcd 無此功能.
6、部署簡單,運維友好,無依賴,go的二進制程序copy過來就能用了,一個程序搞定,可以結合ansible來推送。
廢話不多說直接上部署教程
二、consul cluster go環境設置
官網地址:https://www.consul.io/
1.0版本的consul 需go1.9版本及以上
三、consul cluster 集群部署
假設我們要創建3個節點的集群,IP為:
創建consul配置文件consul節點默認的名字就是本機的hostname,所以盡量起一個含義清晰的名字。
當然也可以在配置文件中使用node指定
mha120: 192.168.56.120
tomcat102: 192.168.56.121
tomcat103: 192.168.56.122
unzip consul_1.0.0_linux_amd64.zip
cp consul /usr/local/bin
which consul
/usr/local/bin/consul
mkdir -p /etc/consul
mkdir /data/consul/data -p
三個服務器都做以上操作
在120上創建配置文件
cat >/etc/consul/consul_config.json <<EOF
{
"advertise_addr": "192.168.56.120",
"bind_addr": "192.168.56.120",
"domain": "consul",
"bootstrap_expect": 3,
"server": true,
"datacenter": "consul-cluster",
"data_dir": "/data/consul/data",
"enable_syslog": true,
"performance": {
"raft_multiplier": 1
},
"dns_config": {
"allow_stale": true,
"max_stale": "15s"
},
"retry_join": [
"192.168.56.121",
"192.168.56.122"
],
"retry_interval": "10s",
"skip_leave_on_interrupt": true,
"leave_on_terminate": false,
"ports": {
"dns": 53,
"http": 80
},
"recursors": [
"114.114.114.114"
],
"rejoin_after_leave": true,
"addresses": {
"http": "0.0.0.0",
"dns": "0.0.0.0"
}
}
EOF
chown -R consul:consul /data/consul/data
=========================================================================================
192.168.56.121 配置文件
=========================================================================================
cat >/etc/consul/consul_config.json <<EOF
{
"advertise_addr": "192.168.56.121",
"bind_addr": "192.168.56.121",
"domain": "consul",
"bootstrap_expect": 3,
"server": true,
"datacenter": "consul-cluster",
"data_dir": "/data/consul/data",
"enable_syslog": true,
"performance": {
"raft_multiplier": 1
},
"dns_config": {
"allow_stale": true,
"max_stale": "15s"
},
"retry_join": [
"192.168.56.120",
"192.168.56.122"
],
"retry_interval": "10s",
"skip_leave_on_interrupt": true,
"leave_on_terminate": false,
"ports": {
"dns": 53,
"http": 80
},
"recursors": [
"114.114.114.114"
],
"rejoin_after_leave": true,
"addresses": {
"http": "0.0.0.0",
"dns": "0.0.0.0"
}
}
EOF
==============================================================================
192.168.56.122 配置文件
===============================================================================
cat >/etc/consul/consul_config.json <<EOF
{
"advertise_addr": "192.168.56.122",
"bind_addr": "192.168.56.122",
"domain": "consul",
"bootstrap_expect": 3,
"server": true,
"datacenter": "consul-cluster",
"data_dir": "/data/consul/data",
"enable_syslog": true,
"performance": {
"raft_multiplier": 1
},
"dns_config": {
"allow_stale": true,
"max_stale": "15s"
},
"retry_join": [
"192.168.56.120",
"192.168.56.121"
],
"retry_interval": "10s",
"skip_leave_on_interrupt": true,
"leave_on_terminate": false,
"ports": {
"dns": 53,
"http": 80
},
"recursors": [
"114.114.114.114"
],
"rejoin_after_leave": true,
"addresses": {
"http": "0.0.0.0",
"dns": "0.0.0.0"
}
}
EOF
分別在120-122服務器執行
nohup /usr/local/bin/consul agent -config-dir=/etc/consul/consul_config.json -rejoin &
查看節點成員
root@mha120:~$ consul members -http-addr=127.0.0.1:80
Node Address Status Type Build Protocol DC Segment
mha120 192.168.56.120:8301 alive server 1.0.0 2 consul-cluster <all>
tomcat102 192.168.56.121:8301 alive server 1.0.0 2 consul-cluster <all>
tomcat103 192.168.56.122:8301 alive server 1.0.0 2 consul-cluster <all>
1、解析域名
root@mha120:~$dig @192.168.56.120 -p 53 tomcat103.node.consul





至此consul cluster三節點集群搭建完成。