Consul簡介
Consul有多個元件,但總體而言,它是基礎架構中的一款服務發現和配置的工具。 它提供了幾個關鍵功能:
- 服務發現:Consul client 可以提供服務,例如api或mysql,也可以使用Consul client來發現指定服務的提供者。 使用DNS或HTTP,應用程式可以輕松找到他們所依賴的服務。
- 健康檢查:Consul client 可以提供任何數量的健康檢查,或者與給定的服務(“Web伺服器是否傳回200 OK”),或與本地節點(“記憶體使用率是否低于90%”)相關聯。 可以使用此資訊來監控叢集運作狀況,服務發現元件使用此資訊将流量從有問題的主機中移除出去。
- KV Store:應用程式可以使用Consul的分層鍵/值存儲,包括動态配置,功能标記,協調,leader選舉等等。 簡單的HTTP API使其易于使用。
-
多資料中心:Consul支援多個資料中心。 這意味着Consul的使用者不必擔心建構額外的抽象層以擴充到多個區域。
Consul旨在對DevOps社群和應用程式開發人員友好,使其成為現代化,彈性基礎架構的完美選擇。
Consul的安裝
打開官網的下載下傳位址

選擇對應的版本進行下載下傳。本文是再Windows環境下的安裝與配置。
下載下傳完成後解壓會得到一個exe檔案。 将這個exe檔案放在
C:\Windows\system32
這個目錄下面然後打開cmd。
輸入consul檢擦是否安裝完成。
PS C:\Windows\system32> consul
Usage: consul [--version] [--help] <command> [<args>]
Available commands are:
acl Interact with Consul's ACLs
agent Runs a Consul agent
catalog Interact with the catalog
config Interact with Consul's Centralized Configurations
connect Interact with Consul Connect
debug Records a debugging archive for operators
event Fire a new event
exec Executes a command on Consul nodes
force-leave Forces a member of the cluster to enter the "left" state
info Provides debugging information for operators.
intention Interact with Connect service intentions
join Tell Consul agent to join cluster
keygen Generates a new encryption key
keyring Manages gossip layer encryption keys
kv Interact with the key-value store
leave Gracefully leaves the Consul cluster and shuts down
lock Execute a command holding a lock
login Login to Consul using an auth method
logout Destroy a Consul token created with login
maint Controls node or service maintenance mode
members Lists the members of a Consul cluster
monitor Stream logs from a Consul agent
operator Provides cluster-level tools for Consul operators
reload Triggers the agent to reload configuration files
rtt Estimates network round trip time between nodes
services Interact with services
snapshot Saves, restores and inspects snapshots of Consul server state
tls Builtin helpers for creating CAs and certificates
validate Validate config files/directories
version Prints the Consul version
watch Watch for changes in Consul
這樣就表示安裝成功了。
Consul運作與配置
輸入consul agent -dev
PS C:\Windows\system32> consul agent -dev
==> Starting Consul agent...
==> Consul agent running!
Version: 'v1.5.1'
Node ID: '262ed46b-3d26-8907-3ce9-b094d1eeca7e'
Node name: 'DESKTOP-FERRARI'
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
==> Log data will now stream in as it occurs:
2019/07/03 11:44:04 [DEBUG] agent: Using random ID "262ed46b-3d26-8907-3ce9-b094d1eeca7e" as node ID
2019/07/03 11:44:04 [DEBUG] tlsutil: Update with version 1
2019/07/03 11:44:04 [DEBUG] tlsutil: OutgoingRPCWrapper with version 1
2019/07/03 11:44:04 [DEBUG] tlsutil: IncomingRPCConfig with version 1
2019/07/03 11:44:04 [DEBUG] tlsutil: OutgoingRPCWrapper with version 1
2019/07/03 11:44:04 [INFO] raft: Initial configuration (index=1): [{Suffrage:Voter ID:262ed46b-3d26-8907-3ce9-b094d1eeca7e Address:127.0.0.1:8300}]
2019/07/03 11:44:04 [INFO] raft: Node at 127.0.0.1:8300 [Follower] entering Follower state (Leader: "")
2019/07/03 11:44:04 [INFO] serf: EventMemberJoin: DESKTOP-FERRARI.dc1 127.0.0.1
2019/07/03 11:44:04 [INFO] serf: EventMemberJoin: DESKTOP-FERRARI 127.0.0.1
2019/07/03 11:44:04 [INFO] consul: Adding LAN server DESKTOP-FERRARI (Addr: tcp/127.0.0.1:8300) (DC: dc1)
2019/07/03 11:44:04 [INFO] consul: Handled member-join event for server "DESKTOP-FERRARI.dc1" in area "wan"
2019/07/03 11:44:04 [DEBUG] agent/proxy: managed Connect proxy manager started
2019/07/03 11:44:04 [INFO] agent: Started DNS server 127.0.0.1:8600 (tcp)
2019/07/03 11:44:04 [INFO] agent: Started DNS server 127.0.0.1:8600 (udp)
2019/07/03 11:44:04 [INFO] agent: Started HTTP server on 127.0.0.1:8500 (tcp)
2019/07/03 11:44:04 [INFO] agent: started state syncer
2019/07/03 11:44:04 [INFO] agent: Started gRPC server on 127.0.0.1:8502 (tcp)
2019/07/03 11:44:04 [WARN] raft: Heartbeat timeout from "" reached, starting election
2019/07/03 11:44:04 [INFO] raft: Node at 127.0.0.1:8300 [Candidate] entering Candidate state in term 2
2019/07/03 11:44:04 [DEBUG] raft: Votes needed: 1
2019/07/03 11:44:04 [DEBUG] raft: Vote granted from 262ed46b-3d26-8907-3ce9-b094d1eeca7e in term 2. Tally: 1
2019/07/03 11:44:04 [INFO] raft: Election won. Tally: 1
2019/07/03 11:44:04 [INFO] raft: Node at 127.0.0.1:8300 [Leader] entering Leader state
2019/07/03 11:44:04 [INFO] consul: cluster leadership acquired
2019/07/03 11:44:04 [INFO] consul: New leader elected: DESKTOP-FERRARI
2019/07/03 11:44:04 [INFO] connect: initialized primary datacenter CA with provider "consul"
2019/07/03 11:44:04 [DEBUG] consul: Skipping self join check for "DESKTOP-FERRARI" since the cluster is too small
2019/07/03 11:44:04 [INFO] consul: member 'DESKTOP-FERRARI' joined, marking health alive
2019/07/03 11:44:04 [DEBUG] agent: Skipping remote check "serfHealth" since it is managed automatically
2019/07/03 11:44:04 [INFO] agent: Synced node info
2019/07/03 11:44:04 [DEBUG] agent: Node info in sync
2019/07/03 11:44:05 [DEBUG] agent: Skipping remote check "serfHealth" since it is managed automatically
2019/07/03 11:44:05 [DEBUG] agent: Node info in sync
出現這樣表示運作成功了。
打開
http://127.0.0.1:8500/ui/dc1/services
檢視consul的運作狀态。
服務注冊
建立consul.d的配置檔案夾
mkdir ./consul.d
建立一個服務的.json的配置檔案。
echo '{"service": {"name": "web", "tags": ["rails"], "port": 80}}' > ./consul.d/web.json
重新開機agent
consul agent -dev -config-dir=./consul.d
==> Starting Consul agent...
...
[INFO] agent: Synced service 'web'
...
DNS API
dig @127.0.0.1 -p 8600 web.service.consul
;; QUESTION SECTION:
;web.service.consul. IN A
;; ANSWER SECTION:
web.service.consul. 0 IN A 127.0.0.1
再用SRV的方式檢查一下
C:\Windows\system32>dig @127.0.0.1 -p 8600 web.service.consul SRV
; <<>> DiG 9.9.7 <<>> @127.0.0.1 -p 8600 web.service.consul SRV
; (1 server found)
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 63393
;; 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:
;web.service.consul. IN SRV
;; ANSWER SECTION:
web.service.consul. 0 IN SRV 1 1 80 DESKTOP-FERRARI.node.dc1.consul.
;; ADDITIONAL SECTION:
DESKTOP-FERRARI.node.dc1.consul. 0 IN A 127.0.0.1
DESKTOP-FERRARI.node.dc1.consul. 0 IN TXT "consul-network-segment="
;; Query time: 0 msec
;; SERVER: 127.0.0.1#8600(127.0.0.1)
;; WHEN: Wed Jul 03 14:14:49 ?D1ú±ê×?ê±?? 2019
;; MSG SIZE rcvd: 150
可以看到他說已經在一台電腦上注冊了這個DNS.
我們還可以使用域名過濾的方式在注冊一個
dig @127.0.0.1 -p 8600 rails.web.service.consul
; <<>> DiG 9.9.7 <<>> @127.0.0.1 -p 8600 rails.web.service.consul
; (1 server found)
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 22904
;; flags: qr aa rd; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 2
;; WARNING: recursion requested but not available
;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 4096
;; QUESTION SECTION:
;rails.web.service.consul. IN A
;; ANSWER SECTION:
rails.web.service.consul. 0 IN A 127.0.0.1
;; ADDITIONAL SECTION:
rails.web.service.consul. 0 IN TXT "consul-network-segment="
;; Query time: 0 msec
;; SERVER: 127.0.0.1#8600(127.0.0.1)
;; WHEN: Wed Jul 03 14:16:52 ?D1ú±ê×?ê±?? 2019
;; MSG SIZE rcvd: 105
再用 HTTP API來檢視服務
curl http://localhost:8500/v1/catalog/service/web
[
{
"ID": "b9896ed0-77f7-6f47-05d0-a76c32bd592d",
"Node": "DESKTOP-FERRARI",
"Address": "127.0.0.1",
"Datacenter": "dc1",
"TaggedAddresses": {
"lan": "127.0.0.1",
"wan": "127.0.0.1"
},
"NodeMeta": {
"consul-network-segment": ""
},
"ServiceKind": "",
"ServiceID": "web",
"ServiceName": "web",
"ServiceTags": [
"rails"
],
"ServiceAddress": "",
"ServiceWeights": {
"Passing": 1,
"Warning": 1
},
"ServiceMeta": {},
"ServicePort": 80,
"ServiceEnableTagOverride": false,
"ServiceProxyDestination": "",
"ServiceProxy": {},
"ServiceConnect": {},
"CreateIndex": 10,
"ModifyIndex": 10
}
]
Consul 健康監測
建立兩個檢測的檔案
echo '{"check": {"name": "ping", "args": ["ping", "-c1", "google.com"], "interval": "30s"}}' >./consul.d/ping.json
echo '{"service": {"name": "web", "tags": ["rails"], "port": 80, "check": {"args": ["curl", "localhost"], "interval": "10s"}}}' >./consul.d/web.json
重新開機一下consul agent
consul agent -dev -config-dir ./consul.d -enable-script-checks=true
現在來檢查一下
curl http://localhost:8500/v1/health/state/critical
StatusCode : 200
StatusDescription : OK
Content : [
{
"Node": "DESKTOP-FERRARI",
"CheckID": "service:web",
"Name": "Service 'web' check",
"Status": "critical",
"Notes": "",
"Output": " % Total %...
RawContent : HTTP/1.1 200 OK
Vary: Accept-Encoding
X-Consul-Effective-Consistency: leader
X-Consul-Index: 28
X-Consul-Knownleader: true
X-Consul-Lastcontact: 0
Content-Length: 786
Content-Type: application/...
Forms : {}
Headers : {[Vary, Accept-Encoding], [X-Consul-Effective-Consistency, leader], [X-Consul-Index, 28], [X-Consul
-Knownleader, true]...}
Images : {}
InputFields : {}
Links : {}
ParsedHtml : mshtml.HTMLDocumentClass
RawContentLength : 786