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.
Table of Contents
- 1 Prerequisites
- 2 Step 1. Setting up hostname & Updating hosts file
- 3 Step 2. Installing Docker on Master and Slave Nodes
- 4 Step 3. Configuring Kubernetes repository on Master & Slave nodes
- 5 Step 4. Disabling swap and installing kubeadm
- 6 Step 5. Starting Kubernetes Cluster using Kubeadm
- 7 Step 6. Deploying Flannel as the pod network
- 8 Step 7. Adding Slave node to the cluster
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
$ sudo hostnamectl set-hostname "k8-master"
On the slave nodes run the following commands
$ 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.
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
$ sudo apt-get update && sudo apt-get upgrade
Next, install Docker both on the Master and Slave nodes using the following command
$ 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.
$ sudo systemctl start docker
$ sudo systemctl enable docker
Output
To verify that docker is running, issue the command on the master and slave Nodes
$ sudo systemctl status docker
Output
To view the Docker version that you’ve just installed run
$ docker --version
Output
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
$ sudo apt-get install apt-transport-https curl -y
Output
Next, add the Kubernetes package repository key by executing the command below
$ curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add
Output
Next, add the Kubernetes repository using the command below.
$ 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.
$ sudo swapoff -a
you can now install the kubeadm package as follows.
$ sudo apt-get install kubeadm -y
Output
Upon successful installation of kubeadm package, verify its version using the command below
$ kubeadm version
Output
Step 5. Starting Kubernetes Cluster using Kubeadm
On the Master node, log in and initialize kubernetes using kubeadm as shown.
$ 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
$ 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.
$ 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
$ 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
$ sudo kubectl get nodes
Output
As seen above, the Master node status has changed to “Ready’. In addition, verify the pod namespaces as shown.
$ sudo kubectl get pods --all-namespaces
Output
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
$ 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
$ 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.
Hello Punkaj
Thanks for your eazy installation guide. i was able to install it.
Regards
VInod
Hello Pankaj
I followed your tutorial and I was able to create a cluster.
But after I restart the VMs, I cannot start the cluster again.
I could restart the master after these steps:
kubectl reset
rm -rf /var/lib/etcd/
kubectl init
I could join the slaves again.
But when I try to execute sudo kubectl get pods –all-namespaces or sudo kubectl get nodes, I get this error:
Unable to connect to the server: x509: certificate signed by unknown authority (possibly because of “crypto/rsa: verification error” while trying to verify candidate authority certificate “kubernetes”)
What is the problem?
Nice Posting !! Thanks for sharing..