现在 consul 版本更新为 1.0.7 ,更新内容比较多,建议使用 1.0.7 版本代替。
部署
基于197、198、199这三台机去部署
第一步:
cd ~
wget https://releases.hashicorp.com/consul/1.0.7/consul_1.0.7_linux_amd64.zip
unzip consul_1.0.7_linux_amd64.zip
rm consul_1.0.7_linux_amd64.zip
sudo mv ./consul /usr/local/bin/consul
cd /usr/local/bin/
sudo chmod +x consul
sudo adduser consul
可以通过consul --version
检查是否安装成功。
第二步:
选择/data
作为数据存储盘,创建数据存储目录并修改权限
sudo mkdir -p /data/production/consul
sudo chown consul:consul /data/production/consul
第三步:
创建配置目录和配置文件
sudo mkdir -p /etc/consul.d/server
cd /etc/consul.d/server
sudo touch config.json
第四步:
创建 /etc/systemd/system/consul.service
文件
sudo vim /etc/systemd/system/consul.service
复制粘贴下面的内容到/etc/systemd/system/consul.service
,然后保存退出
[Unit]
Description=Consul service discovery agent
Requires=network-online.target
After=network.target
[Service]
EnvironmentFile=-/etc/sysconfig/consul
Restart=on-failure
ExecStartPre=/usr/local/bin/consul validate /etc/consul.d/server
ExecStart=/usr/local/bin/consul agent $OPTIONS -config-dir=/etc/consul.d/server
ExecReload=/bin/kill -s HUP $MAINPID
KillSignal=SIGINT
TimeoutStopSec=10
[Install]
WantedBy=multi-user.target
每台服务器重复上面四个步骤
第五步:
先执行 keygen,生成的 key 用于作为节点组成同一个集群的唯一标识,下面的配置文件时需要用到这个key。
consul keygen
AfvvDUiBCYNiz9aPwamu/g==
第六步:
选择 197 作为 bootstrap server
,同时也作为可以打开 ui 控制台
vim /etc/consul.d/server/config.json
配置如下:
{
"domain": "prodenv",
"advertise_addr": "172.25.52.197",
"bind_addr": "172.25.52.197",
"client_addr": "172.25.52.197",
"ports": {
"dns": 53,
"http": 28500,
"serf_lan": 28301,
"serf_wan": 28302,
"server": 28300
},
"dns_config": {
"allow_stale": true,
"max_stale": "15s"
},
"recursors": ["8.8.8.8"],
"ui": true,
"datacenter": "dc-luogang",
"data_dir": "/data/production/consul",
"encrypt": "EXz7LFN8hpQ4id8EDYiFoQ==",
"log_level": "INFO",
"enable_syslog": true,
"enable_debug": true,
"node_name": "luogang-node-01",
"bootstrap": true,
"server": true,
"enable_script_checks": true,
"bootstrap_expect": 1,
"leave_on_terminate": false,
"skip_leave_on_interrupt": true,
"rejoin_after_leave": true,
"retry_join": [
"172.25.52.197:28301",
"172.25.52.198:28301",
"172.25.52.199:28301"
]
}
198和199的配置则为下面的,ip 要修改为对应的 ip ,还有node_name
也要修改
{
"domain": "prodenv",
"advertise_addr": "172.25.52.198",
"bind_addr": "172.25.52.198",
"client_addr": "172.25.52.198",
"ports": {
"dns": 53,
"http": 28500,
"serf_lan": 28301,
"serf_wan": 28302,
"server": 28300
},
"dns_config": {
"allow_stale": true,
"max_stale": "15s"
},
"recursors": ["8.8.8.8"],
"datacenter": "dc-luogang",
"data_dir": "/data/production/consul",
"encrypt": "EXz7LFN8hpQ4id8EDYiFoQ==",
"log_level": "INFO",
"enable_syslog": true,
"enable_debug": true,
"node_name": "luogang-node-02",
"server": true,
"enable_script_checks": true,
"leave_on_terminate": false,
"skip_leave_on_interrupt": true,
"rejoin_after_leave": true,
"retry_join": [
"172.25.52.197:28301",
"172.25.52.198:28301",
"172.25.52.199:28301"
]
}
配置完成后,就到最后一步了
第七步:
启动服务
sudo systemctl daemon-reload
sudo systemctl enable consul
sudo systemctl start consul
sudo systemctl status consul
访问 http://172.25.52.197:28500/ui/ 可看到详细集群节点情况
测试
注册服务
curl http://172.25.52.199:28500/v1/agent/service/register -X PUT -i -H "Content-Type:application/json" -d '{
"ID": "userServiceId",
"Name": "userService",
"Tags": [
"primary",
"v1"
],
"Address": "127.0.0.1",
"Port": 8000,
"EnableTagOverride": false,
"Check": {
"DeregisterCriticalServiceAfter": "90m",
"HTTP": "http://www.baidu.com",
"Interval": "20s"
}
}'
响应返回
HTTP/1.1 200 OK
Date: Mon, 09 Apr 2018 09:45:42 GMT
Content-Length: 0
Content-Type: text/plain; charset=utf-8
服务发现
curl http://172.25.52.197:28500/v1/catalog/service/userService
响应返回
[{"ID":"8e7854a7-cdb2-073e-7faa-d5217c2a35d8","Node":"luogang-node-03","Address":"172.25.52.199","Datacenter":"dc-luogang","TaggedAddresses":{"lan":"172.25.52.199","wan":"172.25.52.199"},"NodeMeta":{"consul-network-segment":""},"ServiceID":"userServiceId","ServiceName":"userService","ServiceTags":["primary","v1"],"ServiceAddress":"127.0.0.1","ServicePort":8000,"ServiceEnableTagOverride":false,"CreateIndex":1496,"ModifyIndex":1496}]
DNS 测试
dig @172.25.52.198 userService.service.prodenv
响应返回
; <<>> DiG 9.9.4-RedHat-9.9.4-51.el7_4.2 <<>> @172.25.52.198 userService.service.prodenv
; (1 server found)
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 261
;; flags: qr aa rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1
;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 4096
;; QUESTION SECTION:
;userService.service.prodenv. IN A
;; ANSWER SECTION:
userService.service.prodenv. 0 IN A 127.0.0.1
;; Query time: 0 msec
;; SERVER: 172.25.52.198#53(172.25.52.198)
;; WHEN: 一 4月 09 18:21:14 CST 2018
;; MSG SIZE rcvd: 69
拿到 userService.service.prodenv
所对应的 ip 地址127.0.0.1
注销服务
执行下面的命令即可注销服务
curl -X PUT http://172.25.52.199:28500/v1/agent/service/deregister/userServiceId
测试完毕~
本文由 Chakhsu Lau 创作,采用 知识共享署名4.0 国际许可协议进行许可。
本站文章除注明转载/出处外,均为本站原创或翻译,转载前请务必署名。