首先準備一台 ubuntu server , 我這邊準備的是 18.04 版本的 ubuntu。
- 安裝 unzip
sudo apt-get install unzip
2. 從官網下載 consul zip
wget https://releases.hashicorp.com/consul/1.19.0/consul_1.19.0_linux_amd64.zip
3. 解壓縮
unzip consul_1.19.0_linux_amd64.zip
4. 將指令一致 /usr/bin
sudo mv consul /usr/bin
5. 檢查指令是否可用
consul -v
倘若可以使用,緊接著安裝 autocomplete, 使 consul 可以幫我們補指令
consul -autocomplete-install
因為會設定在 .bashrc 因此要記得執行此指令, 以 reload bashrc
source ~/.bashrc
consul 開啟 DEV 模式
參數說明:
client: 供 http, webui 使用的 ip
dev: 啟用 dev 模式
consul agent -dev -client=0.0.0.0
consul port 參考文件
AWS EC2 security group 需開啟以下 port
- TCP 8500
- TCP 8600
- UDP 8600
以上是透過 cli 設定一些變數,接下來我們可以透過設定檔去設定參數,首先我們先建置以下檔案。
- logs: consul log 存放位置
- data: consul 啓動會存放的資料
- conf: consul 設定檔 acl, policy, service 等
緊接著在 conf 設定 consul-server.hcl, 檔案如下
# 是否為 server 模式
server = true
# server cluster 數量
bootstrap_expect = 1
# 啟用 ui
ui_config {
enabled = true
}
# datacenter 名稱
datacenter = "dc1"
data_dir = "/home/ubuntu/consul/data"
# 更新檢查
disable_update_check = false
enable_local_script_checks = true
# 節點名稱
node_name = "consul-server1"
# 提供給外部連線的 web ui, http
client_addr = "<private ip address>"
# cluster 內部使用的 ip
bind_addr = "0.0.0.0"
# 在 cluster 中設定該 agent 是否允許連線
connect {
enabled = true
}
# log 設定
log_level = "DEBUG"
log_file = "/home/ubuntu/consul/logs/"
log_rotate_duration = "24h"
log_rotate_max_files = 1
指定使用此 config 啟動 consul
consul agent -config-dir=./conf
consul ACL 權限控制
acl 格式如下
<resource> "<name>" {
policy = "<acl>"
}
resource: consul 資源,有 service, key node 等可以使用
name: resource資源名稱
policy: consul 權限, 有 read, list, deny, write, 多個的話使用逗號區隔
順序為: deny > write > read > list
新增 acl.hcl
acl = {
# 啟用 acl
enabled = true
# 預設 policy 為 deny
default_policy = "deny"
# token 持久化至 disk
enable_token_persistence = true
}
創建一個最高權限的 token
consul agent bootstrap -http-addr=https://<public ip>:8500
結果:
AccessorID: <accessor id>
SecretID: <token>
Description: Bootstrap Token (Global Management)
Local: false
Create Time: 2024-07-05 12:34:52.024284855 +0000 UTC
Policies:
00000000-0000-0000-0000-000000000001 - global-management
資料說明:
AccessorID: token 編號, 倘若之後忘記 token 可以透過此 ID 找回 token
SecretID: token
admin token 權限過高,所以我們建立一個僅供讀取的權限, 在 policy 資料夾新增 acl-webui.hcl
# service 唯讀
service_prefix "" {
policy = "read"
}
# key 唯讀
key_prefix "" {
policy = "read"
}
# node 唯讀
node_prefix "" {
policy = "read"
}
根據 acl 創建 policy, 指令如下
consul acl policy create -name "<policy name>" -description "<policy description>" -rules "@acl-webui.hcl" -http-addr="http://<private ip>:8500" -token="<admin token>"
回傳值如下
參數說明:
ID: policy ID 名稱
Name: policy 名稱
Description: policy 說明
使用此 policy 創建 token
consul acl token create -description="<token description>" -policy-name="<policy name>" -expires-ttl="<過期時間>" -http-addr="http://<public ip>:8500" -token="<admin token>"
參數說明:
expires-ttl: 倘若不要有過期時間的話此參數就不要設定
結果如下:
忘記 token 的話可以特過以下指令找為 token
consul acl token read -id="<accessor id>" -http-addr="http://<public ip>:8500" -token="<admin token>"
ACL 總結:
倘若需要將 ACL 附加到 token 則步驟為:
- 新增 ACL
- 透過新增的 ACL 或者已存在的 ACL 創建 policy
- 透過 policy 創建 token
Consul Client 設定
設定 consul-client.hcl
datacenter = "dc1"
data_dir = "/home/ubuntu/consul/data"
disable_update_check = true
node_name = "consul-client1"
client_addr = "<private ip>"
bind_addr = "<private ip>"
connect {
enabled = true
}
retry_join = ["<consul server public ip>"]
retry_interval = "20s"
log_level = "DEBUG"
log_file = "/home/ubuntu/consul/logs/"
log_rotate_duration = "24h"
log_rotate_max_files = 0
performance {
raft_multiplier = 1
}
此處會發現 port 8301 沒有開,因此記得在 security group 開啟 TCP, UDP 8301 port。緊接著也會發現沒有權限連接至 consul server, 因為 consul 現在需要有 token 權限才能操作,因此需要 consul server 建置 token。
client-policy.hcl
service_prefix "" {
policy = "read"
}
node_prefix "" {
policy = "write"
}
agent_prefix "" {
policy = "write"
}
(在 consul server 操作)透過 policy 檔案新增 policy
consul acl policy create -name="consul-client" -description="consul client description" -rules="@client-policy.hcl" -http-addr="http://<public ip>:8500" -token="<admin token>"
(在 consul server 操作)透過 policy 建置 token
consul acl token create -description="consul client token" -policy-name="consul-client" -http-addr="http://<public ip>:8500" -token="<admin token>"
接著在 client 建置 acl.hcl
acl = {
enabled = true
default_policy = "deny"
enable_token_persistence = true
tokens {
# 在 consul 新增的 token
agent = "74f96c99-3c27-2eaf-943d-d708d3d40208"
}
}
K/V
設定 key-value pair
consul kv put -http-addr="http://<public ip>:8500" -token="<admin token>" a/b hello
取得 key-value pair 的值
consul kv get -http-addr="http://<public ip>:8500" -token="<admin token>" a/b
列出所有的 key
consul kv get -recurse -http-addr="http://<public ip>:8500" -token="<admin token>"
key export
consul kv export -http-addr="http://<public ip>:8500" -token="<admin token>"
結果:
參數說明:
value: 以 base64 編碼
Service Discovery
在 client 設定 service.hcl
service {
name = "nginx-service"
tags = ["web", "http"]
port = 80
token = "<client token>"
}
參數說明:
service: 服務名稱
tags: 標籤
port: service 使用的 port, 這邊以 nginx 為利,所以設定為 80 port
token: client token
註冊服務
consul services register -token="<admin token>" -http-addr="http://<public ip>" service.hcl
注意!! 使用的 token policy 需要有 write 權限
重啟 consul reload
consul reload -http-addr="http://<public ip>:8500" -token="<admin token>"
之後可以在 ui service 看到 http, tcp check
產生 snapshot
consul snapshot save -http-addr="http://<public ip>" -token="<admin token>" backup.snapshot
使用 snapshot 還原
consul snapshot restore -http-addr="http://<public ip>" -token="<admin token>" backup.snapshot
監控設定
設定 mointor-policy.hcl
agent_prefix "" {
policy = "read"
}
使用 acl 創建 policy
consul acl policy create -name="monitor-policy" -description="monitor policy" -rules="@monitor-policy.hcl" -http-addr="http://<public ip>:8500" -token="<admin token>"
使用 policy 創建 token
consul acl token create -description="monitor" -policy-name="monitor-policy" -http-addr="http://<public ip>:8500" -token="<admin token>"
consul agent 參數文件:
安裝 prometheus, grafana
- wget https://github.com/prometheus/prometheus/releases/download/v2.53.0/prometheus-2.53.0.linux-amd64.tar.gz
- tar zxcf prometheus-2.53.0.linux-amd64.tar.gz
- ./prometheus — config.file=”prometheus.yml”
prometheus 預設啟用的 port 是 9090, 所以 security group 記得開啟 tcp 9090 port
consul service 編輯 consul-server.hcl
telemetry {
prometheus_retention_time = "10s"
disable_hostname = false
}
acl 多增加
agent_prefix "" {
policy = "read"
}
agent_prefix "consul-server" {
policy = "read"
}
透過 acl 新增 policy
consul acl policy create -name="monitor-policy" -description="monitor policy" -rules="@monitor-policy.hcl" -http-addr="http://<public ip>:8500" -token="<admin token>"
透過 policy 新增 token
consul acl token create -description="monitor" -policy-name="monitor-policy" -http-addr="http://<public ip>:8500" -token="<admin token>"
修改 prometheus.yml
- job_name: "consul"
static_configs:
- targets: ["<public ip>:8500"]
scheme: "http"
tls_config:
insecure_skip_verify: true
metrics_path: "/v1/agent/metrics"
params:
format: ["prometheus"]
bearer_token: "<mointor token>"
倘若想要而外安裝 exporter 可以類似如下做法,以下為使用 consul-exporter
首先需要在 consul server 安裝 docker , 因為會透過 docker 起 consul_exporter
sudo docker run -d -p 9107:9107 prom/consul-exporter --consul.server=<public ip>:8500
因為 docker 跑在 9107 port, 所以 ec2 security group 要開啟 TCP 9107 port
而 prometheus.yml 則要增加以下設定
- job_name: "consul_exporter"
metrics_path: "/metrics"
static_configs:
- targets: ["<consul server public ip> :9107"]
tls_config:
insecure_skip_verify: true
bearer_token: "<monitor token>"
consul exporter 9107 內容如下
安裝 node_exporter
在 consul server 執行
docker run -d \
--net="host" \
--pid="host" \
-v "/:/host:ro,rslave" \
quay.io/prometheus/node-exporter:latest \
--path.rootfs=/host
因為 node exporter port 預設為 9100, 因此 ec2 security group 虛開啟 tcp 9100 port, 而 prometheus.yml 增加以下設定
- job_name: "node_exporter"
metrics_path: "/metrics"
static_configs:
- targets: ["<consul server public ip>:9100"]
tls_config:
insecure_skip_verify: true
bearer_token: "<monitor token>"
安裝 grafana
sudo apt install -y apt-transport-https
wget -q -O - https://packages.grafana.com/gpg.key | sudo apt-key add -
echo "deb https://packages.grafana.com/oss/deb stable main" | sudo tee /etc/apt/sources.list.d/grafana.list
sudo apt-get update
sudo apt-get install grafana
設定 grafana admin 密碼
sudo grafana-cli admin reset-admin-password <new_password>
sudo systemctl start grafana-server
grafana 預設的 port 為 3000, 因此需要在 ec2 security group 設定 TCP 3000 port
緊接著將 prometheus 設定在 grafana
- 設定 prometheus data source
2. 客製化 dashboard
總結:
- Consul Server 跑在 8500 port
- Prometheus 跑在 9090 port
- Grafana 跑在 3000 port