一、Consul安裝和配置
1、consul是什么?
consul是HashiCorp公司推出的一款工具,主要用於實現分布式系統的服務發現與配置,它提供了以下幾個關鍵特性:
- 服務發現:Consul客戶端可以提供如 api或mysql之類的服務,其他客戶端使用Consul去發現一個指定服務的提供者.通過DNS或者HTTP應用程序可用很容易的找到他所依賴的服務.
- 健康檢查:consul客戶端可以提供任意數量的健康檢查,,可以結合具體的服務(“Web服務器返回200 OK”)或本地節點(“內存利用率低於90%”)。操作者可以通過檢測信息來監視群集運行狀況,服務發現組件會將流量從不健康的主機路由到健康主機。
- K/V存儲:應用程序可以根據需要使用consul的層級K/V存儲,如動態配置,功能標記,一致性協調,leader選舉等。簡單的HTTP API使其易於使用。
- 多數據中心:consul支持開箱即用的多數據中心。用戶不用擔心需要建立額外的抽象層讓業務擴展到多個區域.。
Consul面向DevOps和應用開發者友好.非常適合現代的彈性的基礎設施.
2、Consul基礎架構
1、consul是一個分布式,高可用性的系統。這里只是簡單介紹一下基礎知識,故意省略了一些不必要的細節,以便能夠快速了解consul的工作原理.
2、每個提供Consul服務的節點都會運行一個consul agent。發現服務或者獲取、設置key/value數據的節點不需要運行agent。agent負責對節點上運行的服務以及節點本身進行健康檢查。
3、agent連接一個或者多個Consul server,Consul server存儲,復制數據,選取leaader。雖然Consul server可以只運行一個,但是通常建議每個數據中心運行由3到5個consul server組成的集群,以防單點故障造成數據丟失。
4、需要發現其他服務或節點的組件可以查詢任何consul server或consul agent。consul agent會自動將請求轉發給consul server。
5、每個數據中心都運行一組Consul server集群。當進行跨數據中心服務發現或配置請求時,本地consul server會將請求轉發到遠程數據中心並返回結果。
3、Consul架構圖
4、Consul安裝
和其他軟件一樣,都支持二進制安裝、源碼安裝等。我這里采用最簡單的安裝方式:二進制安裝。
wget https://releases.hashicorp.com/consul/1.5.3/consul_1.5.3_linux_amd64.zip
unzip consul_1.5.3_linux_amd64.zip
mv consul /usr/bin/
安裝完成。
5、Consul啟動
consul agent -server -bootstrap -ui -data-dir=/var/lib/consul-data -bind=172.31.0.21 -client=0.0.0.0 -node=server01
agent:agent模式
server:server模式
ui:啟用UI界面
datadir:數據目錄
bind:綁定偵聽地址
client:指定client
node:節點名稱
6、Consul啟動日志
[root@vm172-31-0-21 ~]# consul agent -server -bootstrap -ui -data-dir=/var/lib/consul-data -bind=172.31.0.21 -client=0.0.0.0 -node=server01 bootstrap = true: do not enable unless necessary ==> Starting Consul agent... Version: 'v1.5.3' Node ID: '381f702d-7495-1b19-d75e-9cd79b1bbf8e' Node name: 'server01' Datacenter: 'dc1' (Segment: '<all>') Server: true (Bootstrap: true) Client Addr: [0.0.0.0] (HTTP: 8500, HTTPS: -1, gRPC: -1, DNS: 8600) Cluster Addr: 172.31.0.21 (LAN: 8301, WAN: 8302) Encrypt: Gossip: false, TLS-Outgoing: false, TLS-Incoming: false, Auto-Encrypt-TLS: false ==> Log data will now stream in as it occurs: 2019/08/02 10:00:12 [INFO] raft: Initial configuration (index=1): [{Suffrage:Voter ID:381f702d-7495-1b19-d75e-9cd79b1bbf8e Address:172.31.0.21:8300}] 2019/08/02 10:00:12 [INFO] raft: Node at 172.31.0.21:8300 [Follower] entering Follower state (Leader: "") 2019/08/02 10:00:12 [INFO] serf: EventMemberJoin: server01.dc1 172.31.0.21 2019/08/02 10:00:12 [INFO] serf: EventMemberJoin: server01 172.31.0.21 2019/08/02 10:00:12 [INFO] consul: Adding LAN server server01 (Addr: tcp/172.31.0.21:8300) (DC: dc1) 2019/08/02 10:00:12 [INFO] consul: Handled member-join event for server "server01.dc1" in area "wan" 2019/08/02 10:00:12 [WARN] agent/proxy: running as root, will not start managed proxies 2019/08/02 10:00:12 [INFO] agent: Started DNS server 0.0.0.0:8600 (udp) 2019/08/02 10:00:12 [INFO] agent: Started DNS server 0.0.0.0:8600 (tcp) 2019/08/02 10:00:12 [INFO] agent: Started HTTP server on [::]:8500 (tcp) 2019/08/02 10:00:12 [INFO] agent: started state syncer ==> Consul agent running! 2019/08/02 10:00:19 [ERR] agent: failed to sync remote state: No cluster leader 2019/08/02 10:00:20 [WARN] raft: Heartbeat timeout from "" reached, starting election 2019/08/02 10:00:20 [INFO] raft: Node at 172.31.0.21:8300 [Candidate] entering Candidate state in term 2 2019/08/02 10:00:20 [INFO] raft: Election won. Tally: 1 2019/08/02 10:00:20 [INFO] raft: Node at 172.31.0.21:8300 [Leader] entering Leader state 2019/08/02 10:00:20 [INFO] consul: cluster leadership acquired 2019/08/02 10:00:20 [INFO] consul: New leader elected: server01 2019/08/02 10:00:20 [INFO] consul: member 'server01' joined, marking health alive 2019/08/02 10:00:22 [INFO] agent: Synced node info
7、dashboard
二、confd安裝和配置
1、confd簡介
當系統變的復雜,配置項越來越多,一方面配置管理變得繁瑣,另一方面配置修改后需要重新上線同樣十分痛苦。這時候,需要有一套集中化配置管理系統,一方面提供統一的配置管理,另一方面提供配置變更的自動下發,及時生效。
統一配置管理系統,大家應該比較熟悉,常見的:zookeeper、etcd、consul、git等
confd流程圖
2、confd是如何工作的?
3、confd的安裝
下載confd
的二進制文件,下載地址為:https://github.com/kelseyhightower/confd/releases。
# Download the binary wget https://github.com/kelseyhightower/confd/releases/download/v0.16.0/confd-0.16.0-linux-amd64 # 重命名二進制文件,並移動到PATH的目錄下 mv confd-0.16.0-linux-amd64 /usr/local/bin/confd chmod +x /usr/local/bin/confd # 驗證是否安裝成功 confd --help
4、confd的配置
1. 創建confdir
mkdir -p /etc/confd/{conf.d,templates}
confdir底下包含兩個目錄:
-
conf.d
:confd的配置文件,主要包含配置的生成邏輯,例如模板源,后端存儲對應的keys,命令執行等。 -
templates
:配置模板Template,即基於不同組件的配置,修改為符合 Golang text templates的模板文件。
2、Template Resources
模板源配置文件是TOML
格式的文件,主要包含配置的生成邏輯,例如模板源,后端存儲對應的keys,命令執行等。默認目錄在/etc/confd/conf.d
。
cat /etc/confd/conf.d/myconfig.toml
[template] src = "myconfig.conf.tmpl" dest = "/tmp/myconfig.conf" keys = [ "/myapp/database/url", "/myapp/database/user", ]
src:指定tmpl源文件
dest:指定生成目標文件
keys:tmpl會用到的變量
3、Template
emplate
定義了單一應用配置的模板,默認存儲在/etc/confd/templates
目錄下,模板文件符合Go的text/template
格式。
模板文件常用函數有base
,get
,gets
,lsdir
,json
等。具體可參考https://github.com/kelseyhightower/confd/blob/master/docs/templates.md。
cat /etc/confd/templates/myconfig.conf.tmpl
[myconfig] database_url = {{getv "/myapp/database/url"}} database_user = {{getv "/myapp/database/user"}}
4、啟動confd
confd -confdir="/etc/confd/" -config-file="/etc/confd/conf.d/myconfig.toml" -onetime=false -interval=3 -backend consul -node 172.31.0.21:8500 -log-level="debug"
confdir:配置文件目錄
config-file:配置文件
interval:檢測文件變化間隔
backend:指定后端數據庫
node:后端數據庫地址
log-level:日志級別
5、啟動日志
[root@vm172-31-0-21 confd]# confd -confdir="/etc/confd/" -config-file="/etc/confd/conf.d/myconfig.toml" -onetime=false -interval=3 -backend consul -node 172.31.0.21:8500 -log-level="debug" 2019-08-02T10:37:11+08:00 vm172-31-0-21.ksc.com confd[1801]: INFO Backend set to consul 2019-08-02T10:37:11+08:00 vm172-31-0-21.ksc.com confd[1801]: INFO Starting confd 2019-08-02T10:37:11+08:00 vm172-31-0-21.ksc.com confd[1801]: INFO Backend source(s) set to 172.31.0.21:8500 2019-08-02T10:37:11+08:00 vm172-31-0-21.ksc.com confd[1801]: DEBUG Loading template resources from confdir /etc/confd/ 2019-08-02T10:37:11+08:00 vm172-31-0-21.ksc.com confd[1801]: DEBUG Found template: /etc/confd/conf.d/myconfig.toml 2019-08-02T10:37:11+08:00 vm172-31-0-21.ksc.com confd[1801]: DEBUG Loading template resource from /etc/confd/conf.d/myconfig.toml 2019-08-02T10:37:11+08:00 vm172-31-0-21.ksc.com confd[1801]: DEBUG Retrieving keys from store 2019-08-02T10:37:11+08:00 vm172-31-0-21.ksc.com confd[1801]: DEBUG Key prefix set to / 2019-08-02T10:37:11+08:00 vm172-31-0-21.ksc.com confd[1801]: DEBUG Got the following map from store: map[/myapp/database/url:db.example.com /myapp/database/user:rob] 2019-08-02T10:37:11+08:00 vm172-31-0-21.ksc.com confd[1801]: DEBUG Using source template /etc/confd/templates/myconfig.conf.tmpl 2019-08-02T10:37:11+08:00 vm172-31-0-21.ksc.com confd[1801]: DEBUG Compiling source template /etc/confd/templates/myconfig.conf.tmpl 2019-08-02T10:37:11+08:00 vm172-31-0-21.ksc.com confd[1801]: DEBUG Comparing candidate config to /tmp/myconfig.conf 2019-08-02T10:37:11+08:00 vm172-31-0-21.ksc.com confd[1801]: DEBUG Target config /tmp/myconfig.conf in sync 2019-08-02T10:37:14+08:00 vm172-31-0-21.ksc.com confd[1801]: DEBUG Loading template resources from confdir /etc/confd/ 2019-08-02T10:37:14+08:00 vm172-31-0-21.ksc.com confd[1801]: DEBUG Found template: /etc/confd/conf.d/myconfig.toml 2019-08-02T10:37:14+08:00 vm172-31-0-21.ksc.com confd[1801]: DEBUG Loading template resource from /etc/confd/conf.d/myconfig.toml 2019-08-02T10:37:14+08:00 vm172-31-0-21.ksc.com confd[1801]: DEBUG Retrieving keys from store 2019-08-02T10:37:14+08:00 vm172-31-0-21.ksc.com confd[1801]: DEBUG Key prefix set to / 2019-08-02T10:37:14+08:00 vm172-31-0-21.ksc.com confd[1801]: DEBUG Got the following map from store: map[/myapp/database/user:rob /myapp/database/url:db.example.com]
6、查看新生成的配置文件
cat /tmp/myconfig.conf
[myconfig]
database_url = db.example.com
database_user = rob
三、整體流程圖