Est. reading time: 5 minutes
How to Install Kubernetes Cluster on Ubuntu 18

How to Install Kubernetes Cluster on Ubuntu 18

Kubernetes (k8s) is an open-source, cloud-native, container orchestration and management platform. It’s the go-to way to automate the deployment, scaling, and maintenance of containerised applications across different nodes. From service discovery to auto-restarts, and from resource allocation tracking to compute utilisation and scaling; a well-configured k8s cluster can manage a lot on its own.


Got CentOS VMs? Learn How to install Kubernetes and deploy a cluster with Docker on CentOS 7

What is a Kubernetes Cluster?

A Kubernetes cluster consists of a Master and at least one to several worker node(s). The Master is the virtual machine (VM) that administers all activities on your cluster. A node is a VM that serves as a worker machine in your k8s cluster to host running applications. We strongly recommend you only use VMs aka Cloud Servers to run Kubernetes, not system containers aka VPS, as these can cause issues with k8s.

Kubernetes Cluster Diagram - Ubuntu

A node is comprised of the Kubelet, a container runtime, and the kube-proxy. The k8s installation’s three core modules: Kubelet, kubeadm, and kubectl are agents that control the node and communicate with the Kubernetes Master. Once they have been installed and other configurations done, you will be able to create your first k8s cluster. You can manage this cluster from the command line on your kubemaster node.

Every Kubernetes instance runs on top of a container runtime, which is software responsible for managing container operations. Containers in this case are not virtualised servers but rather a solution that packages code and dependencies to run a single application (service) in an isolated (containerised) environment, essentially disassociating applications from the host machine. The most popular and recommended one is Docker, and it’s the one we will use for the purpose of this guide. However, if you want to install a different underlying container runtime, you can harness the power of the Container Runtime Interface and use basically any runtime you want.

Kubernetes groups containers into pods, its most basic operational unit, which are basically just groups of containers running on the same node. Pods are connected over a network and share storage resources.

In order to connect your nodes or VMs and make them private, make sure to choose a hosting company who provides a Virtual Local Area Network (VLAN) with their VMs. We offer a VLAN add-on to our Cloud Servers for R200 per month.


• Multiple Ubuntu 18 VMs (Cloud Servers) to house the Master and worker nodes.
• Docker or any other container runtime.
• User with sudo or root privileges on every server.

How to install Kubernetes on Ubuntu 18

Step 1. Install Docker on all Ubuntu 18 VMs

Use our guide on How to install Docker on your Ubuntu 18.04

Step 2. Update the system

Before we proceed with the installations, it’s a recommended practice to update and upgrade our system. Use the following command:

sudo apt-get update && sudo apt-get upgrade

Depending on your system, it may take some time before the command finishes.

Step 3. Set hostnames

Set the appropriate hostnames for your master and worker nodes:

sudo hostnamectl set-hostname "master-node"
exec bash
sudo hostnamectl set-hostname "w-node1"
exec bash

Now open the /etc/hosts file and edit the hostnames for your worker nodes:

sudo cat <<EOF>> /etc/hosts master-node node1 w-node1 node2 w-node2

Step 4. Configure Firewall

For seamless communication across multiple nodes, we need to define rules in firewall. Use the following commands on your master node to do so:

sudo ufw allow 6443/tcp
sudo ufw allow 2379/tcp
sudo ufw allow 2380/tcp
sudo ufw allow 10250/tcp
sudo ufw allow 10251/tcp
sudo ufw allow 10252/tcp
sudo ufw allow 10255/tcp
sudo ufw reload

We also need to execute these commands on each of the worker nodes:

sudo ufw allow 10251/tcp
sudo ufw allow 10255/tcp
sudo ufw reload

Step 5. Install prerequisites

For Kubernetes APIs to work properly and securely, we need to install https and curl on all VMs. Use the following command to do so:

sudo apt-get install apt-transport-https curl -y

Step 6. Add and Configure the Kubernetes repository

We will use curl to add the Kubernetes repository on all the nodes. See commands below to first add the key, and then the repository:

sudo curl -s | sudo apt-key add

terminal output

sudo apt-add-repository "deb kubernetes-xenial main"

terminal output

Step 7. Install Kubernetes on Ubuntu 18

At this stage, we are ready to install the Kubernetes package. Use the following command:
(Note: The kubeadm package will also install all other necessary packages)

sudo apt-get install kubeadm -y

Verify your installation by running the kubeadm command. You should get output similar to this:

terminal output

Step 8. Disable swap

For kubeadm to function properly, we need to turn the swap off. Use the following command on all machines:

sudo swapoff -a

Deploying a Kubernetes Cluster on Ubuntu 18.04

Now that we are done installing Kubernetes, we can deploy our very own cluster. These are the necessary steps:

Step 1. Initialise kubeadm

To initialize our cluster, we need to initialize kubeadm. Run the following command on the Master node:

sudo kubeadm init

This can take some time as Kubernetes allocates resources and starts necessary services. A successful execution of the command should look like this:

terminal output

Go ahead and copy the text following the line Then you can join any number of worker nodes by running the following on each as root:. This is the command we will later use to add worker nodes to our cluster.

Note: If you forgot to copy the command, or have misplaced it, don’t worry. You can retrieve it again by entering the following command:

sudo kubeadm token create --print-join-command

Step 2. Create required directories and start managing Kubernetes cluster

In order to start managing your cluster, you need to create a directory and assume ownership. Run the following commands 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

Step 3. Set up Flannel as the Pod network for the Cluster

Pods within a cluster are connected via the pod network. At this point, it’s not working. This can be verified by entering the following two commands:

sudo kubectl get nodes
sudo kubectl get pods --all-namespaces

terminal output

As you can see, the status of master-node is NotReady. The CoreDNS service is also not running. To fix this, run the following command:

sudo kubectl apply -f

You should get the following output:

terminal output

And now if you verify the statuses of your node and CoreDNS service, you should get Ready and Running like seen below:

terminal output

Step 4. Add nodes to your cluster

As a final step, you need to add worker nodes to your cluster. We will use the kubeadm join auto-generated token in Step 1. here. Run your own version of the following command on all of the worker node VMs:

kubeadm join --token hpv60w.uw784fik3nqgf7fg --discovery-token-ca-cert-hash sha256:1ce91085c95535acaf1c536d5a84b54549680762fbbaf07467c6b8893a78db1a

On successful addition, you should get the following output:

terminal output

Running the following command on the master-node should show your newly added node.

sudo kubectl get nodes

terminal output

To set the role for your worker node, use the following command:

sudo kubectl label node w-node1

terminal output

Now you’re all set up.

Happy Hosting!

Related posts