This comprehensive Linux guide expects that you run the following commands as root user but if you decide to run the commands as a different user then ensure that the user has sudo access and that you precede each of the privileged commands with sudo

Kubernetes is an open source system for applications management across multiple hosts. It provides basic mechanisms for automating deployment, maintenance, and scaling of applications.

This tutorial will guide you in installing Kubernetes on your Ubuntu System using step by step approach.

Installing Dependencies

Dependencies or prerequisites are the software or tools which must be installed before installing Kubernetes as the working on Kubernetes is based on the functioning of these software applications or tools. You must have at-least two servers which are running the same version of operating system to avoid any errors or misconfiguration, which is Ubuntu 18.04 for this tutorial.

Update System Repositories

Its a good practice to update the system before installing any software so to check the system for updates, you have to run the following commands

apt-get update

Installing Docker

To run Kubernetes, you must first install Docker on each system in your node. Install Docker using:

apt-get install docker.io

After installation, verify the Docker installation using:

docker ––version

Start the Docker service and check its status using the following commands on eah server in the node.

systemctl start docker
systemctl status docker

Make Docker to run at system startup automatically on each of the server in your node using:

systemctl enable docker

Kubernetes Installation

First add a signing key on each of the server on the node using:

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

This signing key would ensure that the Kubernetes packages being downloaded is authentic.

Adding Repository

Since Kubernetes is not available on the default Ubuntu repositories, so we need to add it the list of repositories that the system would search while updating its libraries. You must repeat this step on each server in the node.

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

Installation

To install Kubernetes, we first install Kubeadm (Kubernetes Admin) which helps to bootstrap a minimum viable Kubernetes. To install Kubeadm, run the following script in shell for each server in the node

apt-get install kubeadm kubelet kubectl
apt-mark hold kubeadm kubelet kubectl

Verify Install

If everything gets successfully installed you must verify installation on each server.

kubeadm version

Deploying Kubernetes

Disable Swap Space

The idea of Kubernetes is to tightly pack instances to as close to 100% utilized as possible. All deployments should be pinned with CPU/memory limits. So if the scheduler sends a pod to a machine it should never use swap at all. Swap is not needed as it will slow things down. To disable swap, use:

swapoff –a

Naming Servers

In the next step you have to choose the Master and Worker servers in your node. In the server you wish to act as a node, go to the server and enter the following command to assign the server as Master.

hostnamectl set-hostname master-node

The rest of the servers in the node can be assigned any hostname according to your choice to make them unique and easy distinguishable. For example in my case i have assigned ‘slave1’ as the hostname of one of the servers in the node.

hostnamectl set-hostname slave1

Configuring Kubernates On Master Node

Create Pod Network And Directories

Open up a terminal on the server you had already selected to behave as a Master and enter the following commands. Here 192.168.0.100 is the IP Address of my Master node. You may have different IP Address according to your network configuration.

kubeadm init --pod-network-cidr=192.168.0.100/16

After this command has done its work, it will generate a join script at the end of the message. You will need this join message to make other servers connect to the master node. Then Enter the following commands to make a directory for the cluster

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

Deploy Pod Network

Every Pod gets its own IP address. This means you do not need to explicitly create links between Pods and you almost never need to deal with mapping container ports to host ports. This creates a clean, backwards-compatible model where Pods can be treated much like VMs or physical hosts from the perspectives of port allocation, naming, service discovery, load balancing, application configuration, and migration. Thus Kubernetes imposes the following fundamental requirements on any networking implementation

  • Pods on a node can communicate with all pods on all nodes without NAT
  • Agents on a node can communicate with all pods on that node

This tutorial usues fpanel virtual network. To deploy it, use:

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

Check Deployment

To check that everything is working good and view the IP Adresses, use:

kubectl get pods --all-namespaces

Adding Slaves To Cluster

Now you have to join each of the server in your node to the cluster formed. Switch to any of the server except the master server and run the following command replacing the XXXXXX with the respective join script which was generated on the master in previous step and YYYYYY of the Masters IP Address.

kubeadm join --discovery-token XXXXX.XXXXXXXXXX --discovery-token-ca-cert-hash sha256:XXXXXXXX YYY.YYY.YYY.YYY:6443

Verification

Go to the Master server and run the following command to verify connection between the nodes

kubectl get nodes

You have successfully completed the Kubernetes installation and configuration.