DigiCert SSL 证书在 Kubernetes 中的安装部署与自动续期实战
问题背景与根因
在 Kubernetes 中,DigiCert SSL 证书不是直接装到 Node 上,而是以 Secret 形式存放在命名空间,由 Ingress Controller 在 TLS 终止时加载。常见故障不是证书没签发,而是链不完整、Secret 与 Ingress 不在同一命名空间、更新后控制器仍用旧缓存、私钥与证书不匹配。DigiCert 是商业 CA,CertCentral 支持 API 与批量管理,EV/OV 审核严格,但浏览器对 EV 展示已弱化,部署仍以链完整为先。通过 CertCentral 交付的证书通常包含叶子证书和中间 CA,部署时必须把叶子证书与中间 CA 拼成 fullchain.pem,否则浏览器可能正常,但 curl、Java、小程序会报 unknown authority。还应注意:公共 TLS 证书最长有效期已限制为 398 天,Let’s Encrypt 为 90 天,续期必须纳入流程。根因清单:只上传 example.com.crt 未带 DigiCertCA.crt;Secret 在 default,Ingress 在 production;更新 Secret 后未确认 ingress-nginx 是否监听;私钥有 passphrase 或与证书不匹配;仍使用过期证书。
准备 DigiCert 证书文件并校验
从 CertCentral 下载的压缩包通常包含 example.com.crt、DigiCertCA.crt、example.com.key。先在签发服务器上统一为 PEM 格式并拼接完整链:
cat example.com.crt DigiCertCA.crt > fullchain.pem
openssl x509 -in example.com.crt -noout -subject -issuer -dates -ext subjectAltName
openssl rsa -in example.com.key -noout -check
openssl x509 -in example.com.crt -noout -modulus | openssl sha256
openssl rsa -in example.com.key -noout -modulus | openssl sha256
两条 modulus 的 sha256 必须一致,否则说明证书与私钥不匹配。若私钥带口令,Ingress 无法直接加载,需解密:
openssl rsa -in encrypted.key -out example.com.key
验证链是否可被系统信任:
openssl verify -CAfile /etc/ssl/certs/ca-certificates.crt -untrusted DigiCertCA.crt example.com.crt
输出 example.com.crt: OK 才可继续。fullchain.pem 的顺序必须是叶子证书在前、中间 CA 在后,不要把根证书重复拼入;DigiCert 根证书通常已在客户端信任库。若使用 EV 或 OV 证书,subject 中 O、CN 应与业务域名一致,SAN 必须覆盖 Ingress 的 host。
创建与滚动更新 Kubernetes TLS Secret
Secret 必须与 Ingress 同命名空间。创建命令:
kubectl create namespace production --dry-run=client -o yaml | kubectl apply -f -
kubectl create secret tls example-com-tls --cert=fullchain.pem --key=example.com.key -n production
首次创建后检查类型和字段:
kubectl get secret example-com-tls -n production
kubectl get secret example-com-tls -n production -o jsonpath='{.type}'
kubectl get secret example-com-tls -n production -o jsonpath='{.data.tls.crt}' | base64 -d | openssl x509 -noout -subject -issuer -dates -serial
续期后不要直接 delete Secret,推荐 dry-run 生成 YAML 后 apply,实现滚动替换:
kubectl create secret tls example-com-tls --cert=fullchain.pem --key=example.com.key -n production --dry-run=client -o yaml | kubectl apply -f -
kubectl get secret example-com-tls -n production -o jsonpath='{.data.tls.crt}' | base64 -d | openssl x509 -noout -serial -dates
若提示 error: failed to load key pair: tls: failed to find any PEM data in certificate input,说明 –cert 指向的不是 PEM 或链文件为空;若提示 AlreadyExists,不要反复 create,改用上面的 apply。更新后多数 ingress-nginx 会监听 Secret 变化;若控制器未生效,再执行 kubectl rollout restart deployment/ingress-nginx-controller -n ingress-nginx。
Ingress 配置示例与命名空间约束
以下 Ingress 使用 networking.k8s.io/v1,引用 production 命名空间中的 Secret:
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: example-ingress
namespace: production
annotations:
nginx.ingress.kubernetes.io/ssl-redirect: 'true'
spec:
ingressClassName: nginx
tls:
- hosts:
- example.com
secretName: example-com-tls
rules:
- host: example.com
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: web
port:
number: 80
应用并检查事件:
kubectl apply -f ingress.yaml
kubectl describe ingress example-ingress -n production
kubectl get ingress example-ingress -n production -o wide
关键点:tls.hosts 必须与 rules.host 一致;secretName 只写名称,不能写命名空间;Ingress 与 Secret 不同命名空间时不会跨命名空间引用,必须把 Secret 复制到同一命名空间。若使用多个域名,在 tls.hosts 和 rules 中逐一列出,并确保 DigiCert 证书 SAN 覆盖所有域名。ssl-redirect 只负责 HTTP 跳转 HTTPS,不负责证书链修复。
自动签发与续期:cert-manager 与 CertCentral
如果使用 Let’s Encrypt,可直接用 cert-manager ACME Issuer,证书有效期 90 天;若使用 DigiCert 公共 TLS 证书,有效期最长 398 天,建议在到期前 30 天完成续期。cert-manager 的 Certificate 资源可统一管理 Secret:
apiVersion: cert-manager.io/v1
kind: Certificate
metadata:
name: example-com
namespace: production
spec:
secretName: example-com-tls
renewBefore: 720h
dnsNames:
- example.com
issuerRef:
name: letsencrypt-prod
kind: ClusterIssuer
检查状态:
kubectl get certificate -n production
kubectl describe certificate example-com -n production
kubectl get certificaterequest,order,challenge -n production
Ready=True 表示 Secret 已生成。DigiCert 场景可通过 CertCentral API 或 cert-manager 的 Venafi Issuer 对接 DigiCert TLS Protect Cloud(原 Venafi)完成自动化;若采用 API 下载,建议 CronJob 在到期前 30 天执行下载,再用 kubectl create secret tls … –dry-run=client -o yaml | kubectl apply -f – 更新。注意不要把 CertCentral API Key 写入 Git,使用 Secret 挂载环境变量,并限制 RBAC 仅能更新目标命名空间的 Secret。
部署方案对比表
| 方案 | 适用场景 | 续期方式 | 注意事项 |
|---|---|---|---|
| 手动 Secret + Ingress | 少量 DigiCert 证书、审批流固定 | 人工下载 fullchain 后 dry-run apply | 易漏中间链;398 天到期需日历提醒 |
| cert-manager + ACME(Let’s Encrypt) | 测试或互联网业务快速启用 | 自动,90 天周期 | 需公网可达;DigiCert 品牌/EV 需求不适用 |
| cert-manager + Venafi Issuer 对接 DigiCert TLS Protect Cloud | 已采购 DigiCert 且需自动化 | 自动续期 | 需配置 Issuer 凭据与策略 |
| CertCentral API + CronJob | 已有 CertCentral 批量管理/审批 | 定时下载并更新 Secret | 需处理 API 权限、失败告警、Secret RBAC |
选择原则:单证书、审批流固定选手动 Secret;互联网业务快速上线选 cert-manager + ACME;已采购 DigiCert 且需品牌/EV 选 CertCentral API 或 Venafi Issuer;无论哪种,都要在到期前 30 天验证 Secret 中证书 serial 已变化。
常见错误排查与真实报错
1. x509: certificate signed by unknown authority 或 Java 报 PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target。根因是只部署了叶子证书,未带 DigiCert 中间 CA。修复:cat example.com.crt DigiCertCA.crt > fullchain.pem 后重建 Secret,并用 openssl s_client -connect example.com:443 -servername example.com -showcerts </dev/null 检查返回链是否包含中间证书。
2. SSL_CTX_use_PrivateKey() failed (SSL: error:0B080074:x509 certificate routines:X509_check_private_key:key values mismatch)。证书与私钥不匹配。执行 openssl x509 -in example.com.crt -noout -modulus | openssl sha256 与 openssl rsa -in example.com.key -noout -modulus | openssl sha256,两条结果必须一致。
3. error: failed to load key pair: tls: failed to find any PEM data in certificate input。–cert 文件不是 PEM 或为空。用 file fullchain.pem、head -n 1 fullchain.pem 检查是否为 —–BEGIN CERTIFICATE—–;CRT 转 PEM 可用 openssl x509 -in example.com.crt -out example.com.pem。
4. Ingress 事件提示 secret example-com-tls does not exist。Secret 与 Ingress 不在同一命名空间,或名称拼错。执行 kubectl get secret -n production | grep example-com-tls 核对。
5. 续期后 curl 仍显示旧证书。先 kubectl get secret example-com-tls -n production -o jsonpath='{.data.tls.crt}’ | base64 -d | openssl x509 -noout -serial -dates 确认 Secret 已更新;再检查 Ingress Controller 缓存,必要时 kubectl rollout restart deployment/ingress-nginx-controller -n ingress-nginx。若 cert-manager 报 Certificate is not ready: secret example-com-tls not found,检查 Certificate 的 namespace 与 secretName。
效果验证
从集群内和集群外分别验证:
kubectl get ingress,secret -n production
kubectl get certificate -n production
kubectl describe certificate example-com -n production | grep -E 'Ready|Not After|Renewal Time'
curl -vI https://example.com
openssl s_client -connect example.com:443 -servername example.com -showcerts /dev/null | openssl x509 -noout -subject -issuer -dates -serial
openssl s_client -connect example.com:443 -servername example.com -verify_return_error </dev/null
预期:curl 返回 HTTP/2 200 或 301,证书 subject 为 example.com,issuer 为 DigiCert 中间 CA,notAfter 与 CertCentral 订单一致;openssl s_client 末尾出现 Verify return code: 0 (ok)。若浏览器正常但 Java 报 PKIX,重点复查 fullchain;若 curl 解析到旧 IP,用 –resolve example.com:443:1.2.3.4 固定回源验证。最后检查证书剩余天数:openssl x509 -in fullchain.pem -noout -checkend 2592000,返回 not expired 表示 30 天内不会过期。
常见问题
建议必须用。Ingress 只加载 Secret 中的 tls.crt,若只放叶子证书,curl、Java、小程序可能报 unknown authority。把 example.com.crt 与 DigiCertCA.crt 按叶子在前、中间 CA 在后拼接成 fullchain.pem。
多数 ingress-nginx 会监听 Secret 变化,通常无需重启。若确认 Secret 中 serial 已更新但外部仍返回旧证书,再执行 kubectl rollout restart deployment/ingress-nginx-controller -n ingress-nginx 清理缓存。
可对接 DigiCert TLS Protect Cloud 的 Venafi Issuer,或用 CertCentral API 编写 CronJob 下载证书并更新 Secret。普通 ACME Issuer 主要面向 Let’s Encrypt 等 90 天证书,不能直接替代 DigiCert 审批流程。
常见原因是链不完整。浏览器会尝试补链,Java 和小程序更严格。用 openssl s_client -showcerts 检查是否返回中间 CA,若没有,重建包含 DigiCertCA.crt 的 fullchain Secret。
先从 Secret 解码证书并查看 serial、notAfter,再用 openssl s_client 连接域名比对 serial。两者一致且 Verify return code 为 0,说明新证书已生效;不一致则检查 Ingress 缓存或 Secret 命名空间。