想象一下,你的 Kubernetes 集群正在平稳运行,突然某个关键业务开始报错,排查半天才发现是 Admission Webhooks 的证书过期了,手动更新证书不仅麻烦,还容易遗漏,尤其是在大规模集群中,更糟的是,证书过期可能导致 Webhooks 失效,进而影响 Pod 创建、配置验证等核心功能。
这时候,Cert-manager 就能派上用场了,它不仅能自动签发和续期 TLS 证书,还能无缝集成 Admission Webhooks,彻底告别手动管理证书的烦恼。
Admission Webhooks 是 Kubernetes 的一种扩展机制,允许在资源创建或修改时进行自定义验证或变更,为了保证通信安全,Kubernetes 要求 Webhook 服务必须使用 HTTPS,这意味着你需要:
手动管理这些步骤不仅繁琐,还容易出错,Cert-manager 可以自动化整个过程,确保证书始终有效。
Cert-manager 是一个流行的 Kubernetes 原生证书管理工具,支持从多种来源(如 Let's Encrypt、私有 CA)自动签发和更新证书,对于 Admission Webhooks,它可以:
如果你还没安装 Cert-manager,可以用 Helm 快速部署:
helm repo add jetstack https://charts.jetstack.io helm install cert-manager jetstack/cert-manager \ --namespace cert-manager \ --create-namespace \ --set installCRDs=true
Cert-manager 需要一个 Issuer 来签发证书,这里以自签名 CA 为例(生产环境建议使用 Let's Encrypt 或企业 CA):
apiVersion: cert-manager.io/v1 kind: Issuer metadata: name: selfsigned-issuer namespace: your-webhook-namespace spec: selfSigned: {}
定义一个 Certificate 资源,告诉 Cert-manager 为 Webhook 生成证书:
apiVersion: cert-manager.io/v1 kind: Certificate metadata: name: webhook-cert namespace: your-webhook-namespace spec: secretName: webhook-tls-secret # 证书会存储在这个 Secret 中 dnsNames: - your-webhook-service.your-webhook-namespace.svc # Webhook 服务的 DNS 名称 issuerRef: name: selfsigned-issuer kind: Issuer
在 ValidatingWebhookConfiguration 或 MutatingWebhookConfiguration 中引用 Cert-manager 生成的证书:
apiVersion: admissionregistration.k8s.io/v1 kind: ValidatingWebhookConfiguration metadata: name: your-webhook webhooks: - name: your-webhook.example.com clientConfig: service: name: your-webhook-service namespace: your-webhook-namespace path: "/validate" caBundle: {{ .caBundle }} # 这里需要填充 CA 证书
Cert-manager 可以通过 CA Injector 自动将 CA 证书注入 Webhook 配置,只需在 Certificate 中添加注解:
apiVersion: cert-manager.io/v1 kind: Certificate metadata: name: webhook-cert annotations: cert-manager.io/inject-ca-from: your-webhook-namespace/webhook-cert
这样,Cert-manager 会自动更新 caBundle
字段,无需手动干预。
Webhooks 需要对外暴露,可以使用 Let's Encrypt 签发受信任的证书:
apiVersion: cert-manager.io/v1 kind: ClusterIssuer metadata: name: letsencrypt-prod spec: acme: server: https://acme-v02.api.letsencrypt.org/directory email: your-email@example.com privateKeySecretRef: name: letsencrypt-prod-account-key solvers: - http01: ingress: class: nginx
然后在 Certificate 中引用:
apiVersion: cert-manager.io/v1 kind: Certificate metadata: name: webhook-cert spec: secretName: webhook-tls-secret issuerRef: name: letsencrypt-prod kind: ClusterIssuer
通过 Cert-manager,你可以:
✅ 自动签发和续期 Webhook 证书,避免手动管理
✅ 无缝集成 CA 注入,无需手动更新 caBundle
✅ 支持多种证书来源(自签名、Let's Encrypt、私有 CA)
✅ 提升集群可靠性,减少因证书过期导致的故障
如果你的集群还在手动管理 Webhook 证书,现在就是时候切换到 Cert-manager 了!
本文由 邴晗日 于2025-08-10发表在【云服务器提供商】,文中图片由(邴晗日)上传,本平台仅提供信息存储服务;作者观点、意见不代表本站立场,如有侵权,请联系我们删除;若有图片侵权,请您准备原始证明材料和公证书后联系我方删除!
本文链接:https://up.7tqx.com/wenda/581228.html
发表评论