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.
Pre-Requistes:
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 https://packages.cloud.google.com/apt/doc/apt-key.gpg | apt-key add -
cat <<EOF >/etc/apt/sources.list.d/kubernetes.list
deb https://apt.kubernetes.io/ kubernetes-xenial main
EOF
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.
sudo 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 https://apt.kubernetes.io/ kubernetes-xenial main
EOF
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 docker.io -y
sysctl -p
sudo sysctl net.bridge.bridge-nf-call-iptables=1
Install Docker on both Master and Worker nodes
apt-get install docker.io -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
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##########
#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
#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 "https://cloud.weave.works/k8s/net?k8s-version=$(kubectl version | base64 | tr -d '\n')"
kubectl apply -f "https://cloud.weave.works/k8s/net?k8s-version=$(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.
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
--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