本章節會對consul的架構與配置做全面講解。
Consul架構

上圖是官網提供的一個事例系統圖,圖中的Server是consul服務端高可用集群,Client是consul客戶端。consul客戶端不保存數據,客戶端將接收到的請求轉發給響應的Server端。Server之間通過局域網或廣域網通信實現數據一致性。每個Server或Client都是一個consul agent。
Consul集群間使用了GOSSIP協議通信和raft一致性算法。上面這張圖涉及到了很多術語:
-
Agent——agent是一直運行在Consul集群中每個成員上的守護進程。通過運行consul agent來啟動。agent可以運行在client或者server模式。指定節點作為client或者server是非常簡單的,除非有其他agent實例。所有的agent都能運行DNS或者HTTP接口,並負責運行時檢查和保持服務同步。
-
Client——一個Client是一個轉發所有RPC到server的代理。這個client是相對無狀態的。client唯一執行的后台活動是加入LAN gossip池。這有一個最低的資源開銷並且僅消耗少量的網絡帶寬。
-
Server——一個server是一個有一組擴展功能的代理,這些功能包括參與Raft選舉,維護集群狀態,響應RPC查詢,與其他數據中心交互WAN gossip和轉發查詢給leader或者遠程數據中心。
-
DataCenter——雖然數據中心的定義是顯而易見的,但是有一些細微的細節必須考慮。例如,在EC2中,多個可用區域被認為組成一個數據中心。我們定義數據中心為一個私有的,低延遲和高帶寬的一個網絡環境。這不包括訪問公共網絡,但是對於我們而言,同一個EC2中的多個可用區域可以被認為是一個數據中心的一部分。
-
Consensus——一致性,使用Consensus來表明就leader選舉和事務的順序達成一致。為了以容錯方式達成一致,一般有超過半數一致則可以認為整體一致。Consul使用Raft實現一致性,進行leader選舉,在consul中的使用bootstrap時,可以進行自選,其他server加入進來后bootstrap就可以取消。
-
Gossip——Consul建立在Serf的基礎之上,它提供了一個用於多播目的的完整的gossip協議。Serf提供成員關系,故障檢測和事件廣播。Serf是去中心化的服務發現和編制的解決方案,節點失敗偵測與發現,具有容錯、輕量、高可用的特點。
-
LAN Gossip——它包含所有位於同一個局域網或者數據中心的所有節點。
-
WAN Gossip——它只包含Server。這些server主要分布在不同的數據中心並且通常通過因特網或者廣域網通信。
-
RPC——遠程過程調用。這是一個允許client請求server的請求/響應機制。
在每個數據中心,client和server是混合的。一般建議有3-5台server。這是基於有故障情況下的可用性和性能之間的權衡結果,因為越多的機器加入達成共識越慢。然而,並不限制client的數量,它們可以很容易的擴展到數千或者數萬台。
同一個數據中心的所有節點都必須加入gossip協議。這意味着gossip協議包含一個給定數據中心的所有節點。這服務於幾個目的:第一,不需要在client上配置server地址。發現都是自動完成的。第二,檢測節點故障的工作不是放在server上,而是分布式的。這使的故障檢測相比心跳機制有更高的可擴展性。第三:它用來作為一個消息層來通知事件,比如leader選舉發生時。
每個數據中心的server都是Raft節點集合的一部分。這意味着它們一起工作並選出一個leader,一個有額外工作的server。leader負責處理所有的查詢和事務。作為一致性協議的一部分,事務也必須被復制到所有其他的節點。因為這一要求,當一個非leader得server收到一個RPC請求時,它將請求轉發給集群leader。
server節點也作為WAN gossip Pool的一部分。這個Pool不同於LAN Pool,因為它是為了優化互聯網更高的延遲,並且它只包含其他Consul server節點。這個Pool的目的是為了允許數據中心能夠以low-touch的方式發現彼此。這使得一個新的數據中心可以很容易的加入現存的WAN gossip。因為server都運行在這個pool中,它也支持跨數據中心請求。當一個server收到來自另一個數據中心的請求時,它隨即轉發給正確數據中心一個server。該server再轉發給本地leader。
這使得數據中心之間只有一個很低的耦合,但是由於故障檢測,連接緩存和復用,跨數據中心的請求都是相對快速和可靠的。
Consul集群安裝環境
此處啟動的是單實例多端口,如果你是多實例,請自行更改相關配置
| ServerName | IP Addr & Port | Consul Roles |
|---|---|---|
| server1 | 192.168.1.153:8500 | Server1 |
| server2 | 192.168.1.154:8500 | Server2 |
| server3 | 192.168.1.155:8500 | Server3 |
| node-exporter | 192.168.1.151:9100 | Node-Exporter |
二進制安裝Consul 1.7.7
配置為systemd啟動, Docker方式部署鏈接(https://juejin.im/post/5d4289e1e51d45620b21c34a)
# 下載consul
CONSUL_VERSION='1.7.7'
wget https://releases.hashicorp.com/consul/${CONSUL_VERSION}/consul_${CONSUL_VERSION}_linux_amd64.zip
# 解壓安裝
unzip consul_${CONSUL_VERSION}_linux_amd64.zip
chown root:root consul
mv consul /usr/local/bin/
consul --version
# 啟用自動補全
consul -autocomplete-install
complete -C /usr/local/bin/consul consul
# 創建用戶和目錄
useradd -M -s /sbin/nologin consul
mkdir -p /data/consul/server/{data,config}
chown -R consul.consul /data/consul/
# 配置Systemd
# consul-server1
cat > /lib/systemd/system/consul-server1.service << EOF
[Unit]
Description="consul server1"
Requires=network-online.target
After=network-online.target
[Service]
User=consul
Group=consul
ExecStart=/usr/local/bin/consul agent -config-dir=/data/consul/server/config
ExecReload=/usr/local/bin/consul reload
KillMode=process
Restart=on-failure
LimitNOFILE=65536
[Install]
WantedBy=multi-user.target
EOF
# consul-server2
cat > /lib/systemd/system/consul-server2.service << EOF
[Unit]
Description="consul server2"
Requires=network-online.target
After=network-online.target
[Service]
User=consul
Group=consul
ExecStart=/usr/local/bin/consul agent -config-dir=/data/consul/server/config
ExecReload=/usr/local/bin/consul reload
KillMode=process
Restart=on-failure
LimitNOFILE=65536
[Install]
WantedBy=multi-user.target
EOF
# consul-server3
cat > /lib/systemd/system/consul-server3.service << EOF
[Unit]
Description="consul server3"
Requires=network-online.target
After=network-online.target
[Service]
User=consul
Group=consul
ExecStart=/usr/local/bin/consul agent -config-dir=/data/consul/server/config
ExecReload=/usr/local/bin/consul reload
KillMode=process
Restart=on-failure
LimitNOFILE=65536
[Install]
WantedBy=multi-user.target
EOF
創建Server{1-3}配置文件
# 生成密鑰
CONSUL_KEY=`consul keygen`
# node_id 一定不可以重復,server name可以隨便定義
# 創建server1配置文件
cat > /data/consul/server/config/config.json << EOF
{
"datacenter": "prometheus",
"bind_addr":"192.168.1.153",
"log_level": "INFO",
"node_id":"09d82408-bc4f-49e0-1111-61ef1d4842f7",
"node_name": "server1",
"data_dir":"/data/consul/server/data",
"server": true,
"bootstrap_expect": 3,
"encrypt": "${CONSUL_KEY}",
"ui":true,
"client_addr":"0.0.0.0",
"retry_join":["192.168.1.153:8301","192.168.1.154:8301","192.168.1.155:8301"],
"ports": {
"http": 8500,
"dns": 8600,
"serf_lan":8301,
"serf_wan":8302,
"server":8300,
"grpc":8400
},
"acl": {
"enabled": true,
"default_policy": "deny",
"down_policy": "extend-cache",
"tokens":{
"master":"${CONSUL_HTTP_TOKEN}",
"agent":"${CONSUL_HTTP_TOKEN}"
}
}
}
EOF
# 創建server2配置文件
cat > /data/consul/server/config/config.json << EOF
{
"datacenter": "prometheus",
"bind_addr":"192.168.1.154",
"log_level": "INFO",
"node_id":"613ccd6e-68d1-3bbd-2222-3cbc450f019d",
"node_name": "server2",
"data_dir":"/data/consul/server/data",
"server": true,
"bootstrap_expect": 3,
"encrypt": "${CONSUL_KEY}",
"ui":true,
"client_addr":"0.0.0.0",
"retry_join":["192.168.1.153:8301","192.168.1.154:8301","192.168.1.155:8301"],
"ports": {
"http": 8500,
"dns": 8600,
"serf_lan":8301,
"serf_wan":8302,
"server":8300,
"grpc":8400
},
"acl": {
"enabled": true,
"default_policy": "deny",
"down_policy": "extend-cache",
"tokens":{
"master":"${CONSUL_HTTP_TOKEN}",
"agent":"${CONSUL_HTTP_TOKEN}"
}
}
}
EOF
# 創建server3配置文件
cat > /data/consul/server/config/config.json << EOF
{
"datacenter": "prometheus",
"bind_addr":"192.168.1.155",
"log_level": "INFO",
"node_id":"d8a09ffd-7ccb-84bd-3333-8d8b7a01951e",
"node_name": "server3",
"data_dir":"/data/consul/server/data",
"server": true,
"bootstrap_expect": 3,
"encrypt": "${CONSUL_KEY}",
"ui":true,
"client_addr":"0.0.0.0",
"retry_join":["192.168.1.153:8301","192.168.1.154:8301","192.168.1.155:8301"],
"ports": {
"http": 8500,
"dns": 8600,
"serf_lan":8301,
"serf_wan":8302,
"server":8300,
"grpc":8400
},
"acl": {
"enabled": true,
"default_policy": "deny",
"down_policy": "extend-cache",
"tokens":{
"master":"${CONSUL_HTTP_TOKEN}",
"agent":"${CONSUL_HTTP_TOKEN}"
}
}
}
EOF
啟動服務
systemctl enable consul-server1 && systemctl start consul-server1
systemctl enable consul-server2 && systemctl start consul-server2
systemctl enable consul-server3 && systemctl start consul-server3
systemctl status consul-server1
生成http_acl_token,寫入config.jso中的tokens數組中的master與agent。注意,consul acl bootstrap只能執行一次.
consul acl bootstrap
AccessorID: ae4f5026-73e7-ff56-548c-3ae0fc76022f
SecretID: 08ad8862-f702-eb26-0276-d8255b11267e
Description: Bootstrap Token (Global Management)
Local: false
Create Time: 2020-09-02 23:25:47.533701389 +0800 CST
Policies:
00000000-0000-0000-0000-000000000001 - global-management
AccessorID: ae4f5026-73e7-ff56-548c-3ae0fc76022f
SecretID: 08ad8862-f702-eb26-0276-d8255b11267e
export CONSUL_HTTP_TOKEN='your_token'
查看集群
返回空節點是正常的,因為開啟了ACL,所以訪問的時候需要加入token,如果CONSUL_HTTP_TOKEN變量已經加入profile,不需要在指定token。
# 環境變量
cat >> /etc/profile << EOF
export CONSUL_HTTP_TOKEN='08ad8862-f702-eb26-0276-d8255b11267e'
EOF
# consul members --token='08ad8862-f702-eb26-0276-d8255b11267e'
Node Address Status Type Build Protocol DC Segment
server1 192.168.1.153:8301 alive server 1.7.7 2 prometheus <all>
server2 192.168.1.154:8301 alive server 1.7.7 2 prometheus <all>
server3 192.168.1.155:8301 alive server 1.7.7 2 prometheus <all>
# 驗證集群UI
在頁面http://127.0.0.1:8500/ui/prometheus/acls/tokens 輸入配置中的 master token,再刷新界面可以在services和nodes中查看到信息
# 驗證API,通過在header中增加x-consul-token則可返回節點列表
curl http://127.0.0.1:8500/v1/catalog/nodes -H 'x-consul-token: ${CONSUL_HTTP_TOKEN}'
將Consul日志加入Syslog
此處為可選項,如果你需要單獨將日志輸出到ELK,那么此項配置非常有必要,因為默認的日志都打到syslog中了。
# 創建目錄&賦權
mkdir -p /var/log/consul/
chown -R syslog.syslog /var/log/consul/
# 創建日志配置文件
cat >/etc/rsyslog.d/consul.conf <<EOF
local0.* /var/log/consul/consul.log
EOF
# 修改默認配置文件中的以下內容
vim /etc/rsyslog.d/50-default.conf
# 變更前
*.*;auth,authpriv.none -/var/log/syslog
# 變更后
*.*;auth,authpriv.none,local0.none -/var/log/syslog
# 重啟rsyslog讓配置生效。
$ systemctl restart rsyslog
# 創建日志輪循規則
$ cat >/etc/logrotate.d/consul <<EOF
/var/log/consul/*log {
missingok
compress
notifempty
daily
rotate 5
create 0600 root root
}
EOF
# 在Systemd啟動腳本中加入`-syslog`參數
sed -i 's@ExecStart=/usr/local/bin/consul agent@ExecStart=/usr/local/bin/consul agent -syslog@g' /lib/systemd/system/consul-server{1..3}.service
# 重啟服務
systemctl daemon-reload && systemctl restart consul-server1
systemctl daemon-reload && systemctl restart consul-server2
systemctl daemon-reload && systemctl restart consul-server3
# 查看輸出日志,對於加入ELK的配置就不過多描述了,如果想了解,加入我們的qq群與微信群咨詢相關解決方案。
tail -f /var/log/consul/consul.log
FAQ : 如果集群加入失敗 或 提示:Error retrieving members: Unexpected response code: 403 (ACL not found),toekn格式不對,需要重新檢查集群節點tokens配置,格式為:'55eca91c-b5f7-e82d-7777-dba7637e8888',然后刪除/data/consul/server{1..3}/data/目錄下的數據,重啟服務即可。
Prometheus集成Consul
# 基於AWS EC2 REDIS 發現規則
cat >> /data/prometheus/conf/prometheus.yml <<EOF
- job_name: 'ec2_exporter'
consul_sd_configs:
- server: 172.26.42.229:8500
token: '${CONSUL_HTTP_TOKEN}'
services: ['node_exporter']
relabel_configs:
- source_labels: [__address__]
regex: 172.26.42.229:8300
action: drop
- source_labels: [__meta_consul_tags]
regex: ".*,prod,.*"
replacement: prod
action: replace
target_label: env
- job_name: 'redis_exporter'
consul_sd_configs:
- server: 172.26.42.229:28500
token: '${CONSUL_HTTP_TOKEN}'
services: ['redis_exporter']
relabel_configs:
- source_labels: [__address__]
regex: 172.26.42.229:28300
action: drop
- source_labels: [__meta_consul_tags]
regex: ".*,prod,.*"
replacement: prod
action: replace
target_label: env
- job_name: 'mysql_exporter'
consul_sd_configs:
- server: 172.26.42.229:38500
token: '${CONSUL_HTTP_TOKEN}'
services: ['mysql_exporter']
relabel_configs:
- source_labels: [__address__]
regex: 172.26.42.229:38300
action: drop
- source_labels: [__meta_consul_tags]
regex: ".*,prod,.*"
replacement: prod
action: replace
target_label: env
EOF
# registered nginx01 service to consul1 , ID為唯一標識,用於刪除,Name就是consul中service字段的發現關鍵字,tag可以依據自己的需求relableing。
curl -H "x-consul-token: ${CONSUL_HTTP_TOKEN}" -X PUT -d '{"ID": "node_exporter01", "Name": "node_exporter", "Address": "192.168.1.220", "Port": 9100, "Tags": ["prod"], "EnableTagOverride": false}' \
http://192.168.1.153:8500/v1/agent/service/register
# registered redis01 to consul2
curl -H "x-consul-token: ${CONSUL_HTTP_TOKEN}" -X PUT -d '{"ID": "redis_exporter01", "Name": "redis_exporter", "Address": "172.26.42.229", "Port": 9121, "Tags": ["prod"], "EnableTagOverride": false}' \
http://192.168.1.153:8500/v1/agent/service/register
# registered mysql01 to consul2
curl -H "x-consul-token: ${CONSUL_HTTP_TOKEN}" -X PUT -d '{"ID": "mysql_exporter01", "Name": "mysql_exporter", "Address": "172.26.42.229", "Port": 9105, "Tags": ["prod"], "EnableTagOverride": false}' \
http://192.168.1.153:8500/v1/agent/service/register
# delete nginx01 service
curl -X PUT -H "x-consul-token: ${CONSUL_HTTP_TOKEN}" http://192.168.1.153:8500/v1/agent/service/deregister/node_exporter01
# delete redis01 service
curl -X PUT -H "x-consul-token: ${CONSUL_HTTP_TOKEN}" http://192.168.1.153:8500/v1/agent/service/deregister/redis_exporter01
# delete mysql_exporter01 service
curl -X PUT -H "x-consul-token: ${CONSUL_HTTP_TOKEN}" http://192.168.1.153:8500/v1/agent/service/deregister/mysql_exporter01
使用Consul-template動態配置服務
# 安裝Consul-template 下載地址:https://releases.hashicorp.com/consul-template/
wget https://releases.hashicorp.com/consul-template/0.22.0/consul-template_0.22.0_linux_amd64.zip
unzip consul-template_0.22.0_linux_amd64.zip
mv consul-template /usr/local/bin/
# 查看版本
consul-template -v
consul-template v0.22.0 (6cae10fe)
#常用參數的作用:
-consul-auth=<username[:password]> # 設置基本的認證用戶名和密碼。
-consul-addr=<address> # 設置Consul實例的地址。
-max-stale=<duration> # 查詢過期的最大頻率,默認是1s。
-dedup # 啟用重復數據刪除,當許多consul template實例渲染一個模板的時候可以降低consul的負載。
-consul-ssl # 使用https連接Consul。
-consul-ssl-verify # 通過SSL連接的時候檢查證書。
-consul-ssl-cert # SSL客戶端證書發送給服務器。
-consul-ssl-key # 客戶端認證時使用的SSL/TLS私鑰。
-consul-ssl-ca-cert # 驗證服務器的CA證書列表。
-consul-token=<token> # 設置Consul API的token。
-syslog # 把標准輸出和標准錯誤重定向到syslog,syslog的默認級別是local0。
-syslog-facility=<facility> # 設置syslog級別,默認是local0,必須和-syslog配合使用。
-template=<template> # 增加一個需要監控的模板,格式是:'templatePath:outputPath(:command)',多個模板則可以設置多次。
-wait=<duration> # 當呈現一個新的模板到系統和觸發一個命令的時候,等待的最大最小時間。如果最大值被忽略,默認是最小值的4倍。
-retry=<duration> # 當在和consul api交互的返回值是error的時候,等待的時間,默認是5s。
-config=<path> # 配置文件或者配置目錄的路徑。
-pid-file=<path> # PID文件的路徑。
-log-level=<level> # 設置日志級別,可以是"debug","info", "warn" (default), and "err"。
-dry # Dump生成的模板到標准輸出,不會生成到磁盤。
-once # 運行consul-template一次后退出,不以守護進程運行
# 在conf目錄下創建1個nginx.json的配置文件
cat >> /data/consul/server1/config/nginx.json <<EOF
{
"service":{
"name":"nginx",
"tags":[
"web"
],
"port":80,
"check":{
"http":"http://127.0.0.1:80",
"interval":"10s"
},
"token":"233b604b-b92e-48c8-a253-5f11514e4b50"
}
}
EOF
# 熱加載配置文件
consul reload
# 驗證服務是否注冊成功
curl -H "x-consul-token: ${CONSUL_HTTP_TOKEN}" http://172.26.42.229:8500/v1/catalog/service/nginx | python -m json.tool
[
{
"Address": "192.168.1.153",
"CreateIndex": 21233,
"Datacenter": "prometheus",
"ID": "09d82408-bc4f-49e0-4208-61ef1d4842f7",
"ModifyIndex": 21233,
"Node": "server1",
"NodeMeta": {
"consul-network-segment": ""
},
"ServiceAddress": "",
"ServiceConnect": {},
"ServiceEnableTagOverride": false,
"ServiceID": "nginx",
"ServiceKind": "",
"ServiceMeta": {},
"ServiceName": "nginx",
"ServicePort": 80,
"ServiceProxy": {
"MeshGateway": {}
},
"ServiceTags": [
"web"
],
"ServiceWeights": {
"Passing": 1,
"Warning": 1
},
"TaggedAddresses": {
"lan": "192.168.1.153",
"wan": "192.168.1.155"
}
}
]
# 創建模板
cat > tmpltest.ctmpl << EOF
{{range services}}
{{.Name}}
{{range .Tags}}
{{.}}{{end}}
{{end}}
EOF
# 調用模板渲染
consul-template -consul-addr 192.168.1.153:8500 -template "/data/consul/consul-template/conf/tmpltest.ctmpl:result" -once
# 查看模板渲染的輸出結果,返回的結果:consul是系統自帶的服務,nginx是剛才注冊的服務,Tags是web
cat result
consul
nginx
web
# 創建nginx模板文件
cat >> /data/consul/consul-template/conf/nginx.conf.ctmpl << EOF
{{range services}} {{$name := .Name}} {{$service := service .Name}}
upstream {{$name}} {
zone upstream-{{$name}} 64k;
{{range $service}}server {{.Address}}:{{.Port}} max_fails=3 fail_timeout=60 weight=1;
{{else}}server 127.0.0.1:65535; # force a 502{{end}}
} {{end}}
server {
listen 80 default_server;
location / {
root /usr/share/nginx/html/;
index index.html;
}
location /stub_status {
stub_status;
}
{{range services}} {{$name := .Name}}
location /{{$name}} {
proxy_pass http://{{$name}};
}
{{end}}
}
EOF
# 調用模板文件生成Nginx配置文件
consul-template -consul-addr 192.168.1.153:8500 -template "/data/consul/consul-template/conf/nginx.conf.ctmpl:nginx.conf" -once
# 為了更加安全,token從環境變量里讀取,使用CONSUL_TOKEN和VAULT_TOKEN。強烈建議你不要把token放到未加密的文本配置文件中。
# 創建一個nginx.hcl文件
cat >> nginx.hcl << EOF
consul {
address = "192.168.1.153:8500"
}
template {
source = "/data/consul/consul-template/conf/nginx.conf.ctmpl"
destination = "/etc/nginx/conf/conf.d/default.conf"
command = "service nginx reload"
}
EOF
# 執行渲染命令
consul-template -config "nginx.hcl:test.out"
# 同時渲染多個template並放出后台啟動
cat > consul_temp.sh << EOF
#!/bin/bash
#prom
cd /data/consul/consul-template/ && nohup /usr/local/bin/consul-template -config=/data/consul/consul-template/hcl_conf/ & 2>&1
EOF
結語:
相比於直接使用靜態配置和基於文件發現,是不利於雲環境以及k8s環境的,因為我們大多時候更多監控對象都是動態的。因此,通過服務發現,使得Prometheus相比於其他傳統監控解決方案更適用於雲以及k8s環境下的監控需求。

