How to Install Kubernetes on Ubuntu 18.04?

Photo of author

By admin

Kubernetes is an open-source platform designed for the development and management of applications on containers like Docker. It allows you to run Docker and lets you create containers for pre-configured applications and images. With Kubernetes, you can balance workloads between containers and run more than one container on different systems simultaneously. 

On Ubuntu, you can install Kubernetes and use it for free, and Ubuntu always provides you with the updated versions of Kubernetes along with its latest features within a week of upstream release. It is possible to run Kubernetes on Ubuntu on any cloud, be it private, public, or bare-metal.

If you do not know how to install Kubernetes on Ubuntu or are facing difficulty with the installation process, you need not worry because this article is going to provide you with a detailed installation process of Kubernetes on Ubuntu 18.04.

Prerequisites

Before beginning the installation process, there are some prerequisites that you should be aware of:

  • Linux servers (2 or more) running Ubuntu 18.04.
  • A user account on each system along with root privileges. 
  • A default package manager.
  • A command-line/terminal window (Cltrl+Alt+T).

How to Install Kubernetes on Ubuntu

Setting up Docker

Step 1 – Install Docker

To install Kubernetes, the first thing you need to do is install Docker on your Ubuntu computer and if you have already installed Docker, then skip this step and go to the next one. 

However, if you do not have Docker installed on your desktop, here’s how you can install it:

  1. Firstly, you need to update the package list using the following command:
sudo apt-get update
  1. Then enter this command to initiate Docker installation:
sudo apt-get install docker.io
  1. Now you should repeat this process on each of the servers which act as nodes.
  2. To check if the installation is completed and to get information about the version of Docker installed, enter the following command:
docker ––version

Step 2 – Set up and Enable Docker

  1. You can set Docker to launch every time you boot by using the command:
sudo systemctl enable docker
  1. To verify if Docker is running, enter this command:
sudo systemctl status docker
  1. In the case that Docker is not running:
sudo systemctl start docker

You can use the aforementioned commands to set up and enable Docker on each server node.

Kubernetes Installation

Step 3. Put in Kubernetes Signing Key

We are downloading Kubernetes from a non-standard repository and that is why it is important to check the authenticity of the software. You can check the authenticity by simply putting in a signing key.

  1. For the addition of a signing key, enter the following command:
curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add

After entering this command, you may see that an error has occurred which says curl is not installed. To remedy this, you can install it by running the following command:

sudo apt-get install curl
  1. After installing curl, repeat the previous step to install the signing key and then repeat it for all the other nodes.

Step 4. Add Software Repositories

You will not be able to find Kubernetes in the default repositories and to add it, you will need to enter the following command:

sudo apt-add-repository "deb http://apt.kubernetes.io/ kubernetes-xenial main"

Now you will have to repeat this step for each server node. 

Step 5. Kubernetes Installation Tools

You will need to access the Kubernetes installation tools to initialize a cluster. Kubeadm (Kubernetes Admin) is one such tool, which speeds up the Kubernetes setup with the help of community-sourced best practices. The word package is Kubelet that runs on every node and initiates containers. The kubeadm tool provides command-line access to the Kubernetes clusters and here is how you can install the tool. 

  1. Start the installation of Kubernetes tools by entering the following command:
sudo apt-get install kubeadm kubelet kubectl
sudo apt-mark hold kubeadm kubelet kubectl

Now, wait for the process to complete.

  1. You can verify that the installation is complete by entering this command:
kubeadm version

Now you need to repeat this step for all server nodes. 

Step 6. Kubernetes Deployment Initiation

To initiate the Kubernetes deployment, you will need to disable the swap memory on every server by running the following command:

sudo swapoff –a

Step 7. Assign a Unique Hostname to Every Server Node

  1. You will need to decide which server you want to select as the master node. After you have decided, you need to use this command:
sudo hostnamectl set-hostname master-node
  1. Now, you have to assign a worker node hostname by running a command on the worker server, that is mentioned below:
sudo hostnamectl set-hostname worker01

In case you have more worker nodes, repeat the second part of this step to assign a unique hostname to each of them. 

Step 8. Initialize Kubernetes on Master Node

You can initialize Kubernetes on the master server node but first, you need to switch to the master node by entering this command:

 

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

After this, you will see a message that says “kubeadm join” at the end. Ensure that you note down the whole entry somewhere because you will need it to connect the worker nodes with the cluster. 

Now you can make a directory for the cluster by running the following commands:

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

Step 9. Deploy Pod Network on Cluster

A pod network enables communication between different nodes in the cluster. To deploy a pod network on the cluster, you can follow the given steps and keep in mind that these steps will use the flannel virtual network. Enter the following command:

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

Wait for a while for the process to be completed and then you can verify whether everything is running and communicating properly by running the following command:

kubectl get pods --all-namespaces

Step 10: Connect the Worker Node with the Cluster

We mentioned in Step 7 that it is possible to use the kubeadm join command to join all of the worker nodes and the cluster. 

To do this, you require switching to the worker01 system and then use the command that you made a note of in step 7:

kubeadm join --discovery-token abcdef.1234567890abcdef --discovery-token-ca-cert-hash sha256:1234..cdef 1.2.3.4:6443

You must remember to enter the codes from your master server in place of the alphanumeric codes shown above.

Do this for all of the worker nodes on the cluster. After waiting for some time, you should be able to see the state of the nodes by switching to the master server by running the command:

kubectl get nodes

Now you will be able to see the worker nodes that you have connected with the cluster. 

Conclusion

With the help of this guide, you should be able to properly install Kubernetes on Ubuntu 18.04. 

If you are a beginner and hardly know anything about running multiple containers, you can use Minikube in the beginning. Minikube is a system that allows you to run one node cluster locally and it can help you get familiar with the basics, and then you can move on to Kubernetes. 

Assuming that you have followed all the steps mentioned above, you are ready for launching and managing Docker containers on Kubernetes across numerous server nodes in the pod.

Leave a Comment