Q: How do you create a self-signed TLS certificate for an Ingress?
To create a self-signed TLS certificate for an Ingress, generate a key and certificate with openssl req -x509 including a SubjectAltName, store them as a kubernetes.io/tls Secret with kubectl create secret tls, and reference that.
Want to master this scenario in a live sandbox? KodeKloud's CKA & CKAD Hands-On Certification Track covers this exact problem with hands-on terminal drills.
🛠️ Production Runbook & Step-by-Step Resolution
Direct Answer: To <strong>create a self-signed TLS certificate for an Ingress</strong>, generate a key and certificate with <code>openssl req -x509</code> including a SubjectAltName, store them as a <code>kubernetes.io/tls</code> Secret with <code>kubectl create secret tls</code>, and reference that Secret in the Ingress <code>tls</code> block. It is a good fit for dev and test clusters. Interviewers ask it to see if you know the Secret and Ingress wiring, why SAN matters, and when to switch to cert-manager.
Mental model: key, certificate, Secret, Ingress
Four pieces connect, and the failure is usually in how they are linked.
- Certificate and key: a self-signed certificate is its own issuer, so clients do not trust it unless you add it to their trust store or skip verification.
- Secret: type
kubernetes.io/tlswith keystls.crtandtls.key, in the same namespace as the Ingress. - Ingress: the
tlssection lists hostnames andsecretName; the controller loads the certificate and serves it through SNI. - SAN requirement: modern clients ignore the Common Name and validate the SubjectAltName, so always include the DNS name there.
- When it is appropriate: local clusters, CI environments and internal tooling where you control the clients and can distribute the certificate.
Hands-on: create a self-signed TLS certificate for an Ingress
Generate the files and create the Secret.
openssl req -x509 -newkey rsa:2048 -nodes -sha256 -days 365 \
-keyout tls.key -out tls.crt \
-subj "/CN=app.example.com" \
-addext "subjectAltName=DNS:app.example.com,DNS:www.example.com"
kubectl create secret tls app-example-tls \
--cert=tls.crt --key=tls.key -n prod
kubectl -n prod get secret app-example-tls -o jsonpath='{.type}{"\n"}'
- One command:
openssl req -x509 -newkey rsa:2048 -nodescreates the key and certificate together;-dayssets validity. - SAN via -addext: supported in OpenSSL 1.1.1 and newer. macOS ships LibreSSL by default, where
-addextmay not work, so use Homebrew OpenSSL or a config file. - Create the Secret:
kubectl create secret tls app-example-tls --cert=tls.crt --key=tls.key -n prod. - Wildcard: use
DNS:*.dev.example.comin the SAN when several hosts share the certificate. - Key size and algorithm: RSA 2048 is widely compatible; ECDSA keys with
-newkey ec -pkeyopt ec_paramgen_curve:prime256v1are smaller and also supported by common controllers. - Keep the private key private: do not commit
tls.keyto Git; create the Secret from the files and delete them, or generate them in CI.
Wire the Secret into the Ingress
Host names in tls.hosts must match the rule host and the certificate.
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: my-app
namespace: prod
spec:
ingressClassName: nginx
tls:
- hosts:
- app.example.com
secretName: app-example-tls
rules:
- host: app.example.com
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: my-app
port:
number: 80
- tls block: list the hosts and
secretName; the Secret must be in the Ingress namespace. - ingressClassName: make sure the controller you expect handles the Ingress.
- Redirects: ingress-nginx redirects HTTP to HTTPS by default when TLS is configured; adjust via annotation if needed.
- Rotation: to replace the cert, update the Secret in place; most controllers reload it without a restart.
- One Secret per host group: several Ingress objects in the same namespace can reuse one Secret if the SAN covers all their hosts.
Verify the served certificate and troubleshoot
Check what the controller actually presents, not what you think you configured.
openssl x509 -in tls.crt -noout -subject -dates -ext subjectAltName
curl -v --resolve app.example.com:443:203.0.113.10 --cacert tls.crt https://app.example.com/
openssl s_client -connect 203.0.113.10:443 -servername app.example.com </dev/null 2>/dev/null | openssl x509 -noout -subject -issuer
- Inspect the file:
openssl x509 -in tls.crt -noout -subject -dates -ext subjectAltName(the-extflag needs OpenSSL 1.1.1 or newer). - Test without DNS:
curl --resolve app.example.com:443:<ingress-ip> https://app.example.com -k -v; remove-kand add--cacert tls.crtto check proper trust. - SNI view:
openssl s_client -connect <ingress-ip>:443 -servername app.example.comshows the presented certificate. - Default fake certificate: if you see the controller's 'Kubernetes Ingress Controller Fake Certificate', the Secret name, namespace, type or host does not match, and the controller log says why.
- Controller logs: ingress-nginx logs messages such as an unexpected error loading the secret, which pinpoint a wrong type or missing keys.
- Browser warnings: a self-signed certificate warns unless the certificate is imported as trusted, which is expected and not a cluster fault.
Production practice: use cert-manager or a real CA
Self-signed certificates do not scale beyond local use.
- cert-manager SelfSigned or CA issuer: automates creation and renewal inside the cluster, with the Certificate resource producing the Secret.
- Public hosts: use ACME with Let's Encrypt for internet-facing names.
- Local dev trust: a tool like mkcert creates a local CA so browsers trust the certificate without warnings.
- Expiry monitoring: alert on certificate expiry, since self-signed certs expire silently.
- Trust distribution: for service-to-service calls, mount the CA certificate into clients through a ConfigMap instead of disabling verification.
- Renewal drill: rehearse replacing the certificate before expiry, including updating the Secret and confirming the controller serves the new serial number.
- I generate a key and self-signed certificate with openssl including a SAN, then create a kubernetes.io/tls Secret.
- I reference it in the Ingress tls block and verify with openssl s_client and curl --resolve.
- I automate with cert-manager issuers and monitor expiry instead of hand-made certificates.
Senior hiring managers and bar raisers evaluate how you prioritize during outages. Avoid these common anti-patterns:
- Omitting the SubjectAltName from the certificate Modern clients check the SAN and reject the certificate even when the CN matches.
- Creating the TLS Secret in a different namespace than the Ingress The controller cannot read it and serves its default fake certificate.
- Using -k permanently instead of trusting the certificate It hides real problems and trains teams to ignore TLS errors.
- Using self-signed certificates for production traffic Clients cannot verify them and renewal is manual, which leads to outages when they expire.