In this guide we will learn how to configure TLS/SSL in Kubernetes using ingress for already existing SSL certificates (either Self-Signed) or purchased ones.
For this to work, you should also have a working ingress set up. We will use Nginx Ingress. You should also have the domain used already configured with the Nginx ingress endpoint record.
Checkout related content:
- Kubernetes Nginx Ingress in aws With Certbot Letsencrypt
- How To Create and use a Self-Signed SSL Certificate for Apache
- How to create a Kubernetes TLS/SSL Secret
Obtaining a TLS Certificate
The basic requirement for ingress TLS is a TLS/SSL certificate. You can obtain these certificates in the following ways.
- Self-Signed Certificates: TLS certificate created and signed by our own Certificate Authority. It is a great option for development environments where you can share the root CA with the team so that browsers can trust the certificate.
- Purchase an SSL Certificate: You can buy an SSL Certificate from one of the many well-known certificate authority trusted by browsers & operating systems for production use cases.
- Use Letsencrypt Certificate: Letsencrypt is a non-profit trusted certificate authority that provides free TLS certificates.
Every SSL certificate comes with an expiry date. So you need to rotate the certificate before it expires. For example, Letsencrypt certificates expire every three months.
In this guide we will need a certificate from one of the first two options. If you do not have a certificate already, create a Self-Signed certificate. Use this command:
| |
This will prompt you to fill in the certificate details. This command produces two files: citizix-k8s.key and citizix-k8s.crt. In production, you’d generate a key file and use it to obtain a certificate from a certificate authority.
This is the output from my machine:
| |
TLS/SSL with Ingress in Kubernetes
SSL is handled by the ingress controller, not the ingress resource. Meaning, when you add TLS certificates to the ingress resource as a Kubernetes secret, the ingress controller accesses it and makes it part of its configuration.
For example, in the Nginx controller, the SSL certificates are dynamically handled by the following block in nginx.conf
| |
Deploy a Test Application
In this section, we will create a simple application in Kubernetes that we can use to test out the certificate. We will create a deployment, service and an ingress.
Make sure you also have access to a Kubernetes cluster and are in the right Kubernetes cluster. Confirm with this command:
| |
Then create a namespace where all our resources will reside:
| |
Now create a deployment in the namespace:
| |
Use this command to confirm that the deployment was created as expected:
| |
Confirm that the pods are running:
| |
Then expose our deployment as a service:
| |
Confirm:
| |
Create a Kubernetes TLS Secret
We can issue the kubectl create command to create a secret from the command line.
When creating a TLS Secret using kubectl, you can use the tls subcommand as shown in the following example:
| |
Following is the equivalent YAML file where you have to add the certificate and key file contents. Note: The certificate and key must be base64-encoded when using YAML. You can encode them using:
| |
| |
The public/private key pair must exist beforehand. Verify that the secret was added using this command:
| |
To view the YAML source of the secret:
| |
Create an ingress and add the tls block
We have to create an ingress in the same namespace where the resources have been created.
Save the following YAML as ingress.yaml. Replace citizix-k8s.citizix.com with your hostname.
| |
Please note the TLS block with the domain name and the TLS secret created earlier. The host in the TLS block and rules block should match.
Create the ingress resources with this command:
| |
Verify that the ingress was created and picked up the TLS configuration:
| |
Validating ingress TLS
You can validate the ingress by visiting the site in your browser. If it is a self-signed certificate you will receive the “Your connection is not private” warning and if you check the certificate details, you will see the certificate information you added when generating.
If you are using curl:
| |
Check this information, it should have information about the certificate being used.
| |
Ingress SSL Termination
By default, SSL gets terminated in the ingress controller. So all the traffic from the controller to the pod will be without TLS (decrypted traffic).
If you want full SSL end-to-end encryption, you can add the supported annotation by the ingress controller you are using. For example, in the NGINX ingress controller, to allow SSL traffic to the application, you can use the annotation. For this, your application should have SSL configured.
| |
Conclusion
In this guide, we learnt how to configure Ingress TLS certificates with Kubernetes.
To clean up the resources we created, use these commands:
| |