Complete Guide: How to Install Kubernetes on Ubuntu

TL;DR: In this guide, you will learn how to install Kubernetes on Ubuntu, set up a basic cluster, and verify its functionality. This tutorial includes step-by-step instructions to help you easily set up Kubernetes for container orchestration.

What is Kubernetes?

Kubernetes, often abbreviated as K8s, is an open-source platform used for automating deployment, scaling, and management of containerized applications. Kubernetes helps manage clusters of Docker containers, ensuring they run smoothly and can be scaled as needed.

In this tutorial, we will go through how to install Kubernetes on Ubuntu and set up a basic cluster for experimentation and learning purposes.

Step 1: Prepare Your Environment

Before we start installing Kubernetes, make sure your system is updated.

1.1 Update your system
Open the terminal and run the following command to update your system:

sudo apt update && sudo apt upgrade -y

1.2 Install Docker
Kubernetes uses Docker as its container runtime. If you haven’t installed Docker yet, use the command below:

sudo apt install docker.io -y

After installing Docker, enable and start the Docker service:

sudo systemctl enable docker sudo systemctl start docker

Step 2: Install Kubernetes Components

Kubernetes has three main components: kubeadm, kubelet, and kubectl.

2.1 Add the Kubernetes Repository
Add the Kubernetes apt repository:

sudo apt-get install -y apt-transport-https ca-certificates curl curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add - echo "deb https://apt.kubernetes.io/ kubernetes-xenial main" | sudo tee /etc/apt/sources.list.d/kubernetes.list

2.2 Install kubeadm, kubelet, and kubectl
Run the following command to install the necessary components:

sudo apt-get update sudo apt-get install -y kubelet kubeadm kubectl

2.3 Hold the packages
To prevent these packages from being automatically updated, hold them:

sudo apt-mark hold kubelet kubeadm kubectl

Step 3: Set Up a Kubernetes Cluster

Now that Kubernetes is installed, let’s set up a cluster.

3.1 Disable Swap
Kubernetes requires swap to be disabled. To disable swap, run:

sudo swapoff -a

To permanently disable swap, edit the /etc/fstab file and comment out the swap line.

3.2 Initialize the Master Node
Use kubeadm to initialize the cluster:

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

After running the command, you will see instructions on how to set up kubectl for the current user:

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

3.3 Set Up a Pod Network
To allow pods to communicate with each other, set up a pod network. In this example, we will use Calico:

kubectl apply -f https://docs.projectcalico.org/manifests/calico.yaml

Step 4: Join Worker Nodes (Optional)

If you want to add worker nodes to your cluster, use the kubeadm join command provided when you initialized the master node. Run this command on each worker node to add it to the cluster.

Step 5: Verify the Cluster

To verify that your cluster is running correctly, use the following command:

kubectl get nodes

This command will list all nodes in your cluster, and their status should be Ready.

Conclusion

In this guide, you learned how to install Kubernetes on Ubuntu, set up a basic cluster, and verify its functionality. Kubernetes is a powerful tool for managing containerized applications, and this setup is just the beginning. You can now explore deploying applications, scaling them, and managing your cluster with Kubernetes.

Try experimenting with different pod networks, deploying applications, and understanding how Kubernetes can simplify container orchestration in your development and production environments!