Step 9: Certificate management
- Certificates quickly become a headache: generation, renewal, incidents.
- Cert-Manager automates the entire certificate lifecycle.
- Installation and validation with Traefik and Let's Encrypt.
- This logic also applies to internal PKIs via ACME proxies.
Legacy version of the Kubernetes cookbook
This article is part of the first version of the cookbook (VMware and PhotonOS). A new version, on XCP-ng and Rocky Linux, is being published: certificate management has not yet been released; in the meantime, the article acme2certifier and cert-manager covers the use of an internal PKI. The article remains online for reference.
Introduction
The cluster is now operational, ready to host, run, and expose applications.
However, there is still one point to address: certificate management.
More and more application environments are built on web models, where communication flows through SSL streams. The use of certificates quickly becomes mandatory, especially for exposing assets outside the cluster.
The more deployments are made, the more certificate management can become a real headache. Between their generation and renewal, incidents can quickly occur.
That's why there are add-ons to Kubernetes such as Cert-Manager. It is a cloud-native certificate manager. It allows you to automate and manage the entire certificate lifecycle.
Furthermore, it integrates very well with Traefik, which we deployed in the previous article.
The logic is as follows:
Cert-Manager extends the Kubernetes API with new objects. These objects allow you to build and declare certification chains, starting with Issuers.
An Issuer is an object to which a certificate request can be submitted; it processes the information and, based on its configuration, handles the generation of the certificate and then stores it in a secret.
This secret will be declared at the Ingress level, responsible for exposing the application outside the cluster under the management of the Ingress controller.
While in the previous article the CSR (Certificate Signing Request SSL) was generated manually, submitted to an external PKI (public key infrastructure), and the secret had to be created yourself from the received certificate, with CertManager all these operations will be automated.
An Issuer can be cluster-wide, meaning it can be accessed from any namespace (in this case, we refer to it as a ClusterIssuer), or it can be defined within a specific namespace.
It is of course possible to have several Issuers, each one able to call on a different PKI. This makes it easy to set up Issuers per need, scope or security requirement.
In the following example, Let's Encrypt will be used as the certificate authority (or PKI). It is free, open to everyone, and can be used via the ACME protocol.
Principle of operation
Indeed, if you want to automate the obtaining of a certificate, you must be able to prove ownership of the domain for which that certificate will be deployed.
To achieve this, the ACME protocol will offer various challenges that the requester must solve to prove their legitimacy in generating a certificate for the desired domain.
ACME is fully integrated into Cert-Manager, which can use it to communicate with Let's Encrypt.
The type of challenge we will be using is http.
When Let's Encrypt receives a certificate request, it attempts to access specific web content exposed at a URL belonging to the domain for which the certificate is requested. This content is a unique key generated by Let's Encrypt with each request.
The URL being called is always in the format: https://websiteaddress/.well-known/acme-challenge/the_key.
If the content is found, then this proves that the requester owns the domain, since they were able to provide the unique key required by Let's Encrypt.
Cert-Manager will therefore make the request and provision, in the form of a pod, a mini web server answering at the URL called by Let's Encrypt, where the latter expects to find the key.
That's where Traefik comes in, as it will detect the deployment of this temporary pod and enable the exposure of the associated service by dynamically creating the routing rule.
The call to the sub-URL https://adressedusite/.well-known can now be redirected not to the pod of the base application, but to the temporary pod generated by Cert-Manager.
The content delivered by the pod becomes accessible from the outside, and therefore to Let's Encrypt.
Once the challenge is passed, the certificate is issued by Let's Encrypt, and then the pod is destroyed by Cert-Manager, which will store the certificate in a secret.
This same secret will be used by the Ingress specific to the exposed application…and that's it.

Click the image to enlarge.
Each certificate provided by Let's Encrypt is valid for 6 months…and the best part is that Cert-Manager will automatically take care of making renewal requests as the expiration date approaches.
Installing Cert-Manager
The first step is to dedicate a namespace to hosting the resources specific to Cert-Manager.
This is not mandatory, but it's always cleaner.
To do this, I stick to my naming convention defined at the beginning of the project, and I run the following command to create a namespace named inf-cert-lan.
kubectl create ns inf-cert-lan

Click the image to enlarge.
Next, we switch to using Helm (deployed in this article), since Cert-Manager has a package available.
Simply call the following instructions:
helm repo add jetstack https://charts.jetstack.io
helm repo update
helm install cert-manager jetstack/cert-manager --namespace inf-cert-lan --version v1.15.1 --set installCRDs=true --set nodeSelector.network=lan

Click the image to enlarge.

Click the image to enlarge.
The only customization needed at this level is declaring the version of Cert-Manager to use. At the time of this article, it's 1.15.1
Cert-Manager is now available on the cluster. You can verify this by typing the command.
kubectl get pod -n inf-cert-lan -o wide

