通过SPIFFE ID在Istio网格内实现Prometheus到Exporter的mTLS身份认证
解读
国内云原生面试中,这道题把“Prometheus 采集”与“零信任网络”两大热点揉在一起,重点考察候选人是否真正落地过 Istio 全链路 mTLS,而不仅是“会写 ServiceMonitor”。
面试官想听到的是:
- 你清楚 Istio 如何把 SPIFFE ID 注入到证书 SAN 字段;
- 你知道 Prometheus 在 Pull 场景下如何完成双向 TLS 握手,而不是简单地把 CA 挂进去;
- 你能给出可复制的配置片段,并解释每个字段对身份认证的影响;
- 你能预判证书轮换、端口冲突、Headless Service 等线上坑点。
如果答案只停留在“把 mode: STRICT 一配就行”,会被直接判定为“纸上谈兵”。
知识点
- SPIFFE/SPIRE 标准:SVID(X.509 证书)格式、SPIFFE ID 固定 URI 形式 spiffe://trust-domain/ns/namespace/sa/service-account。
- Istio CSR 流程:istio-agent → istiod → k8s ServiceAccount Token → 签发短周期证书(默认 24h),SAN 带 SPIFFE ID。
- Prometheus Pull 与 mTLS:
- Prometheus 本身不感知 SPIFFE,需要借助“TLS 配置段”完成双向校验;
- 必须提供:
– ca_file:信任链,验证对端证书由同一个 mesh CA 签发;
– cert_file / key_file:Prometheus 自身身份,SAN 里同样要有 SPIFFE ID;
– server_name:可选,若 exporter 使用 Headless Service 需显式匹配 SPIFFE ID。
- Istio 自动 TLS 透传:
- Sidecar 拦截 outbound 流量,自动附加客户端证书;
- Prometheus 若跑在 Sidecar 模式,可直接用“文件挂载”方式读取 /etc/certs 下的动态文件(软链到 Envoy SDS)。
- 端口隔离:
- 业务容器 8080 暴露指标,istio-proxy 15090 暴露 envoy-stats;
- Prometheus 抓取目标必须指向业务端口,而不是 15090,否则证书 SAN 对不上。
- 证书热更新:SDS 下发新证书后,Prometheus 需要 reload 配置;官方 2.45+ 支持 –enable-feature=expand-env –web.enable-lifecycle,配合 file_sd 热更新。
- 策略对象:
- PeerAuthentication:mode: STRICT 强制 mTLS;
- DestinationRule:trafficPolicy.tls.mode: ISTIO_MUTUAL,声明使用 Istio 自签证书;
- AuthorizationPolicy:可基于 principals=["cluster.local/ns/foo/sa/prometheus"] 做细粒度放行。
- 国内环境差异:
- 阿里/腾讯/华为托管网格默认关闭 istiod 的 CA,需手动开启“网格证书管理”插件;
- 金融合规场景要求国密双证,需把 istio 的插件 CA 替换为云厂商的国密中间 CA,此时 SPIFFE ID 仍保持一致。
答案
下面给出一套可直接落地的“最小可运行”配置,并逐行解释其与 SPIFFE ID 身份认证的关联。
步骤 1:确认全局 mTLS 已开启
kubectl apply -f - <<EOF
apiVersion: security.istio.io/v1beta1
kind: PeerAuthentication
metadata:
name: default
namespace: istio-system
spec:
mtls:
mode: STRICT
EOF
说明:STRICT 模式会拒绝所有非 mTLS 连接,保证后续握手一定携带证书,SAN 里一定带 SPIFFE ID。
步骤 2:为 Prometheus 创建独立 ServiceAccount
kubectl create sa prometheus -n monitoring
该 SA 的 SPIFFE ID 固定为 spiffe://cluster.local/ns/monitoring/sa/prometheus,后续 AuthorizationPolicy 直接匹配即可。
步骤 3:部署 Prometheus(Sidecar 注入开启)
apiVersion: apps/v1
kind: Deployment
metadata:
name: prometheus
namespace: monitoring
spec:
template:
metadata:
annotations:
sidecar.istio.io/inject: "true"
spec:
serviceAccountName: prometheus
containers:
- name: prometheus
image: prom/prometheus:v2.45.0
args:
- "--config.file=/etc/prometheus/prometheus.yml"
- "--web.enable-lifecycle"
volumeMounts:
- name: istio-certs
mountPath: /etc/prometheus/certs
readOnly: true
volumes:
- name: istio-certs
projected:
sources:
- secret:
name: prometheus-token-istio
items:
- key: ca.crt
path: ca.crt
- key: tls.crt
path: cert.crt
- key: tls.key
path: key.pem
说明:
- 通过 projected volume 把 istio-agent 生成的证书挂到 Prometheus 容器;
- 文件路径与下一步 prometheus.yml 中的 tls_config 保持一致;
- 证书 24h 轮换,无需重启 Pod,istio-agent 负责更新文件。
步骤 4:编写 prometheus.yml,启用双向 TLS
scrape_configs:
- job_name: 'istio-mtls-app'
kubernetes_sd_configs:- role: pod
namespaces:
names: ['prod']
relabel_configs:
只保留带指标端口 8080 且注解 prometheus.io/scrape=true 的 Pod
- source_labels: [__meta_kubernetes_pod_annotation_prometheus_io_scrape]
action: keep
regex: true - source_labels: [__meta_kubernetes_pod_annotation_prometheus_io_port]
action: replace
target_label: address
regex: (.+)
replacement: ${1}:8080
显式指定使用 https 方案
- target_label: scheme
replacement: https
tls_config:
ca_file: /etc/prometheus/certs/ca.crt
cert_file: /etc/prometheus/certs/cert.crt
key_file: /etc/prometheus/certs/key.pem验证对端 SAN,防止 DNS 劫持
server_name: '%{meta_kubernetes_pod_name}.%{meta_kubernetes_namespace}.pod.cluster.local'
可选:跳过证书时间校验,方便调试
insecure_skip_verify: false
- role: pod
说明:
- ca_file 用于校验 exporter 的证书是否由同一个 mesh CA 签发;
- cert/key 证明 Prometheus 自身身份,SAN 里带 spiffe://cluster.local/ns/monitoring/sa/prometheus;
- server_name 写成 Pod 级 FQDN,与 exporter 证书 SAN 中的 SPIFFE ID 对应,保证双向校验通过。
步骤 5:给 exporter 端加上 AuthorizationPolicy
kubectl apply -f - <<EOF
apiVersion: security.istio.io/v1beta1
kind: AuthorizationPolicy
metadata:
name: allow-prometheus
namespace: prod
spec:
selector:
matchLabels:
app: my-app
rules:
- from:
- source:
principals: ["cluster.local/ns/monitoring/sa/prometheus"]
EOF
说明:
- source:
- 只有携带 spiffe://cluster.local/ns/monitoring/sa/prometheus 的客户端才能访问 prod 命名空间下 my-app 的 8080 端口;
- 如果未匹配,istio-proxy 会返回 RBAC 403,Prometheus 端看到“connection reset by peer”。
步骤 6:验证
- 查看 Prometheus UI,Target 页面状态为 UP;
- istioctl authn tls-check my-app.prod.svc.cluster.local 8080 返回 “OK” 且 “mTLS: yes”;
- 在 istio-proxy 日志里能看到 “RBAC allowed, matched policy allow-prometheus”。
至此,Prometheus 到 Exporter 的整条链路已完成基于 SPIFFE ID 的 mTLS 身份认证,且具备可观测、可灰度、可回滚的线上能力。
拓展思考
- 大规模场景下,每个 Prometheus 实例都走 SDS 文件挂载会导致 inode 耗尽,可考虑:
- 使用 Prometheus-Operator 的 PrometheusAgent 模式,直接复用 istio-proxy 的 SDS socket;
- 或者把 Prometheus 移出网格,通过“ egress-gateway + TLS Origination ”统一收口,网格外只需维护一份网关证书。
- 多集群联邦:
- 集群间 trust-domain 不同,SPIFFE ID 分别为 cluster-a 与 cluster-b,需要配置 Istio 的 “federation CA” 插件做跨域信任;
- 联邦 Prometheus 抓取远程集群时,要在 tls_config 里额外提供 server_name 别名,否则 SAN 校验失败。
- 国密合规:
- 把 istiod 的 CA 插件替换为支持 SM2 的“云加密服务”,证书里仍保留 SPIFFE ID,Prometheus 端需使用国密版 OpenSSL 库重新编译;
- 国密场景下,Prometheus 的 tls_config 需要指定 cipher_suite,禁用 ECDHE-RSA 系列算法。
- 零信任审计:
- 利用 Istio 的 Telemetry API 把 RBAC 拒绝日志打到 Loki,再让 Prometheus 自己监控“istio_request_total{response_code=403}”指标,实现“监控系统的自监控”。