Kubernetes cookbook · Intermediate
Expose a service over HTTPS with automatic certificates
Serve an app on a domain with TLS certificates that renew themselves.
Approach
Install an Ingress controller and cert-manager with a Let's Encrypt ClusterIssuer, then annotate the Ingress;
cert-manager obtains the certificate into the referenced Secret and renews it.
Solution
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: shop
annotations:
cert-manager.io/cluster-issuer: letsencrypt-prod
spec:
ingressClassName: nginx
tls:
- hosts: [shop.example.com]
secretName: shop-tls
rules:
- host: shop.example.com
http:
paths:
- path: /
pathType: Prefix
backend:
service: {name: web, port: {number: 80}}
Pitfalls
- Using the Let's Encrypt production issuer while testing and hitting rate limits; test with staging.
- DNS not pointing at the load balancer yet, so HTTP-01 challenges fail.
Variations
- DNS-01 challenges for wildcard certificates or private clusters.
- The Gateway API with cert-manager's Gateway support.
Further reading: cert-manager: securing Ingress resources
Reference: https://cert-manager.io/docs/usage/ingress/
More Kubernetes recipes
- Autoscale a Deployment on CPU and memory
- Debug a distroless pod with an ephemeral container
- Default-deny network policies that keep DNS working
- Enforce the restricted Pod Security Standard
- Give a CI pipeline least-privilege deploy access
- Handle SIGTERM in your application for Kubernetes
- Manage dev, staging and prod with Kustomize overlays
- Restart pods automatically when configuration changes