本作品由Galen Suen采用知识共享署名-非商业性使用-禁止演绎 4.0 国际许可协议进行许可。由原作者转载自个人站点。
概述
本文用于整理基于Kubernetes环境的cert-manager部署与应用,实现证书管理和Ingress启用 TLS 配置。
随着各相关组件版本的更新,笔者将在验证通过后对本文进行补充和更新,请参考更新记录。
本次演练环境为Kubernetes集群环境,环境配置可参考笔者另一篇笔记《Kubernetes集群部署笔记》。
本次演练使用Traefik作为Ingress Controller实现,环境配置可参考笔者另一篇笔记《Kubernetes环境Traefik部署与应用》。
本次演练使用Cloudflare提供的DNS解析服务,并假定读者已经注册了Cloudflare并正确配置了网站。有关Cloudflare的配置和使用,请参考Cloudflare帮助中心或相关文档。
组件版本
配置过程
安装 cert-manager
kubectl apply -f https://github.com/cert-manager/cert-manager/releases/download/v1.11.0/cert-manager.yaml
复制代码
配置 Issuer 和 ClusterIssuer
- Permissions
- Zone - DNS - Edit
- Zone - Zone - Read
- Zone Resources:
- Include - All Zones
复制代码
cat <<EOF | kubectl apply -f -
apiVersion: v1
kind: Secret
metadata:
name: cloudflare-api-token-secret
namespace: apps-choral
type: Opaque
stringData:
api-token: '<REDACTED>' # 这里的值为[配置API Token]一节中创建的API Token值
---
apiVersion: cert-manager.io/v1
kind: Issuer
metadata:
name: cloudflare-acme-issuer
namespace: apps-choral
spec:
acme:
email: '<REDACTED>'
# 配置证书目录,演练环境使用Staging环境
# server: https://acme-v02.api.letsencrypt.org/directory
server: https://acme-staging-v02.api.letsencrypt.org/directory
privateKeySecretRef:
name: acme-issuer-account-key
solvers:
- dns01:
cloudflare:
apiTokenSecretRef:
name: cloudflare-api-token-secret # 引用当前文档中创建的Secret名称
key: api-token
EOF
复制代码
创建一个ClusterIssuer对象。
cat <<EOF | kubectl apply -f -
apiVersion: v1
kind: Secret
metadata:
name: cloudflare-api-token-secret
namespace: cert-manager # 这里配置为安装cert-manager资源的命名空间
type: Opaque
stringData:
api-token: '<REDACTED>' # 这里的值为[配置API Token]一节中创建的API Token值
---
apiVersion: cert-manager.io/v1
kind: ClusterIssuer
metadata:
name: cloudflare-acme-cluster-issuer
spec:
acme:
email: '<REDACTED>'
# 配置证书目录,演练环境使用Staging环境
# server: https://acme-v02.api.letsencrypt.org/directory
server: https://acme-staging-v02.api.letsencrypt.org/directory
privateKeySecretRef:
name: acme-issuer-account-key
solvers:
- dns01:
cloudflare:
apiTokenSecretRef:
name: cloudflare-api-token-secret # 引用当前文档中创建的Secret名称
key: api-token
EOF
复制代码
配置 Ingress TLS
可以通过手动创建或基于注解自动创建Certificate资源,cert-manager会自动管理签发证书并保存至指定的Secret
对象中,并自动管理续期。
cat <<EOF | kubectl apply -f -
apiVersion: cert-manager.io/v1
kind: Certificate
metadata:
name: cert-local-choral-io
namespace: apps-choral
spec:
dnsNames:
- 'local.choral.io'
- '*.local.choral.io'
issuerRef:
kind: ClusterIssuer
name: cloudflare-acme-cluster-issuer
secretName: cert-local-choral-io
EOF
复制代码
证书签发成功后,配置Ingress
使用指定的Secret
实现 TLS。
cat <<EOF | kubectl apply -f -
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: whoami
namespace: apps-choral
annotations:
traefik.ingress.kubernetes.io/router.entrypoints: websecure
spec:
tls:
- secretName: cert-local-choral-io
rules:
- host: whoami.local.choral.io
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: whoami
port:
number: 80
EOF
复制代码
cat <<EOF | kubectl apply -f -
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: whoami
namespace: apps-choral
annotations:
cert-manager.io/issuer: cloudflare-acme-issuer
traefik.ingress.kubernetes.io/router.entrypoints: websecure
spec:
tls:
- hosts:
- whoami.local.choral.io
secretName: cert-local-choral-io
rules:
- host: whoami.local.choral.io
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: whoami
port:
number: 80
EOF
复制代码
参考资料
评论