Click the image to enlarge.
We can clearly see the pods specific to CertManager.
Validating the installation
To illustrate how everything works, we'll start from the example in the previous article, but this time we'll publish the application on the web, through a URL on my domain myprivatelab.tech.
Therefore, we declare a new namespace named dev-democertmanager-dmz:
kubectl create ns dev-democertmanager-dmz

Click the image to enlarge.
The first file used is 01-deploy-democertmanager-front.yaml, and its content is as follows:
apiVersion: apps/v1
kind: Deployment
metadata:
name: deploy-democertmanager-front
namespace: dev-democertmanager-dmz
labels:
environment: dev
network: dmz
application: democertmanager
spec:
strategy:
type: Recreate
selector:
matchLabels:
environment: dev
network: dmz
application: democertmanager
template:
metadata:
labels:
environment: dev
network: dmz
application: democertmanager
spec:
nodeSelector:
network: dmz
tolerations:
- key: "node-role.kubernetes.io/worker-dmz"
operator: "Exists"
effect: "NoSchedule"
containers:
- name: demo-nginx-traefik-lan
image: nginx:1.27
resources:
requests:
memory: "8Mi"
cpu: "250m"
limits:
memory: "256Mi"
cpu: 1
livenessProbe:
initialDelaySeconds: 5
periodSeconds: 5
exec:
command:
- ls /usr/share/nginx/html
ports:
- containerPort: 80
It reuses the creation of a Deployment object that was used previously, but this time with the execution of the attached pod running on a node in the DMZ (see infrastructure presentation).
This selection is done via the following options:
nodeSelector:
network: dmz
tolerations:
- key: "node-role.kubernetes.io/worker-dmz"
operator: "Exists"
effect: "NoSchedule"
Make sure to choose a node with the label network: dmz, and allow the pod to support the worker-dmz constraint (see here for more explanation).
We are still using a simple NGINX image in version 1.27.
We apply the filekubectl apply -f 01-deploy-democertmanager-front.yaml

Click the image to enlarge.
Next, we create the service using the file 02-svc-democertmanager-front.yaml.
This will connect to the pod thanks to the labels defined in the selector option (again, we will see the overall logic of deploying an application in a dedicated article).
The content of the file is as follows:
---
kind: Service
apiVersion: v1
metadata:
name: svc-democertmanager-front
namespace: dev-democertmanager-dmz
labels:
environment: dev
network: dmz
application: democertmanager
spec:
ports:
- name: http
port: 80
protocol: TCP
targetPort: 80
selector:
environment: dev
network: dmz
application: democertmanager
The application is deployed using the following command:
kubectl apply -f 02-svc-democertmanager-front.yaml

Click the image to enlarge.
The following file 03-issuer-democertmanager-letsencrypt.yaml concerns the issuer.
---
apiVersion: cert-manager.io/v1
kind: Issuer
metadata:
name: issuer-democertmanager-letsencrypt
namespace: dev-democertmanager-dmz
labels:
environment: dev
network: dmz
application: democertmanager
spec:
acme:
email: [email protected]
server: https://acme-v02.api.letsencrypt.org/directory
privateKeySecretRef:
name: sec-issuer-democertmanager
solvers:
- http01:
ingress:
class: traefik-dmz
podTemplate:
metadata:
labels:
network: dmz
spec:
nodeSelector:
network: dmz
tolerations:
- key: "node-role.kubernetes.io/worker-dmz"
operator: "Exists"
effect: "NoSchedule"
ingressTemplate:
metadata:
labels:
network: dmz
We call a different branch of the K8S API, specific to Cert-Manager.
Besides the standard labels and the specification of the namespace to use, we reach the spec section.
From this point onwards, we will indicate that this issuer, called issuer-democertmanager-letsencrypt, is based on the ACME protocol.
Then we enter the protocol subsection to define the properties associated with it. The most important thing is the URL of the PKI that supports ACME to be contacted in order to make a request and establish the challenge.
Let's Encrypt has several URLs, some of which are dedicated to test environments, while others are for production environments. Keep in mind that you cannot make too many requests in a very short period.
You must respect the terms of use and avoid constantly requesting the service, otherwise you may be banned for a defined period.
I invite you to read the associated documentation to learn more and obtain more information about the terms of use of Let's Encrypt.
I would also like to point out that for a certificate generated by Let's Encrypt to be recognized on your devices, these devices must consider Let's Encrypt as a trusted PKI and therefore have the public CA of Let's Encrypt in their repository.
This is the case for most recent operating systems and browsers, but it is possible that some specific devices require prior configuration to recognize Let's Encrypt.
The other important component of our issuer configuration concerns the solver section. This is where we specify the use of an HTTP-based challenge. It is also at this level, and particularly in the sub-sections podTemplate and IngressTemplate, that we will define the characteristics that the temporary pods and Ingresses must have.
Without this information, Traefik will not be able to detect the creation of these components, and therefore will not create the necessary routing rules.
The challenge will then never be completed, and the certificate will never be obtained.
We apply the following command to apply our file:
kubectl apply -f 03-issuer-democertmanager-letsencrypt.yaml

