1. 程式人生 > >consul(第一篇)consul 入門

consul(第一篇)consul 入門

consul 入門

consul是什麼

consul包含多個元件,從整體上看可以理解為一個服務發現、配置的工具,包含一下幾個特點:

  • 服務發現(Service Discovery):客戶端通過consul提供服務,其他客戶端可以通過consul利用dns或者http發現依賴服務

  • 健康檢查(Health Checking): consul提供任務的健康檢查,可以用來操作或者監控叢集的健康,也可以在服務發現時去除失效的服務

  • 鍵值對儲存(Key/Value Store): 儲存層級鍵值對

  • 多資料中心(Multi Datacenter): consul支援開箱即用的多資料中心

consul 安裝

1.下載consul安裝包並解壓

wget https://releases.hashicorp.com/consul/0.6.4/consul_0.6.4_linux_amd64.zip
unzip consul_0.6.4_linux_amd64.zip

解壓後得到可執行的二進位制檔案,執行./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 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 reload Triggers the agent to reload configuration files rtt Estimates network round trip time between nodes version Prints the Consul version watch Watch for changes in Consul

表示consul成功安裝,同時將consul所在目錄配置到path ,例如這裡consulsoftware努力下,進行如下配置

vim ~/.bash_profile

export GOROOT=/software/go
export GOPATH=/root/work
PATH=$PATH:$HOME/bin:$GOROOT/bin:/software

export PATH

source ~/.bash_profile

啟動

首先下載web-ui

wget https://releases.hashicorp.com/consul/0.6.4/consul_0.6.4_web_ui.zip

通過下面命令啟動consul

consul agent -server -bootstrap-expect 1 -data-dir /tmp/consul -advertise 120.76.145.187 -client 120.76.145.187 -ui-dir /software/consul-ui
  • server 表示啟動的為consul server ,構建一個consul cluster 一般建議使用3或者5個consul server

  • bootstrap-expect 1 表示期望的服務節點數目為1

  • -data-dir 資料目錄,如果該資料夾不存在則手工建立,如果在consul發生錯誤後,建議先清理該目錄檔案

  • advertise 設定廣播地址,ip可以設定為公網ip

  • client 設定client訪問的地址

  • ui-dir web控制檯目錄位置

注意 通過advertise client 才能在WAN上訪問consul,否則只能在LAN內訪問.

官方給出的快捷啟動為 consul agent -dev 如果想簡單體驗一下可以使用該命令

啟動後訪問http://ip:8500/ui得到如下介面

consul-ui

獲取基本資訊

獲取consul server叢集中的成員列表

使用命令方式

  • 如果以區域網中,直接使用 consul members
  • 如果是廣域網中,使用 consul members --rpc-addr=ip:8400
    得到結果:
Node          Address              Status  Type    Build  Protocol  DC
iZ94235juoyZ  120.76.145.187:8301  alive   server  0.6.4  2         dc1

使用http介面方式

 curl 120.76.145.187:8500/v1/catalog/nodes

返回結果:

[{"Node":"iZ94235juoyZ","Address":"120.76.145.187","TaggedAddresses":{"wan":"120.76.145.187"},"CreateIndex":3,"ModifyIndex":4}]

通過dns方式

 dig @120.76.145.187 -p 8600 iZ94235juoyZ.node.consul

返回結果:

; <<>> DiG 9.8.2rc1-RedHat-9.8.2-0.47.rc1.el6 <<>> @120.76.145.187 -p 8600 iZ94235juoyZ.node.consul
; (1 server found)
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 625
;; flags: qr aa rd; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 0
;; WARNING: recursion requested but not available

;; QUESTION SECTION:
;iZ94235juoyZ.node.consul.      IN      A

;; ANSWER SECTION:
iZ94235juoyZ.node.consul. 0     IN      A       120.76.145.187

;; Query time: 0 msec
;; SERVER: 120.76.145.187#8600(120.76.145.187)
;; WHEN: Wed Jun  1 21:24:31 2016
;; MSG SIZE  rcvd: 82

ok ~ it’s work ! more about is here

歡迎關注,您的肯定是對我最大的支援

這裡寫圖片描述