背景介绍
cert-manager是Kubernetes上一个管理SSL证书的插件,配合nginx-ingress可以对网站配置https访问,在加上letsencrypt提供免费的SSL证书,所有就产生了cert-manager+nginx-ingress+letsencrypt的免费套餐。 但是cert-manager默认是不支持阿里云的DNS的,需要自己编写webhook,借助网上大神编写的webhook,自己修改后用于本篇文章使用
环境准备
Kubernetes 1.12.1+
nginx-ingress(必须能够有公网IP,不然DNS认证过不了),阿里云的Kubernetes集群已经提供这个功能
cert-manager
阿里云DNS(已经备案,如果没有备案www是打不开的)
部署大致步骤
一. 在Kubernetes上安装cert-manager
二. 安装alidns的webhook
三. 配置Issuer
四. 配置Ingress
一 K8S上安装cert-manager
$ kubectl create namespace cert-manager
$ kubectl label namespace cert-manager certmanager.k8s.io/disable-validation=true
如果是1.12或者以下的k8s集群,需要添加--validate=false,我的集群就需要添加
$ kubectl apply -f https://github.com/jetstack/cert-manager/releases/download/v0.10.0/cert-manager.yaml --validate=false
$ kubectl get po -n cert-manager
NAME READY STATUS RESTARTS AGE
cert-manager-78d674b8b5-wfqh4 1/1 Running 0 3h39m
cert-manager-cainjector-664bb64c66-9h9sc 1/1 Running 0 3h39m
cert-manager-webhook-9c6fd5f7f-tz2gj 1/1 Running 0 3h39m
#验证cert-manager
$ cat <<EOF > test-resources.yaml
apiVersion: v1
kind: Namespace
metadata:
name: cert-manager-test
---
apiVersion: certmanager.k8s.io/v1alpha1
kind: Issuer
metadata:
name: test-selfsigned
namespace: cert-manager-test
spec:
selfSigned: {}
---
apiVersion: certmanager.k8s.io/v1alpha1
kind: Certificate
metadata:
name: selfsigned-cert
namespace: cert-manager-test
spec:
commonName: example.com
secretName: selfsigned-cert-tls
issuerRef:
name: test-selfsigned
EOF
$ kubectl apply -f test-resources.yaml
$ kubectl describe certificate -n cert-manager-test , 最后一行看到Certificate issued successfully,就代表安装成功
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal CertIssued 4s cert-manager Certificate issued successfully
$ kubectl delete -f test-resources.yaml
二 安装alidns的webhook
$ git clone https://github.com/kevinniu666/cert-manager-webhook-alidns.git
$ cd cert-manager-webhook-alidns
$ helm install --name cert-manager-webhook-alidns --namespace=cert-manager ./deploy/webhook-alidns
#查看webhook
$ kubectl get po -n cert-manager
NAME READY STATUS RESTARTS AGE
cert-manager-78d674b8b5-wfqh4 1/1 Running 0 3h50m
cert-manager-cainjector-664bb64c66-9h9sc 1/1 Running 0 3h50m
cert-manager-webhook-9c6fd5f7f-tz2gj 1/1 Running 0 3h50m
cert-manager-webhook-alidns-6f9695b7c4-cmdwz 1/1 Running 0 65m
三 配置Issuer
#cert-manager有两种issuer,Issuer和ClusterIssuer,区别就是Role和ClusterRole的区别吧(自己理解的)
#通过阿里云RAM创建一个账号,并授权DNSFullAccess权限,将账号的AK记下来,并通过下面的命令创建secret,这个secret用于webhook在DNS认证的时候,会向DNS解析里面写入一条txt类型的记录,认证完成后删除.如果要在一个cert-manager配置多个在不同阿里云账号下的DNS,后面有说明,主要是ClusterRole要改一下。
$ kubectl -n cert-manager create secret generic alidns-credentials --from-literal=accessKeySecret='evNH0A***fONnnTy2r'
$ kubectl apply -f letsencrypt-clusterissuer.yaml #这个文件里面就引用了刚刚创建的secret,AK的ID是直接输入的,也可以把ID放在secret里面,然后应用。
$ kubectl get clusterissuer
NAME AGE
letsencrypt-prod 1h
四 配置ingress
$ kubectl apply -f ingress.yaml , 这里面加入了cluster-issuer的注解,cert-manager会自动根据域名去创建certificate,order,challenge等
$ kubectl get ing
NAME HOSTS ADDRESS PORTS AGE
demo-ingress cert.****.com 1**.2*.164.22* 80, 443 70m
$ kubectl get certificate, REDAY是True就代表证书从letsencrypt下发成功了。
NAME READY SECRET AGE
da***-com-tls True da**-com-tls 1h
#如果是false,看看challenge状态,dns的验证需要等一会,为了让txt记录生效
$ kubectl get challenge
NAME STATE DOMAIN AGE
**-tls-2231756264-0 pending ***.com 5m
$ kubectl describe challenge ***-tls-2231756264-0 ,成功后会有如下的提示,challenge在成功验证后会被自动删除
....
Normal Presented 28s cert-manager Presented challenge using dns-01 challenge mechani
五 打开浏览器,访问ingress里面的地址
PS:对于不同阿里云账号下的域名,需要创建不同的ClusterIssuer然后分别应用不同的secret,在ingress创建的时候,注解也需要引用响应的issuer.
$ kubectl -n cert-manager create secret generic alidns-credentials-2 --from-literal=accessKeySecret='evNH*****fONnnTy2r'
$ kubectl apply -f letsencrypt-clusterissuer-2.yaml
$ kubectl apply -f ingress-2.yaml
$ kubectl edit clusterrole cert-manager-webhook-alidns:secret-reader #将刚刚创建的secret加入到resourceNames里面
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
creationTimestamp: 2019-09-18T07:31:41Z
name: cert-manager-webhook-alidns:secret-reader
resourceVersion: "100733766"
selfLink: /apis/rbac.authorization.k8s.io/v1/clusterroles/cert-manager-webhook-alidns%3Asecret-reader
uid: 5bcdb127-d9e6-11e9-bd87-00163e08a2e5
rules:
- apiGroups:
- ""
resourceNames:
- alidns-credentials-2
- alidns-credentials
resources:
- secrets
verbs:
- get
- watch
访问ingress-2中的域名验证