Monday, June 22, 2020

How to setup Kubernetes Cluster in Ubuntu | Install Kubernetes Cluster in Ubuntu | Setup Master Node and one worker Node in AWS

Kubernetes  is an open source container platform that eliminates many of the manual processes involved in deploying and scaling containerized applications. We will learn how to setup Kubernetes Cluster in Ubuntu 18.0.4. You can setup Kubernetes Cluster in many ways. One of the ways is to use Kubeadm. 

Kubeadm is a tool built to provide kubeadm init and kubeadm join as best-practice “fast paths” for creating Kubernetes clusters.


1. Ubuntu instance with 4 GM RAM - Master Node - (with port open to all traffic
2. Ubuntu instance with at least 2 GM RAM - Worker Node - (with ports open to all traffic)

Kubernetes Setup using Kubeadm

###Start - Execute the below commands in both Master/worker nodes##########

Login to both instances execute the below commands:
sudo apt-get update -y  && sudo apt-get install apt-transport-https -y

Change to root user
sudo su -
sudo curl -s | apt-key add -

cat <<EOF >/etc/apt/sources.list.d/kubernetes.list
deb kubernetes-xenial main

sudo apt-get update

#Disable swap memory
swapoff -a
sudo sed -i '/ swap / s/^\(.*\)$/#\1/g' /etc/fstab

Enable IP tables
#We need to enable IT tables for pod to pod communication.
modprobe br_netfilter
sysctl -p
sudo sysctl net.bridge.bridge-nf-call-iptables=1

Install Docker on both Master and Worker nodes
apt-get install -y

Add ubuntu user to Docker group
usermod -aG docker ubuntu
systemctl restart docker
systemctl enable docker.service

Install Kubernetes Modules
sudo apt-get install -y kubelet kubeadm kubectl kubernetes-cni

sudo systemctl daemon-reload
sudo systemctl start kubelet
sudo systemctl enable kubelet.service
sudo systemctl status docker
#End - Execute the above commands in both Master/worker nodes##########

Initialize Kubeadm on Master Node(only on Master Node)

#Execute the below command as root user
sudo su -
kubeadm init

#exit from root user and execute as normal user

mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config

Installing the Weave Net Add-On
kubectl apply -f "$(kubectl version | base64 | tr -d '\n')"
It make take a few mins to execute the above command

You will get below messages.
serviceaccount "weave-net" created
clusterrole "weave-net" created
clusterrolebinding "weave-net" created
role "weave-net" created
rolebinding "weave-net" created
daemonset "weave-net" created

Now execute the below command to see the pods.

kubectl get pods  --all-namespaces

Now login to Worker Node

Join worker node to Master Node
The below command will join worker node to master node.
sudo kubeadm join <master_node_ip>:6443 --token xrvked.s0n9771cd9x8a9oc \
    --discovery-token-ca-cert-hash sha256:288084720b5aad132787665cb73b9c530763cd1cba10e12574b4e97452137b4a

Go to Master and type the below command
kubectl get nodes
the above command should display both Master and worker nodes.

It means Kubernetes Cluster - both Master and worker nodes are setup successfully and up and running!!!

Deploy Nginx on a Kubernetes Cluster
Let us run some apps to make sure they are deployed to Kuberneter cluster. We will do this in master node. The below command will create deployment:

kubectl create deployment nginx --image=nginx

View Deployments
kubectl get deployments
Create as a service 
kubectl create service nodeport nginx --tcp=80:80
kubectl get svc
run the above command to see a summary of the service and the ports exposed.

Now go Master or worker node, enter public dns and access it with port exposed
You should see the welcome page!

1 comment: