Kubeadm is a simple tool that helps you set up a cluster of computers running Kubernetes. It takes care of many of the initial steps and configurations needed to create a functioning Kubernetes cluster, making the process easier and more streamlined.
Before you begin
One or more machines running a deb/rpm-compatible Linux OS; for example Ubuntu or CentOS.
2 GiB or more of RAM on the machine any less leaves little room for your apps.
At least 2 CPUs on the machine that you use as a control-plane node.
Full network connectivity among all machines in the cluster. You can use either a public or a private network.
You also need to use a version kubeadm
that can deploy the version of Kubernetes that you want to use in your new cluster.
Creating EC2 instances
We are tasked with establishing a pair of instances, where one will take on the role of the control plane or master, while the other will effectively serve as the diligent worker node.
The instances will stay as they are, but remember, the control-plane node needs a bit more power: 2 CPUs and 4 GiB of RAM. So, here's the plan: go with t2.medium
for the master node to handle things, and a t2.micro
is just fine for the worker node.
Here's how our instances look in the end.
If you're interested in learning how to set up an EC2 Instance, just Click here.
Creating the K8s Cluster
Just stick with the commands I'll be using in this blog, and in no time, you'll have your very own K8s cluster up and running on your system. Toward the end of the blog, I'll provide a link to a script containing all the necessary commands to set up a K8s cluster using Kubeadm. Here we go...
I'm breaking down this implementation into 3 modules:
The first module covers commands for both master and worker nodes.
The second module focuses on commands exclusively for the master node.
The third module presents commands solely for the worker node.
Module - 1
Just a quick reminder, the commands in module 1 are meant to be run on both the master and worker nodes's devices.
Update the system.
sudo apt update -y
Install docker on both machines.
sudo apt install docker.io -y
Start and enable the docker service.
sudo systemctl start docker sudo systemctl enable docker
Get the key from the URL to get authenticated packages.
sudo curl -fsSL "https://packages.cloud.google.com/apt/doc/apt-key.gpg" | sudo gpg --dearmor -o /etc/apt/trusted.gpg.d/kubernetes-archive-keyring.gpg
Sign the key in the deb package.
echo 'deb https://packages.cloud.google.com/apt kubernetes-xenial main' > /etc/apt/sources.list.d/kubernetes.list
Once more, make sure to update the system.
sudo apt update -y
Get the required tools installed.
sudo apt install kubeadm=1.20.0-00 kubectl=1.20.0-00 kubelet=1.20.0-00 -y
Module - 2
Remember, the commands in module 2 are specifically for the master's machine.
Switch to the root user.
sudo su
Designate this machine as the control plane (master).
kubeadm init
Execute the following command.
export KUBECONFIG=/etc/kubernetes/admin.conf
Configuring the CNI network.
kubectl apply -f https://github.com/weaveworks/weave/releases/download/v2.8.1/weave-daemonset-k8s.yaml
Create the token.
kubeadm token create --print-join-command
This action will generate a token, similar to what you see in the image below. This token is crucial for establishing a connection between the worker and the master.
Module - 3
Keep in mind that the commands in module 3 are designed for the worker's machine.
Switch to the root user.
sudo su
Reset the configurations to join the cluster as a new worker node.
kubeadm reset pre-flight checks
Connect to the cluster.
<Paste the Join command on worker node and append> --v=5 <at the end>
As an example:
kubeadm join 172.31.84.66:6443 --token n4tfb4.grmew1s1unug0get --discovery-token-ca-cert-hash sha256:c3fda2eaf5960bed4320d8175dc6a73b1556795b1b7f5aadc07642ed85c51069 --v=5
Important note: Remember to open the necessary ports on the master's host to allow the worker to establish a connection. You can achieve this by modifying the inbound rules for the master's host. Learn more from here.
Verify the connection by running the following command on the master's machine.
kubectl get nodes
The output will look like this.
Congratulations! ๐ You've achieved it! Your efforts have paid off, and now you have a fully functional K8s cluster up and running, all thanks to your work with Kubeadm.
To access the script with all the commands, simply Click here.