一: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>]
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
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 43040
;; flags: qr aa rd; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 3
;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 4096
;web2.service.consul. IN SRV
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="
;; 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節點的狀态資訊可能是有誤的)