带Istio Sidecar的Pod的Prometheus指标采集配置问题(含PodMonitor配置报错排查)
嘿,我来帮你搞定这个Istio环境下的Prometheus指标采集问题!咱们先来拆解下你遇到的报错原因,再一步步给出解决方案。
为啥会出现x509证书验证错误?
你碰到的x509: cannot validate certificate for 192.168.3.29 because it doesn't contain any IP SANs错误,核心原因是Istio的MTLS证书是绑定Pod的DNS全名(FQDN)签发的,而不是Pod的IP地址。当你用IP直接访问metrics端口时,TLS验证环节会检查证书里的SAN字段,发现没有这个IP,自然就报错了。
修正后的PodMonitor配置
要解决这个问题,我们需要让Prometheus用Pod的DNS名称去访问,同时配置TLS验证规则适配Istio的证书。这里给你两种配置方案:
方案1:生产环境推荐(验证Istio证书)
这个方案会让Prometheus信任Istio的CA证书,同时指定服务器名称匹配Pod的FQDN:
apiVersion: monitoring.coreos.com/v1 kind: PodMonitor metadata: name: mymonitor labels: release: podwithistiosidecar spec: selector: matchLabels: app: podwithistiosidecar podMetricsEndpoints: - port: "metrics" path: "/metrics" scheme: https tlsConfig: # 挂载Istio CA证书的路径,后面会讲怎么给Prometheus挂载这个证书 caFile: /etc/prometheus/secrets/istio-ca/root-cert.pem # 指定Pod的FQDN通配符,格式为 *.你的命名空间.pod.cluster.local serverName: "*.your-namespace.pod.cluster.local"
方案2:测试环境临时用(跳过证书验证)
如果只是测试阶段不想折腾证书配置,可以临时关闭证书验证,但生产环境绝对不推荐,会失去MTLS的安全保障:
apiVersion: monitoring.coreos.com/v1 kind: PodMonitor metadata: name: mymonitor labels: release: podwithistiosidecar spec: selector: matchLabels: app: podwithistiosidecar podMetricsEndpoints: - port: "metrics" path: "/metrics" scheme: https tlsConfig: insecureSkipVerify: true
那Prometheus服务器需要更新配置吗?
是的,如果用方案1(验证Istio证书),你需要给Prometheus Pod挂载Istio的CA证书Secret,步骤如下:
如果你的Prometheus是用Prometheus Operator部署的,修改Prometheus资源的配置,添加卷和挂载:
apiVersion: monitoring.coreos.com/v1 kind: Prometheus metadata: name: k8s namespace: monitoring spec: # 保留你原有的其他配置 volumes: - name: istio-ca secret: # Istio的CA证书Secret,默认在每个命名空间下都有,或者从istio-system拉取 secretName: istio-ca-secret optional: true volumeMounts: - name: istio-ca mountPath: /etc/prometheus/secrets/istio-ca readOnly: true
配置完成后,Prometheus会自动重启,挂载上Istio的CA证书,就能正常验证Pod的MTLS证书了。
额外小提示
如果你的应用metrics端口没有被Istio纳入服务网格管理(也就是Pod的容器端口没有被Istio Sidecar接管),其实也可以尝试用scheme: http去采集,但这种情况比较少见——毕竟你已经开启了全局MTLS,大概率所有端口都会被Istio保护。
备注:内容来源于stack exchange,提问作者Nipun Talukdar




