Building a Kubernetes v1.31 Cluster with kubeadm
In this guide, we’ll walk you through setting up a simple two-node Kubernetes cluster using kubeadm
. This setup consists of one control plane node and one worker node named node-summer
.
Note for CKA Candidates: In environments like the CKA exam (e.g., Killer Koda), pod networking (e.g., Calico) is typically pre-installed, so you can skip those steps unless explicitly instructed to configure it.
Related Work: Kubernetes Installation for v1.20
Previously, I wrote an article on Installing Kubernetes v1.20 with kubeadm, which covers the setup for an earlier Kubernetes version. While the core process remains consistent, key differences include:
Kubernetes Version:
- The previous article used
v1.20
, whereas this guide usesv1.31.0
, reflecting newer features and updates.
Pod Network Add-On:
- In
v1.20
, setting up a pod network like Calico was essential. Inv1.31.0
(especially for CKA exam environments), pod networking is often pre-installed, making this step optional.
System Requirements and Preflight Checks:
- For
v1.31.0
, we explicitly ignore preflight errors for CPU and memory to accommodate resource-constrained environments, which may not have been necessary forv1.20
.
For a deeper understanding of Kubernetes installation differences, check out the previous article.
Prerequisites
Before we begin, ensure you have the following:
Two Virtual Machines (VMs):
- controlplane: This will serve as the Kubernetes control plane node.
- node-summer: This will act as the worker node.
Installed Software on Both VMs:
- kubeadm: Kubernetes cluster manager.
- kubelet: Kubernetes node agent.
- kubectl: Kubernetes command-line tool.
Networking: (Not needed for CKA exam)
- Both VMs should be able to communicate with each other over the network.
- Ensure that ports required by Kubernetes are open (e.g., 6443 for the API server).
System Requirements:
- Adequate CPU and memory resources. (Note: In this guide, we’ll ignore preflight errors related to CPU and memory.)
Step 1: Initialize the Control Plane Node
We’ll start by setting up the controlplane
node as the Kubernetes control plane.
1.1. Configure kubeadm
Initialization
Execute the following command on the controlplane
node to initialize the Kubernetes cluster:
sudo kubeadm init \
--kubernetes-version v1.31.0 \
--pod-network-cidr=192.168.0.0/16 \
--ignore-preflight-errors=NumCPU,Mem
Explanation of Flags:
--kubernetes-version v1.31.0
: Specifies the Kubernetes version to deploy.--pod-network-cidr=192.168.0.0/16
: Defines the CIDR range for the pod network. This should match the network configuration of your chosen pod network add-on.--ignore-preflight-errors=NumCPU,Mem
: Ignores preflight checks related to the number of CPUs and available memory. This is useful for environments with limited resources.
1.2. Set Up kubectl
Configuration (Not needed for CKA)
After the initialization completes, set up the kubectl
configuration to manage the cluster as the root user.
sudo mkdir -p /root/.kube
sudo cp -i /etc/kubernetes/admin.conf /root/.kube/config
sudo chown root:root /root/.kube/config
Explanation:
- Create the
.kube
directory: This directory stores Kubernetes configuration files. - Copy
admin.conf
: Theadmin.conf
file contains the cluster's administrative credentials. Copying it to/root/.kube/config
allows thekubectl
command to interact with the cluster. - Set Ownership: Ensures that the root user owns the configuration file.
Step 2: Deploy a Pod Network Add-On (Not needed for CKA exam)
Kubernetes requires a pod network add-on to manage networking between pods. We’ll use Calico in this guide.
2.1. Install Calico
Run the following command on the controlplane
node:
kubectl apply -f https://docs.projectcalico.org/manifests/calico.yaml
Note: Ensure that the --pod-network-cidr
specified during kubeadm
initialization matches the configuration expected by the pod network add-on.
Step 3: Join the Worker Node to the Cluster
Now, we’ll add the node-summer
worker node to our Kubernetes cluster.
3.1. Retrieve the Join Command
On the controlplane
node, execute:
kubeadm token create --print-join-command
This command will output a kubeadm join
command with a token and hash, which looks similar to:
kubeadm join <controlplane-ip>:6443 --token <token> --discovery-token-ca-cert-hash sha256:<hash>
Note: If the token has expired or is missing, you can generate a new one using:
kubeadm token create
And retrieve the discovery token CA cert hash with: (Not needed for CKA exam)
openssl x509 -pubkey -in /etc/kubernetes/pki/ca.crt | \
openssl rsa -pubin -outform der 2>/dev/null | \
openssl dgst -sha256 -hex | sed 's/^.* //'
3.2. Execute the Join Command on node-summer
Log in to the node-summer
VM and run the kubeadm join
command obtained from the previous step. It will look like this:
sudo kubeadm join <controlplane-ip>:6443 --token <token> --discovery-token-ca-cert-hash sha256:<hash>
Example:
sudo kubeadm join 192.168.1.100:6443 --token abcdef.0123456789abcdef \
--discovery-token-ca-cert-hash sha256:1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef
Explanation:
<controlplane-ip>:6443
: The IP address and port of the control plane node's API server.--token <token>
: A token used for authentication during the join process.--discovery-token-ca-cert-hash
: A hash to verify the CA certificate of the control plane node.
3.3. Verify the Worker Node is Joined
Back on the controlplane
node, check if node-summer
has successfully joined the cluster:
kubectl get nodes
You should see an output similar to:
NAME STATUS ROLES AGE VERSION
controlplane Ready ControlPlane 10m v1.31.0
node-summer Ready <none> 5m v1.31.0
Conclusion
You’ve successfully set up a two-node Kubernetes cluster using kubeadm
with one control plane node and one worker node. This setup provides a foundational environment for deploying and managing containerized applications. From here, you can explore more advanced configurations, add more worker nodes, and deploy your applications using Kubernetes manifests.
Next Steps:
- Deploy Applications: Use
kubectl
to deploy your containerized applications. - Scale the Cluster: Add more worker nodes to handle increased workloads.
- Implement Security Measures: Configure RBAC, Network Policies, and other security features to protect your cluster.
- Monitor and Maintain: Set up monitoring tools like Prometheus and Grafana to monitor cluster health and performance.
Happy Kubernetizing!