Step 4: Deploying the K8S configuration
- Direct continuation of step 3: generating the K8S configuration files.
- Details of the 05-configure-k8s.yml playbook and its templates.
- Explanations of the configured Kubernetes components.
- Choosing a manual cluster initialization, with automation stopping at the configuration stage.
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: the equivalent step is Step 04 - Kubernetes Prerequisites. The article remains online for reference.
Introduction
After seeing the part of the Ansible role responsible for deploying all the prerequisites for installing Kubernetes on the servers, it's time to move on to preparing the actual configuration of the K8S components.
This article is directly related to the previous one. It is recommended to review what was said previously before starting to read what follows.
For reference:
- 1 - The target architecture has been defined
- 2 - The associated VMs have been deployed with OpenTofu (Terraform)
- 3 - A set of actions have been written in the form of a playbook (playbook_deploy_k8s.yml) and an Ansible role (role_k8s_deploy) to establish the prerequisites.
This role will continue with the rest of the files and templates associated with the Ansible tree presented in the previous article.
We had stopped at the deployment of containerd through the file 04-containerd.yml.
05-configure-k8s.yml
The file 05-configure-k8s.yml is responsible for preparing the configuration elements that will be used by Kubernetes.
As with the YAML files presented in the previous article, it will first be displayed in its entirety (see below), and then the logic and purpose of the Ansible actions that compose it will be described.
---
#Configuration pour Kubeadm
- name: "Set kubeadm configuration"
become: yes
template:
src: "kubeadm.conf.j2"
dest: "/etc/kubernetes/kubeadm.conf"
when: "'TAG-COOLCORP-ROLE-MASTER' in tags"
tags: role_k8s_deploy.configure-k8s.kubeadm-config
#Création des repertoires de destination pour les configurations
- name: Creates configuration directory for admin user
become: yes
file:
path: "/home/{{ var_admin_user }}/{{ var_vip_kub_endpoint }}"
state: directory
owner: "{{ var_admin_user }}"
mode: 0770
when: "'TAG-COOLCORP-ROLE-MASTER' in tags"
- name: Creates network configuration directory for admin user
become: yes
file:
path: "/home/{{ var_admin_user }}/{{ var_vip_kub_endpoint }}/network"
state: directory
owner: "{{ var_admin_user }}"
mode: 0770
when: "'TAG-COOLCORP-ROLE-MASTER' in tags"
- name: Creates cloud configuration directory for admin user
become: yes
file:
path: "/home/{{ var_admin_user }}/{{ var_vip_kub_endpoint }}/cloud"
state: directory
owner: "{{ var_admin_user }}"
mode: 0770
when: "'TAG-COOLCORP-ROLE-MASTER' in tags"
- name: Creates cloud cpi configuration directory for admin user
become: yes
file:
path: "/home/{{ var_admin_user }}/{{ var_vip_kub_endpoint }}/cloud/cpi"
state: directory
owner: "{{ var_admin_user }}"
mode: 0770
when: "'TAG-COOLCORP-ROLE-MASTER' in tags"
tags: role_k8s_deploy.configure-k8s.cpi
- name: Creates cloud csi configuration directory for admin user
become: yes
file:
path: "/home/{{ var_admin_user }}/{{ var_vip_kub_endpoint }}/cloud/csi"
state: directory
owner: "{{ var_admin_user }}"
mode: 0770
when: "'TAG-COOLCORP-ROLE-MASTER' in tags"
- name: Creates kube directory for adminuser
become: yes
file:
path: "/home/{{ var_admin_user }}/.kube"
state: directory
owner: "{{ var_admin_user }}"
mode: 0770
when: "'TAG-COOLCORP-ROLE-MASTER' in tags"
#Configuration cilium
- name: "Set cilium configuration"
become: yes
template:
src: "cilium-values.yaml.j2"
dest: "/home/{{ var_admin_user }}/{{ var_vip_kub_endpoint }}/network/cilium-values.yaml"
owner: "{{ var_admin_user }}"
mode: 0770
when: "'TAG-COOLCORP-ROLE-MASTER' in tags"
tags: role_k8s_deploy.configure-k8s.cilium
#Récupération de la CLI de cilium
- name: Download and extract cilium cli
become: yes
ansible.builtin.unarchive:
src: "{{ var_cilium_cli }}"
dest: /usr/local/bin
remote_src: yes
when: "'TAG-COOLCORP-ROLE-MASTER' in tags"
tags: role_k8s_deploy.configure-k8s.cilium"
#Configuration du Cloud Provider Interface (vSphere)
- name: "Set vSphere CPI configuration"
become: yes
template:
src: "vsphere-cloud-controller-manager.yaml.j2"
dest: "/home/{{ var_admin_user }}/{{ var_vip_kub_endpoint }}/cloud/cpi/vsphere-cloud-controller-manager.yaml"
owner: "{{ var_admin_user }}"
mode: 0770
when: "'TAG-COOLCORP-ROLE-MASTER' in tags"
tags: role_k8s_deploy.configure-k8s.cpi
#Configuration du Container Storage Interface (vSphere)
- name: "Set namespace csi config"
become: yes
template:
src: "csi-namespace.yaml.j2"
dest: "/home/{{ var_admin_user }}/{{ var_vip_kub_endpoint }}/cloud/csi/namespace.yaml"
owner: "{{ var_admin_user }}"
mode: 0770
when: "'TAG-COOLCORP-ROLE-MASTER' in tags"
tags: role_k8s_deploy.configure-k8s.csi-namespace-config
- name: "Set csi config"
become: yes
template:
src: "csi-vsphere.conf.j2"
dest: "/home/{{ var_admin_user }}/{{ var_vip_kub_endpoint }}/cloud/csi/csi-vsphere.conf"
owner: "{{ var_admin_user }}"
mode: 0770
when: "'TAG-COOLCORP-ROLE-MASTER' in tags"
tags: role_k8s_deploy.configure-k8s.csi-config
- name: "Set csi yaml"
become: yes
template:
src: "vsphere-csi-driver.yaml.j2"
dest: "/home/{{ var_admin_user }}/{{ var_vip_kub_endpoint }}/cloud/csi/vsphere-csi-driver.yaml"
owner: "{{ var_admin_user }}"
mode: 0770
when: "'TAG-COOLCORP-ROLE-MASTER' in tags"
tags: role_k8s_deploy.configure-k8s.csi-config
For reference, all values for variables called are declared in the main.yml file in the defaults folder of the Ansible tree.
This same tree contains the templates folder, which contains the *.j2 files that serve as a basis for being copied to the servers.
When copied to the destination, variables declared within the templates will be replaced with their values from the main.yml file in the defaults directory.
kubeadm configuration
The process starts by creating the kubadm.conf file, which will be deployed to each control plane at /etc/kubernetes
The file is based on the kubeadm.conf.j2 template, located in the templates directory of the Ansible role.
---
apiVersion: kubeadm.k8s.io/{{ var_api_version }}
kind: ClusterConfiguration
kubernetesVersion: {{ var_kub_version }}
networking:
podSubnet: "{{ var_pod_subnet }}"
serviceSubnet: "{{ var_service_subnet }}"
controlPlaneEndpoint: "{{ var_vip_kub_endpoint }}:6443"
clusterName: "{{ var_kub_name }}"
controllerManager:
extraArgs:
cloud-provider: external
apiServer:
certSANs:
- {{ var_vip_kub_vip }}
- {{ var_vip_kub_endpoint }}
Kubeadm is the default installer for Kubernetes; it follows the project's versions and allows you to initialize the cluster, add nodes, and update it.
Other methods are available for deploying K8S, but kubeadm remains widely used. However, it is important to note that a cluster installed with kubeadm must be maintained with kubeadm. It is strongly discouraged to use other management and installation methods later on.
It is possible to run kubeadm directly, but in this case, we will pass the configuration file generated by Ansible when we reach the cluster initialization step.
This allows you to fix the parameters in advance. Most of these parameters are defined as variables and are therefore available in the main.yaml file in the defaults directory of the Ansible role.
var_api_version refers to the version of the Kubernetes API that kubeadm uses to define and manage the cluster configuration. Since this evolves over time, it is useful to be able to change it easily. Currently, it is v1beta3
var_kub_version is the version of Kubernetes that you want to deploy; in this example, it is 1.29.4.
var_pod_subnet represents the subnet dedicated to the pod. This is an internal network within the cluster. We will have the opportunity to revisit this later, but a pod is the basic element for K8S. There is no lower-level object. The container itself is not represented in the Kubernetes API; it is the pod that is used. A pod, on the other hand, can contain one or more containers.
Always choose a subnet for this network with enough IP addresses, as the number of pods tends to increase very quickly. In my case, I use the subnet 10.11.0.0/16.
It is also very important not to use a network that is already in use within your infrastructure and/or routed by you, otherwise you may encounter problems.
var_service_subnet is also a network, but this time it is associated with objects of type service. We will have the opportunity to discuss this again later, but a service allows you to expose your pods. Without the use of services, your pods, and the containers within them, are isolated and cannot communicate either within the cluster or outside of it.
As with the network dedicated to the pod, you need to choose a subnet that has many IP addresses and an unused range on your infrastructure. In the cluster deployed here, we will use 10.12.0.0/16.

