KubeVirt - Part 3: Traefik, Gateway API & CertManager
- Useful additional components for the following steps: Traefik, Gateway API, Cert-Manager.
- Exposing GUIs without worrying about publishing.
- A single-node Traefik instance on the LAN is available here.
- A valid combination even outside of KubeVirt.
Introduction
All essential components have been deployed in previous parts for running KubeVirt. The target was defined with the host server configuration, and then the main components were installed.
Before proceeding further, it is necessary to implement some additional components that are not directly related to KubeVirt but are still very useful for the following steps and often essential for a Kubernetes cluster.
Let's not forget that just because we are running VMs doesn't mean we don't want the option to run more traditional containerized applications.
The goal is to have a use of Kubernetes for both virtual machines and containers.
In this context, it becomes necessary to add an ingress controller to the K8S cluster that will be compatible with the Gateway API, specifically Traefik, and a solution for automated certificate management, namely Cert-Manager.
Either one will allow access to the GUIs of the various tools that we will deploy later.
Target schema
Here is an illustration diagram summarizing the components that we are going to implement
Click the image to enlarge.
Traefik
I have already had the opportunity to introduce Traefik. Feel free to read my dedicated article on the subject and integrated into my K8S CookBook.
Traefik is an ingress controller, meaning it allows you to expose your K8S services externally from the cluster, offering features such as automatic certificate addition, URL rewriting, and password protection.
With the introduction of the Gateway API that I presented in this article, Traefik has become a GatewayClass and therefore allows you to take advantage of this new way of doing things for managing your external publications.
Creation of the namespace and default certificate
We start by implementing a dedicated namespace: inf-traefik-lan
kubectl create ns inf-traefik-lan
Click the image to enlarge.
This will allow me to run all the necessary dependencies and distinguish my Traefik instance, which is dedicated to my internally published applications, from a future Traefik instance in the DMZ. If I were to dedicate nodes in my cluster to hosting applications in the DMZ, I could do so. This is the use case that I describe in my K8S cookbook, but here, since I only have one node, I will stick with my LAN instance of Traefik.
Next, we create a generic certificate that will serve as both the certificate for accessing the Traefik dashboard and the default certificate for the Gateway.
Everyone has their own technique. I won't go into details at this stage.
For my part, I will choose the publication name traefikrubikub-gtw-lan.coolcorp.priv
I generate the private key and the CSR (certificate signing request) using OpenSSL
openssl req -new -nodes -sha256 -keyout traefikrubikub-gtw-lan.coolcorp.priv.key -out traefikrubikub-gtw-lan.coolcorp.priv.csr -newkey rsa:4096 -subj "/C=FR/ST=Ile-de-France/L=Paris/O=COOLCORP/OU=Infrastructure/CN=traefikrubikub-gtw-lan.coolcorp.priv" -reqexts SAN -config <(printf "[req]\ndistinguished_name = req_distinguished_name\n[req_distinguished_name]\n[SAN]\nsubjectAltName=DNS:traefikrubikub-gtw-lan.coolcorp.priv")
Then, I submit my CSR to my internal PKI (a Windows Server CA integrated into my AD domain).
certreq -attrib "CertificateTemplate:TPL-SRV-WEB-DEFAULT" -submit .\traefikrubikub-gtw-lan.coolcorp.priv.csr
To retrieve my certificate.
Obtaining this certificate is specific to each individual and their internal certificate solution. You just need to be able to generate a standard web server certificate, with a CN attribute and an identical SubjectAltName.
We will store this certificate and the associated private key in a Kubernetes secret within the namespace dedicated to Traefik
kubectl create secret tls sec-gateway-default-cert --cert=traefikrubikub-gtw-lan.coolcorp.priv.cer --key=traefikrubikub-gtw-lan.coolcorp.priv.key -n inf-traefik-lan
Click the image to enlarge.
Deployment of Traefik
Deployment of the service account
Traefik needs to interact with the cluster to query the API.
Therefore, we will use a service account for this. Rather than letting the installation create an account, we will generate it beforehand using the svc-traefik-lan.yaml file
apiVersion: v1
kind: ServiceAccount
metadata:
name: "svc-prd-traefik"
namespace: "inf-traefik-lan"
Simply apply it.
kubectl apply -f svc-traefik-lan.yaml
Click the image to enlarge.
I won't go into more detail; feel free to consult my dedicated article on this topic.
Deployment of the Helm package
As with Cilium deployed in the previous article, we will use Helm to deploy Traefik.
We declare the repositories
helm repo add traefik https://helm.traefik.io/traefik
helm repo update
Click the image to enlarge.
I label my node so that it is selected for running Traefik pods, in relation to the configuration that I will associate with Traefik.
kubectl label node prdk8sctp001 network=lan
kubectl label node prdk8sctp001 traefik-lan=yes
Click the image to enlarge.
This configuration is located in the traefik-config-lan.yaml file
image:
tag: "3.6.4"
pullPolicy: IfNotPresent
deployment:
enabled: true
kind: DaemonSet
labels:
network: lan
podLabels:
network: lan
gatewayClass:
enabled: true
name: traefik-lan
labels:
network: lan
gateway:
enabled: false
providers:
kubernetesGateway:
enabled: true
labelSelector: "network=lan"
kubernetesIngress:
labelSelector: "network=lan"
allowExternalNameServices: true
kubernetesCRD:
enabled: true
allowCrossNamespace: false
allowExternalNameServices: true
allowEmptyServices: false
namespaces:
- inf-traefik-lan
- longhorn-system
- inf-kubevirtmanager-lan
serviceAccount:
name: "svc-prd-traefik"
logs:
general:
level: ERROR
hostNetwork: true
ports:
web:
port: 80
expose:
default: true
redirections:
entryPoint:
to: websecure
scheme: https
permanent: true
websecure:
port: 443
expose:
default: true
proxyProtocol:
trustedIPs:
- "192.168.10.160"
forwardedHeaders:
trustedIPs:
- "192.168.10.160"
tls:
enabled: true
securityContext:
capabilities:
drop: [ALL]
add: [NET_BIND_SERVICE]
readOnlyRootFilesystem: true
runAsGroup: 0
runAsNonRoot: false
runAsUser: 0
updateStrategy:
rollingUpdate:
maxSurge: 0
maxUnavailable: 1
type: RollingUpdate
additionalArguments:
- "--providers.kubernetesingress.ingressclass=traefik-lan"
- "--serversTransport.insecureSkipVerify=true"
Overall, we indicate that we are deploying Traefik as a DaemonSet, which will allow for automatic scaling of the number of pods associated with any new node that joins the cluster and meets the eligibility criteria.
Next, we specify that we will be using the Gateway API by enabling Traefik as the gatewayClass, which we will name traefik-lan.
However, we continue to enable the more traditional providers, which are:
- kubernetesIngress: The original default solution provided by K8S for exposing assets
- kubernetesCRD: Custom Resource Definition of Traefik, i.e., the custom objects specific to Traefik that allow you to use features specific to Traefik.
Note that CRDs are activated per namespace, which means that you may need to redeploy Traefik later if a new namespace requires the use of custom objects from Traefik. In my case, I anticipated this by specifying the namespaces that we will use later.
This way, we can cover all needs.
We specify the usage of the service account that we deployed previously.
Now, you just need to execute the following helm command:
helm install --namespace=inf-traefik-lan traefik-lan traefik/traefik -f traefik-config-lan.yaml --set nodeSelector.traefik-lan=yes --set tolerations[0].key=node-role.kubernetes.io/control-plane --set tolerations[0].operator=Exists --set tolerations[0].effect=NoSchedule
Note the additions to the command that support running a Traefik pod on a control plane and the use of the traefik-lan selector set to yes to match the node labeling performed previously.
Click the image to enlarge.
At this stage, we can verify that Traefik has been deployed correctly.
kubectl get pod -n inf-traefik-lan
Click the image to enlarge.
Deployment of the Gateway API.
We want to use the API Gateway with Traefik in order to comply with the new trends that favor the Gateway API over Ingress.
Feel free to read my dedicated article on this topic for a better understanding of this point.
Note that, in this article, I discuss the installation of the Gateway API beforehand because it is not included by default in Kubernetes. But here, it is implicitly installed with its activation in the Traefik configuration.
That's why we can directly use the gateway-lan.yaml file to create the gateway object.
Here is its content:
apiVersion: gateway.networking.k8s.io/v1
kind: Gateway
metadata:
name: traefik-gateway-lan
namespace: inf-traefik-lan
labels:
network: lan
spec:
gatewayClassName: traefik-lan
listeners:
- name: web
port: 80
protocol: HTTP
allowedRoutes:
namespaces:
from: Selector
selector:
matchLabels:
network: lan
- name: websecure
port: 443
protocol: HTTPS
allowedRoutes:
namespaces:
from: Selector
selector:
matchLabels:
network: lan
tls:
mode: Terminate
certificateRefs:
# Domaine pour les certificat dynamique
- name: sec-tls-wildcard-coolcorp-priv
namespace: inf-cert-lan
# Certificat par défaut
- name: sec-gateway-default-cert
We host it in the same namespace as Traefik.
Note that we mention the use of a wildcard certificate referenced under the secret sec-tls-wildcard-coolcorp-priv. For now, don't worry about it; it will come later. For now, we have our default certificate.
It is crucial to note that, in the architecture using the Gateway API, the Gateway object serves to link a certificate to a URL, which allows it to be accessed from the outside by relying on the Gateway class, in this case Traefik.
We deploy the gateway:
kubectl apply -f gateway-lan.yaml
Click the image to enlarge.
At this stage, if you check its status, it is possible that it is not operational due to a missing secret, but this will be addressed later
kubectl describe gateway -n inf-traefik-lan
Click the image to enlarge.
Click the image to enlarge.
Deploying the dashboard.
All that remains is to deploy the Traefik dashboard to provide a GUI for the tool and monitor what is happening.
Although the Traefik GUI only allows read access, we will still protect it with a login and password.
To do this, we first use the htpasswd tool available in the httpd-tools package (sudo dnf install httpd-tools) to generate the necessary files in the correct format
htpasswd -Bbn bvivi57 'pass' > traefik-dashboard-lan-password
Click the image to enlarge.
We then use the file containing the credentials to include them in a Kubernetes secret
kubectl create secret generic sec-traefik-dashboard-lan-password --from-file=traefik-dashboard-lan-password --namespace inf-traefik-lan
Then, we apply the contents of the traefik-lan-dashboard.yaml file.
apiVersion: traefik.io/v1alpha1
kind: IngressRoute
metadata:
name: traefik-lan-dashboard
namespace: "inf-traefik-lan"
spec:
entryPoints:
- websecure
routes:
- kind: Rule
match: Host(`traefikrubikub-gtw-lan.coolcorp.priv`) && (PathPrefix(`/api`) || PathPrefix(`/dashboard`))
services:
- name: api@internal
kind: TraefikService
middlewares:
- name: traefik-dashboard-lan-auth # Referencing the BasicAuth middleware
namespace: inf-traefik-lan
tls:
secretName: sec-gateway-default-cert
---
apiVersion: traefik.io/v1alpha1
kind: Middleware
metadata:
name: traefik-dashboard-lan-auth
namespace: inf-traefik-lan
spec:
basicAuth:
secret: sec-traefik-dashboard-lan-password
using the command kubectl apply -f traefik-lan-dashboard.yaml
Click the image to enlarge.
In my example, this allows me to expose the GUI on the URL https://traefikrubikub-gtw-lan.coolcorp.priv/dashboard, using the certificate created previously and shared by the gateway.
The URL is protected by the login and password that we have chosen.
All that remains is to verify that the URL is accessible. (It is important to note that I have created a DNS alias on my DNS server to redirect the default IP address of my node, and that this node has ports 443 and 80 open (configured during the deployment of the node in the first article via the Ansible role)).
In this case, the URL is served by a custom Traefik object and not by the Gateway. However, we can see in the GUI that the Gateway API is being taken into account by Traefik.
Click the image to enlarge.
We will use it later for other purposes.
Cert-Manager
Cert-Manager is an additional component that can be deployed in Kubernetes to automate the entire lifecycle of certificates associated with applications deployed on the cluster.
I detail its operation and usage here. Feel free to take a look.
In our kubevirt tutorial, it will be used to manage all the certificates that we might need for third-party tools associated with KubeVirt, and even to cover any future needs for publishing applications.
Unlike the article where I associate Cert-Manager with acme2certifier, an ACME proxy that allows interaction with a Windows PKI, I will choose a different strategy here, which is simpler and more effective.
I will simply ask my root PKI hosted on my Windows server (Active Directory Certificate Services (AD CS)) to generate an intermediate certificate authority.
I will associate a ClusterIssuer object, which is an object specific to Cert-Manager and responsible for issuing certificates internally within the cluster, namely the intermediate certificate authority.
In this way, this ClusterIssuer will be a trusted authority validated by my CA ROOT. It will then be able to issue a wildcard certificate on my domain, automatically renewed every three months.
This strategy will allow me to completely eliminate the constraints of certificates. The critical point will be the Kubernetes secret that will store the private key and the intermediate authority certificate. This secret must absolutely not be accessible by third parties; remember that, under K8S, a secret is nothing more than a base 64 value from which the content can be easily retrieved. Therefore, it is important to properly manage your access strategies to your K8S clusters. Feel free to read my articles on the subject.
Deployment of Cert-Manager
As with Traefik, we will use Helm.
But first, we create a dedicated namespace
kubectl create ns inf-cert-lan
Click the image to enlarge.
We can deploy Cert-Manager directly into it, after making sure to declare the repository.
helm repo add jetstack https://charts.jetstack.io
helm repo update
helm install cert-manager jetstack/cert-manager --namespace inf-cert-lan --version v1.19.2 --set installCRDs=true --set nodeSelector.network=lan
Click the image to enlarge.
Click the image to enlarge.
Note that here, we are not using any configuration files. We simply request a specific version of Cert-Manager (here, 1.19.2), install the Cert-Manager Custom Resources, and select the node with the correct label (the same one we used for Traefik).
At this stage, we can verify that everything is in order with Cert-Manager.
Kubectl get pod -n inf-cert-lan
Deployment of the issuer
As explained previously, we will create a certificate specific to an intermediate CA.
We start by creating the key and the CSR (Certificate Signing Request)
openssl req -new -newkey rsa:4096 -nodes -keyout subordinate-ca-rubikube.key -out subordinate-ca-rubikube.csr -config ca-csr.conf
Then we create the certificate. In the Microsoft world, there is a template for this called SubCA
certreq -attrib "CertificateTemplate:SubCA" -submit .\subordinate-ca-rubikube.csr
Click the image to enlarge.
With the key and the certificate (which are both very sensitive data, I repeat), we can create the secret that will contain everything
kubectl create secret tls sec-subordinate-ca-rubikube --cert=subordinate-ca-rubikube.cer --key=subordinate-ca-rubikube.key --namespace=inf-cert-lan
Click the image to enlarge.
Everything is ready for the issuer, whose description is contained in the 01-clusterissuer-lan.yml file
apiVersion: cert-manager.io/v1
kind: ClusterIssuer
metadata:
name: clusterissuer-lan
spec:
ca:
secretName: sec-subordinate-ca-rubikube
It references the secret we just created and explicitly defines it as a ClusterIssuer, which means an issuer compatible with any namespace.
Feel free to consult my article on Cert-Manager from my K8S cookbook if you want to know more.
kubectl apply -f 01-clusterissuer-lan.yml
Click the image to enlarge.
We can verify that the issuer is operational.
kubectl get clusterissuer
Creation of the wildcard
Now that the issuer is up, we can use it to create our wildcard certificate via the 02-certificat-wildcard.yml file
apiVersion: cert-manager.io/v1
kind: ClusterIssuer
metadata:
name: clusterissuer-lan
spec:
ca:
secretName: sec-subordinate-ca-rubikube
apiVersion: cert-manager.io/v1
kind: Certificate
metadata:
name: cert-wildcard-coolcorp
namespace: inf-cert-lan
spec:
secretName: sec-tls-wildcard-coolcorp-priv
issuerRef:
name: clusterissuer-lan
kind: ClusterIssuer
commonName: "*.coolcorp.priv"
dnsNames:
- "coolcorp.priv"
- "*.coolcorp.priv"
# Optionnel : Renouvellement auto 30 jours avant expiration
renewBefore: 720h
Within this file, I am requesting my newly created ClusterIssuer to generate a wildcard certificate for my internal domain
kubectl apply -f 02-certificat-wildcard.yml
Click the image to enlarge.
I can see the certificate in my dedicated namespace
kubectl get cert -n inf-cert-lan
Click the image to enlarge.
This namespace is not the gateway's namespace. However, if you look higher up in the configuration of the gateway, I was already referencing this certificate via its associated secret in that specific namespace.
To allow the gateway to use a certificate hosted outside of its default namespace, we need to add a delegation using the ReferenceGrant object.
This is described in my file 03-referentgrant-wildcart.yml
---
apiVersion: gateway.networking.k8s.io/v1beta1
kind: ReferenceGrant
metadata:
name: allow-traefik-read-coolcorp
namespace: inf-cert-lan
spec:
from:
- group: gateway.networking.k8s.io
kind: Gateway
namespace: inf-traefik-lan
to:
- group: ""
kind: Secret
name: sec-tls-wildcard-coolcorp-priv[
This allows the gateway to utilize the new wildcard certificate that I just created thanks to my ClusterIssuer, which itself is authorized as an intermediate CA.
kubectl apply -f 03-referentgrant-wildcart.yml
Click the image to enlarge.
We can verify that this works because if we query the gateway again, it is now in the 'true' state.
kubectl get gateway -n inf-traefik-lan
Click the image to enlarge.
Intermediate conclusion
This part of the tutorial moves slightly away from KubeVirt and touches on a more general use of Kubernetes. However, I felt it was important to include it, as it will greatly simplify subsequent operations by eliminating the need to worry about issues related to publishing GUIs, such as that of Longhorn, which we will need later.
Feel free to use this configuration for your K8S clusters, even if KubeVirt is not part of your requirements. The combination of Traefik, Gateway API, and Cert-Manager can be used for many purposes.
For the next steps, it's over there