使用 TLS 加密保护 Prometheus API 与 UI 端点
Prometheus 支持对到 Prometheus 实例(即表达式浏览器或 HTTP API)的连接进行 TLS 加密。如果你希望强制在这些连接上使用 TLS,则需要创建特定的网络配置文件。
注意:本指南主要关于到 Prometheus 实例的 TLS 连接。Prometheus 也支持从 Prometheus 实例到抓取 Target 的 TLS 连接。
前提条件
假设你已经有一个运行中的 Prometheus 实例,并希望对其进行调整。本指南不会覆盖初始 Prometheus 设置。
假设你想要使用 TLS 在example.com
(你拥有的域名)上运行 Prometheus 实例。
此外,请确保你使用 OpenSSL 或类似工具生成了以下内容:
- 证书文件位于
/home/prometheus/certs/example.com/example.com.crt
- 私钥文件位于
/home/prometheus/certs/example.com/example.com.key
你可以使用此命令生成自签名证书和私钥:
mkdir -p /home/prometheus/certs/example.com && cd /home/prometheus/certs/example.comopenssl req \ -x509 \ -newkey rsa:4096 \ -nodes \ -keyout example.com.key \ -out example.com.crt
在提示中填写适当的详细信息,并确保在<font style="color:rgb(51, 51, 51);background-color:rgb(249, 242, 244);">Common Name</font>
提示中输入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
该文档基于 Prometheus 官方文档翻译而成。