Kubernetes Hands-On Lab - Building a Kubernetes Cluster with kubeadm

Kubernetes Hands-On Lab - Building a Kubernetes Cluster with kubeadm

Kubernetes Learning Path for Cloud and DevOps Engineers

📝Introduction

In this lab, we will practice building a new Kubernetes cluster. You will be given a set of Linux servers and the opportunity to turn these servers into a functioning Kubernetes cluster. This will help you build the skills necessary to create your own Kubernetes clusters in the real world.

These are the objectives of this lab:

  • Install Packages

  • Initialize the Cluster

  • Install the Calico Network Add-On

  • Join the Worker Nodes in the Cluster

📝Log in to the AWS Management Console

Using your credentials, make sure you're using the right Region. In my case, I am using AWS as my cloud provider and chose us-east-1. However, for this hands-on lab, you can select any cloud provider to create your Linux server VMs (I am using an Ubuntu 20.04 LTS distro), kubelet, kubeadm and kubectl version 1.27.

📌Note: You must create the AWS Access Key and AWS Secret Access Key and configure the AWS CLI in the terminal to use it.

You can use link1 and link2 for it.

📝Install Packages

  1. Log in to the control plane node.

    📌Note: The following steps must be performed on all 3 nodes(1 Control Plane and 2 Worker Nodes).

  2. Create the configuration file for containerd:

     cat <<EOF | sudo tee /etc/modules-load.d/containerd.conf
     overlay
     br_netfilter
     EOF
    
  3. Load the modules:

     sudo modprobe overlay
     sudo modprobe br_netfilter
    
  4. Set the system configurations for Kubernetes networking:

     cat <<EOF | sudo tee /etc/sysctl.d/99-kubernetes-cri.conf
     net.bridge.bridge-nf-call-iptables = 1
     net.ipv4.ip_forward = 1
     net.bridge.bridge-nf-call-ip6tables = 1
     EOF
    
  5. Apply the new settings:

     sudo sysctl --system
    
  6. Install containerd:

     sudo apt-get update && sudo apt-get install -y containerd.io
    
  7. Create the default configuration file for containerd:

     sudo mkdir -p /etc/containerd
    
  8. Generate the default containerd configuration, and save it to the newly created default file:

     sudo containerd config default | sudo tee /etc/containerd/config.toml
    
  9. Restart containerd to ensure the new configuration file is used:

     sudo systemctl restart containerd
    
  10. Verify that containerd is running:

    sudo systemctl status containerd
    
  11. Disable swap:

    sudo swapoff -a
    
  12. Install the dependency packages:

    sudo apt-get update && sudo apt-get install -y apt-transport-https curl
    
  13. Download and add the GPG key:

    curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add -
    
  14. Add Kubernetes to the repository list:

    cat <<EOF | sudo tee /etc/apt/sources.list.d/kubernetes.list
    deb https://apt.kubernetes.io/ kubernetes-xenial main
    EOF
    
  15. Update the package listings:

    sudo apt-get update
    
  16. Install Kubernetes packages:

    📌Note: If you get a dpkg lock message, just wait a minute or two before trying the command again.

    sudo apt-get install -y kubelet=1.27.0-00 kubeadm=1.27.0-00 kubectl=1.27.0-00
    
  17. Turn off automatic updates:

    sudo apt-mark hold kubelet kubeadm kubectl
    
  18. Perform the same previous steps in the Worker Nodes.

📝Initialize the Cluster

  1. Initialize the Kubernetes cluster on the control plane node using kubeadm:

     sudo kubeadm init --pod-network-cidr 192.168.0.0/16 --kubernetes-version 1.27.0
    
  2. Set kubectl access:

     mkdir -p $HOME/.kube
     sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
     sudo chown $(id -u):$(id -g) $HOME/.kube/config
    
  3. Test access to the cluster:

     kubectl get nodes
    

    📌Note: The Status of the node must be NotReady since there is no network add-on already created and set.

📝Install the Calico Network Add-On

  1. On the control plane node, install Calico Networking:

     kubectl apply -f https://raw.githubusercontent.com/projectcalico/calico/v3.25.0/manifests/calico.yaml
    
  2. Check the status of the control plane node:

     kubectl get nodes
    

    📌Note: You may have to wait a few seconds to observe the node to become Ready

📝Join the Worker Nodes in the Cluster

  1. In the control plane node, create the token and copy the kubeadm join command:

     kubeadm token create --print-join-command
    

    📌Note: This output will be used as the next command for the worker nodes.

  2. Copy the full output from the previous command used in the control plane node. This command starts with kubeadm join.

  3. In both worker nodes, paste the full kubeadm join command to join the cluster. Use sudo to run it as root:

     sudo kubeadm join...
    

  4. In the control plane node, view the cluster status:

     kubectl get nodes
    

    📌Note: You may have to wait a few moments to allow all nodes to become Ready

    Verify that all three nodes are successfully registered and in the READY state.

📌Note - At the end of each hands-on Lab, always clean up all previous resources created to avoid being charged if you used a Cloud Provider to provision them.

Congratulations — you have completed this hands-on lab covering the basics of building a Kubernetes cluster.

Thank you for reading. I hope you understood and learned something helpful from my blog.

Please follow me on CloudDevOpsToLearn and LinkedIn, franciscojblsouza