Consul 入门指南
一、安裝 Consul
Consul 下載地址:https://www.consul.io/downloads.html,下載后解壓就是一個(gè)可執(zhí)行的二進(jìn)制文件consul,配置好環(huán)境變量,檢查 consul 是否可用:
[root@localhost ~]# consul Usage: consul [--version] [--help] <command> [<args>]Available commands are:agent Runs a Consul agentcatalog Interact with the catalogevent Fire a new eventexec Executes a command on Consul nodesforce-leave Forces a member of the cluster to enter the "left" stateinfo Provides debugging information for operators.join Tell Consul agent to join clusterkeygen Generates a new encryption keykeyring Manages gossip layer encryption keyskv Interact with the key-value storeleave Gracefully leaves the Consul cluster and shuts downlock Execute a command holding a lockmaint Controls node or service maintenance modemembers Lists the members of a Consul clustermonitor Stream logs from a Consul agentoperator Provides cluster-level tools for Consul operatorsreload Triggers the agent to reload configuration filesrtt Estimates network round trip time between nodessnapshot Saves, restores and inspects snapshots of Consul server statevalidate Validate config files/directoriesversion Prints the Consul versionwatch Watch for changes in Consul[root@localhost ~]#- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
如果出現(xiàn)上面這樣代表consul是沒(méi)問(wèn)題的。
二、運(yùn)行 Consul Agent
Consul安裝之后,代理必須運(yùn)行。 代理可以在服務(wù)器或客戶(hù)端模式下運(yùn)行。 每個(gè)數(shù)據(jù)中心都必須至少有一臺(tái)服務(wù)器,但推薦使用3臺(tái)或5臺(tái)服務(wù)器。 一個(gè)單一的服務(wù)器部署是非常不推薦的,因?yàn)樵诠收锨闆r下數(shù)據(jù)丟失是不可避免的。?
所有其他代理以客戶(hù)端模式運(yùn)行。 客戶(hù)端是一個(gè)非常輕量級(jí)的進(jìn)程,它注冊(cè)服務(wù),運(yùn)行健康檢查,并將查詢(xún)轉(zhuǎn)發(fā)給服務(wù)器。 代理程序必須在集群中的每個(gè)節(jié)點(diǎn)上運(yùn)行。
三、啟動(dòng) Agent
為了簡(jiǎn)單起見(jiàn),我們現(xiàn)在將以開(kāi)發(fā)模式啟動(dòng)Consul代理。 這種模式對(duì)于快速簡(jiǎn)單地啟動(dòng)單節(jié)點(diǎn)Consul環(huán)境非常有用。 它并不打算在生產(chǎn)中使用,因?yàn)樗粫?huì)持續(xù)任何狀態(tài)。
[root@localhost consul]# consul agent -dev ==> Starting Consul agent... ==> Consul agent running!Version: 'v1.0.1'Node ID: '590309a6-71f6-6145-fe40-d2c5e203687f'Node name: 'localhost.localdomain'Datacenter: 'dc1' (Segment: '<all>')Server: true (Bootstrap: false)Client Addr: [127.0.0.1] (HTTP: 8500, HTTPS: -1, 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:2017/11/25 15:15:54 [DEBUG] Using random ID "590309a6-71f6-6145-fe40-d2c5e203687f" as node ID2017/11/25 15:15:54 [INFO] raft: Initial configuration (index=1): [{Suffrage:Voter ID:590309a6-71f6-6145-fe40-d2c5e203687f Address:127.0.0.1:8300}]2017/11/25 15:15:54 [INFO] serf: EventMemberJoin: localhost.localdomain.dc1 127.0.0.12017/11/25 15:15:54 [INFO] serf: EventMemberJoin: localhost.localdomain 127.0.0.12017/11/25 15:15:54 [INFO] agent: Started DNS server 127.0.0.1:8600 (udp)2017/11/25 15:15:54 [INFO] raft: Node at 127.0.0.1:8300 [Follower] entering Follower state (Leader: "")2017/11/25 15:15:54 [INFO] consul: Adding LAN server localhost.localdomain (Addr: tcp/127.0.0.1:8300) (DC: dc1)2017/11/25 15:15:54 [INFO] consul: Handled member-join event for server "localhost.localdomain.dc1" in area "wan"2017/11/25 15:15:54 [INFO] agent: Started DNS server 127.0.0.1:8600 (tcp)2017/11/25 15:15:54 [INFO] agent: Started HTTP server on 127.0.0.1:8500 (tcp)2017/11/25 15:15:54 [INFO] agent: started state syncer2017/11/25 15:15:54 [WARN] raft: Heartbeat timeout from "" reached, starting election2017/11/25 15:15:54 [INFO] raft: Node at 127.0.0.1:8300 [Candidate] entering Candidate state in term 22017/11/25 15:15:54 [DEBUG] raft: Votes needed: 12017/11/25 15:15:54 [DEBUG] raft: Vote granted from 590309a6-71f6-6145-fe40-d2c5e203687f in term 2. Tally: 12017/11/25 15:15:54 [INFO] raft: Election won. Tally: 12017/11/25 15:15:54 [INFO] raft: Node at 127.0.0.1:8300 [Leader] entering Leader state2017/11/25 15:15:54 [INFO] consul: cluster leadership acquired2017/11/25 15:15:54 [DEBUG] consul: Skipping self join check for "localhost.localdomain" since the cluster is too small2017/11/25 15:15:54 [INFO] consul: member 'localhost.localdomain' joined, marking health alive2017/11/25 15:15:54 [INFO] consul: New leader elected: localhost.localdomain2017/11/25 15:15:54 [DEBUG] Skipping remote check "serfHealth" since it is managed automatically2017/11/25 15:15:54 [INFO] agent: Synced node info2017/11/25 15:15:54 [DEBUG] agent: Node info in sync2017/11/25 15:15:57 [DEBUG] Skipping remote check "serfHealth" since it is managed automatically2017/11/25 15:15:57 [DEBUG] agent: Node info in sync2017/11/25 15:16:54 [DEBUG] consul: Skipping self join check for "localhost.localdomain" since the cluster is too small2017/11/25 15:17:51 [DEBUG] Skipping remote check "serfHealth" since it is managed automatically2017/11/25 15:17:51 [DEBUG] agent: Node info in sync2017/11/25 15:17:54 [DEBUG] manager: Rebalanced 1 servers, next active server is localhost.localdomain.dc1 (Addr: tcp/127.0.0.1:8300) (DC: dc1)2017/11/25 15:17:54 [DEBUG] consul: Skipping self join check for "localhost.localdomain" since the cluster is too small- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
如您所見(jiàn),Consul代理已經(jīng)啟動(dòng)并輸出了一些日志數(shù)據(jù)。 從日志數(shù)據(jù)中,您可以看到我們的代理正在服務(wù)器模式下運(yùn)行,并聲稱(chēng)擁有集群領(lǐng)導(dǎo)權(quán)。 此外,當(dāng)?shù)爻蓡T已被標(biāo)記為該集群的健康成員。
四、集群成員
在另一個(gè)終端運(yùn)行consul members,可以看到Consul集群的成員。 應(yīng)該只看到一個(gè)成員(你自己):
[root@localhost ~]# consul members Node Address Status Type Build Protocol DC Segment localhost.localdomain 127.0.0.1:8301 alive server 1.0.1 2 dc1 <all>- 1
- 2
- 3
輸出顯示了我們自己的節(jié)點(diǎn),它正在運(yùn)行的地址,運(yùn)行狀況,在集群中的角色以及一些版本信息。 額外的元數(shù)據(jù)可以通過(guò)提供-detailed標(biāo)志來(lái)查看。
[root@localhost ~]# consul members -detailed Node Address Status Tags localhost.localdomain 127.0.0.1:8301 alive build=1.0.1:9564c29,dc=dc1,id=590309a6-71f6-6145-fe40-d2c5e203687f,port=8300,raft_vsn=3,role=consul,segment=<all>,vsn=2,vsn_max=3,vsn_min=2,wan_join_port=8302- 1
- 2
- 3
?members命令的輸出基于gossip協(xié)議,并最終一致。 也就是說(shuō),在任何時(shí)候,當(dāng)?shù)卮硭吹降目赡芘c服務(wù)器上的狀態(tài)不完全一致。 要獲得完全一致,請(qǐng)使用HTTP API再將HTTP請(qǐng)求轉(zhuǎn)發(fā)給Consul服務(wù)器:
[root@localhost ~]# curl localhost:8500/v1/catalog/nodes [{"ID": "590309a6-71f6-6145-fe40-d2c5e203687f","Node": "localhost.localdomain","Address": "127.0.0.1","Datacenter": "dc1","TaggedAddresses": {"lan": "127.0.0.1","wan": "127.0.0.1"},"Meta": {"consul-network-segment": ""},"CreateIndex": 5,"ModifyIndex": 6} ]- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
除了HTTP API之外,還可以使用DNS接口查詢(xún)節(jié)點(diǎn)。 請(qǐng)注意,必須確保將DNS查找默認(rèn)情況下指向在端口8600上運(yùn)行的Consul代理的DNS服務(wù)器。 DNS條目的格式(如“Armons-MacBook-Air.node.consul”)將在后面詳細(xì)介紹。
[root@localhost ~]# dig @127.0.0.1 -p 8600 localhost.localdomain.node.consul ' ; <<>> DiG 9.9.4-RedHat-9.9.4-51.el7 <<>> @127.0.0.1 -p 8600 localhost.localdomain.node.consul ; (1 server found) ;; global options: +cmd ;; Got answer: ;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 43915 ;; flags: qr aa rd; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1 ;; WARNING: recursion requested but not available;; OPT PSEUDOSECTION: ; EDNS: version: 0, flags:; udp: 4096 ;; QUESTION SECTION: ;localhost.localdomain.node.consul. IN A;; ANSWER SECTION: localhost.localdomain.node.consul. 0 IN A 127.0.0.1;; Query time: 0 msec ;; SERVER: 127.0.0.1#8600(127.0.0.1) ;; WHEN: 六 11月 25 15:39:49 CST 2017 ;; MSG SIZE rcvd: 78- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
五、停止 Agent
可以使用Ctrl-C(中斷信號(hào))正常停止代理。 中斷代理之后,您應(yīng)該看到它離開(kāi)集群并關(guān)閉。?
通過(guò)優(yōu)雅地離開(kāi),Consul通知其他集群成員該節(jié)點(diǎn)離開(kāi)。 如果強(qiáng)行殺死了代理進(jìn)程,則集群的其他成員將檢測(cè)到該節(jié)點(diǎn)失敗。 成員離開(kāi)時(shí),其服務(wù)和檢查將從目錄中刪除。 當(dāng)一個(gè)成員失敗時(shí),其健康被簡(jiǎn)單地標(biāo)記為關(guān)鍵,但不會(huì)從目錄中刪除。 Consul將自動(dòng)嘗試重新連接到失敗的節(jié)點(diǎn),使其能夠從特定的網(wǎng)絡(luò)條件恢復(fù),而不再聯(lián)系離開(kāi)的節(jié)點(diǎn)。?
此外,如果代理正在作為服務(wù)器運(yùn)行,那么優(yōu)雅的離開(kāi)對(duì)于避免造成影響一致協(xié)議的潛在可用性中斷很重要。 有關(guān)如何安全地添加和刪除服務(wù)器的詳細(xì)信息,請(qǐng)參閱指南部分。
六、注冊(cè)服務(wù)
1、服務(wù)定義
服務(wù)可以通過(guò)提供服務(wù)定義或通過(guò)對(duì)HTTP API進(jìn)行適當(dāng)?shù)恼{(diào)用來(lái)注冊(cè)。?
服務(wù)定義是注冊(cè)服務(wù)最常用的方式,所以我們將在這一步中使用這種方法。 我們將建立在上一步中介紹的代理配置。?
首先,為Consul配置創(chuàng)建一個(gè)目錄。 Consul將所有配置文件加載到配置目錄中,因此Unix系統(tǒng)上的一個(gè)通用約定是將目錄命名為/etc/consul.d(.d后綴意味著“該目錄包含一組配置文件”)。
- 1
接下來(lái),我們將編寫(xiě)一個(gè)服務(wù)定義配置文件。 假設(shè)我們有一個(gè)名為“web”的服務(wù)在端口80上運(yùn)行。另外,我們給它一個(gè)標(biāo)簽,我們可以使用它作為查詢(xún)服務(wù)的附加方式:
[root@localhost ~]# echo '{"service": {"name": "web", "tags": ["rails"], "port": 80}}' | sudo tee /etc/consul.d/web.json- 1
現(xiàn)在,重新啟動(dòng)代理程序,提供配置目錄:
[root@localhost ~]# consul agent -dev -config-dir=/etc/consul.d ==> Starting Consul agent... ==> Consul agent running!Version: 'v1.0.1'Node ID: '94236f1c-2a29-85c5-b235-dd916485be5b'Node name: 'localhost.localdomain'Datacenter: 'dc1' (Segment: '<all>')Server: true (Bootstrap: false)Client Addr: [127.0.0.1] (HTTP: 8500, HTTPS: -1, 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:2017/11/25 16:16:51 [DEBUG] Using random ID "94236f1c-2a29-85c5-b235-dd916485be5b" as node ID2017/11/25 16:16:51 [INFO] raft: Initial configuration (index=1): [{Suffrage:Voter ID:94236f1c-2a29-85c5-b235-dd916485be5b Address:127.0.0.1:8300}]2017/11/25 16:16:51 [INFO] serf: EventMemberJoin: localhost.localdomain.dc1 127.0.0.12017/11/25 16:16:51 [INFO] serf: EventMemberJoin: localhost.localdomain 127.0.0.12017/11/25 16:16:51 [INFO] agent: Started DNS server 127.0.0.1:8600 (udp)2017/11/25 16:16:51 [INFO] raft: Node at 127.0.0.1:8300 [Follower] entering Follower state (Leader: "")2017/11/25 16:16:51 [INFO] consul: Adding LAN server localhost.localdomain (Addr: tcp/127.0.0.1:8300) (DC: dc1)2017/11/25 16:16:51 [INFO] consul: Handled member-join event for server "localhost.localdomain.dc1" in area "wan"2017/11/25 16:16:51 [INFO] agent: Started DNS server 127.0.0.1:8600 (tcp)2017/11/25 16:16:51 [INFO] agent: Started HTTP server on 127.0.0.1:8500 (tcp)2017/11/25 16:16:51 [INFO] agent: started state syncer2017/11/25 16:16:52 [WARN] raft: Heartbeat timeout from "" reached, starting election2017/11/25 16:16:52 [INFO] raft: Node at 127.0.0.1:8300 [Candidate] entering Candidate state in term 22017/11/25 16:16:52 [DEBUG] raft: Votes needed: 12017/11/25 16:16:52 [DEBUG] raft: Vote granted from 94236f1c-2a29-85c5-b235-dd916485be5b in term 2. Tally: 12017/11/25 16:16:52 [INFO] raft: Election won. Tally: 12017/11/25 16:16:52 [INFO] raft: Node at 127.0.0.1:8300 [Leader] entering Leader state2017/11/25 16:16:52 [INFO] consul: cluster leadership acquired2017/11/25 16:16:52 [DEBUG] consul: Skipping self join check for "localhost.localdomain" since the cluster is too small2017/11/25 16:16:52 [INFO] consul: member 'localhost.localdomain' joined, marking health alive2017/11/25 16:16:52 [INFO] consul: New leader elected: localhost.localdomain2017/11/25 16:16:52 [DEBUG] Skipping remote check "serfHealth" since it is managed automatically2017/11/25 16:16:52 [INFO] agent: Synced service "web"2017/11/25 16:16:52 [DEBUG] agent: Node info in sync2017/11/25 16:16:52 [DEBUG] agent: Service "web" in sync2017/11/25 16:16:52 [DEBUG] agent: Node info in sync2017/11/25 16:16:52 [DEBUG] Skipping remote check "serfHealth" since it is managed automatically2017/11/25 16:16:52 [DEBUG] agent: Service "web" in sync2017/11/25 16:16:52 [DEBUG] agent: Node info in sync- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
您會(huì)注意到它在輸出中“同步”了Web服務(wù)。 這意味著代理程序從配置文件加載了服務(wù)定義,并已成功將其注冊(cè)到服務(wù)目錄中。?
如果您想注冊(cè)多個(gè)服務(wù),您可以在Consul配置目錄中創(chuàng)建多個(gè)服務(wù)定義文件。
2、查詢(xún)服務(wù)
一旦代理啟動(dòng)并且服務(wù)同步,我們可以使用DNS或HTTP API來(lái)查詢(xún)服務(wù)。
DNS API
我們首先使用DNS API來(lái)查詢(xún)我們的服務(wù)。 對(duì)于DNS API,服務(wù)的DNS名稱(chēng)是NAME.service.consul。 默認(rèn)情況下,所有DNS名稱(chēng)始終在consul命名空間中,盡管這是可配置的。 服務(wù)子域告訴Consul我們正在查詢(xún)服務(wù),NAME是服務(wù)的名稱(chēng)。?
對(duì)于我們注冊(cè)的Web服務(wù),這些約定和設(shè)置會(huì)生成web.service.consul的完全限定的域名:
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
正如你所看到的,一個(gè)A記錄返回了服務(wù)可用的節(jié)點(diǎn)的IP地址。 A記錄只能保存IP地址。?
您也可以使用DNS API來(lái)檢索整個(gè)地址/端口對(duì)作為SRV記錄:
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
SRV記錄表示W(wǎng)eb服務(wù)正在端口80上運(yùn)行,并且存在于節(jié)點(diǎn)localhost.localdomain.node.dc1.consul.上。DNS使用該記錄的A記錄返回附加部分。?
最后,我們也可以使用DNS API來(lái)按標(biāo)簽過(guò)濾服務(wù)。 基于標(biāo)記的服務(wù)查詢(xún)的格式是TAG.NAME.service.consul。 在下面的例子中,我們向Consul詢(xún)問(wèn)所有帶有“rails”標(biāo)簽的web服務(wù)。 自從我們使用該標(biāo)簽注冊(cè)我們的服務(wù)后,我們得到了成功的回應(yīng):
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
HTTP API
除了DNS API之外,HTTP API還可以用來(lái)查詢(xún)服務(wù):
[root@localhost ~]# curl http://localhost:8500/v1/catalog/service/web [{"ID": "94236f1c-2a29-85c5-b235-dd916485be5b","Node": "localhost.localdomain","Address": "127.0.0.1","Datacenter": "dc1","TaggedAddresses": {"lan": "127.0.0.1","wan": "127.0.0.1"},"NodeMeta": {"consul-network-segment": ""},"ServiceID": "web","ServiceName": "web","ServiceTags": ["rails"],"ServiceAddress": "","ServicePort": 80,"ServiceEnableTagOverride": false,"CreateIndex": 6,"ModifyIndex": 6} ]- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
目錄API提供了托管給定服務(wù)的所有節(jié)點(diǎn)。 正如我們稍后將看到的健康檢查一樣,您通常只需要查詢(xún)檢查通過(guò)的健康實(shí)例。 這是DNS正在做的事情。 這是一個(gè)查詢(xún)只查找健康的實(shí)例:
[root@localhost ~]# curl 'http://localhost:8500/v1/health/service/web?passing' [{"Node": {"ID": "94236f1c-2a29-85c5-b235-dd916485be5b","Node": "localhost.localdomain","Address": "127.0.0.1","Datacenter": "dc1","TaggedAddresses": {"lan": "127.0.0.1","wan": "127.0.0.1"},"Meta": {"consul-network-segment": ""},"CreateIndex": 5,"ModifyIndex": 6},"Service": {"ID": "web","Service": "web","Tags": ["rails"],"Address": "","Port": 80,"EnableTagOverride": false,"CreateIndex": 6,"ModifyIndex": 6},"Checks": [{"Node": "localhost.localdomain","CheckID": "serfHealth","Name": "Serf Health Status","Status": "passing","Notes": "","Output": "Agent alive and reachable","ServiceID": "","ServiceName": "","ServiceTags": [],"Definition": {},"CreateIndex": 5,"ModifyIndex": 5}]} ]- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
3、更新服務(wù)
服務(wù)定義可以通過(guò)更改配置文件并向代理發(fā)送SIGHUP來(lái)更新。 這使您可以更新服務(wù),而不會(huì)出現(xiàn)任何停機(jī)或無(wú)法提供服務(wù)查詢(xún)的情況。?
或者,可以使用HTTP API動(dòng)態(tài)地添加,刪除和修改服務(wù)。
七、Consul 集群
我們已經(jīng)開(kāi)始了我們的第一個(gè)代理,并注冊(cè)和查詢(xún)?cè)摯淼姆?wù)。 這顯示了使用Consul是多么的容易,但并沒(méi)有表明如何將其擴(kuò)展到可擴(kuò)展的生產(chǎn)級(jí)服務(wù)發(fā)現(xiàn)基礎(chǔ)設(shè)施。 在這一步中,我們將創(chuàng)建我們的第一個(gè)真正的集群與多個(gè)成員。?
當(dāng)一個(gè)Consul代理啟動(dòng)時(shí),它不知道任何其他節(jié)點(diǎn):它是一個(gè)孤立的集群。 要了解其他集群成員,代理必須加入現(xiàn)有集群。 要加入現(xiàn)有的集群,只需要知道一個(gè)現(xiàn)有的成員。 代理加入后,會(huì)與該成員通訊,并迅速發(fā)現(xiàn)集群中的其他成員。 Consul代理可以加入任何其他代理,而不僅僅是服務(wù)器模式下的代理。
1、啟動(dòng)代理
在我們之前的例子中,我們使用了-dev標(biāo)志來(lái)快速設(shè)置一個(gè)開(kāi)發(fā)服務(wù)器。 但是,這不足以在集群環(huán)境中使用。 我們將從這里省略-dev標(biāo)志,而是指定我們的集群標(biāo)志。?
集群中的每個(gè)節(jié)點(diǎn)都必須具有唯一的名稱(chēng)。 默認(rèn)情況下,Consul使用機(jī)器的主機(jī)名,但我們將使用-node命令行選項(xiàng)手動(dòng)覆蓋它。?
我們還將指定一個(gè)-bind:這是Consul偵聽(tīng)的地址,它必須可以被集群中的所有其他節(jié)點(diǎn)訪(fǎng)問(wèn)。 雖然綁定地址不是絕對(duì)必要的,但最好提供一個(gè)。 Consul將默認(rèn)嘗試偵聽(tīng)系統(tǒng)上的所有IPv4接口,但如果找到多個(gè)私有IP,將無(wú)法啟動(dòng)錯(cuò)誤。 由于生產(chǎn)服務(wù)器通常具有多個(gè)接口,因此指定一個(gè)綁定地址可確保您永遠(yuǎn)不會(huì)將Consul綁定到錯(cuò)誤的接口。?
第一個(gè)節(jié)點(diǎn)將作為我們?cè)谶@個(gè)集群中唯一的服務(wù)器,我們用-server來(lái)指明這一點(diǎn)。?
-bootstrap-expect選項(xiàng)向Consul服務(wù)器提示我們期望加入的其他服務(wù)器節(jié)點(diǎn)的數(shù)量。 此選項(xiàng)的用途是延遲復(fù)制日志的引導(dǎo),直到預(yù)期數(shù)量的服務(wù)器成功加入。?
我們已經(jīng)將-enable_script_checks選項(xiàng)設(shè)置為true,以啟用可以執(zhí)行外部腳本的運(yùn)行狀況檢查。 這將在后面的例子中使用。 對(duì)于生產(chǎn)用途,您希望將ACL配置為與此配合使用,以控制注冊(cè)任意腳本的能力。?
最后,我們添加-config-dir選項(xiàng),標(biāo)記可以找到服務(wù)和檢查定義的位置。?
總而言之,這些設(shè)置產(chǎn)生一個(gè)這樣的consul代理命令:
- 1
現(xiàn)在,在另一個(gè)終端中,我們將連接到第二個(gè)節(jié)點(diǎn)。?
這次,我們將-bind設(shè)置為第二個(gè)節(jié)點(diǎn)的IP,并將-node設(shè)置為agent-two。 由于這個(gè)節(jié)點(diǎn)不會(huì)是Consul服務(wù)器,所以我們不提供-server。?
總而言之,這些設(shè)置產(chǎn)生一個(gè)這樣的consul代理命令:
- 1
此時(shí),您有兩個(gè)Consul代理正在運(yùn)行:一個(gè)服務(wù)器和一個(gè)客戶(hù)端。 兩個(gè)Consul代理人對(duì)彼此還是一無(wú)所知,都是他們自己的單節(jié)點(diǎn)集群的一部分。 您可以通過(guò)對(duì)每個(gè)代理運(yùn)行consul members來(lái)驗(yàn)證這一點(diǎn),并注意到每個(gè)代理只能看到一個(gè)成員。
2、加入集群
現(xiàn)在,我們將通過(guò)在新終端中運(yùn)行以下命令來(lái)告訴第一個(gè)代理加入第二個(gè)代理:
[root@localhost ~]# consul join 192.168.100.101 Successfully joined cluster by contacting 1 nodes.- 1
- 2
如果在虛擬機(jī)里面運(yùn)行上面的命令提示下面的失敗的話(huà),在每臺(tái)虛擬機(jī)上執(zhí)行下這個(gè)命令:
[root@localhost ~]# consul join 192.168.100.101 Error joining address '192.168.100.101': Unexpected response code: 500 (1 error(s) occurred:* Failed to join 192.168.100.101: dial tcp 192.168.100.101:8301: getsockopt: no route to host) Failed to join any nodes.[root@localhost ~]# sudo iptables -F- 1
- 2
- 3
- 4
- 5
- 6
- 7
您應(yīng)該在每個(gè)代理日志中看到一些日志輸出。 如果仔細(xì)閱讀,您會(huì)看到他們收到了加入信息。 如果你對(duì)每個(gè)代理執(zhí)行consul members,你會(huì)看到兩個(gè)代理人現(xiàn)在彼此了解:
[root@localhost ~]# consul members Node Address Status Type Build Protocol DC Segment agent-one 192.168.100.101:8301 alive server 1.0.1 2 dc1 <all> agent-two 192.168.100.102:8301 alive client 1.0.1 2 dc1 <default>- 1
- 2
- 3
- 4
記住:要加入集群,Consul代理只需要了解一個(gè)現(xiàn)有的成員。 加入集群后,代理人互相傳播完整的會(huì)員信息。
3、在啟動(dòng)時(shí)自動(dòng)加入集群
理想情況下,每當(dāng)新節(jié)點(diǎn)出現(xiàn)在您的數(shù)據(jù)中心時(shí),它就會(huì)自動(dòng)加入Consul集群,無(wú)需人工干預(yù)。 Consul通過(guò)啟用AWS,Google Cloud或Azure中的實(shí)例的自動(dòng)發(fā)現(xiàn)功能,使用給定的標(biāo)簽 key/value來(lái)促進(jìn)自動(dòng)加入。 要使用集成,請(qǐng)將retry_join_ec2,retry_join_gce或retry_join_azure嵌套對(duì)象添加到您的Consul配置文件。 這將允許新的節(jié)點(diǎn)加入集群,而不需要任何硬編碼的配置。 或者,您可以在啟動(dòng)時(shí)使用-join選項(xiàng)或start_join設(shè)置以及其他已知Consul代理的硬編碼地址加入集群。
4、查詢(xún)節(jié)點(diǎn)
就像查詢(xún)服務(wù)一樣,Consul也有查詢(xún)節(jié)點(diǎn)的API。 您可以通過(guò)DNS或HTTP API執(zhí)行此操作。?
對(duì)于DNS API,名稱(chēng)的結(jié)構(gòu)是NAME.node.consul或NAME.node.DATACENTER.consul。 如果數(shù)據(jù)中心被省略,Consul將僅搜索本地?cái)?shù)據(jù)中心。?
例如,從“agent-one”中,我們可以查詢(xún)節(jié)點(diǎn)“agent-two”的地址:
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
除了服務(wù)之外,查找節(jié)點(diǎn)的能力對(duì)于系統(tǒng)管理任務(wù)來(lái)說(shuō)是非常有用的。 例如,知道要通過(guò)SSH連接的節(jié)點(diǎn)的地址與將節(jié)點(diǎn)作為Consul集群的一部分并查詢(xún)它一樣簡(jiǎn)單。
5、離開(kāi)集群
要離開(kāi)集群,可以正常退出代理(使用Ctrl-C)或強(qiáng)制終止其中一個(gè)代理。 優(yōu)雅地離開(kāi)允許節(jié)點(diǎn)轉(zhuǎn)換到離開(kāi)狀態(tài); 否則,其他節(jié)點(diǎn)將檢測(cè)到它失敗。
八、健康檢查
現(xiàn)在我們已經(jīng)看到了運(yùn)行Consul,添加節(jié)點(diǎn)和服務(wù)以及查詢(xún)這些節(jié)點(diǎn)和服務(wù)的簡(jiǎn)單性。 在本節(jié)中,我們將介紹為節(jié)點(diǎn)和服務(wù)添加健康檢查。 健康檢查是服務(wù)發(fā)現(xiàn)的關(guān)鍵組件,可以防止使用不健康的服務(wù)。?
此步驟建立在之前創(chuàng)建的Consul集群上。 此時(shí),您應(yīng)該運(yùn)行一個(gè)雙節(jié)點(diǎn)集群。
1、檢查定義
與服務(wù)類(lèi)似,可以通過(guò)提供檢查定義或通過(guò)對(duì)HTTP API進(jìn)行適當(dāng)?shù)恼{(diào)用來(lái)注冊(cè)檢查。?
我們將使用檢查定義方法,因?yàn)榫拖穹?wù)一樣,定義是設(shè)置檢查最常用的方法。?
在第二個(gè)節(jié)點(diǎn)的Consul配置目錄中創(chuàng)建兩個(gè)定義文件:
- 1
- 2
第一個(gè)定義添加了一個(gè)名為“ping”的主機(jī)級(jí)別的檢查。 此檢查運(yùn)行間隔30秒,調(diào)用ping -c1 google.com。 在基于腳本的運(yùn)行狀況檢查上,檢查以與啟動(dòng)Consul進(jìn)程相同的用戶(hù)身份運(yùn)行。 如果該命令以非零退出碼退出,則該節(jié)點(diǎn)將被標(biāo)記為不健康。 這是任何基于腳本的健康檢查的約定。?
第二個(gè)命令修改名為web的服務(wù),添加一個(gè)檢查,每隔10秒通過(guò)curl發(fā)送一個(gè)請(qǐng)求,以驗(yàn)證Web服務(wù)器是否可訪(fǎng)問(wèn)。 與主機(jī)級(jí)運(yùn)行狀況檢查一樣,如果腳本以非零退出代碼退出,服務(wù)將被標(biāo)記為不健康。?
現(xiàn)在,重新啟動(dòng)第二個(gè)代理,用consul reload加載它,或者發(fā)送一個(gè)SIGHUP信號(hào)。 您應(yīng)該看到以下日志行:
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
前幾行表示代理已經(jīng)同步了新的定義。 最后一行表明我們?yōu)閃eb服務(wù)添加的檢查是至關(guān)重要的。 這是因?yàn)槲覀儗?shí)際上沒(méi)有運(yùn)行Web服務(wù)器,所以curl測(cè)試失敗了!
2、檢查健康狀態(tài)
現(xiàn)在我們已經(jīng)添加了一些簡(jiǎn)單的檢查,我們可以使用HTTP API來(lái)檢查它們。 首先,我們可以使用這個(gè)命令查找任何失敗的檢查(注意,這可以在任一節(jié)點(diǎn)上運(yùn)行):
[root@localhost etc]# curl http://localhost:8500/v1/health/state/critical [{"Node":"agent-two","CheckID":"service:web","Name":"Service 'web' check","Status":"critical","Notes":"","Output":"","ServiceID":"web","ServiceName":"web","ServiceTags":["rails"],"Definition":{},"CreateIndex":230,"ModifyIndex":262}]- 1
- 2
我們可以看到,只有一個(gè)檢查,我們的Web服務(wù)檢查,在危險(xiǎn)(critical)狀態(tài)。?
另外,我們可以嘗試使用DNS查詢(xún)Web服務(wù)。 由于服務(wù)不健康,Consul不會(huì)返回任何結(jié)果:
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
九、KV Data
Consul除了提供服務(wù)發(fā)現(xiàn)和綜合健康檢查之外,還提供一個(gè)易于使用的KV存儲(chǔ)。 這可以用來(lái)保存動(dòng)態(tài)配置,協(xié)助服務(wù)協(xié)調(diào),建立leader選舉,并使開(kāi)發(fā)人員可以考慮構(gòu)建的任何東西。
1、簡(jiǎn)單使用
為了演示有多簡(jiǎn)單,我們將操作K / V存儲(chǔ)中的幾個(gè)鍵。 有兩種方式與Consul KV存儲(chǔ)進(jìn)行交互:通過(guò)HTTP API和Consul KV CLI。 以下示例顯示使用Consul KV CLI,因?yàn)樗亲钊菀兹腴T(mén)的。 對(duì)于更高級(jí)的集成,您可能需要使用Consul KV HTTP API。?
首先讓我們探索KV存儲(chǔ)。 我們可以向Consul詢(xún)問(wèn)名為redis / config / minconns的路徑上的key的值:
- 1
- 2
正如你所看到的,我們沒(méi)有得到任何結(jié)果,這是合理的,因?yàn)镵V存儲(chǔ)沒(méi)有數(shù)據(jù)。 接下來(lái),我們可以insert或put 值到 KV 存儲(chǔ)中。
[root@localhost ~]# consul kv put redis/config/minconns 1 Success! Data written to: redis/config/minconns[root@localhost ~]# consul kv put redis/config/maxconns 25 Success! Data written to: redis/config/maxconns[root@localhost ~]# consul kv put -flags=42 redis/config/users/admin abcd1234 Success! Data written to: redis/config/users/admin- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
現(xiàn)在我們?cè)诖鎯?chǔ)中有key,我們可以查詢(xún)單個(gè) key 的 value:
[root@localhost ~]# consul kv get redis/config/minconns 1- 1
- 2
Consul保留有關(guān)使用-detailed標(biāo)志檢索的字段的其他元數(shù)據(jù):
[root@localhost ~]# consul kv get -detailed redis/config/minconns CreateIndex 517 Flags 0 Key redis/config/minconns LockIndex 0 ModifyIndex 517 Session - Value 1- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
對(duì)于“redis / config / users / admin”這個(gè)鍵,我們?cè)O(shè)置了一個(gè)標(biāo)志值42。所有的鍵都支持設(shè)置一個(gè)64位的整數(shù)標(biāo)志值。 這不是Consul在內(nèi)部使用的,但客戶(hù)可以使用它為任何KV添加有意義的元數(shù)據(jù)。?
可以使用遞歸選項(xiàng)列出存儲(chǔ)的所有 key和 value。 結(jié)果將以字典順序返回:
- 1
- 2
- 3
- 4
要從Consul KV中刪除一個(gè) key,發(fā)出“刪除” 命令:
[root@localhost ~]# consul kv delete redis/config/minconns Success! Deleted key: redis/config/minconns- 1
- 2
也可以使用遞歸選項(xiàng)刪除整個(gè)前綴:
[root@localhost ~]# consul kv delete -recurse redis Success! Deleted keys with prefix: redis- 1
- 2
要更新現(xiàn)有key的值,請(qǐng)?jiān)谙嗤窂缴蟨ut一個(gè)值:
[root@localhost ~]# consul kv put foo bar Success! Data written to: foo[root@localhost ~]# consul kv get foo bar[root@localhost ~]# consul kv put foo zip Success! Data written to: foo[root@localhost ~]# consul kv get foo zip- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
Consul可以使用Check-And-Set操作提供原子鍵更新。 要執(zhí)行CAS操作,請(qǐng)指定-cas選項(xiàng):
[root@localhost ~]# consul kv get -detailed foo CreateIndex 710 Flags 0 Key foo LockIndex 0 ModifyIndex 716 Session - Value bar[root@localhost ~]# consul kv put -cas -modify-index=716 foo bar Success! Data written to: foo[root@localhost ~]# consul kv put -cas -modify-index=716 foo bar Error! Did not write to foo: CAS failed- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
在這種情況下,第一個(gè)CAS更新成功,因?yàn)樗饕?16。第二個(gè)操作失敗,因?yàn)樗饕辉偈?16。
十、Consul Web UI
Consul支持web ui界面。UI可用于查看所有服務(wù)和節(jié)點(diǎn),查看所有運(yùn)行狀況檢查及其當(dāng)前狀態(tài),以及讀取和設(shè)置鍵/值數(shù)據(jù)。 用戶(hù)界面自動(dòng)支持多數(shù)據(jù)中心。?
要設(shè)置自帶的UI,請(qǐng)使用-ui參數(shù)啟動(dòng)Consul代理:
- 1
UI可以在與HTTP API相同的端口上的/ui路徑中使用。 默認(rèn)情況下,這是http://localhost:8500/ui。?
您可以在這里查看Consul Web UI的現(xiàn)場(chǎng)演示。
十一、參考文獻(xiàn)
Consul 官方入門(mén)指南
總結(jié)
以上是生活随笔為你收集整理的Consul 入门指南的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: consul 命令行参数
- 下一篇: Etcd 架构与实现解析