使用 TLS 加密保护 Prometheus API 和 UI 端点
Prometheus 支持 传输层安全 (TLS) 加密,用于连接到 Prometheus 实例(即表达式浏览器或 HTTP API)。如果您想强制对这些连接使用 TLS,则需要创建一个特定的 Web 配置文件。
注意本指南介绍的是 *到* Prometheus 实例的 TLS 连接。Prometheus 实例 *到* 抓取目标 的连接也支持 TLS。
前提条件
假设您已经有一个正在运行的 Prometheus 实例,并且您想对其进行改造。本指南不涵盖 Prometheus 的初始设置。
假设您想运行一个通过 TLS 提供服务的 Prometheus 实例,该实例可在您拥有的 example.com
域下访问。
另外,假设您已使用 OpenSSL 或类似工具生成了以下文件
- 一个 SSL 证书,位于
/home/prometheus/certs/example.com/example.com.crt
- 一个 SSL 密钥,位于
/home/prometheus/certs/example.com/example.com.key
您可以使用此命令生成自签名证书和私钥
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`](/docs/prometheus/latest/configuration/https/) 配置文件。通过此配置,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