Sub topic 8
Terminating TLS at the Ingress
This chapter assumes you can write and apply YAML manifests with kubectl, that you know what a certificate and private key are, and that you have an Ingress and a controller working over plain HTTP. If you don't, work through the earlier chapters on Ingress resources and routing first.
What TLS termination means
TLS termination means the Ingress controller handles the encrypted connection with the client. It decrypts the request and forwards it to the backend Service. By default the hop from controller to Pod is plain HTTP inside the cluster. Your applications do not need their own certificates.
Step 1: Store the certificate in a Secret
The Ingress does not contain the certificate. It points to a Secret of type kubernetes.io/tls. This command builds that Secret from a certificate file and a key file:
kubectl create secret tls shop-example-tls --cert=tls.crt --key=tls.keyThe Secret holds two keys, tls.crt and tls.key. The Secret must be in the same namespace as the Ingress. An Ingress cannot reference a Secret in another namespace.
The certificate must be valid for the hostname you serve. A certificate issued for a different name makes clients show a warning, or the controller may fall back to its own default certificate.
Step 2: Reference the Secret in the Ingress
Add a tls section to the Ingress spec. Each entry lists the hosts and the Secret that covers them:
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: shop-tls
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: 80The tls section and the rules section are separate. The tls section only says which certificate to present. The rules section still decides where traffic goes. The host under tls.hosts should match the host in a rule. The controller uses the requested hostname, sent in the TLS handshake through SNI (Server Name Indication), to pick the right certificate. SNI is what lets one controller serve many hostnames, each with its own certificate, on a single IP address and port 443.
Behavior to expect
- Most controllers redirect HTTP requests to HTTPS once a
tlssection exists. The exact behavior is controller-specific and is often adjusted with annotations. - If you omit
secretName, the controller uses its default certificate. How that default is set depends on the controller. - If the Secret is missing or malformed, the controller usually serves its default or a self-signed certificate. Clients then see a certificate warning.
- When a Secret changes, such as a renewed certificate, controllers normally reload it without a restart.
Verifying the setup
Check that the Ingress has an address and lists the TLS host, then test with curl. The --resolve option lets you test before DNS exists. Replace 203.0.113.10 with your Ingress address.
kubectl get ingress shop-tls
kubectl describe ingress shop-tls
curl -v --resolve shop.example.com:443:203.0.113.10 https://shop.example.com/In the curl -v output, look at the subject and issuer lines. If they name your certificate, the Secret is working. If they show something like a controller "fake" certificate, the controller did not load your Secret.
Common errors
| Symptom | Likely cause |
|---|---|
| Browser warns about an untrusted or default certificate | Secret missing, wrong namespace, or misspelled `secretName` |
| Certificate name mismatch | The certificate does not cover the requested host |
| Secret rejected | The Secret type is not `kubernetes.io/tls`, or the key does not match the certificate |
| HTTPS works but HTTP does not redirect | Controller redirect behavior is off or configured by annotation |
Issuing and renewing certificates by hand does not scale. A later chapter shows how a tool such as cert-manager automates it, creating and updating the same kind of Secret for you.
Exercise
You created the Secret shop-example-tls in the default namespace and applied the TLS Ingress in the namespace shop. Clients receive the controller's default certificate. What is the most likely cause and the fix?
Show answer
The Ingress can only reference a Secret in its own namespace. The Secret is in default, but the Ingress is in shop. Create the Secret in the shop namespace, for example with kubectl create secret tls shop-example-tls --cert=tls.crt --key=tls.key -n shop, and confirm with curl -v that the presented certificate is yours.
Exercise
Why can a single controller serve shop.example.com and blog.example.com with different certificates on the same IP address and port 443?
Show answer
The client sends the requested hostname during the TLS handshake using SNI. The controller uses that name to choose the matching certificate from the Secrets listed in the Ingress tls sections.