DEV Community

Abhay Singh Kathayat
Abhay Singh Kathayat

Posted on

How to Set Up and Install a Kubernetes Cluster: A Step-by-Step Guide

Kubernetes Cluster Setup and Installation

Setting up a Kubernetes cluster is an essential step for deploying and managing containerized applications at scale. A Kubernetes cluster consists of a control plane (master node) and multiple worker nodes that run the applications. There are several ways to install and configure a Kubernetes cluster, depending on the environment, including on-premises, cloud-based solutions, or local development environments.

This guide walks you through setting up a Kubernetes cluster using kubeadm, a tool provided by Kubernetes to simplify the cluster installation process. We will cover steps for setting up a single-node cluster (ideal for learning and testing) and a multi-node cluster (for production or more complex setups).

Prerequisites

Before beginning the installation, ensure you have the following:

  1. Linux Servers: At least two machines (physical or virtual) running Linux. The nodes must have:
    • Ubuntu 20.04 or later (or another supported distribution like CentOS or Debian)
    • Minimum 2GB of RAM and 2 CPU cores per node
    • Root or sudo access
  2. Container Runtime: Docker or another container runtime (containerd, CRI-O) installed on each node.
  3. Internet Access: For downloading the necessary Kubernetes images and dependencies.

Steps for Kubernetes Cluster Installation

Step 1: Prepare Your Nodes

You'll need at least two nodes:

  1. Master Node: This node will host the control plane components like the API Server, Controller Manager, Scheduler, and etcd.
  2. Worker Nodes: These nodes will run your application containers in the form of pods.

Make sure that each node has the following pre-requisites installed:

  • Disable Swap Memory (required for Kubernetes to function):
   sudo swapoff -a
Enter fullscreen mode Exit fullscreen mode
  • Install Docker (or any container runtime):
   sudo apt-get update
   sudo apt-get install -y docker.io
   sudo systemctl enable docker
   sudo systemctl start docker
Enter fullscreen mode Exit fullscreen mode
  • Enable IP forwarding for networking:
   sudo sysctl net.ipv4.ip_forward=1
Enter fullscreen mode Exit fullscreen mode
  • Configure Firewall (allowing Kubernetes ports):
   sudo ufw allow 6443,2379:2380,10250:10252,10255,30000:32767/tcp
Enter fullscreen mode Exit fullscreen mode

Step 2: Install Kubernetes Components

Install the necessary Kubernetes components on each node (master and worker nodes). This includes kubeadm, kubelet, and kubectl.

  1. Install Kubernetes APT repository: First, add the Kubernetes APT repository to your nodes:
   curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add -
   sudo apt-add-repository "deb https://apt.kubernetes.io/ kubernetes-xenial main"
Enter fullscreen mode Exit fullscreen mode
  1. Install kubeadm, kubelet, and kubectl:
   sudo apt-get update
   sudo apt-get install -y kubelet kubeadm kubectl
Enter fullscreen mode Exit fullscreen mode

Note: kubelet runs on all nodes, kubectl is the command-line tool for interacting with the cluster, and kubeadm is the tool used for setting up and managing the cluster.

  1. Hold the installed Kubernetes packages (to prevent automatic updates):
   sudo apt-mark hold kubelet kubeadm kubectl
Enter fullscreen mode Exit fullscreen mode

Step 3: Initialize the Kubernetes Master Node

  1. On the Master Node, run the following command to initialize the cluster:
   sudo kubeadm init --pod-network-cidr=10.244.0.0/16
Enter fullscreen mode Exit fullscreen mode
  • --pod-network-cidr=10.244.0.0/16 specifies the CIDR block for the pod network. This is important for setting up networking between pods and nodes. You can choose other network ranges if needed, depending on the network plugin you use (like Flannel, Calico, etc.).
  1. Once the command completes, you will see output that includes instructions for setting up the kubeconfig file and joining worker nodes to the cluster. Save this information, especially the command for joining the worker nodes.

Example output:

   Your Kubernetes master has initialized successfully!
   To start using your cluster, you need to run the following as a regular user:
     mkdir -p $HOME/.kube
     sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
     sudo chown $(id -u):$(id -g) $HOME/.kube/config
Enter fullscreen mode Exit fullscreen mode
  1. Set up your kubeconfig file to allow the kubectl tool to access the cluster:
   mkdir -p $HOME/.kube
   sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
   sudo chown $(id -u):$(id -g) $HOME/.kube/config
Enter fullscreen mode Exit fullscreen mode

Step 4: Install a Network Plugin

To allow communication between pods, you need to install a network plugin. Popular network plugins include Flannel, Calico, and Weave. For this example, we'll use Flannel.

  1. Apply the Flannel network plugin:
   kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
Enter fullscreen mode Exit fullscreen mode
  1. Confirm that the network is running:
   kubectl get pods --all-namespaces
Enter fullscreen mode Exit fullscreen mode

This command should show you the Flannel pods running in the kube-system namespace. Wait for all the pods to be in a Running state before proceeding.

Step 5: Join Worker Nodes to the Cluster

On each Worker Node, run the command provided by the output of kubeadm init to join the node to the cluster. It will look something like this:

kubeadm join <Master-Node-IP>:6443 --token <token> --discovery-token-ca-cert-hash <hash>
Enter fullscreen mode Exit fullscreen mode
  • Replace <Master-Node-IP> with the IP address of the master node.
  • Replace <token> and <hash> with the values generated during the master node initialization.

Once the worker nodes have joined the cluster, you can confirm the nodes by running:

kubectl get nodes
Enter fullscreen mode Exit fullscreen mode

The worker nodes should appear with a Ready status.

Step 6: Verify the Cluster

Once the nodes are up and running, you can verify the cluster status using kubectl:

kubectl get nodes
kubectl get pods --all-namespaces
Enter fullscreen mode Exit fullscreen mode

This will show you the status of the nodes and pods in your cluster.

Step 7: (Optional) Set Up kubectl for Non-Root Users

If you're not using the root user to run kubectl, make sure your user has permission to interact with the Kubernetes cluster. You can add the current user to the kube group:

sudo usermod -aG docker <your-user>
Enter fullscreen mode Exit fullscreen mode

Troubleshooting

  1. Pods in Pending State: Check the nodes' resources (memory, CPU) and ensure that there's enough space for the pods to run.
  2. Networking Issues: If there are problems with pod communication, check the network plugin status (kubectl get pods -n kube-system).
  3. Node Not Ready: Ensure that the worker node has properly joined the cluster and that the necessary services (like kubelet) are running on the node.

Conclusion

You've successfully set up a Kubernetes cluster using kubeadm! This cluster can now be used for deploying and managing containerized applications at scale. Kubernetes provides powerful features such as automatic scaling, self-healing, and high availability, making it ideal for production environments. You can now proceed with deploying your applications, managing configurations, and scaling your workloads.

Top comments (0)