一:consul介紹#
consul用於提供服務發現和服務配置的工具。有以下特性:
1. 服務發現
consul的客戶端提供一個服務,比如api或者mysql,另外一個客戶端就可以去發現指定服務的服務提供者。通過DNS或者HTTP應用程序可以容易找到所依賴的服務
2. 健康檢查
consul 可以提供健康檢查服務(比如:webserver是否返回了200 ok狀態碼)或者使用本地節點(比如:內存使用大於90%)。這個信息可以監視集群的健康。可以用來避免將流量發送到不健康的主機
3.key/value 存儲
應用程序可以使用consul的層級的key/value存儲,比如動態配置,協調服務。直接可以用HTTP API來操作
4.多數據中心
consul支持開箱即用的多數據中心。
二:consul安裝配置#
consul的安裝非常容易,直接到 https://www.consul.io/downloads.html 下載你所在平台的安裝包,可以直接下載二進制包, 然后unzip解壓,會得到一個 consul 的文件,然后把它 cp 到/usr/local/bin 目錄下
驗證安裝:在終端下直接輸入 consul ,輸出下面的內容說明安裝成功
Usage: consul [--version] [--help] <command> [<args>]
Available commands are:
agent Runs a Consul agent
catalog Interact with the catalog
connect Interact with Consul Connect
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
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
snapshot Saves, restores and inspects snapshots of Consul server state
validate Validate config files/directories
version Prints the Consul version
watch Watch for changes in Consul
三:consul啟動#
完成consul的安裝后,必須運行agent. agent可以運行為 server 或者 client模式。還有個開發模式dev。 每個數據中心至少必須擁有一台server。建議在一個集群中有3或者5個server。部署單一server,在出現失敗時,會不可避免的出現數據丟失。
以dev模式啟動consul
還有一個特殊的運行模式 -dev的模式,命令如下
./consul agent -dev
說明:
-dev 表示開發模式運行,默認客戶端地址是在127.0.0.1 上
-dev(該節點的啟動不能用於生產環境,因為該模式下不會持久化任何狀態),該啟動模式僅僅是為了快速便捷的啟動單節點consul
以server模式啟動consul
consul agent -server -bind=192.168.0.109 -data-dir=/etc/consul.d
其實沒有 -server,默認就是以client模式啟動的
以client模式啟動consul
consul agent -client=0.0.0.0 -bind=192.168.0.109 -data-dir=/etc/consul.d
四:consul的常用命令#
https://www.consul.io/docs/commands/index.html 官方命令大全地址
consul常用命令+常用選項
agent
作用:運行一個consul agent
join
作用:將agent加入到consul cluster
members
作用:列出consul cluster集群中的members
常用選項option:
-data-dir
作用:指定agent儲存狀態的數據目錄
這是所有agent都必須的
對於server尤其重要,因為他們必須持久化集群的狀態
-config-dir
作用:指定service的配置文件和檢查定義所在的位置
通常會指定為"某一個路徑/consul.d"(通常情況下,.d表示一系列配置文件存放的目錄)
-config-file
作用:指定一個要裝載的配置文件
該選項可以配置多次,進而配置多個配置文件(后邊的會合並前邊的,相同的值覆蓋)
-dev
作用:創建一個開發環境下的server節點
該參數配置下,不會有任何持久化操作,即不會有任何數據寫入到磁盤
這種模式不能用於生產環境(因為第二條)
-bootstrap-expect
作用:該命令通知consul server我們現在准備加入的server節點個數,該參數是為了延遲日志復制的啟動直到我們指定數量的server節點成功的加入后啟動。
-node
作用:指定節點在集群中的名稱
該名稱在集群中必須是唯一的(默認采用機器的host)
推薦:直接采用機器的IP
-bind
作用:指明節點的IP地址
-server
作用:指定節點為server
每個數據中心(DC)的server數推薦為3或5(理想的是,最多不要超過5)
所有的server都采用raft一致性算法來確保事務的一致性和線性化,事務修改了集群的狀態,且集群的狀態保存在每一台server上保證可用性
server也是與其他DC交互的門面(gateway)
-client
作用:指定節點為client
若不指定為-server,其實就是-client
-join
作用:將節點加入到集群
-domain
-dc
作用:指定機器加入到哪一個dc中
查看consul的命令 ./consul
# ./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
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
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
查看某一個子命令的使用方法
比如查看 catalog 的使用方法,使用下面的命令:
./consul catalog help
Usage: consul catalog <subcommand> [options] [args]
This command has subcommands for interacting with Consul's catalog. The
catalog should not be confused with the agent, although the APIs and
responses may be similar.
Here are some simple examples, and more detailed examples are available
in the subcommands or the documentation.
List all datacenters:
$ consul catalog datacenters
List all nodes:
$ consul catalog nodes
List all services:
$ consul catalog services
For more examples, ask for subcommand help or view the documentation.
Subcommands:
datacenters Lists all known datacenters
nodes Lists all nodes in the given datacenter
services Lists all registered services in a datacenter
查看組成consul服務的node各種命令
a: 命令行查詢
[root@localhost]# ./consul catalog nodes Node ID Address DC localhost.localdomain 901b6ebb 192.168.0.109 dc1
b: http接口查詢,用 curl 來查詢
[root@localhost ]# curl localhost:8500/v1/catalog/nodes | python -m json.tool % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 256 100 256 0 0 15280 0 --:--:-- --:--:-- --:--:-- 16000 [ { "Address": "192.168.0.109", "CreateIndex": 5, "Datacenter": "dc1", "ID": "901b6ebb-8a9b-1930-62e2-65a264ff0fd1", "Meta": { "consul-network-segment": "" }, "ModifyIndex": 6, "Node": "localhost.localdomain", "TaggedAddresses": { "lan": "192.168.0.109", "wan": "192.168.0.109" } } ]
c: 通過dns查詢成員node的地址,默認后綴是 node.consul
root@localhost]# dig @127.0.0.1 -p 8600 192.168.0.109.node.cosul; <<>> DiG 9.9.4-RedHat-9.9.4-61.el7 <<>> @127.0.0.1 -p 8600 192.168.0.109.node.cosul
; (1 server found)
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: SERVFAIL, id: 51942
;; flags: qr rd; QUERY: 1, ANSWER: 0, AUTHORITY: 0, ADDITIONAL: 0
;; WARNING: recursion requested but not available;; QUESTION SECTION:
;192.168.0.109.node.cosul. IN A;; Query time: 0 msec
;; SERVER: 127.0.0.1#8600(127.0.0.1)
;; MSG SIZE rcvd: 42
五:添加,查詢服務:(服務注冊,服務發現)#
下面我們以dev的模式啟動consul#
5.1 添加一個服務(也叫 服務注冊)
1. 新建一個文件夾 mkdir /etc/consul.d
2. 寫入一個json格式信息到 /etc/consul.d/web.json 文件中
echo '{"service": {"name": "web", "tags": ["rails"], "port": 80}}' | sudo tee /etc/consul.d/web.json
3. 重新啟動 cosul
consul agent -dev -config-dir=/etc/consul.d
或者通過 api 來注冊服務, api是:agent/service:
1. 先編寫一個json文件,把它命名為web2.json
vi web2.json
{
"Name": "web2",
"Tags": [
"rails"
],
"Address": "",
"Port": 81,
"ServiceEnableTagOverride": false
}
2. 然后用curl命令把這個json文件通過http接口寫入到consul里
curl --request PUT --data @web2.json http://127.0.0.1:8500/v1/agent/service/register
查詢剛才注冊的服務, 看看服務是否注冊成功
[root@localhost]# ./consul catalog services
consul
web2
5.2 查詢定義服務 (也叫 服務發現)
第一:通過DNS來查詢
我們可以用DNS API 來查詢服務,服務名默認為: NAME.service.consul
eg1: 查詢名字為 web 的服務
dig @127.0.0.1 -p 8600 web.service.consul
; OPT PSEUDOSECTION: ; EDNS: version: 0, flags:; udp: 4096 ;; QUESTION SECTION: ;web.service.consul. IN A;; ANSWER SECTION:
web.service.consul. 0 IN A 127.0.0.1
eg2: 查詢名字為 web2 的服務
查詢類型要指定為為srv,才能看到服務端口. 我們來查詢web2服務
[root@localhost]# dig @127.0.0.1 -p 8600 web2.service.consul srv; <<>> DiG 9.9.4-RedHat-9.9.4-61.el7 <<>> @127.0.0.1 -p 8600 web2.service.consul srv
; (1 server found)
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 43040
;; 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:
;web2.service.consul. IN SRV;; ANSWER SECTION:
web2.service.consul. 0 IN SRV 1 1 81 localhost.localdomain.node.dc1.consul.;; ADDITIONAL SECTION:
localhost.localdomain.node.dc1.consul. 0 IN A 192.168.0.109
localhost.localdomain.node.dc1.consul. 0 IN TXT "consul-network-segment=";; Query time: 0 msec
;; SERVER: 127.0.0.1#8600(127.0.0.1)
;; MSG SIZE rcvd: 157
第二:通過http接口查詢
也可以用HTTP API來查詢服務
eg1: 查詢 web 服務
curl http://localhost:8500/v1/catalog/service/web | python -m json.tool
[
{
"ID": "5901c710-c6c4-f8f8-a9eb-77e76e39f034", "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": "" }, "ServiceKind": "", "ServiceID": "web", "ServiceName": "web", "ServiceTags": [ "rails" ], "ServiceAddress": "", "ServiceMeta": { }, "ServicePort": 80, "ServiceEnableTagOverride": false, "ServiceProxyDestination": "", "ServiceConnect": { "Native": false, "Proxy": null }, "CreateIndex": 10, "ModifyIndex": 10 }
]
eg2:查詢web2服務
curl 命令后面的 python -m json.tool 是格式化返回的json,不然返回就是一串字符串,不便於閱讀
[root@localhost]# curl http://127.0.0.1:8500/v1/catalog/service/web2 | python -m json.tool % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 542 100 542 0 0 394k 0 --:--:-- --:--:-- --:--:-- 529k [ { "Address": "192.168.0.109", "CreateIndex": 407, "Datacenter": "dc1", "ID": "901b6ebb-8a9b-1930-62e2-65a264ff0fd1", "ModifyIndex": 407, "Node": "localhost.localdomain", "NodeMeta": { "consul-network-segment": "" }, "ServiceAddress": "", "ServiceConnect": {}, "ServiceEnableTagOverride": false, "ServiceID": "web2", "ServiceKind": "", "ServiceMeta": {}, "ServiceName": "web2", "ServicePort": 81, "ServiceProxy": {}, "ServiceProxyDestination": "", "ServiceTags": [ "rails" ], "ServiceWeights": { "Passing": 1, "Warning": 1 }, "TaggedAddresses": { "lan": "192.168.0.109", "wan": "192.168.0.109" } } ]
5.3 查詢服務健康狀況
curl http://localhost:8500/v1/health/service/web?passing
[
{
"Node": { "ID": "5901c710-c6c4-f8f8-a9eb-77e76e39f034", "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": 9, "ModifyIndex": 10 }, "Service": { "ID": "web", "Service": "web", "Tags": [ "rails" ], "Address": "", "Meta": null, "Port": 80, "EnableTagOverride": false, "ProxyDestination": "", "Connect": { "Native": false, "Proxy": null }, "CreateIndex": 10, "ModifyIndex": 10 }, "Checks": [ { "Node": "localhost.localdomain", "CheckID": "serfHealth", "Name": "Serf Health Status", "Status": "passing", "Notes": "", "Output": "Agent alive and reachable", "ServiceID": "", "ServiceName": "", "ServiceTags": [ ], "Definition": { }, "CreateIndex": 9, "ModifyIndex": 9 } ] }
]
5.4 查詢agent上所有的服務
[root@localhost]# curl http://127.0.0.1:8500/v1/agent/services | python -m json.tool % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 298 100 298 0 0 171k 0 --:--:-- --:--:-- --:--:-- 291k { "web": { "Address": "", "EnableTagOverride": false, "ID": "web", "Meta": {}, "Port": 80, "Service": "web", "Tags": [ "rails" ], "Weights": { "Passing": 1, "Warning": 1 } }, "web2": { "Address": "", "EnableTagOverride": false, "ID": "web2", "Meta": {}, "Port": 81, "Service": "web2", "Tags": [ "rails" ], "Weights": { "Passing": 1, "Warning": 1 } } }
六: 刪除服務#
刪除名字為 web 的服務
curl --request PUT http://127.0.0.1:8500/v1/agent/service/deregister/web
七: kv操作 #
官方地址: https://www.consul.io/api/kv.html
1. 寫入一個名為 “key1” 的key, 值為 hello
命令:
curl -X PUT --data "hello" http://127.0.0.1:8500/v1/kv/key1
2: 查詢 key1:
命令:
# curl http://127.0.0.1:8500/v1/kv/key1 |python -m json.tool % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 95 100 95 0 0 77614 0 --:--:-- --:--:-- --:--:-- 95000 [ { "CreateIndex": 601, "Flags": 0, "Key": "key1", "LockIndex": 0, "ModifyIndex": 601, "Value": "aGVsbG8=" //base64編碼 } ]
3: 查詢指定路徑下的所有key:
命令:
# curl 127.0.0.1:8500/v1/kv/key1?keys ["key1"]
4:刪除key
curl -X DELETE http://127.0.0.1:8500/v1/kv/key1
八:查看consul中的每一個consul的節點信息#
# consul members
Node Address Status Type Build Protocol DC localhost.localdomain 127.0.0.1:8301 alive server 0.7.5 2 dc1
說明:
Address:節點地址
Status:alive表示節點健康
Type:server運行狀態是server狀態
DC:dc1表示該節點屬於DataCenter1
members命令的輸出是基於gossip協議的,並且是最終一致的(也就是說,某一個時刻你去運用該命令查到的consul節點的狀態信息可能是有誤的)
參考:
https://www.consul.io/intro/getting-started/agent.html
https://www.consul.io/docs/index.html
https://book-consul-guide.vnzmi.com/
http://www.liangxiansen.cn/2017/04/06/consul/