To setup Kubernetes cluster, I will be setting the cluster in my local environment using Hyper-V. You can use any Hypervisor you like. I recommend VirtualBox if you want to setup local environment. Otherwise, below tutorial can be followed on any Virtual Machine, Physical machine, or any cloud Computing IAAS instance.

I created a virtual machine called k8s-master-node. You can call anything you like. For the purpose of this tutorial I will stick with this name.

Before you proceed forward, ensure that swap is disabled on the system. Otherwise kubernetes setup will fail.

System updates and Docker installation

Let's go ahead and first perform updates before setting up kubernetes environment. If you have installed fresh OS and haven't performed below execution of the command, I highly recommand it. It will get all the security patches and updates to system tools.

sudo apt-get update
sudo apt-get upgrade -y

Once you have performed above updates, let's go ahead and install dependencies for kubernetes. apt-transport-https package will allow http & https access in apt-get repositories and sources.

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

Once the above package is installed, now we are going to install docker which is the core engine that will run our container.

sudo apt install docker.io -y

Now we will start docker service and enable docker to start on system reboot. So docker can start on reboot if reboot is performed.

sudo systemctl start docker
sudo systemctl enable docker

If you haven't encountered any errors that means system updates and docker installation was successful and we are ready to setup Kubernetes.

Kubernetes Installation

Before we start Kubernetes setup, we need to download and add the key to allow Kubernetes installation.

sudo curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add -

If you receive OK in return then key was added successfully.

Now we need to add apt repository to install kubernetes.

We will first create a file called kubernetes.list

sudo vi /etc/apt/sources.list.d/kubernetes.list

Once the file is in the edit mode. Go ahead and copy paste below line in to the file and save it.

deb http://apt.kubernetes.io/ kubernetes-xenial main

Execute below commands to install kubernetes and tools required to manage kubernetes.

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

If no error occurred that means all kubernetes components are successfully installed!

Initializing Kubernetes

Now we need to initialize kubernetes cluster to run as Master. Execute below commands:

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

While initializing, if you received swap related errors as showed below. Please make sure swap is disabled in fstab file and reboot the server.

[ERROR Swap]: running with swap on is not supported. Please disable swap
[preflight] If you know what you are doing, you can make a check non-fatal with `--ignore-preflight-errors=...`

If you didn't receive above error, then proceed forward with additional configuration. Once the initialization is completed, you will receive a message with additional configuration to complete master node setup.

You should receive similar output.

k8s-master-setup-success

Note: Token that is shown in above end selected highlight might vary and will be different system to system as it is auto generated. Please make sure you make a note of your token to add additional nodes to join the cluster.

To complete setup. I will go ahead and execute below commands.

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

Pod Network Configuration

Before we start deploying pods in Kubernetes cluster. We need to ensure network configurations are applied to Kubernetes Cluster. It allows pod to pod communication and it's a dependency for kube-dns as well. In this case it is Master node where you will execute below commands. Go ahead and execute below commands on Master node to apply network settings.

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

Once you have applied above commands to setup networking. You will see that dns and flannel pods will be created. Without this, you will not able to run dns and communication between pods in Kubernetes cluster.

Go ahead and execute below command to see results of all pods.

sudo kubectl get pods --all-namespaces

k8s-pod-network-dns-setup

Once all the pods are created and up and running. You should see something like below.

k8s-pod-network-dns-setup-all-runing

Joining other linux node to Cluster

If you would like to add other node to kubernetes cluster; follow all the steps above provided in this tutorial under System updates and Docker installation & Kubernetes Installation section.

Once I have completed kubernetes setup on other nodes. I will execute below command to join the node to the cluster.

sudo kubeadm join --token 59f9e5.ca7acb3f48d73813 192.168.0.150:6443 --discovery-token-ca-cert-hash sha256:397f05b795e279a189e18dd9b17381c540a8d1513ca31cd40c8e3d91325a628a

Optional add-on for Kubernetes Cluster

If you like to add Kubernetes Dashboard to setup and configure pods via UI. You can install Kubernetes Dashboard that is free to install. Go ahead and execute below command.

$ kubectl apply -f https://raw.githubusercontent.com/kubernetes/dashboard/master/src/deploy/recommended/kubernetes-dashboard.yaml

Once you have executed above command. The pod will go in ContainerCreating status. You can check the status by executing below command.

sudo kubectl get pods --all-namespaces

k8s-dashboard

Try executing above command every 30 secs until the pod status becomes running. Once it is in running status, you can create proxy url to access the dashboard from your local system where kubernetes components are installed. I will create another tutorial soon that will show you how to setup kubernetes client components to manage kubernetes cluster remotely. But if you want to know how to access dashboard, go ahead and execute below command.

sudo kubectl proxy

Let us know if you have any questions by dropping us a comment below.