概述
cert-manager 支持许多 DNS provider,但不支持国内的 DNSPod,不过 cert-manager 提供了 Webhook 机制来扩展 provider,社区也有 DNSPod 的 provider 实现。本文将介绍如何结合 cert-manager 与 cert-manager-webhook-dnspod 来实现为 DNSPod 上的域名自动签发免费证书。
基础知识
操作步骤
1. 创建 DNSPod 密钥
2. 安装 cert-manager
3. 安装 cert-manager-webhook-dnspod
使用 HELM 来安装 cert-manager-webhook-dnspod,需准备 HELM 配置文件。
dnspod-webhook-values.yaml
示例如下:groupName: example.your.domain # 写一个标识 group 的名称,可以任意写secrets: # 将前面生成的 id 和 token 粘贴到下面apiID: "<ID>"apiToken: "<Token>"clusterIssuer:enabled: true # 自动创建出一个 ClusterIssueremail: your@email.com # 填写您的邮箱地址
使用 HELM 进行安装:
git clone --depth 1 https://github.com/qqshfox/cert-manager-webhook-dnspod.githelm upgrade --install -n cert-manager -f dnspod-webhook-values.yaml cert-manager-webhook-dnspod ./cert-manager-webhook-dnspod/deploy/cert-manager-webhook-dnspod
4. 创建证书
使用如下所示 YAML 文件创建
Certificate
对象来签发免费证书:apiVersion: cert-manager.io/v1kind: Certificatemetadata:name: example-com-crtnamespace: istio-systemspec:secretName: example-com-crt-secret # 证书保存在这个 secret 中issuerRef:name: cert-manager-webhook-dnspod-cluster-issuer # 这里使用自动生成出来的 ClusterIssuerkind: ClusterIssuergroup: cert-manager.iodnsNames: # 填入需要签发证书的域名列表,确保域名是使用 dnspod 管理的- example.com- test.example.com
等待状态变成 Ready 表示签发成功:
$ kubectl -n istio-system get certificates.cert-manager.ioNAME READY SECRET AGEexample-com-crt True example-com-crt-secret 25d
若签发失败可通过 describe 查看原因:
kubectl -n istio-system describe certificates.cert-manager.io example-com-crt
5. 使用证书
证书签发成功后会保存到指定的 Secret 中,可参考以下使用示例:
apiVersion: networking.k8s.io/v1beta1kind: Ingressmetadata:name: test-ingressannotations:kubernetes.io/ingress.class: nginxspec:rules:- host: test.example.comhttp:paths:- path: /backend:serviceName: webservicePort: 80tls:hosts:- test.example.comsecretName: example-com-crt-secret # 引用证书 secret
apiVersion: networking.istio.io/v1alpha3kind: Gatewaymetadata:name: example-gwnamespace: istio-systemspec:selector:app: istio-ingressgatewayistio: ingressgatewayservers:- port:number: 80name: HTTP-80protocol: HTTPhosts:- example.com- test.example.comtls:httpsRedirect: true # http 重定向 https (强制 https)- port:number: 443name: HTTPS-443protocol: HTTPShosts:- example.com- test.example.comtls:mode: SIMPLEcredentialName: example-com-crt-secret # 引用证书 secret---apiVersion: networking.istio.io/v1beta1kind: VirtualServicemetadata:name: example-vsnamespace: testspec:gateways:- istio-system/example-gw # 转发规则绑定到 ingressgateway,将服务暴露出去hosts:- 'test.example.com'http:- route:- destination:host: exampleport:number: 80