Run the Consul Agent
安装 Consul 很简单,其实际就是一个可执行的二进制包,官网参考文档。
#consul
aptitude show consul
#consul template管理工具,需要在本地进行动态配置文件管理则安装,否者可以不用安装。
aptitude show consul-template
开发模式
为了简单起见,我们现在将以开发模式启动 Consul agent 。 此模式对于快速轻松地启动单节点 Consul 环境非常有用。 它不打算在生产中使用,因为它不持续任何状态。
Starting the Agent
# consul agent -dev
==> Starting Consul agent...
==> Starting Consul agent RPC...
==> Consul agent running!
Version: 'v0.7.2'
Node name: 'i4-jrocket2-104-02-1'
Datacenter: 'dc1'
Server: true (bootstrap: false)
Client Addr: 127.0.0.1 (HTTP: 8500, HTTPS: -1, DNS: 8600, RPC: 8400)
Cluster Addr: 127.0.0.1 (LAN: 8301, WAN: 8302)
Gossip encrypt: false, RPC-TLS: false, TLS-Incoming: false Atlas:
==> Log data will now stream in as it occurs:
......
列出集群成员
# consul members
root@localhost:~$ consul members
member 命令的输出基于 gossip 协议,并且最终是一致的。 也就是说,在任何时间点,您的本地 agent 看到的 world view 可能与服务器上的状态不完全匹配。 对于一 个强一致性 world view,请使用 HTTP API 做为转发请求转到到 Consul 服务器.
HTTP API 接口查询
root@localhost:~$ curl -s localhost:8500/v1/catalog/nodes|python -m json.tool
[{
"Address": "10.120.184.251",
"CreateIndex": 1648,
"Datacenter": "dc1",
"ID": "e047bfa3-906f-2f85-485f-721ba8ff7473",
"Meta": {
"consul-network-segment": ""
},
"ModifyIndex": 1649,
"Node": "node01",
"TaggedAddresses": {
"lan": "10.120.184.251",
"wan": "10.120.184.251"
}
}, {
"Address": "10.120.169.72",
"CreateIndex": 1434,
"Datacenter": "dc1",
"ID": "39bad123-0f54-8b1f-6222-57115084aff7",
"Meta": {
"consul-network-segment": ""
},
"ModifyIndex": 1436,
"Node": "node02",
"TaggedAddresses": {
"lan": "10.120.169.72",
"wan": "10.120.169.72"
}
}, .....
]
DNS 接口查询
root@localhost:~$ dig @127.0.0.1 -p 8600 test.service.consul SRV
; <<>> DiG 9.9.5-9+deb8u14-Debian <<>> @127.0.0.1 -p 8600 test.service.consul SRV
; (1 server found)
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 59706
;; flags: qr aa rd; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 3 ;; WARNING: recursion requested but not available
;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 4096
;; QUESTION SECTION:
;test.service.consul. IN SRV
;; ANSWER SECTION:
test.service.consul. 0 IN SRV 1 1 31030 node02.node.dc1.consul.
;; ADDITIONAL SECTION:
node02.node.dc1.consul. 0 IN CNAME 10.120.169.723\.
node02.node.dc1.consul. 0 IN TXT "consul-network-segment="
;; Query time: 0 msec
;; SERVER: 127.0.0.1#8600(127.0.0.1) ;; WHEN: Mon Aug 13 16:23:23 CST 2018 ;; MSG SIZE rcvd: 156
Stopping the Agent
您可以使用 Ctrl-C(中断信号)正常停止 agnet。 中断 agent 后,您应该看到它离开集群并关闭。
启动端口修改
"ports": {
"http": 8501,
"dns": 8601,
"serf_lan": 8311,
"serf_wan": 8312,
"server": 8310
}