How to Setup Kubernetes Cluster Using Kubeadm in Your Server?

Leave a comment on How to Setup Kubernetes Cluster Using Kubeadm in Your Server?

Kubernetes, a robust container orchestration technology, enables you to manage containerized applications across numerous servers smoothly. Setting up a Kubernetes cluster with Kubeadm is one of the most straightforward ways to get started. In this post, we’ll walk you through the process of installing Kubeadm on your server and creating a Kubernetes cluster.

How to Setup Kubernetes Cluster Using Kubeadm in Your Server

Requirements:

  • At least one server. While Kubernetes is typically deployed with separate control-plane and worker nodes, you can set up a single-node cluster for testing or development purposes. (Ideally, you should have 2 servers to setup a cluster)
  • The server should have at least 2 CPUs and 2GB RAM.
  • A user with ‘sudo’ privileges.

Here we are using Ubuntu 22.04 server

Configuring Network:

Disable swap using the following command:

sudo swapoff -a 

(Update /etc/fstab to prevent swap from re-enabling after a reboot.)

Open the necessary firewall ports:

Master Node Ports: 2379,6443,10250-10252

sudo ufw allow 6443/tcp 

sudo ufw allow 2379:2380/tcp sudo ufw allow 10250:10252/tcp sudo ufw allow 8472/udp 

sudo ufw reload 

Container Runtime:

Kubernetes needs a container runtime to manage containers. Follow these instructions to install Docker as the container runtime.

Update the System and Install Required Packages:

sudo apt update && sudo apt upgrade -y

sudo apt install -y ca-certificates curl gnupg lsb-release 

Add the Docker GPG Key and Repository:

sudo mkdir -p /etc/apt/keyrings

curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg –dearmor -o

/etc/apt/keyrings/docker.gpg

echo “deb [arch=$(dpkg –print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable” | sudo tee

/etc/apt/sources.list.d/docker.list > /dev/null

Install Docker:

  sudo apt update
  sudo apt install -y docker-ce docker-ce-cli containerd.io docker-compose-plugin docker-buildx-plugin

Enable and Start Docker:

sudo systemctl enable --now docker 

Configure Docker for Kubernetes: Create or update the Docker daemon configuration file to use systemd as the cgroup driver:

sudo mkdir -p /etc/docker 
cat <<EOF | sudo tee /etc/docker/daemon.json
{
"exec-opts": ["native.cgroupdriver=systemd"], "log-driver": "json-file", 
"log-opts": {
"max-size": "100m"
}, 
"storage-driver": "overlay2" 
} EOF 

Restart Docker to apply the changes:

  sudo systemctl restart docker

Now, let’s move on to the Installation process.

Step 1 – Install Kubeadm, Kubelet, and Kubectl

Update all the System Packages to make sure your Ubuntu server is up-to-date.

sudo apt update && sudo apt upgrade -y

Add the Kubernetes Repository:

sudo apt install -y apt-transport-https ca-certificates curl

echo ‘deb [signed-by=/usr/share/keyrings/kubernetes-archive-keyring.gpg] https://apt.kubernetes.io/ kubernetes-xenial main’ | sudo tee

/etc/apt/sources.list.d/kubernetes.list

curl -fsSL https://pkgs.k8s.io/core:/stable:/v1.28/deb/Release.key | sudo gpg –dearmor

-o /etc/apt/keyrings/kubernetes-apt-keyring.gpg

Install Kubeadm, Kubelet, and Kubectl:

sudo apt update

sudo apt install -y kubelet kubeadm kubectl

To prevent automatic updates/removals of Kubernetes packages, use the following command:

  sudo apt-mark hold kubelet kubeadm kubectl

Enable Kubelet Service:

  sudo systemctl enable --now kubelet

Step 2: Initialize the Control Panel

Run Kubeadm Init: On the server, initialize the cluster:

The ‘–pod-network-cidr’ flag specifies the network CIDR for the pod network. Adjust it as needed.

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

Set Up kubeconfig: Configure kubectl for the 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

Step 3: Install a Pod Network Add-On

Install Flannel: A pod network add-on is necessary for communication between pods.


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

Verify the Node:


  kubectl get nodes

The server should display a ‘Ready’ status.

Step 4: Deploy a Test Application

Ensure your cluster is functional by deploying a simple application.

Create a Deployment:

  kubectl create deployment nginx --image=nginx

Expose the Deployment:

kubectl expose deployment nginx --port=80 --type=NodePort

Access the Application: Get the NodePort:

kubectl get svc 

    Access the application at http://your_ip:<node-port>

    Conclusion

    You’ve successfully configured a single-node Kubernetes cluster on Ubuntu 22.04 using Kubeadm. While this configuration is appropriate for development and testing, production applications require numerous nodes to ensure high availability and scalability. Kubernetes streamlines container orchestration and application administration, making it an invaluable tool for current development workflows.

    Server Hosting Solutions by RackNerd:

    Shared Hosting
    cPanel Web Hosting in US, Europe, and Asia datacenters
    Logo
    Reseller Hosting
    Create your new income stream today with a reseller account
    Logo
    VPS (Virtual Private Server)
    Fast and Affordable VPS services - Instantly Deployed
    Logo
    Dedicated Servers
    Bare-metal servers, ideal for the performance-demanding use case.
    Logo

    Leave a comment

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