HashiCorp
Consul 是由 HashiCorp 公司開發的,它是一家專注於 DevOps 工具鏈的公司,旗下的明星級產品包括 Vagrant、Terraform、Vault、Nomad 以及 Consul,這些工具貫穿了持續交付的整個流程。
下載
在Consul 官網可以知道不同系統對應的下載方式
macOS
$ brew tap hashicorp/tap
$ brew install hashicorp/tap/consu
Windows
提供可二進位制可執行程式的壓縮包,分32位和64位
Linux系
提供相應包管理器的下載方式
- Ubuntu/Debian
$ curl -fsSL https://apt.releases.hashicorp.com/gpg | sudo apt-key add -
$ sudo apt-add-repository "deb [arch=amd64] https://apt.releases.hashicorp.com $(lsb_release -cs) main"
$ sudo apt-get update && sudo apt-get install consul
- CentOS/RHEL
$ sudo yum install -y yum-utils
$ sudo yum-config-manager --add-repo https://rpm.releases.hashicorp.com/RHEL/hashicorp.repo
$ sudo yum -y install consul
- Fedora
$ sudo dnf install -y dnf-plugins-core
$ sudo dnf config-manager --add-repo https://rpm.releases.hashicorp.com/fedora/hashicorp.repo
$ sudo dnf -y install consul
- Amazon Linux
$ sudo yum install -y yum-utils
$ sudo yum-config-manager --add-repo https://rpm.releases.hashicorp.com/AmazonLinux/hashicorp.repo
$ sudo yum -y install consul
- Homebrew
$ brew tap hashicorp/tap
$ brew install hashicorp/tap/consul
也提供對應操作架構的二進位制下載連結
另外,Consul 是通過 Golang 編寫的開源軟體,可以從 Github 上找到原始碼自行編譯。
$ git clone https://github.com/hashicorp/consul.git -depth=1
$ cd consul
$ make tools
$ make dev
執行 Server&Client
回顧上一篇《Consul 入門-初識》,我們知道 Consul 是 C/S 架構以及各端能力
執行 Server
$ consul agent --server=true --ui=true --data-dir=s-data --node=server1 --dev
==> Starting Consul agent...
Version: '1.9.4'
Node ID: '34d5bfeb-3ed4-6aa1-f68d-d30950450fc9'
Node name: 'server1'
Datacenter: 'dc1' (Segment: '<all>')
Server: true (Bootstrap: false)
Client Addr: [127.0.0.1] (HTTP: 8500, HTTPS: -1, gRPC: 8502, DNS: 8600)
Cluster Addr: 127.0.0.1 (LAN: 8301, WAN: 8302)
Encrypt: Gossip: false, TLS-Outgoing: false, TLS-Incoming: false, Auto-Encrypt-TLS: false
==> Log data will now stream in as it occurs:
...
可以從輸出資訊裡面看到 Server 節點的一些關鍵資訊以及競選 Leader 成功(畢竟只有它一個人)
執行 Client
$ consul agent --data-dir=c-data --join=127.0.0.1:8301 --serf-lan-port=8303 --serf-wan-port=8305 --dns-port=8601 --server-port=8304 --http-port=8503 --server=false --enable-script-checks --node=client1 --config-dir=config
==> Starting Consul agent...
Version: '1.9.4'
Node ID: '2b05ee18-1b09-3b0b-7431-ef79783d9758'
Node name: 'client1'
Datacenter: 'dc1' (Segment: '')
Server: false (Bootstrap: false)
Client Addr: [127.0.0.1] (HTTP: 8503, HTTPS: -1, gRPC: -1, DNS: 8601)
Cluster Addr: 10.8.124.122 (LAN: 8303, WAN: 8305)
Encrypt: Gossip: false, TLS-Outgoing: false, TLS-Incoming: false, Auto-Encrypt-TLS: false
...
程式同樣會顯示 Client 的一些相資訊,由於Server 和 Client 是執行在同一臺機器上,預設埠與 Server 的衝突了,所以通過引數--serf-lan-port=8303 --serf-wan-port=8305 --dns-port=8601 --server-port=8304 --http-port=8503
進行了指定。
此時我們觀察 Server 端的程式輸出內容,可以看到 Client1 與 Server 的連線資訊:
2021-08-29T16:47:10.385+0800 [DEBUG] agent.router.manager: Rebalanced servers, new active server: number_of_servers=1 active_server="server1 (Addr: tcp/127.0.0.1:8300) (DC: dc1)"
2021-08-29T16:47:10.385+0800 [DEBUG] agent.router.manager: Rebalanced servers, new active server: number_of_servers=1 active_server="server1.dc1 (Addr: tcp/127.0.0.1:8300) (DC: dc1)"
2021-08-29T16:48:04.265+0800 [DEBUG] agent: Skipping remote check since it is managed automatically: check=serfHealth
2021-08-29T16:48:04.265+0800 [DEBUG] agent: Node info in sync
2021-08-29T16:49:18.849+0800 [DEBUG] agent: Skipping remote check since it is managed automatically: check=serfHealth
2021-08-29T16:49:18.849+0800 [DEBUG] agent: Node info in sync
2021-08-29T16:49:18.993+0800 [DEBUG] agent.server.memberlist.lan: memberlist: Stream connection from=127.0.0.1:54711
2021-08-29T16:49:18.993+0800 [INFO] agent.server.serf.lan: serf: EventMemberJoin: client1 10.8.124.122
2021-08-29T16:49:18.993+0800 [INFO] agent.server: member joined, marking health alive: member=client1
2021-08-29T16:49:19.176+0800 [DEBUG] agent.server.serf.lan: serf: messageJoinType: client1
2021-08-29T16:49:19.192+0800 [DEBUG] agent.server.serf.lan: serf: messageJoinType: client1
2021-08-29T16:49:19.294+0800 [DEBUG] agent.server.serf.lan: serf: messageJoinType: client1
2021-08-29T16:49:19.393+0800 [DEBUG] agent.server.serf.lan: serf: messageJoinType: client1
通過 Consul 的命令可以檢視現在的成員情況:
$ consul members
Node Address Status Type Build Protocol DC Segment
server1 127.0.0.1:8301 alive server 1.9.4 2 dc1 <all>
client1 10.8.124.122:8303 alive client 1.9.4 2 dc1 <default>
註冊一個服務
我們先用Ctrl-C
暫停 Client 端程式碼,在配置資料夾config
中增加檔案redis.json
{
"service": [
{
"name": "redis",
"tags": ["master"],
"address": "127.0.0.1",
"port": 6379,
}
]
}
然後重新執行,可以看到服 redis 服務已經被註冊
2021-08-29T17:03:04.556+0800 [INFO] agent: Synced node info
2021-08-29T17:03:04.556+0800 [INFO] agent: Synced service: service=redis
來驗證一下是否可以通過 Consul 的dns服務找到
$ dig @127.0.0.1 -p 8601 redis.sevice.consul
; <<>> DiG 9.10.6 <<>> @127.0.0.1 -p 8601 redis.sevice.consul
; (1 server found)
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NXDOMAIN, id: 39225
;; flags: qr aa rd; QUERY: 1, ANSWER: 0, AUTHORITY: 1, ADDITIONAL: 1
;; WARNING: recursion requested but not available
;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 4096
;; QUESTION SECTION:
;redis.sevice.consul. IN A
;; AUTHORITY SECTION:
consul. 0 IN SOA ns.consul. hostmaster.consul. 1630227874 3600 600 86400 0
;; Query time: 0 msec
;; SERVER: 127.0.0.1#8601(127.0.0.1)
;; WHEN: Sun Aug 29 17:04:34 CST 2021
;; MSG SIZE rcvd: 98
除了通過 DNS 服務獲取,還可以通過 HTTP 服務訪問http://localhost:8503/v1/health/service/redis
總結
本篇講述了 Consul 背後的公司資訊、Consul 的安裝方式以及 Consul 的執行方式,最後通過註冊 redis 服務的例子,體驗了一把簡單的服務註冊和發現的流程。