Kubernetes is a free and open-source container management framework providing a forum for automated deployment, scaling, and container operations across host clusters. You can use your hybrid, local and public cloud infrastructure free of charge with Kubernetes to operate your organisation’s deployment tasks. 

In this tutorial, we will illustrate the installation of Kubernetes in Ubuntu and deploy Kubernetes in a two-node Ubuntu cluster. 

Kubernetes (or, as is informally known, the K8s) is an open-source system which is used for containerized systems automation, scaling and management. Some of the advantages of Kubernetes include: 

  • Automatic installation and machine rollback:  Kubernetes updates an interface progressively and monitors its health to ensure that all instances are not destroyed simultaneously. If something goes wrong, Kubernetes will reverse changes for us. 
  • Load balancing and service exploration: The Pods can be loaded by Kubernetes and can distribute IP addresses and DNS names for the collection of Pods. 
  • Repository orchestration: Users can install local or cloud storage systems automatically. 
  • Container automatic packaging: Kubernetes distributes containers automatically according to their resource specifications and other restrictions. The greater the allocation of the container and its resources, the better the device efficiency. 
  • Use of Kubernetes packages: Kubernetes can handle your package configurations, in addition to its services. 
  • Self-recovery: Kubernetes tracks the container situation and can substitute it with new containers if anything goes wrong. Containers are also recreated that are already destroyed. 
  • Kubeadm: automates Kubernetes installation and setup, including the API, Controller Manager and Kube DNS server.

What is Kubeadm? 

Kubeadm automates Kubernetes components including API servers, Controller Manager and Kube DNS installation and setup. However, it does not build users or manage the installation and configuration of operating system dependencies. 

A configuration management tool like Ansible or SaltStack can be used for these preliminary tasks. The use of these tools makes it much easier and more feasible to create new clusters or to recreate existing clusters. 

Prerequisites: 

  • Hardware Requirement:    

    • For Master: 
      • RAM: 2 GB 
      • CPU: 2 Cores  
    • For Slave: 
      • RAM: 1 GB 
      • CPU: 1 Core 
  • Software Requirement:   

    • 2 or more Ubuntu 18.04 servers 
    • Access to a sudo or root privileged user account on each device 
    • The apt package manager. 

Swap disabled. You MUST disable swap in order for the kubernetes to work properly.  

The commands and procedures specified in this article were executed on an Ubuntu 18.04 LTS framework. You can open all commands through either the Dash, or the Ctrl+Alt+T shortcut, as we will use the Ubuntu command line, the Terminal. 

The two-node cluster will consist of a Master Node and Slave Node. These two nodes must be installed with Kubernetes. 

Master Node: The master Node is responsible for the administration of the cluster state (a node in Kubernetes refers to a server). 

SlavNode: The servers on which your workloads (i.e., container applications and services) are running are worker nodes or slave nodes. When delegated, the workload is continued by a worker even though the master falls when the schedule is complete. By adding workers, the capacity of a cluster can be increased. 

Follow the steps below to install Kubernetes on the two nodes in Ubuntu. 

Installation Procedure: 

Step 1: Installing Docker   

On both master and slave nodes, the following must be performed. 

1. Docker’s installation is the first thing to do. Login into the server to do this and export the command: 

sudo apt-get install docker.io 

How To Install Kubernetes on Ubuntu

In order to continue the installation, you will be asked to choose a Y/N option. Please enter Y to continue, then press Enter and your machine will be configured with Docker. 

2. The installation can also be checked by using the following instruction, and the Docker version number: 

$ docker --version 

How To Install Kubernetes on Ubuntu

You must add your user to the docker community once Docker is installed (otherwise you must carry out all the sudo docker commands which could lead to security problems). 

3. Use the command to connect your user to the docker group: 

sudo usermod -aG docker $USER 

Log out and log in, so that adjustments come into effect. 

4. Launch and activate the docker daemon with the commands: 

sudo systemctl start docker 
sudo systemctl enable docker 

Step 2: Installing Kubernetes 

Now, install Kubernetes on both the machines. As Kubernetes is downloaded from a non-standard repository, it is essential for the software to be authentic. This is achieved with the addition of a signing key. 

1. Add the GPG Kubernetes key with the command:  

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

How To Install Kubernetes on Ubuntu

2. If curl is not installed on your system, then you can install it with the commands: 

sudo apt install curl 

In order to continue the installation, you will be asked to choose a Y/N option. Please enter Y, then press Enter. You will then install the Curl utility on your device. 

