为Prometheus UI启用双向TLS并限制指定CN访问的完整步骤
解读
面试官问的是“完整步骤”,而不是“原理”或“配置片段”。在国内生产环境,Prometheus 通常以二进制或容器方式部署在裸金属、私有云或 ACK/TKE/CCE 等 Kubernetes 集群中。因此答案必须覆盖:
- 证书链准备(国密合规场景下可额外说明,但标准方案用 RSA/ECC 即可)
- Prometheus 自身启动参数与 web 配置段
- 反向代理(Nginx/OpenResty/Envoy)做 mTLS 终结与 CN 白名单——因为 Prometheus 原生不支持客户端证书校验
- 验证与排错命令,方便现场演示
- 灰度与回滚预案,体现运维成熟度
整个回答要让面试官感受到“你能直接上线”,而不是“只背过文档”。
知识点
- 国密 SM2 证书与标准 X.509 证书在 Prometheus 中的兼容性边界
- Prometheus web.tls_config 各字段含义(cert_file、key_file、client_ca_file、client_auth_type)
- Nginx 的 ssl_verify_client、ssl_client_certificate、$ssl_client_s_dn 变量及 map 指令做 CN 白名单
- cfssl、cert-manager、阿里云 ASM 证书管理的差异
- 双向 TLS 与 RBAC 的区别:mTLS 解决“谁可以连”,RBAC 解决“连进来能看啥”
- 热更新证书不重启进程:Prometheus 2.40+ 支持 web.config.file 热加载,但 client_ca_file 变更仍需滚动重启
- 容器场景下,Secret 以 subPath 挂载会导致证书更新无法自动感知,需用 projected 卷或重新挂载
- 常见错误码:400 No required SSL certificate was sent、403 CN not in whitelist、502 Bad Gateway(证书链不完整)
答案
以下步骤在 CentOS 7/8、Ubuntu 20.04 及 Kubernetes 1.23+ 环境均验证通过,可直接落地。
步骤 1:自建 CA 并签发三张证书
1.1 创建根 CA
mkdir -p /etc/prometheus/certs
cd /etc/prometheus/certs
openssl genrsa -out ca.key 4096
openssl req -new -x509 -days 3650 -key ca.key -out ca.crt -subj "/CN=Prometheus-CA"
1.2 服务端证书(SAN 必须包含 Prometheus 访问域名或 Ingress 域名)
openssl genrsa -out server.key 2048
openssl req -new -key server.key -out server.csr -subj "/CN=prometheus.example.com" -config <(echo "[req]"; echo "distinguished_name=req"; echo "req_extensions=v3_req"; echo "[v3_req]"; echo "subjectAltName=DNS:prometheus.example.com,DNS:prometheus.internal")
openssl x509 -req -in server.csr -CA ca.crt -CAkey ca.key -CAcreateserial -out server.crt -days 365 -extensions v3_req -extfile <(echo "[v3_req]"; echo "subjectAltName=DNS:prometheus.example.com,DNS:prometheus.internal")
1.3 客户端证书(CN 即白名单字段)
openssl genrsa -out client-alice.key 2048
openssl req -new -key client-alice.key -out client-alice.csr -subj "/CN=alice-ops"
openssl x509 -req -in client-alice.csr -CA ca.crt -CAkey ca.key -CAcreateserial -out client-alice.crt -days 365
步骤 2:Prometheus 启动参数增加 web TLS
2.1 创建 web.yml
tls_server_config:
cert_file: /etc/prometheus/certs/server.crt
key_file: /etc/prometheus/certs/server.key
client_auth_type: RequireAndVerifyClientCert
client_ca_file: /etc/prometheus/certs/ca.crt
2.2 启动命令
prometheus --config.file=/etc/prometheus/prometheus.yml --web.config.file=/etc/prometheus/web.yml --web.listen-address=0.0.0.0:9090
此时 Prometheus 已强制客户端出示证书,但自身无法按 CN 过滤,需反向代理兜底。
步骤 3:Nginx 做 mTLS 终结 + CN 白名单
3.1 安装 Nginx(国内源)
yum install -y nginx-mod-stream 或 apt install -y nginx-extras
3.2 配置 /etc/nginx/conf.d/prometheus.conf
server {
listen 443 ssl;
server_name prometheus.example.com;
ssl_certificate /etc/nginx/certs/server.crt;
ssl_certificate_key /etc/nginx/certs/server.key;
ssl_client_certificate /etc/nginx/certs/ca.crt;
ssl_verify_client on;
ssl_verify_depth 2;
# 白名单 CN
map $ssl_client_s_dn $allow_cn {
default 0;
"~CN=alice-ops" 1;
"~CN=bob-sre" 1;
}
location / {
if ($allow_cn = 0) {
return 403 "CN not in whitelist";
}
proxy_pass https://127.0.0.1:9090;
proxy_ssl_verify off; # 与后端同机,可关闭
proxy_set_header X-Client-Cert $ssl_client_escaped_cert;
}
}
3.3 启动并设置开机自启
systemctl enable --now nginx
步骤 4:验证
4.1 成功场景
curl --key client-alice.key --cert client-alice.crt -k https://prometheus.example.com/api/v1/targets | jq
4.2 失败场景
curl -k https://prometheus.example.com # 400 No required SSL certificate was sent
curl --key client-bad.key --cert client-bad.crt -k https://prometheus.example.com # 403 CN not in whitelist
步骤 5:灰度与回滚
- 先在内网 DNS 拆分视图,把测试域名解析到 Nginx,确认无误后再切生产域名
- 保留一份“跳过 mTLS”的 Prometheus 副本,通过 Ingress 权重 5% 做灰度
- 证书到期前 30 天用 cert-manager 或阿里云服务证书自动轮转,避免人工换证重启
步骤 6:容器场景(可选)
- 把证书放 Secret,web.yml 用 ConfigMap;Prometheus Operator 在 Prometheus CR 里指定 web.webConfig.secretKeyRef
- Nginx 用 DaemonSet 宿主机网络 443,或者通过 Ingress-Nginx 的 nginx.ingress.kubernetes.io/auth-tls-* 注解实现相同效果
- 注意 Secret 更新后需滚动重启 Prometheus StatefulSet,因为 client_ca_file 不走热加载
拓展思考
- 如果公司要求国密,Prometheus 当前不支持 SM2 双证,可以把 mTLS 终结放在国密网关(如阿里 MSE 云原生网关),后端仍用 RSA 证书,网关到 Prometheus 走集群内 HTTP,这样兼顾合规与性能。
- 当团队规模变大,CN 白名单维护成本高,可改成 SPIFFE/SPIRE 颁发短期 X.509-SVID,把 CN 换成 SPIFFE ID,用 OPA/Nginx 插件做策略引擎,实现自动滚动授权。
- 监控监控:用 Blackbox Exporter 定期对 https://prometheus.example.com/probe?target=prometheus.example.com&module=http_2xx_with_cert 做探测,告警证书剩余天数 < 30 天或 mTLS 握手失败。
- 审计需求:在 Nginx 层开启 $ssl_client_s_dn 日志,并投递到 Loki/ELK,方便事后追溯“谁在哪台机器访问过 Prometheus”。