Kubernetes Deep-Dive

Setting Up Your Kubernetes Cluster: A Step-by-Step Guide

Damian Igbe, Phd
Sept. 5, 2024, 10:43 a.m.

Subscribe to Newsletter

Be first to know about new blogs, training offers, and company news.

Kubernetes is a powerful container orchestration platform that automates the deployment, scaling, and management of containerized applications. Setting up a Kubernetes cluster can seem daunting, but following a structured approach can make the process manageable. This guide will walk you through the steps to set up a basic Kubernetes cluster.

Prerequisites

  1. Basic Understanding: Familiarity with Linux command line, containerization concepts, and Kubernetes fundamentals.
  2. Hardware Requirements: At least two machines (one master node and one worker node) or a local environment with virtual machines.
  3. Software Requirements:
  •    Operating System: Linux-based OS (e.g., Ubuntu 20.04 or later).
  •    Package Manager: `apt` for Debian-based distributions or `yum` for Red Hat-based distributions.
  •    Docker: For container runtime.
  •    Kubeadm, Kubelet, and Kubectl: Kubernetes tools.

Step 1: Prepare Your Environment

  1. Update Your System:

      sudo apt-get update
    sudo apt-get upgrad
  1. Install Docker:

         - Install prerequisites:

     sudo apt-get install apt-transport-https ca-certificates curl software-properties-common

         - Add Docker’s official GPG key:

    curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -

        - Set up the stable repository:

     sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"

       - Install Docker Engine:

     sudo apt-get update

     sudo apt-get install docker-ce
  1. Configure Docker (if needed):

     sudo systemctl enable docker
   sudo systemctl start docker   

Step 2: Install Kubernetes Tools

  1. Install `kubeadm`, `kubelet`, and `kubectl`:

        - Add Kubernetes APT repository:

     curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add -
    sudo apt-add-repository "deb http://apt.kubernetes.io/ kubernetes-xenial main"

        - Install packages:

     sudo apt-get update
     sudo apt-get install -y kubelet kubeadm kubectl
  1. Hold the packages to prevent them from being updated automatically:

   sudo apt-mark hold kubelet kubeadm kubectl

Step 3: Initialize the Kubernetes Master Node

  1. Initialize the Cluster:

   sudo kubeadm init --pod-network-cidr=10.244.0.0/16

      - The `--pod-network-cidr` flag is for setting up the Flannel network plugin.

  1. Set Up `kubectl` Access:

     - As a regular user, set up the Kubernetes configuration:

     mkdir -p $HOME/.kube
    sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
     sudo chown $(id -u):$(id -g) $HOME/.kube/config
  1. Install a Pod Network Add-on:

   - For Flannel:

     kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml

Step 4: Join Worker Nodes to the Cluster

  1. Get the Join Command from the master node:

      - After initializing the master, `kubeadm` provides a command to join worker nodes. It looks like:

     kubeadm join <master-ip>:<port> --token <token> --discovery-token-ca-cert-hash sha256:<hash>
  1. Run the Join Command on each worker node.

Step 5: Verify the Cluster from the master node

  1. Check the Nodes:

   kubectl get nodes

   - All nodes should appear as `Ready` after a few moments.

  1. Check the Pods:

   kubectl get pods --all-namespaces

   - Ensure that all system pods are up and running.

 

Conclusion

Congratulations! You've set up a basic Kubernetes cluster. From here, you can start deploying applications, managing resources, and scaling your services. This setup is ideal for learning and testing; for production environments, consider more advanced configurations and security practices.

Zero-to-Hero Program: We Train and Mentor you to land your first Tech role