How to Set Up and Install a Kubernetes Cluster: A Step-by-Step Guide
December 23, 2024

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


Kubernetes cluster configuration and installation

set a Kubernetes Clustering is an important step in deploying and managing containerized applications at scale. A Kubernetes cluster consists of control plane (master node) and multiple Work node to run the application. There are several ways to install and set up a Kubernetes cluster, depending on the environment, including on-premises, a cloud-based solution, or a local development environment.

This guide will walk you through setting up a Kubernetes cluster using the following commands Kubedema tool provided by Kubernetes to simplify the cluster installation process. We will introduce setting up Single node cluster (great for learning and testing) and multi-node cluster (for production or more complex setups).


Prerequisites

Before starting the installation, make sure you have the following:

  1. Linux server: At least two computers (physical or virtual) running Linux. The node must have:

    • Ubuntu 20.04 or higher (or other supported distributions such as CentOS or Debian)
    • At least 2GB RAM and 2 CPU cores per node
    • root or sudo access
  2. container runtime: Docker or other container execution time (containerd, CRI-O) installed on each node.
  3. network access: Used to download the necessary Kubernetes images and dependencies.


Kubernetes cluster installation steps


Step 1: Prepare Nodes

You need at least two nodes:

  1. master node: This node will host control plane elements such as API server, controller manager, scheduler and etcd.
  2. Work node: These nodes will run your application containers as Pods.

Make sure each node has the following prerequisites installed:

  • Disable swap memory (Required for Kubernetes to function):
   sudo swapoff -a
Enter full screen mode

Exit full screen 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 full screen mode

Exit full screen mode

  • Enable IP forwarding For the web:
   sudo sysctl net.ipv4.ip_forward=1
Enter full screen mode

Exit full screen mode

  • Configure firewall (Allow Kubernetes ports):
   sudo ufw allow 6443,2379:2380,10250:10252,10255,30000:32767/tcp
Enter full screen mode

Exit full screen mode


Step 2: Install Kubernetes components

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

  1. Install the Kubernetes APT repository: First, add the Kubernetes APT repository to your node:
   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 full screen mode

Exit full screen mode

  1. Install kubeadm, kubeletand kubectl:
   sudo apt-get update
   sudo apt-get install -y kubelet kubeadm kubectl
Enter full screen mode

Exit full screen mode

notes: kubelet Run on all nodes, kubectl is a command-line tool for interacting with clusters, and kubeadm Is a tool for setting up and managing clusters.

  1. Store installed Kubernetes packages (Prevent automatic updates):
   sudo apt-mark hold kubelet kubeadm kubectl
Enter full screen mode

Exit full screen mode


Step 3: Initialize the Kubernetes master node

  1. superior master nodeexecute the following command to initialize the cluster:
   sudo kubeadm init --pod-network-cidr=10.244.0.0/16
Enter full screen mode

Exit full screen mode

  • --pod-network-cidr=10.244.0.0/16 Specify the CIDR block of the Pod network. This is important for establishing networking between Pods and nodes. If desired, you can select a different network range, depending on the web plugin you are using (such as Flannel, Calico, etc.).
  1. Once the command completes, you will see output that includes instructions for setting up the kubeconfig file and joining the worker nodes to the cluster. Save this message, especially the command to join the worker node.

Output example:

   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 full screen mode

Exit full screen mode

  1. Set the kubeconfig file to allow kubectl Tools for accessing clusters:
   mkdir -p $HOME/.kube
   sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
   sudo chown $(id -u):$(id -g) $HOME/.kube/config
Enter full screen mode

Exit full screen mode


Step 4: Install web plug-in

To allow communication between Pods, you need to install the network plug-in. Popular web plug-ins include flannel, calicoand braid. For this example we will use flannel.

  1. Apply Flannel network plug-in:
   kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
Enter full screen mode

Exit full screen mode

  1. Confirm that the network is working:
   kubectl get pods --all-namespaces
Enter full screen mode

Exit full screen mode

This command should show you the Flannel pods running at kube-system namespace. Wait for all Pods to be at Running A disclaimer before continuing.


Step 5: Add worker nodes to the cluster

in every Work noderun the command that outputs the provided kubeadm init Add the node to the cluster. It looks like this:

kubeadm join :6443 --token  --discovery-token-ca-cert-hash <hash>
Enter full screen mode

Exit full screen mode

  • replace with the IP address of the master node.
  • replace and Use the value generated during master node initialization.

Once the worker node joins the cluster, you can confirm the node by executing the following command:

kubectl get nodes
Enter full screen mode

Exit full screen mode

The worker node should have Ready status.


Step 6: Verify the cluster

Once the node is up and running, you can verify the cluster status using the following command kubectl:

kubectl get nodes
kubectl get pods --all-namespaces
Enter full screen mode

Exit full screen mode

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


Step 7: (Optional) Setup kubectl For non-root users

If you are not running as root user kubectlmake sure your users have permission to interact with the Kubernetes cluster. You can add current users to kube group:

sudo usermod -aG docker 
Enter full screen mode

Exit full screen mode


troubleshooting

  1. Pods are located at Pending state: Check the node’s resources (memory, CPU) and ensure there is enough space for the Pod to operate.
  2. network problems: If there is a problem with pod communication, please check the network plug-in status (kubectl get pods -n kube-system).
  3. Node is not ready: Ensure that worker nodes have joined the cluster correctly and that necessary services (e.g. kubelet) is running on the node.


in conclusion

You have successfully set up a Kubernetes cluster using the following command kubeadm! This cluster can now be used to deploy and manage containerized applications at scale. Kubernetes provides powerful functions such as automatic scaling, self-healing, and high availability, making it very suitable for production environments. Now you can continue deploying applications, managing configurations, and scaling workloads.

2024-12-23 11:04:03

Leave a Reply

Your email address will not be published. Required fields are marked *