How to Setup Kubernetes Cluster in Google Cloud Virtual Machine using "kubeadm"? | Ubuntu 20.04/22.04 LTS
Hello Friends,
In this post, we are going to setup Kubernetes Cluster on Virtual Machine in Google Cloud Platform using kubeadm tool. Hope this post will help you in Kubernetes learning Hand-On Labs (HOL).
Requirements:
Master Node: No. of VMs 1
Specifications - 2 vCPUs, 4GB RAM, 20 GB HDD (Balanced PD or SSD PD), Operating System (OS) Ubuntu 20.04 LTS x86/64, amd64
Firewall Rule - Ingress Allow 6443 (API Server) | 2379 (ETCD) | 10251 (Scheduler) | 10252 (Controller Manager) 10250 (Kubelet), sudo access with admin access
Worker Node: No. of VMs 2
Specifications - 2 vCPUs, 4GB RAM, 20 GB HDD (Balanced PD or SSD PD), Operating System (OS) Ubuntu 20.04 LTS x86/64, amd64
Firewall Rule - Ingress Allow 30000-32767 (Services) | 10250 (Kubelet), sudo access with admin access
Disable Swap and comment fstab entry:
First, Disable Swap and remove or comment the Swap entries from fstab file :
sudo swapoff -a
sudo sed -i '/ swap / s/^\(.*\)$/#\1/g' /etc/fstabNext, Load the below kernel module on all the nodes (master and workers) | Bridge Traffic.
sudo tee /etc/modules-load.d/containerd.conf <<EOF overlay br_netfilter EOF
sudo modprobe overlay
sudo modprobe br_netfilter
lsmod | grep br_netfilter
Now, we have to set the following Kernel parameters
sudo tee /etc/sysctl.d/kubernetes.conf <<EOF
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
net.ipv4.ip_forward = 1
EOF
Apply the changes, reload system configuration
sudo systemctl --system
Install Docker/Containerd runtime: (Run on both Master and worker node)
sudo apt install -y curl gnupg2 software-properties-common apt-transport-https ca-certificatesEnable docker repository and Install Docker and Containerd Runtime
sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmour -o /etc/apt/trusted.gpg.d/docker.gpg
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
sudo apt update
sudo apt install -y containerd.io docker.io
containerd config default | sudo tee /etc/containerd/config.toml >/dev/null 2>&1
sudo sed -i 's/SystemdCgroup \= false/SystemdCgroup \= true/g' /etc/containerd/config.tomlRestart and enable containerd service
sudo systemctl restart containerd
sudo systemctl enable containerd
sudo systemctl restart docker
sudo systemctl enable docker
Installation of "kubeadm" "kubelet" and "kubectl"
Adding APT repository for Kubernetes
sudo systemctl enable docker
curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo gpg --dearmour -o /etc/apt/trusted.gpg.d/kubernetes-xenial.gpg
sudo apt-add-repository "deb http://apt.kubernetes.io/ kubernetes-xenial main"
Installing kubeadm, kubelet and kubectl
sudo apt update
sudo apt install -y kubelet kubeadm kubectl
Pause/Hold the auto update for the all 3 above installed packages
sudo apt-mark hold -y kubelet kubeadm kubectl
Now, finally start and enable the kubelet
sudo systemctl daemon-reload
sudo systemctl enable kubelet
sudo systemctl restart kubelet
sudo systemctl status kubelet
Initializing Kubernetes Control Plane with "kubeadm" | Run on Master Node only
sudo kubeadm init
Once, Your Kubernetes Control Plan initialized successfully. It will prompt you few commands for 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/configNow, run the following kubectl commands to view cluster and node status.
kubectl cluster-info
kubectl get nodes
Join Worker Node to the Kubernetes Master Node/Control Plane
Run this command in each worker node to join master. Run the command given below after initialized successfully messege. (Similar to below) | Only Worker Nodes
kubeadm join 10.128.0.255:6443 --token ug227.1t6vnwhra8cr95m \ --discovery-token-ca-cert-hash sha256:f5d5fffad20df7d81799f98162c0a7c6c54d07ec17cefffd84578371118d116b
Now, run the Following command on master node/control plane to see if the worker node joined.
kubectl get nodes -o wide
Setup Pod Network (Weave Network)
Now, the final steps is to deploy a pod network to the cluster. Documentation link given same screen after Control Plane initialized successfully screen. Open that link and setup pod network as your choice.
Here we are going to setup Weave Net kube-addon:
kubectl apply -f https://github.com/weaveworks/weave/releases/download/v2.8.1/weave-daemonset-k8s.yaml
Check the status of all the kube-system namespace pods, if everything is in "Running" state:
kubectl get pods -n kube-system
kubectl get nodes -o wide
Thanks a lot for your time reading this blog post. Stay tune for new exciting contents.