Click the image to enlarge.
We can check the status of our issuer using the following instruction:
kubectl describe issuer -n dev-democertmanager-dmz

Click the image to enlarge.
(Please note that I do not specify the name of the issuer because, as it is the only one in the namespace, it is selected by default... this works for other objects).
The most interesting part is the Status section.
We can confirm that our issuer has been correctly registered with Let's Encrypt. It is therefore ready to relay certificate requests.
Now it's time for the last file: 04-ing-democertmanager-front.yaml.
For once, I will not declare that there is one object per file, but rather two.
The first is a Certificate object from the Cert-Manager API.
---
apiVersion: cert-manager.io/v1
kind: Certificate
metadata:
name: cert-myprivatelab-default
namespace: dev-democertmanager-dmz
labels:
environment: prd
network: dmz
application: myprivatelab
tier: default
spec:
commonName: democertmanager.myprivatelab.tech
secretName: sec-certificate-democertmanager
dnsNames:
- democertmanager.myprivatelab.tech
issuerRef:
kind: Issuer
name: issuer-democertmanager-letsencrypt
In the spec section of this object, we find characteristics specific to certificates: the commonName and one or more dnsNames.
This is typically what you would find in a CSR (Certificate Signing Request SSL), as we saw in the previous article. We could also add additional characteristics and information that we want to be included in the certificate.
We also specify the secretName, which is the name of the secret where the certificate will be stored.
Finally, in the issueRef sub-section, we specify the issuer to call, in this case, the one we just created.
We move on to the second part of the file, which concerns the Ingress object. Here, we are essentially repeating what we saw previously.
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: ing-democertmanager-default
namespace: dev-democertmanager-dmz
labels:
environment: dev
network: dmz
application: democertmanager
annotations:
traefik.ingress.kubernetes.io/router.entrypoints: web,websecure
ingressClassName: traefik-dmz
traefik.ingress.kubernetes.io/router.tls: "true"
spec:
ingressClassName: traefik-dmz
tls:
- hosts:
- democertmanager.myprivatelab.tech
secretName: sec-certificate-democertmanager
rules:
- host: democertmanager.myprivatelab.tech
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: svc-democertmanager-front
port:
number: 80
Only the "dmz" environment replaces the "lan" environment because this time we are exposing our application on the web.
Here, we find the routing rules, as well as a reference to the secret containing the certificate, which must of course be the same as the one specified just above.
We apply everything.
kubectl apply -f 04-ing-democertmanager-front.yaml

Click the image to enlarge.
Several things are interesting.
If we list the pods in the namespace:
kubectl get pod -n dev-democertmanager-dmz -o wide

Click the image to enlarge.
We now see two pods. The first belongs to our application, and a second one starts with cm-acme-http-solver.
It is this second pod that will deliver the web content expected by Let's Encrypt.
If we observe the Traefik dashboard in the dmz, we can see the new routing rules specific to redirecting requests for the challenge.

Click the image to enlarge.
These components only appear for a very short time, because once the challenge is successful, the pod deployed by Cert-Manager and its associated objects are automatically deleted.
If we list the pods again, only our application pod remains, and on the Traefik side, only the routing rule specific to our test application is visible.

Click the image to enlarge.

Click the image to enlarge.
In fact, we can check the status of the certificate directly with the following command:
kubectl get certificate -n dev-democertmanager-dmz o wide

Click the image to enlarge.
The READY section is set to true
All that remains is to verify the success of the operation. This is very simple: just go to the URL of our site.
It responds, delivers its content, and we can see that the certificate used is valid and issued by Let's Encrypt.

Click the image to enlarge.

Click the image to enlarge.
Conclusion
Certificate management is a very important element in modern applications. They are widely used, and their usage is becoming increasingly restrictive regarding their security level (encryption used, lifespan...).
The use of components like Cert-Manager is a key advantage in a microservices environment and provides welcome peace of mind for administrators.
Once again, we see that the kubernetes ecosystem benefits from broad support from third-party tools, with complementary interactions such as Cert-Manager and Traefik.
As I mentioned in my definition of kubernetes, we are truly in a modular logic where everyone can use the building blocks that interest them to build a platform that meets their requirements.
In the example shown here, a public PKI, specifically Let's Encrypt, is used. However, nothing prevents this logic from being applied to other PKIs, such as internal PKIs. Even a Microsoft PKI, like the one used in the previous article, can be used… thanks to ACME proxies like acme2certifier.
We can finally deploy a real application. All that remains is to build upon all the components deployed previously… but for that, you need to refer to this article.