3. Sometimes Kubernetes is not added in default repository. To add the Xenial Kubernetes repository, runthe following command on both nodes: 

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

How To Install Kubernetes on Ubuntu4. Installing Kubeadm (Kubernetes Admin) on both nodes by command is the final stage of the installation process: 

sudo apt-get install kubeadm kubelet kubectl 

Run the below command to check whether the versions of the components installed are compatible with each other. 

sudo apt-mark hold kubeadm kubelet kubectl

In order to continue the installation, you will be asked to choose a Y/N option. Please enter Y to continue, then press Enter. Kubeadm will then be installed on your device. 

5. You can also check Kubeadm’s version number and verify the installation using the following command: 

kubeadm version 

How To Install Kubernetes on Ubuntu

Repeat for each server node. 

Ensure that on each computer you install the same package version. Instability can arise from various versions. This mechanism also prevents Kubernetes from being updated automatically. 

Step 3: Running and Deploying Kubernetes 

1. Disable swap memory: 

You can deactivate the swap memory in both nodes because Kubernetes does not work properly on a swap-based device. Run the following command to disable swap memory on both nodes: 

sudo swapoff -a 

2. Unique hostnames to each node: 

In order to facilitate things, each server needs to be assigned specific hostname. 

  • To assign it a unique hostname, run the following command in the master node: 

sudo hostnamectl set-hostname master-node 
  • To assign it unique hostname, run the following command in the slave node: 

  sudo hostnamectl set-hostname slave-node 

  In case of additional worker nodes, use the command to set a unique hostname on each. 

3. Initialize Kubernetes on Master Node: 

Switch to the master server node and type: 

 sudo kubeadm init –pod-network-cidr=192.168.43.211/24  

You can find pod network CIDR in /etc/kubernetes/manifests/kube-controller-manager.yaml 

sudo grep cidr /etc/kubernetes/manifests/kube-* 

How To Install Kubernetes on UbuntuUpon completion of the command, a kubeadm join message is displayed at the end. Depending on your Internet connection, the process can take a minute or more. Make a note of the entire entry. This is used for joining the cluster’s nodes. 

  • You need to run the following as a daily user to start using your cluster: 

mkdir  -p $HOME/ .kube 
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config 
 $sudo chown $(id -u):$(id -g) $HOME/.kube/config 
  • By running the following command, you can check the status of the master node: 

kubectl get nodes 

How To Install Kubernetes on Ubuntu

4. Deploy Pod Network to Cluster: 

A pod network is a communication medium between network nodes. 

  • This article uses the following command to deploy a Flannel pod network in our cluster: 

sudo kubectl apply -f  https://raw.githubusercontent.com/coreos/  flannel/master/Documentation/kube-flannel.yml 
  • To display the network status, use the following command: 

kubectl get pods --all-namespaces

How To Install Kubernetes on Ubuntu

5. Add the slave node to form a cluster: 

you can enter thekubeadm joincommand on each slave node to connect it to the cluster. 

  • Run the command that you created when you initialized Kubernetes on your master node at the slave node: 

sudo kubeadm join 192.168.100.6:6443 --token        06tl4c.oqn35jzecidg0r0m --discovery-token-ca-cert-hash                            sha256:c40f5fa0aba6ba311efcdb0e8cb637ae0eb8ce27b7a03d47be6d966142f2204c 

How To Install Kubernetes on Ubuntu

  • So now you run the command on the master node, it confirms that your device has two nodes, the Master node and Server nodes. 
sudo kubectl get nodes 

This shows that the two-node cluster now operates via the container management system in Kubernetes.

4) Uninstalling Kubernetes   

1. Clear Pods 

Using this command, you can delete all the nodes: 

kubectl delete node --all 

2. Remove  kubeadm completely 

sudo apt-get purge kubeadm kubectl kubelet kubernetes-cni kube* 
sudo apt-get autoremove 

How To Install Kubernetes on Ubuntu

Learn more about the core concepts of Kubernetes Components, Architecture, Pods and Deployment with Kubernetes Training Course. 

Conclusion: 

You should now have Kubernetes installed on Ubuntu, after following carefully the steps stated in this article. 

This network uses many servers for back-and-forth communication. You can launch and manage Docker containers on several servers on the pod from Kubernetes. 

The Kubernetes container management system installed on two Ubuntu nodes was discussed in this post. We then developed and deployed Kubernetes to a single two-node cluster. This clustered network is now available for use,and you can use any service like a Nginx server or Apache container.