Kubernetes also referred to as k8 , is a free and opensource tool used for management of Docker containers. It’s a container orchestration platform that is tailored to automate the deployment, scaling, and management of containerized applications. In this guide, you will learn how to install and configure Kubernetes on Ubuntu 18.04 LTS.
Prerequisites
Before we get started , we are going to have a test lab comprising 3 Ubuntu 18.04 nodes as shown below
- Kubernetes Master Node IP address: 172.31.4.36 Hostname: k8-master
- Kubernetes Slave Node 1 IP address: 172.31.4.170 Hostname: k8-slave
- Kubernetes Slave Node 2 IP address: 172.31.10.30 Hostname: k8-slave2
In addition, ensure that your system has the following minimum requirements.
- 2 CPUS
- 4 GB RAM
- 8 GB free Hard disk space
Let’s now dive in and get started.
Step 1. Setting up hostname & Updating hosts file
To start off, you are going to log in the Master Node via SSH and set up the hostname as shown
1 |
$ sudo hostnamectl set-hostname "k8-master" |
On the slave nodes run the following commands
1 2 3 4 |
$ sudo hostnamectl set-hostname k8-slave $ sudo hostnamectl set-hostname k8-slave2 |
Using your favorite text editor, make the following modifications in the /etc/hosts
file for each of the 3 Nodes i.e k8-master, k8-slave and k9-slave2 respectively.
1 2 3 4 5 |
172.31.4.36 k8s-master 172.31.4.170 k8-slave 172.31.10.30 k8-slave2 |
Step 2. Installing Docker on Master and Slave Nodes
To install docker on the Master node, first, update and upgrade the system using the command below
1 |
$ sudo apt-get update && sudo apt-get upgrade |
Next, install Docker both on the Master and Slave nodes using the following command
1 |
$ sudo apt-get install docker.io -y |
Output
Once Docker is successfully installed, start and enable Docker service on both the Master and slave nodes using the commands below.
1 2 3 4 |
$ sudo systemctl start docker $ sudo systemctl enable docker |
To verify that docker is running, issue the command on the master and slave Nodes
1 |
$ sudo systemctl status docker |
To view the Docker version that you’ve just installed run
1 |
$ docker --version |
Step 3. Configuring Kubernetes repository on Master & Slave nodes
Before going to the next step, We need to install some useful packages. run the following commands on all the nodes
1 |
$ sudo apt-get install apt-transport-https curl -y |
Output
Next, add the Kubernetes package repository key by executing the command below
1 |
$ curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add |
Next, add the Kubernetes repository using the command below.
1 |
$ sudo apt-add-repository "deb https://apt.kubernetes.io/ kubernetes-xenial main" |
Output
NOTE:
Kubernetes package repository for Ubuntu 18.04 LTS is not available. Nevertheless, We have used the Xenial Kubernetes package repository.
Step 4. Disabling swap and installing kubeadm
We are going to install kubeadm package to allow us to deploy multiple nodes on our cluster.
But before we do so, Kubernetes Offical site recommends disabling the OS swap feature. To accomplish this run the following command.
1 |
$ sudo swapoff -a |
you can now install the kubeadm package as follows.
1 |
$ sudo apt-get install kubeadm -y |
Output
Upon successful installation of kubeadm package, verify its version using the command below
1 |
$ kubeadm version |
Output
Step 5. Starting Kubernetes Cluster using Kubeadm
On the Master node, log in and initialize kubernetes using kubeadm as shown.
1 |
$ sudo kubeadm init --pod-network-cidr=172.31.4.0/20 |
Sample output
The output above is a confirmation that we have successfully initiated the Kubernetes Master node To start the cluster, execute the commands enclosed inside the green highlight, one after the other
1 2 3 4 5 |
$ mkdir -p $HOME/.kube $ sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config $ sudo chown $(id -u):$(id -g) $HOME/.kube/config |
Now we are going to verify the status of the Master node by running the command below.
1 |
$ kubectl get nodes |
At this point, you will get a prompt that the Master Node is not ready because we have not yet deployed any pod. In the next step, we are going to deploy a pod network which is the network that our cluster nodes will be able to communicate with each other. To accomplish this, we are going to deploy Flannel as our pod network. Flannel is going to provide an overlay network between cluster nodes
Step 6. Deploying Flannel as the pod network
To deploy the pod network, run the following command in the Master node
1 |
$ sudo kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml |
Sample output
At this point, we can now verify the status of the Master Node using the kubectl
command
1 |
$ sudo kubectl get nodes |
As seen above, the Master node status has changed to “Ready’. In addition, verify the pod namespaces as shown.
1 |
$ sudo kubectl get pods --all-namespaces |
The output above shows that all the pod namespaces are in a running state. the final step will be joining the slave nodes to the cluster.
Step 7. Adding Slave node to the cluster
In this step, we are going to log in to both slave nodes (k8-slave and k8-slave2) and execute the following command which appears hightlighted in red in Step 5
1 2 |
$ kubeadm join 172.31.4.36:6443 --token w8kbni.wiyevyov0yxwwtdj --discovery-token-ca-cert-hash sha256:29adc042c538f59f0c7339ebad2126d5836de06ffe0ae22c54ce0aef2eb4cb76 |
Sample output
Now head out to the Master slave and check the status of the master and slave nodes using the kubectl
command
1 |
$ kubectl get nodes |
Output
The output above confirms that we have successfully added our two slave nodes to the cluster and their status, as well as the Master node, is ready!
Wonderful! We have concluded our tutorial on how to install and configure Kubernetes on Ubuntu 18.04 LTS. Your feedback is most welcome.