Sub topic 12
Automating certificate issuance
This chapter assumes you can write an Ingress with a tls section, you know that a kubernetes.io/tls Secret holds a certificate and key, and you have a working Ingress controller with an IngressClass.
The problem with manual certificates
Earlier you created the TLS Secret by hand:
kubectl create secret tls shop-example-tls --cert=tls.crt --key=tls.keyThis works, but certificates expire. Public certificate authorities such as Let's Encrypt issue short-lived certificates, so a manual process means someone must renew and replace the Secret again and again. A forgotten renewal causes browser warnings and failed API calls.
Ingress has no built-in way to request or renew certificates. It only reads the Secret you name. Automation therefore comes from an add-on that creates and updates that Secret for you.
cert-manager
cert-manager is a widely used add-on that runs in the cluster. It adds custom resources, including these:
- Issuer and ClusterIssuer describe a certificate authority. An Issuer works in one namespace. A ClusterIssuer works in every namespace.
- Certificate describes the certificate you want, such as its host names and the Secret that should hold it.
cert-manager watches Certificates, requests the certificate from the authority, and writes the result into a kubernetes.io/tls Secret. It renews the certificate before it expires and updates the same Secret. The Ingress controller then picks up the new contents.
The request flow
For Let's Encrypt, cert-manager uses the ACME protocol. With the HTTP-01 challenge, the authority checks that you control the host name by fetching a token over plain HTTP on port 80.
For this to work, the host name must resolve in public DNS to your controller's address, and port 80 must be reachable from the internet. The solver creates a temporary Ingress, so it needs to know which class to use.
Defining an issuer
This ClusterIssuer uses the Let's Encrypt staging server, which has relaxed rate limits and issues certificates that browsers do not trust. Use it for testing.
apiVersion: cert-manager.io/v1
kind: ClusterIssuer
metadata:
name: letsencrypt-staging
spec:
acme:
server: https://acme-staging-v02.api.letsencrypt.org/directory
email: [email protected]
privateKeySecretRef:
name: letsencrypt-staging-account-key
solvers:
- http01:
ingress:
ingressClassName: example-classThe privateKeySecretRef names a Secret where cert-manager stores your ACME account key. The ingressClassName must match the controller that serves your Ingress.
Annotating the Ingress
The Ingress below is the TLS example from the previous chapter with one added annotation. cert-manager reads this annotation, sees the tls section, and creates a Certificate for shop.example.com that writes to shop-example-tls.
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: shop-tls
annotations:
cert-manager.io/cluster-issuer: letsencrypt-staging
spec:
ingressClassName: example-class
tls:
- hosts:
- shop.example.com
secretName: shop-example-tls
rules:
- host: shop.example.com
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: web
port:
number: 80You do not create the Secret yourself. Once the staging flow works, change the annotation to a production issuer.
Checking progress
cert-manager adds resources you can inspect:
kubectl get certificate
kubectl describe certificate shop-example-tls
kubectl get secret shop-example-tlsA Certificate reports READY as True once the Secret is filled in. If it stays False, the events in describe usually name the cause. Common causes are:
- DNS does not point at the controller.
- Port 80 is blocked or redirected in a way the challenge cannot follow.
- The solver's
ingressClassNamedoes not match your controller.
Exercise
Your Certificate stays not ready and describe shows the HTTP-01 challenge pending. Name two things to check.
Show answer
Check that the host name resolves in public DNS to the Ingress controller's address, and that port 80 is reachable from the internet. Also confirm the ClusterIssuer's solver ingressClassName matches the controller serving the Ingress.