Deploying an On-Premise Kubernetes Cluster Using VM’s
What is Kubernetes?
Kubernetes is a container management tool for automating deployment, scaling, and management of containerized applications.
What Does Containerized Applications Mean?
Application containerization is an OS-level virtualization method used to deploy and run distributed applications without launching an entire Virtual Machine(VM) for each app.
Why Kubernetes?
When you deploy a containerized application you create multiple instances of the application for better availability and this scaling and management of the containers can become tedious when done manually this is where Kubernetes comes into the picture.
Kubernetes is an orchestration tool for containerized applications, with Kubernetes the management of the docker containers is automated. Some of the features of Kubernetes are:
- single click scaling of pods (pods are nothing but a group of one or more containers)
- Self Healing Properties like if any container fails Kubernetes will do health checks and will restart the container whereas if a node fails Kubernetes will Shift all the containers from the failed node to another node.
- Service Discovery and Load balancing: There is no need to worry about networking and communication Kubernetes will automatically assign a DNS name and will perform load balancing across all the containers.
- Storage Orchestration: Automatically mount the storage system of your choice, whether from local storage, a public cloud provider such as GCP or AWS.
Prerequisites For On-premise Kubernetes
System Requirements: Minimum 8Gb Ram (Optimal Performance with 16GB) and Minimum 8 CPUs.
Nodes: A Node may be a VM or a Physical Machine, In this example, we need a Minimum of Two VMS For Two Nodes (Master Node and Worker Node).
For Demonstration You need two VM’s Running Ubuntu.
Kubernetes Installation On Master Node
Open the Terminal and enter the below commands
- # sudo su
# apt-get update - Turn off Swap Space: By default, Linux assigns some memory in the swap space, make sure you’re system is not using swap memory.
# swapoff -a
# nano /etc/fstab
[fs tab is a file which will have an entry for swap space we have to disable that, comment the line with a # then save and exit] - Update the Hostname:
# nano /etc/hostname [Change it to eg: kmaster / kworker for worker node] - Note the IP address #ifconfig
Set a static IP so that it does not change on restarting the VM/system
# nano /etc/network/interfaces
//add the below lines
auto enp0s8
iface enp0s8 inet static
address <IP-Address-of-VM>
[these three lines will ensure that the machine will have a static IP address] - Update the hosts' file
# nano /etc/hosts
[ add the IP address of the master and worker with the name ]
Restart the system after this step.
Install Open SSH Server
# sudo apt-get install OpenSSH-server
Install Docker on ubuntu
# sudo su
# apt-get update
# apt install -y docker.io
The Docker service needs to be set up to run at startup. To do so, type in each command followed by enter (very important steps) :
# sudo systemctl start docker
# sudo systemctl enable docker
Download Kubernetes Package
# apt-get update && apt-get install -y apt-transport-https curl
# curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | apt-key add -
#
cat <<EOF >/etc/apt/sources.list.d/kubernetes.list
deb http://apt.kubernetes.io/ kubernetes-xenial main
EOF
# apt-get update
Install Kubernetes Components (Kubeadm, Kubelet, Kubectl)
# apt-get install -y kubelet kubeadm kubectl
Update the Kubernetes Configuration
*kubeadm is going to let us administrate the Kubernetes so we need to set the cgroups environment first*
# nano /etc/systemd/system/kubelet.service.d/10-kubeadm.conf
This will open a text editor, enter the following line after the last Environment Variable:
# Environment=”cgroup-driver=systemd/cgroup-driver=cgroupfs”
Master Node Setup Done!
Kubernetes Installation on Worker Node
- Similarly, follow the above steps for the worker node with the following changes.
- Change the IP when setting the static IP
Eg: change it to 192.168.56.102 for the worker node.
Additional Steps for both the nodes
- add the IP address for the master node in the worker node’s host file and vice versa.
Setup Only for Kubernetes Master Node (Initiating the cluster)
- We will now start our Kubernetes cluster from the master’s machine. Run the following command
# sudo kubeadm init- -apiserver-advertise-address=(IP of master VM) — — pod-network-cidr=192.168.0.0/16
*Note: The above command will take some time to complete* - All the other nodes will join through the IP address of the master mentioned above
- We have to also specify the pod network
Since we are using the calico pod network, for starting a calico CNI (container network interface) we have to use 192.168.0.0/16 network range, for the fannel pod network the range is 10.244.0.0/16 - Calico site:- https://www.projectcalico.org/calico-networking-for-kubernetes/
- The output:-
First, perform the Below steps on the master node and then enter the join command in the worker node
- As mentioned before, run the commands from the above output as a non-root user
$ mkdir -p $HOME/.kube
$ sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
$ sudo chown $(id -u):$(id -g) $HOME/.kube/config - To install the CALICO pod network, run the following command
#
kubectl apply -f https://docs.projectcalico.org/v3.9/manifests/calico.yaml
Now enter The join command in worker node!
- To create join command again in the future use the command :
# sudo kubeadm token create — print-join-command
Installing the Kubernetes Dashboard
- To bring up the Kubernetes dashboard use the below command
# kubectl apply -f https://raw.githubusercontent.com/kubernetes/dashboard/v2.0.0-beta8/aio/deploy/recommended.yaml - You can access the Dashboard using the kubectl command-line tool by running the following command:
- # kubectl proxy
Kubectl will make Dashboard available at http://localhost:8001/api/v1/namespaces/kubernetes-dashboard/services/https:kubernetes-dashboard:/proxy/. - The UI can only be accessed from the machine where the command is executed.
Kubernetes Cluster Setup and Dashboard Installation Done!