使用 TLS 加密保護 Prometheus API 和 UI 端點
Prometheus 支援對到 Prometheus 例項(即表示式瀏覽器或 HTTP API)的連線進行 傳輸層安全 (TLS) 加密。如果您希望對這些連線強制執行 TLS,則需要建立一個特定的 Web 配置檔案。
注意本指南介紹的是到 Prometheus 例項的 TLS 連線。此外,Prometheus 例項到抓取目標的連線也支援 TLS。
前提條件
假設您已經有一個正在執行的 Prometheus 例項,並且想要對其進行調整。本指南將不涉及 Prometheus 的初始安裝設定。
假設您想執行一個使用 TLS 的 Prometheus 例項,並且可以透過您擁有的 example.com 域名進行訪問。
同樣假設您已經使用 OpenSSL 或類似工具生成了以下內容
- 位於
/home/prometheus/certs/example.com/example.com.crt的 SSL 證書 - 位於
/home/prometheus/certs/example.com/example.com.key的 SSL 金鑰
您可以使用此命令生成自簽名證書和私鑰
mkdir -p /home/prometheus/certs/example.com && cd /home/prometheus/certs/certs/example.com
openssl req \
-x509 \
-newkey rsa:4096 \
-nodes \
-keyout example.com.key \
-out example.com.crt
根據提示填寫相應資訊,並確保在 Common Name 提示處輸入 example.com。
Prometheus 配置
以下是一個 web-config.yml 配置檔案示例。透過此配置,Prometheus 將在 TLS 保護下提供其所有端點服務。
tls_server_config:
cert_file: /home/prometheus/certs/example.com/example.com.crt
key_file: /home/prometheus/certs/example.com/example.com.key
要讓 Prometheus 使用此配置,您需要在啟動時加上 --web.config.file 標誌。
prometheus \
--config.file=/path/to/prometheus.yml \
--web.config.file=/path/to/web-config.yml \
--web.external-url=https://example.com/
這裡的 --web.external-url= 標誌是可選的。
測試
如果您想在本地使用 example.com 域名測試 TLS,可以向 /etc/hosts 檔案新增一條記錄,將 example.com 重定向到 localhost
127.0.0.1 example.com
然後您可以使用 cURL 與本地 Prometheus 例項進行互動
curl --cacert /home/prometheus/certs/example.com/example.com.crt \
https://example.com/api/v1/label/job/values
您可以使用 --insecure 或 -k 標誌,在不指定證書的情況下連線到 Prometheus 伺服器
curl -k https://example.com/api/v1/label/job/values