iT邦幫忙

2025 iThome 鐵人賽

DAY 14
1
Cloud Native

駕馭商用容器叢集,汪洋漂流術系列 第 14

【Day 14】 Cert Manager / 懶人必學自動更新憑證

  • 分享至 

  • xImage
  •  

前言

  • 雖然在前面提到不想要寫程式去整理那些快過期的憑證,不過,我手上那些憑證四千多筆,一筆一筆對,根本對不完...所以馬上打臉自己,我寫程式去比對了。 今天再來寫一篇管理憑證會用的工具 - Cert Manager

簡介

  • 先看官網介紹:

    cert-manager is a powerful and extensible X.509 certificate controller for Kubernetes and OpenShift workloads. It will obtain certificates from a variety of Issuers, both popular public Issuers as well as private Issuers, and ensure the certificates are valid and up-to-date, and will attempt to renew certificates at a configured time before expiry.

  • Cert Manager 是 K8s / OCP 的 Controller
  • 會自動幫你去跟憑證發行者 (Issuer) 取得、更新憑證。

    cert-manager can obtain certificates from a variety of certificate authorities, including: Let's Encrypt, HashiCorp Vault, CyberArk Certificate Manager and private PKI.

  • 除了常見的 CA 之外,還有 Private PKI

使用場景

  1. Ingress / Route TLS 憑證自動化
    • CertificateIssuer(如 Let's Encrypt DNS-01)、Ingress / Route

    自動為網站設定 HTTPS(如 myapp.example.com)
    搭配 Let's Encrypt 或企業內部 CA 自動簽發憑證
    不需手動更新 TLS 憑證

  2. Webhook 憑證(Mutating / Validating Webhook)
    • CertificateSecret(自動產生憑證)、MutatingWebhookConfiguration / ValidatingWebhookConfiguration

    K8s 中的 webhook 需要使用 TLS 憑證與 API Server 建立安全連線
    cert-manager 自動為 webhook 產生與輪換 TLS 憑證

  3. OpenShift Route TLS 憑證自動化

    OpenShift 使用 Route 對外公開服務,想搭配 cert-manager 來簽發 TLS 憑證
    將 cert-manager 產出的 TLS Secret 內容寫入 Route 的 .spec.tls

  4. 內部服務之間的 mTLS(mutual TLS)
    • 自建 CA Issuer、每個 microservice 有對應的 Certificate 資源

    微服務之間通訊需要雙向 TLS 驗證(例如 service mesh、金融場景)
    cert-manager 為每個 service 簽發 client certificate

  5. CI/CD 自動化簽發憑證(如 Jenkins、GitOps 工具)

    部署新環境時,自動幫 webhook、UI、API 建立憑證
    GitOps 工具(如 ArgoCD、Flux)配合 cert-manager 自動申請/更新憑證

  6. 整合 HashiCorp Vault、Venafi、企業 CA

    企業已經有內部 CA(如 Microsoft AD CS、Venafi、Vault PKI),想讓 cert-manager 發 CSR 給它簽發憑證
    增強整個集群的 TLS 信任鏈與審計能力

  7. ServiceAccount / Token JWT 簽署

    cert-manager 也可以簽發 JWT 憑證(非 TLS),搭配 OIDC、SPIFFE 使用
    用於 workload identity、service identity(特別是在零信任架構中)

  8. 證書輪換通知與觀察(Certificate lifecycle automation)

    cert-manager 可搭配 Kubernetes Event、Prometheus、Alertmanager 對憑證快過期發出告警
    也可用 GitOps 方式監看哪些證書即將過期

使用方式及操作

在 OpenShift 中使用 cert-manager 為一個 Route 自動配置 TLS 憑證,需要進行以下操作:

  1. 建立一個 ClusterIssuer(用自簽 CA 為例)
  2. 發出 Certificate(自動產生 Secret)
  3. Route 使用該 Secret 的 key/cert

1. 建立自簽 CA Secret + ClusterIssuer

# 1-1 自簽 CA Secret(CA 憑證 + 私鑰)
apiVersion: v1
kind: Secret
metadata:
  name: my-ca-secret
  namespace: cert-manager
type: kubernetes.io/tls
data:
  tls.crt: <base64-encoded-ca-cert>
  tls.key: <base64-encoded-ca-key>
# 1-2 ClusterIssuer(用上面的自簽 CA)
apiVersion: cert-manager.io/v1
kind: ClusterIssuer
metadata:
  name: my-selfsigned-clusterissuer
spec:
  ca:
    secretName: my-ca-secret

2. 建立 Certificate(憑證自動產生)

apiVersion: cert-manager.io/v1
kind: Certificate
metadata:
  name: my-service-cert
  namespace: my-namespace
spec:
  secretName: my-service-tls  # 憑證與金鑰會存在這個 Secret 中
  duration: 2160h             # 有效期 90 天
  renewBefore: 360h           # 15 天前開始續約
  commonName: my-service.example.com
  dnsNames:
    - my-service.example.com
  issuerRef:
    name: my-selfsigned-clusterissuer
    kind: ClusterIssuer

3. 建立 Route 並掛上 TLS 憑證

apiVersion: route.openshift.io/v1
kind: Route
metadata:
  name: my-service
  namespace: my-namespace
spec:
  host: my-service.example.com
  to:
    kind: Service
    name: my-service
  port:
    targetPort: 8080
  tls:
    termination: edge
    key: |
      <填入自動產生 Secret 中的 key>
    certificate: |
      <填入自動產生 Secret 中的 cert>
  • 使用下列指令取得 key/cert
oc get secret my-service-tls -n my-namespace -o jsonpath='{.data.tls\.key}' | base64 -d > tls.key
oc get secret my-service-tls -n my-namespace -o jsonpath='{.data.tls\.crt}' | base64 -d > tls.crt

結論

  • 沒理由不去使用 Cert Manager,除非你想給你的老闆驚喜。

參考資料

  1. cert-manager / Cloud native certificate management

上一篇
【Day 13】 Deployment 和 雖然被棄坑但也需要了解的 DeploymentConfig
下一篇
【Day 15】 DaemonSet / StatefulSet / PVC - 在叢集中部署監控、有狀態容器所需的工作負載
系列文
駕馭商用容器叢集,汪洋漂流術15
圖片
  熱門推薦
圖片
{{ item.channelVendor }} | {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言