← 中文资源

简体中文版本

在 TLS 证书过期前告警

通过 Prometheus 规则监控 cert-manager 的续期状态与剩余有效期,让失败的续期在真正故障之前通知到值班人员。

阅读英文原文

概述

TLS 证书过期是少数有明确日期的故障,因此不可接受。cert-manager 已经记录了每个 Certificate 的过期时间与就绪状态,缺少的是在仍有处理时间时触发的告警。剩余 21 天的告警可以覆盖首次续期失败的情况,就绪状态告警则能在失败当天暴露原因。两者结合,把午夜故障变成提前一周的工单。

步骤

  1. 对剩余有效期不足 21 天的证书告警:
- alert: CertificateExpiringSoon
  expr: certmanager_certificate_expiration_timestamp_seconds - time() < 21 * 24 * 3600
  for: 1h
  labels:
    severity: warning
  annotations:
    summary: "Certificate {{ $labels.name }} expires in under 21 days"
  1. 对 cert-manager 报告为未就绪的证书告警:
- alert: CertificateNotReady
  expr: certmanager_certificate_ready_status{condition="False"} == 1
  for: 30m
  labels:
    severity: critical
  annotations:
    summary: "Certificate {{ $labels.name }} in {{ $labels.namespace }} is not ready"
  1. 检查具体证书并按需触发续期:
cmctl status certificate app-example-tls -n prod
cmctl renew app-example-tls -n prod
  1. 在临时命名空间用短有效期证书验证告警链路。

验证

cmctl status 应显示 issuer、计划续期时间与失败原因;cmctl renew 后应看到新的签发流程。测试证书触发告警时,通知中应带有正确的 name 与 namespace 标签,并能在值班渠道中收到。若指标为空,说明 cert-manager 的 metrics 端口未被采集,应先补齐 ServiceMonitor,而不是调整阈值。

注意事项

  • cert-manager 自身故障时指标会消失,必须额外部署外部 TLS 探测。
  • 有效期极短的证书应按比例设置阈值,固定 21 天不适用。
  • 续期成功但 Ingress 未加载新 Secret 时,只有外部探测能发现。
  • ACME 账户密钥与 DNS 凭证需要定期轮换,它们是静默失败的主要原因。
  • 告警必须配套处理手册,否则只会被长期静默。