Consul包含多個組件,可是做爲一個總體,爲你的基礎設施提供服務發現和服務配置的工具.他提供如下關鍵特性:css
服務發現 Consul的客戶端可用提供一個服務,好比 api 或者mysql ,另一些客戶端可用使用Consul去發現一個指定服務的提供者.經過DNS或者HTTP應用程序可用很容易的找到他所依賴的服務.
健康檢查 Consul客戶端可用提供任意數量的健康檢查,指定一個服務(好比:webserver是否返回了200 OK 狀態碼)或者使用本地節點(好比:內存使用是否大於90%). 這個信息可由operator用來監視集羣的健康.被服務發現組件用來避免將流量發送到不健康的主機.
Key/Value存儲 應用程序可用根據本身的須要使用Consul的層級的Key/Value存儲.好比動態配置,功能標記,協調,領袖選舉等等,簡單的HTTP API讓他更易於使用.
多數據中心 Consul支持開箱即用的多數據中心.這意味着用戶不須要擔憂須要創建額外的抽象層讓業務擴展到多個區域.
Consul面向DevOps和應用開發者友好.是他適合現代的彈性的基礎設施.
html
Consul是一個分佈式高可用的系統. 這節將包含一些基礎,咱們忽略掉一些細節這樣你能夠快速瞭解Consul是如何工做的.若是要了解更多細節,請參考深刻的架構描述.node
每一個提供服務給Consul的階段都運行了一個Consul agent . 發現服務或者設置和獲取 key/value存儲的數據不是必須運行agent.這個agent是負責對節點自身和節點上的服務進行健康檢查的.python
Agent與一個和多個Consul Server 進行交互.Consul Server 用於存放和複製數據.server自行選舉一個領袖.雖然Consul能夠運行在一臺server , 可是建議使用3到5臺來避免失敗狀況下數據的丟失.每一個數據中心建議配置一個server集羣.mysql
你基礎設施中須要發現其餘服務的組件能夠查詢任何一個Consul 的server或者 agent.Agent會自動轉發請求到server .linux
每一個數據中運行了一個Consul server集羣.當一個跨數據中心的服務發現和配置請求建立時.本地Consul Server轉發請求到遠程的數據中心並返回結果.nginx
更多介紹查看官網點擊前往git
安裝Consul,找到適合你係統的包下載他.Consul打包爲一個’Zip’文件.前往下載github
下載後解開壓縮包.拷貝Consul到你的PATH路徑中,在Unix系統中~/bin
和/usr/local/bin
是一般的安裝目錄.根據你是想爲單個用戶安裝仍是給整個系統安裝來選擇.在Windows系統中有能夠安裝到%PATH%
的路徑中.web
完成安裝後,經過打開一個新終端窗口檢查consul
安裝是否成功.經過執行 consul
你應該看到相似下面的輸出
[root@dhcp-10-201-102-248 ~]# consul usage: consul [--version] [--help] <command> [<args>] Available commands are: agent Runs a Consul agent configtest Validate config file 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 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 version Prints the Consul version watch Watch for changes in Consul
若是你獲得一個consul not be found
的錯誤,你的PATH
可能沒有正確設置.請返回檢查你的consul的安裝路徑是否包含在PATH
中.
完成Consul的安裝後,必須運行agent. agent能夠運行爲server
或client
模式.每一個數據中心至少必須擁有一臺server . 建議在一個集羣中有3或者5個server.部署單一的server,在出現失敗時會不可避免的形成數據丟失.
其餘的agent運行爲client模式.一個client是一個很是輕量級的進程.用於註冊服務,運行健康檢查和轉發對server的查詢.agent必須在集羣中的每一個主機上運行.
查看啓動數據中心的細節請查看這裏.
consul agent -server -bootstrap-expect 3 -data-dir /tmp/consul -node=s1 -bind=10.201.102.198 -ui-dir ./consul_ui/ -rejoin -config-dir=/etc/consul.d/ -client 0.0.0.0
運行cosnul agent以server
模式,
-server
: 定義agent運行在server模式-bootstrap-expect
:在一個datacenter中指望提供的server節點數目,當該值提供的時候,consul一直等到達到指定sever數目的時候纔會引導整個集羣,該標記不能和bootstrap共用-bind
:該地址用來在集羣內部的通信,集羣內的全部節點到地址都必須是可達的,默認是0.0.0.0-node
:節點在集羣中的名稱,在一個集羣中必須是惟一的,默認是該節點的主機名-ui-dir
: 提供存放web ui資源的路徑,該目錄必須是可讀的-rejoin
:使consul忽略先前的離開,在再次啓動後仍舊嘗試加入集羣中。-config-dir
:配置文件目錄,裏面全部以.json結尾的文件都會被加載-client
:consul服務偵聽地址,這個地址提供HTTP、DNS、RPC等服務,默認是127.0.0.1因此不對外提供服務,若是你要對外提供服務改爲0.0.0.0[root@dhcp-10-201-102-198 consul]# consul agent -server -bootstrap-expect 1 -data-dir /tmp/consul -node=s1 -bind=10.201.102.198 -ui-dir ./consul_ui/ -rejoin -config-dir=/etc/consul.d/ -client 0.0.0.0 ==> WARNING: Expect Mode enabled, expecting 3 servers ==> Starting Consul agent... ==> Starting Consul agent RPC... ==> Consul agent running! Version: 'v0.7.4' Node ID: '422ec677-74ef-8f29-2f22-01effeed6334' Node name: 's1' Datacenter: 'dc1' Server: true (bootstrap: false) Client Addr: 0.0.0.0 (HTTP: 8500, HTTPS: -1, DNS: 8600, RPC: 8400) Cluster Addr: 10.201.102.198 (LAN: 8301, WAN: 8302) Gossip encrypt: false, RPC-TLS: false, TLS-Incoming: false Atlas: <disabled> ==> Log data will now stream in as it occurs: 2017/03/17 18:03:08 [INFO] raft: Restored from snapshot 139-352267-1489707086023 2017/03/17 18:03:08 [INFO] raft: Initial configuration (index=6982): [{Suffrage:Voter ID:10.201.102.199:8300 Address:10.201.102.199:8300} {Suffrage:Voter ID:10.201.102.200:8300 Address:10.201.102.200:8300} {Suffrage:Voter ID:10.201.102.198:8300 Address:10.201.102.198:8300}] 2017/03/17 18:03:08 [INFO] raft: Node at 10.201.102.198:8300 [Follower] entering Follower state (Leader: "") 2017/03/17 18:03:08 [INFO] serf: EventMemberJoin: s1 10.201.102.198 2017/03/17 18:03:08 [INFO] serf: Attempting re-join to previously known node: s2: 10.201.102.199:8301 2017/03/17 18:03:08 [INFO] consul: Adding LAN server s1 (Addr: tcp/10.201.102.198:8300) (DC: dc1) 2017/03/17 18:03:08 [INFO] consul: Raft data found, disabling bootstrap mode 2017/03/17 18:03:08 [INFO] serf: EventMemberJoin: s2 10.201.102.199 2017/03/17 18:03:08 [INFO] serf: EventMemberJoin: s3 10.201.102.200 2017/03/17 18:03:08 [INFO] serf: Re-joined to previously known node: s2: 10.201.102.199:8301 2017/03/17 18:03:08 [INFO] consul: Adding LAN server s2 (Addr: tcp/10.201.102.199:8300) (DC: dc1) 2017/03/17 18:03:08 [INFO] consul: Adding LAN server s3 (Addr: tcp/10.201.102.200:8300) (DC: dc1) 2017/03/17 18:03:08 [INFO] serf: EventMemberJoin: s1.dc1 10.201.102.198 2017/03/17 18:03:08 [INFO] consul: Adding WAN server s1.dc1 (Addr: tcp/10.201.102.198:8300) (DC: dc1) 2017/