在部署 cert-manager 成功后,便能够使用它申请 Let’s Encrypt 证书。该笔记将记录:在 Kubernetes 中,如何使用 cert-mananager 申请 TLS 证书。
根据 cert-manager/DNS01/Webhook 指引,其提供两个项目:
- 我们使用 DEVmachine-fr/cert-manager-alidns-webhook 开源项目,
- 而 pragkent/alidns-webhook 并未提供 Helm Chart 包,所以我们未使用该项目。
alidns-webhook by pragkent | 废弃
alidns-webhook/README.md at master · pragkent/alidns-webhook
DNS01 | cert-manager
Boulder: The Let’s Encrypt CA
Staging Environment – Let’s Encrypt
Project Status 2025-10-12 5f8d1d6 · 3 years ago
前置条件
完成 cert-manager 部署;
背景知识
需要阅读如下文档:
cert-manager/Configuration/ACME
—- cert-manager/Configuration/ACME/DNS01
——– cert-manager/Configuration/ACME/DNS01/Webhook
我们使用实现 ACME 的 Let’s Encrypt 服务来申请证书,并使用 DNS01 完成质询,所以应该创建类似如下 ClusterIssuer 资源文件:
apiVersion: cert-manager.io/v1
kind: ClusterIssuer
metadata:
name: example-issuer
spec:
acme:
email: user@example.com
server: https://acme-staging-v02.api.letsencrypt.org/directory
privateKeySecretRef:
name: example-issuer-account-key
solvers:
- dns01:
cloudDNS:
project: my-project
serviceAccountSecretRef:
name: prod-clouddns-svc-acct-secret
key: service-account.json
但是,我们使用阿里云 DNS 服务,而不是 cloudDNS 服务,所以上述配置是不行的(无法完成 DNS01 质询)。并且官方仅支持 ACMEDNS、Akamai、AzureDNS、CloudFlare、Google、Route53、DigitalOcean、RFC2136 这些服务,并不包含我们所使用的阿里云服务商。
好在 cert-manager 支持 Webhook 以允许定义自己的 DNS provider(这些便是 out-of-tree DNS provider),而这里便有开源的 AliDNS-Webhook 供我们使用。参考 alidns-webhook/README.md at master 文档,获取详细的配置使用方法。
第一步、部署 AliDNS-Webhook 实现
结合 AliDNS-Webhook 文档,我们所使用的资源文件及部署过程如下:
./01-bundle.yaml
替换文件的 acme.yourcompany.com 修改为公司域名,比如 acme.example.com 域名;
./02-alidns-secret.yaml
填写阿里云 API 访问的 Access ID 与 Access Key 参数;
./03-letsencrypt-clusterissuer.yaml
文件的 groupName 要与 ./01-bundle.yaml 的 group 要保持一致。这里设置为 acme.example.com 参数。
./04-examplexyz-certificate.yaml
kubectl apply -f ./01-bundle.yaml kubectl apply -f ./02-alidns-secret.yaml kubectl apply -f ./03-letsencrypt-clusterissuer.yaml kubectl apply -f ./04-examplexyz-certificate.yaml
第二步、检查证书是否申请成功
# kubectl describe -n default certificates.cert-manager.io example-xyz
...
Status:
Conditions:
Last Transition Time: 2021-05-07T01:30:33Z
Message: Certificate is up to date and has not expired
Observed Generation: 1
Reason: Ready
Status: True
Type: Ready
Not After: 2021-08-05T00:30:32Z
Not Before: 2021-05-07T00:30:32Z
Renewal Time: 2021-07-06T00:30:32Z
Revision: 1
Events: <none>
# kubectl get certificates.cert-manager.io
NAME READY SECRET AGE
example-xyz True example-xyz 2m
// 此时,证书申请成功
第三步、切换到 Let’s Encrypt 生产环境
证书申请成功之后,还有最后一件事情:我们这里仅是在 Let’s Encrypt 的 Staging 环境中完成申请证书的测试,还需要切换到 Let’s Encrypt 生产环境。
修改 ./03-letsencrypt-clusterissuer.yaml 的 server: 为 https://acme-v02.api.letsencrypt.org/directory 以使用生产环境来申请真正的证书。
第四步、配置 Ingress 证书
参考 3.2 Solutions and Applications 笔记。
补充说明
更多 DNS 服务商,参考 cert-manager-webhook · GitHub Topics 页面。
cert-manager-alidns-webhook by DEVmachine-fr | 推荐
前面步骤演示如何部署 cert-manager 组件,并成功申请自签名证书,但这并非我们的实际应用场景。我们希望通过 cert-manager 组件,在集群内完成 Let’s Encrypt 证书申请和管理:
我们使用阿里云 DNS,并通过 DNS01 完成域名所有权认证(部分集群在内网,无法使用 HTTP01 认证);
需要阅读如下文档,以了解相关内容:
cert-manager/Configuration/ACME
—- cert-manager/Configuration/ACME/DNS01
——– cert-manager/Configuration/ACME/DNS01/Webhook
我们这里使用 DEVmachine-fr/cert-manager-alidns-webhook 来完成证书申请;
容器镜像
[I] SRC: ghcr.io/devmachine-fr/cert-manager-alidns-webhook/cert-manager-alidns-webhook:0.3.1
[I] DST: ccr.ccs.tencentyun.com/d3rm-3rd/ghcr.io_devmachine-fr_cert-manager-alidns-webhook_cert-manager-alidns-webhook:0.3.1
第一步、安装 Webhook 部分
helm repo add cert-manager-alidns-webhook https://devmachine-fr.github.io/cert-manager-alidns-webhook
helm repo update cert-manager-alidns-webhook
helm search repo cert-manager-alidns-webhook/
helm pull cert-manager-alidns-webhook/alidns-webhook
helm show values ./alidns-webhook-0.8.3.tgz > alidns-webhook-0.8.3.tgz.helm-values.yaml
vim alidns-webhook-0.8.3.tgz.helm-values.yaml
... groupName:
... nameOverride: "webhook-alidns"
... fullnameOverride: "webhook-alidns"
... image.repository:
helm upgrade --install --namespace cert-manager --create-namespace \
webhook-alidns ./alidns-webhook-0.6.1.tgz -f alidns-webhook-0.6.1.helm-values.yaml
第二步、创建 ClusterIssuer 资源
伴随着 apiVersion 的变化,资源的 YAML 结构也将发生变化,如下配置仅提供参考,建议参照 DEVmachine-fr/cert-manager-alidns-webhook 的配置示例:
# 完成 DNS 质询需要访问阿里云接口来修改 DNS 记录,所以需要使用 KEY 与 TOKEN 来认证
cat > alidns-secret.yaml <<EOF
apiVersion: v1
kind: Secret
metadata:
name: alidns-secret
namespace: cert-manager
stringData:
access-token: your-token
secret-key: your-secret-key
EOF
kubectl apply -f alidns-secret.yaml
# 创建 ClusterIssuer 资源
cat > letsencrypt-cluster-issuer.yaml <<EOF
apiVersion: cert-manager.io/v1
kind: ClusterIssuer
metadata:
name: letsencrypt
spec:
acme:
# 修改,邮箱地址
email: contact@example.com
# 修改,生产地址:https://acme-v02.api.letsencrypt.org/directory
server: https://acme-staging-v02.api.letsencrypt.org/directory
privateKeySecretRef:
name: letsencrypt
solvers:
- dns01:
webhook:
# 修改,应用我们刚才创建的 Secret 资源
config:
accessTokenSecretRef:
key: access-token
name: alidns-secret
regionId: cn-beijing
secretKeySecretRef:
key: secret-key
name: alidns-secret
# 修改,需要填写在安装时指定的 groupName 信息
groupName: example.com
solverName: alidns-solver
EOF
kubectl apply -f letsencrypt-cluster-issuer.yaml
第三步、申请 Ingress TLS 证书
# 在 Ingress 中,使用 HTTPS 证书:
# https://cert-manager.io/docs/usage/ingress/
cat > ingress-testing.yaml <<EOF
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
annotations:
# 修改,指示要使用的 issuer 资源,由管理员提供(我们前面定义的 ClusterIssuer 使用 letsencrypt 名称)
cert-manager.io/cluster-issuer: letsencrypt
name: cert-manager-testing
namespace: default
spec:
ingressClassName: nginx
tls:
- hosts:
# 修改,需要签发证书的域名
- cert-manager-testing.example.com
# 修改,保存证书的 Secret 资源(cert-manger 负责创建)
secretName: cert-manager-testing-tls
rules:
- http:
paths:
- path: /testpath
pathType: Prefix
backend:
service:
name: test
port:
number: 80
EOF
kubectl apply -f ingress-testing.yaml
版本升级(1.7 ⇒ 1.8)
鉴于我们最开始是通过 cmctl 进行安装,所以升级过程分为两步:
1)下载并升级 CRD 资源;
2)升级 cert-manager 服务;
// ---------------------------------------------------------------------------- // 第一步、下载并升级 CRD 资源
wget https://github.com/cert-manager/cert-manager/releases/download/v1.8.2/cert-manager.crds.yaml
kubectl apply -f cert-manager.crds.yaml
// ---------------------------------------------------------------------------- // 第二步、升级 cert-manager 服务
helm repo add jetstack https://charts.jetstack.io
helm repo update
helm pull jetstack/cert-manager --version v1.8.2 # cert-manager-v1.8.2.tgz
helm show values ./cert-manager-v1.8.2.tgz > cert-manager-v1.8.2.helm-values.yaml
helm --namespace cert-manager \
upgrade cert-manager ./cert-manager-v1.8.2.tgz \
-f cert-manager-v1.8.2.helm-values.yaml
// ---------------------------------------------------------------------------- // 第三步、创建 Ingress TLS 进行测试
...
参考文献
GitHub – DEVmachine-fr/cert-manager-alidns-webhook
Installation | cert-manager
cmctl | cert-manager
Securing Ingress Resources | cert-manager