The first step towards Kubernetes Certification is installing Kubernetes. This blog is a step by step guide to install Kubernetes on top of Ubuntu VMs (Virtual Machines). Here, one VM will act as the master and the other VM will be the node. You can then replicate the same steps to deploy the Kubernetes cluster onto your prod.

Note: For this installation, we recommend a fresh Ubuntu 16.04 image since Kubernetes can take up a lot of resources. If your installation fails at any time, then execute all the steps mentioned from the very beginning in a fresh VM, because debugging would take longer.

To install Kubernetes, you have to diligently follow the 3 phases that come as part of the installation process:

Pre-requisites To Install Kubernetes

Since we are dealing with VMs, we recommend the following settings for the VMs:-

Master:

2 GB RAM

2 Cores of CPU

Slave/ Node:

1 GB RAM

1 Core of CPU

By this point of time, I have assumed you have 2 plain Ubuntu VMs imported onto your Oracle Virtual Box. So, I’l just get along with the installation process.

Pre-Installation Steps On Both Master & Slave (To Install Kubernetes)

The following steps have to be executed on both the master and node machines. Let’s call the the master as ‘kmaster‘ and node as ‘knode‘.

First, login as ‘sudo’ user because the following set of commands need to be executed with ‘sudo’ permissions. Then, update your ‘apt-get’ repository.

$ sudo su# apt-get update

Note: After logging-in as ‘sudo’ user, note that your shell symbol will change to ‘#’ from ‘$’.

Turn Off Swap Space

Next, we have to turn off the swap space because Kubernetes will start throwing random errors otherwise. After that you need to open the ‘fstab’ file and comment out the line which has mention of swap partition.

# swapoff -a# nano /etc/fstab

Then press ‘Ctrl+X’, then press ‘Y’ and then press ‘Enter’ to Save the file.

Update The Hostnames

To change the hostname of both machines, run the below command to open the file and subsequently rename the master machine to ‘kmaster’ and your node machine to ‘knode’.

# nano /etc/hostname

Then press ‘Ctrl+X’, then press ‘Y’ and then press ‘Enter’ to Save the file.

Update The Hosts File With IPs Of Master & Node

Run the following command on both machines to note the IP addresses of each.

# ifconfig

Make a note of the IP address from the output of the above command. The IP address which has to be copied should be under “enp0s8”, as shown in the screenshot below.

Now go to the ‘hosts’ file on both the master and node and add an entry specifying their respective IP addresses along with their names ‘kmaster’ and ‘knode’. This is used for referencing them in the cluster. It should look like the below screenshot on both the machines.

# nano /etc/hosts

Then press ‘Ctrl+X’, then press ‘Y’ and then press ‘Enter’ to Save the file.

Setting Static IP Addresses

Next, we will make the IP addresses used above, static for the VMs. We can do that by modifying the network interfaces file. Run the following command to open the file:

# nano /etc/network/interfaces

Now enter the following lines in the file.

auto enp0s8
iface enp0s8 inet static
address <IP-Address-Of-VM>

It will look something like the below screenshot.

Then press ‘Ctrl+X’, then press ‘Y’ and then press ‘Enter’ to Save the file.

After this, restart your machine(s).

Install OpenSSH-Server

Now we have to install openshh-server. Run the following command:

# sudo apt-get install openssh-server

Install Docker

Now we have to install Docker because Docker images will be used for managing the containers in the cluster. Run the following commands:

# sudo su
# apt-get update
# apt-get install -y docker.io

Next we have to install these 3 essential components for setting up Kubernetes environment: kubeadm, kubectl, and kubelet.

Run the following commands before installing the Kubernetes environment.

Install kubeadm, Kubelet And Kubectl

Now its time to install the 3 essential components. Kubelet is the lowest level component in Kubernetes. It’s responsible for what’s running on an individual machine. Kuebadm is used for administrating the Kubernetes cluster. Kubectl is used for controlling the configurations on various nodes inside the cluster.

# apt-get install -y kubelet kubeadm kubectl

Updating Kubernetes Configuration

Next, we will change the configuration file of Kubernetes. Run the following command:

# nano /etc/systemd/system/kubelet.service.d/10-kubeadm.conf

This will open a text editor, enter the following line after the last “Environment Variable”:

Environment=”cgroup-driver=systemd/cgroup-driver=cgroupfs”

Now press Ctrl+X, then press Y, and then press Enter to Save.

Voila! You have successfully installed Kubernetes on both the machines now!

As of now, only the Kubernetes environment has been setup. But now, it is time to install Kubernetes completely, by moving onto the next 2 phases, where we will individually set the configurations in both machines.

Steps Only For Kubernetes Master VM (kmaster)

Note: These steps will only be executed on the master node (kmaster VM).

Step 1: We will now start our Kubernetes cluster from the master’s machine. Run the following command:

Step 3: You will notice from the previous command, that all the pods are running except one: ‘kube-dns’. For resolving this we will install a pod network. To install the CALICO pod network, run the following command:

Step 5: Your dashboard is now ready with it’s the pod in the running state.

Step 6: By default dashboard will not be visible on the Master VM. Run the following command in the command line:

$ kubectl proxy

Then you will get something like this:

To view the dashboard in the browser, navigate to the following address in the browser of your Master VM: http://localhost:8001/api/v1/namespaces/kube-system/services/https:kubernetes-dashboard:/proxy/

You will then be prompted with this page, to enter the credentials:

Step 7: In this step, we will create the service account for the dashboard and get it’s credentials.Note: Run all these commands in a new terminal, or your kubectl proxy command will stop.

Run the following commands:

1. This command will create a service account for dashboard in the default namespace

$ kubectl create serviceaccount dashboard -n default

2. This command will add the cluster binding rules to your dashboard account