How to Install Kubernetes on Linux Server

Kubernetes (often referred to as K8s) is an open-source platform for automating the deployment, scaling, and management of containerized applications. It helps developers and operations teams deploy applications efficiently, even in complex environments, by managing clusters of hosts running containers.

Kubernetes on Linux

In this guide, we will take you through the process of installing Kubernetes on a Linux server, covering prerequisites, installation steps for different Linux distributions, and tips for ensuring your setup works flawlessly.

Prerequisites

Before installing Kubernetes, make sure your server meets the following requirements:

Hardware and Software Requirements

  • CPU: At least 2 CPUs (more for production)
  • RAM: Minimum 2GB (4GB or more recommended for production)
  • Disk Space: 20GB of free space (preferably SSD for faster performance)
  • Networking: The server should have internet connectivity and be able to communicate with other nodes in the cluster.

Required Tools

  • Docker: For running containers
  • kubeadm: A tool that helps you set up Kubernetes clusters
  • kubelet: An agent running on each node to ensure containers are running in Pods
  • kubectl: Command-line tool for interacting with Kubernetes

Install Docker

Kubernetes relies on Docker for container management. Here’s how to install Docker on your system.

  • Install Docker on Ubuntu/Debian
sudo apt install -y docker.io
  • Install Docker on CentOS
sudo yum install -y docker
  • Start and Enable Docker
sudo systemctl enable --now docker
  • Verify Docker Installation
docker --version
  • Verify that the Docker service is running:
sudo systemctl status docker

Check Out | How to Set Up and Install FTP Server on Linux

Install Kubernetes on Various Linux Distros

Kubernetes installation can vary slightly depending on your Linux distribution. Here’s how to install it on the most common Linux distros.

Install Kubernetes on Ubuntu/Debian

Install Kubernetes on an Ubuntu or Debian-based system involves several steps, including setting up the server, installing necessary dependencies, and configuring Kubernetes tools.

  • Update and Upgrade the System
sudo apt update && sudo apt upgrade -y
  • Add Kubernetes GPG Key:
curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add -
  • Add Kubernetes Repository:
sudo apt-add-repository "deb https://apt.kubernetes.io/ kubernetes-xenial main"
  • Install Kubernetes Packages:
sudo apt install -y kubeadm kubelet kubectl
  • Hold Kubernetes Packages:
sudo apt-mark hold kubeadm kubelet kubectl
  • Verify Installation:

Check if the Kubernetes tools are installed correctly by verifying their versions:

kubeadm version
kubectl version --client
kubelet --version
  • Disable Swap Memory

Kubernetes requires that swap memory be disabled for optimal performance. To disable swap:

sudo swapoff -a
  • Initialize the Master Node:

Once Kubernetes and Docker are set up, you can initialize the cluster using the kubeadm command. The --pod-network-cidr flag specifies the IP range for the pod network (you can adjust it if you’re using a specific network plugin like Calico or Flannel):

sudo kubeadm init --pod-network-cidr=192.168.0.0/16
  • Set Up kubectl:

To use kubectl to interact with the Kubernetes cluster, you need to configure it to use the Kubernetes API server. Run the following commands to set up the kubeconfig file:

mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
  • Set Up Networking

Kubernetes requires a network plugin to enable communication between pods. You can choose from various networking solutions like Flannel, Calico, or Weave. For example, to install Flannel as the network plugin, run the following command:

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

Install Kubernetes on CentOS

To install Kubernetes on CentOS, you’ll need to follow these steps. This guide assumes that you’re starting from a CentOS 7 or CentOS 8 machine.

  • Prepare your system

First, update your CentOS system:

sudo yum update -y
  • Add Kubernetes Repository

To install Kubernetes, you’ll need to add the Kubernetes repository to your CentOS system.

cat <<EOF | sudo tee /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=https://packages.cloud.google.com/yum/repos/kubernetes-el7-x86_64
enabled=1
gpgcheck=1
repo_gpgcheck=1
gpgkey=https://packages.cloud.google.com/yum/doc/yum-key.gpg
         https://packages.cloud.google.com/yum/doc/rpm-package-key.gpg
EOF
  • Disable SELinux

Kubernetes recommends disabling SELinux as it can cause issues. To do this, edit the SELinux config file:

sudo nano /etc/selinux/config

Change SELINUX=enforcing to SELINUX=disabled, then save and exit.

  • Restart the system to apply the changes:
sudo reboot
  • Install Kubernetes packages:
sudo yum install -y kubelet kubeadm kubectl
  • Enable and start kubelet:
sudo systemctl enable kubelet
sudo systemctl start kubelet
  • Disable Swap

Kubernetes requires that swap be disabled. To disable it, run the following command:

sudo swapoff -a
  • Initialize the Kubernetes Master Node

To set up the Kubernetes master node, run the following kubeadm command:

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

This will initialize the Kubernetes master node, setting up the control plane. The --pod-network-cidr flag is required for network configuration (if you are using Flannel for networking).

  • Configure kubectl for the Master Node

After the initialization, to interact with the Kubernetes cluster using kubectl, set up the kubeconfig file:

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

Kubernetes requires a network plugin to run pods. One of the most commonly used network solutions is Flannel. To install Flannel, run the following:

kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
  • Verify the Cluster
kubectl get nodes

Conclusion

Installing Kubernetes on a Linux server is a straightforward process when following the outlined steps. By setting up the Kubernetes repository, installing essential components like kubelet, kubeadm, and kubectl, and configuring the necessary system settings, you’ve successfully created a Kubernetes cluster. The addition of a network plugin, like Flannel, ensures smooth communication between the pods within your cluster.

After initializing the master node and joining worker nodes, you can begin to manage your containerized applications with ease. With Kubernetes’ powerful features for scalability, management, and orchestration, you’re now equipped to deploy and monitor applications in a distributed environment.

Leave A Comment