Click the image to enlarge.
var_vip_kub_endpoint is the DNS name of the cluster on which the K8S API will be exposed.
As a reminder, the API is provided by the control plane. In the target architecture, we have three servers of this type to ensure high availability of the service.
We use a load balancer upstream that redirects requests to one of the control planes. The DNS name must therefore resolve to the IP address of the load balancer. The chosen name is kub.coolcorp.priv.
var_kub_name is the short name of the cluster, which is simply 'kub' in my case.

Click the image to enlarge.
var_vip_kub_vip is the IP address of the load balancer (192.168.10.91) and is used a little further down, in the certSANs section of the template, along with var_vip_kub_endpoint, which was previously mentioned.
In Kubernetes, all traffic is encrypted. To achieve this, Kubernetes provides its own root certificate authority that is responsible for creating all internal system certificates. It is therefore important that the certificate that will be attached to the API includes the IP address and name by which it will be called.
Creating the configuration folders
The subsequent steps described in the Ansible file 05-configure-k8s.yml allow you to define directories for hosting the various configuration files that we will generate.
Personally, I use my home directory, within which I create subfolders by scope.
- For network configuration, in order to contain the configurations specific to the CNI driver used here (Cilium).
- For configuring the cloud provider, to contain configurations specific to managing the cloud infrastructure on which the cluster is deployed. In the architecture presented here, the cloud is actually the VMware environment, or simply access to vCenter. This is a prerequisite for the CSI storage driver.
- For storage configuration. It is planned to use the driver provided by VMware to rely on datastores as a solution for provisioning persistent volumes. (Feel free to review this article for more explanation).
I will not go into detail about the directories, but will proceed directly to creating the Cilium configuration file.
Configuration for Cilium
Cilium has been chosen in the architecture as a networking solution. As a reminder, Kubernetes, in its modular approach, does not provide a driver for managing the network. There are many available, with different characteristics, such as the routing protocol used and the integrated filtering capabilities. The important thing is to select a provider compatible with CNI (feel free to review the architecture section).
Cilium is increasingly deployed due to its integration of eBPF (extended Berkeley Packet Filter), an advanced technology for analyzing and manipulating network packets. We will go into detail about this at the appropriate time.
A cilium-values.yaml.j2 file, located in the templates folder of the Ansible role's directory structure, allows you to define basic parameters.
kubeProxyReplacement: strict
k8sServiceHost: {{ var_vip_kub_vip }}
k8sServicePort: 6443
ipam:
mode: "cluster-pool"
operator:
clusterPoolIPv4PodCIDRList:
- "{{ var_cilium_subnet }}"
hubble:
relay:
enabled: true
ui:
enabled: true
frontend:
server:
ipv6:
enabled: false
tls:
auto:
enabled: true
method: helm
certValidityDuration: 1095
var_cilium_subnet
: As with pods and services, it will be necessary to define a new subnet. Cilium, due to its use of VXLAN ("Virtual Extensible LAN") technology and the associated tunneling, requires an additional network. Also, reserve an unused subnet on your side (in the example 10.13.0.0/16).The rest of the configuration allows you to disable IPv6, enable TLS, and also enable the hubble relay function, which is responsible for collecting and aggregating network traffic data, as well as the hubble ui function, which is a GUI for visualizing and analyzing network traffic within the cluster.
Note that we also deploy the Cilium CLI, as it can be useful for running diagnostics or managing Cilium components.
CPI configuration
Next, the Cloud Provider Interface (CPI) is configured via the vsphere-cloud-controller-manager.yaml.j2 template.
---
apiVersion: v1
kind: ServiceAccount
metadata:
name: cloud-controller-manager
labels:
vsphere-cpi-infra: service-account
component: cloud-controller-manager
namespace: kube-system
---
apiVersion: v1
kind: Secret
metadata:
name: vsphere-cloud-secret
labels:
vsphere-cpi-infra: secret
component: cloud-controller-manager
namespace: kube-system
# NOTE: this is just an example configuration, update with real values based on your environment
stringData:
{{ var_vcenter }}.username: "{{ var_vcenter_user }}"
{{ var_vcenter }}.password: "{{ var_vcenter_password }}"
---
Contenu directement récupéré sur https://raw.githubusercontent.com/kubernetes/cloud-provider-vsphere/master/releases/v1.29/vsphere-cloud-controller-manager.yaml
This file largely reproduces the content provided by VMware, which can be found here.
(Note: this file may evolve over time and with different versions of Kubernetes. As of the time of writing, we are trying to deploy version 1.29 of K8S, so the URL refers to version 1.29).
Only the beginning of the file is customized with variables to create a template. It includes the connection details for vCenter, including the dedicated service account.
It retrieves a password from the variable var_vcenter_password.
As explained in the previous article, it is not good practice to have this value in plain text within the variables file main.yml in the defaults directory.
CSI configuration
We conclude this section with the configuration of the CSI driver, which will allow us to subsequently use persistent volumes stored in VMware datastores.
First, a file describing a dedicated namespace is created using the template csi-namespace.yaml.j2, and its content is as follows.
apiVersion: v1
kind: Namespace
metadata:
name: vmware-system-csi
We will have the opportunity to discuss namespaces again, but they are a Kubernetes object that allows you to define a logical zone within your cluster, to which you can specify specific access rights and resource quotas (CPU, memory...). You can also indirectly use namespaces to segment network traffic within the cluster (using network policies).
Next, the template csi-vsphere.conf.j2 will serve as a reference.
[Global]
cluster-id = "{{ var_kub_name }}"
[VirtualCenter "{{ var_vcenter }}"]
insecure-flag = "true"
user = "{{ var_vcenter_user }}"
password = "{{ var_vcenter_password }}"
port = "443"
datacenters = "{{ var_vcenter_datacenter }}"
Here, we reuse variables that are already known, such as access to vCenter and the name of the K8S cluster.
The last component to be created is based on the template sphere-csi-driver.yaml.j2. This is directly copied from what VMware provides at this URL.
This time, the version specified in the URL is directly the version of the driver (in this example, 3.0.2).
---
apiVersion: storage.k8s.io/v1
kind: CSIDriver
metadata:
name: csi.vsphere.vmware.com
spec:
attachRequired: true
podInfoOnMount: false
---
kind: ServiceAccount
apiVersion: v1
metadata:
name: vsphere-csi-controller
namespace: vmware-system-csi
---
kind: ClusterRole
apiVersion: rbac.authorization.k8s.io/v1
metadata:
name: vsphere-csi-controller-role
rules:
Contenu directement récupéré sur https://raw.githubusercontent.com/kubernetes-sigs/vsphere-csi-driver/v3.0.2/manifests/vanilla/vsphere-csi-driver.yaml.
As with the CPI, the CSI evolves over time and must be compatible with the version of k8s on which it will be deployed. It is important to carefully follow the compatibility matrices.
Before finishing, I would like to briefly discuss the access account used by the various VMware add-ons (CPI/CSI). This account must have specific permissions that are described in the installation documentation. You can even use two different accounts. In my case, as with Terraform and Ansible, I am using an AD account.
We can simplify things by granting administrator privileges, but this remains a bad practice. In any case, it is best to use at least one specific account.

Click the image to enlarge.

Click the image to enlarge.
These access rights are necessary because when we instruct K8S to deploy a persistent volume based on the vSphere CSI, K8S will use this account to create the volume on the vSphere datastore and map it to the VM that will host the pod using that volume at that moment.

Click the image to enlarge.
Conclusion
At this stage of the Ansible role dedicated to cluster deployment, the VMs have all the basic configuration files associated with the components needed to initialize the cluster.
My approach is not intended to completely automate the installation of the cluster with Ansible, as Kubespray could do.
The goal is to have an operational platform for initializing a cluster with all the necessary dependencies and configuration ready to be passed as arguments during the installation of the various components.
It is, of course, possible to go much further in automation, but within an educational context and for my own needs, I prefer to limit myself to industrializing the configuration and keep a manual cluster initialization later.
Nevertheless, I still have some Ansible files on hand to prepare the components ancillary to the cluster. These components are presented in the rest of the cookbook.