Comprehensive Guide: Setting Up a Kubernetes(kubeadm) Cluster on Google Cloud Platform
Hello everyone, In this article, I will explain in detail the steps required to create two virtual machines on Google Cloud Platform (GCP) and install Kubernetes. You can expand your cluster by increasing the number of machines. Let’s get started right away.
1. Creating a GCP Account and Signing In
Firstly, you need to have a Google Cloud Platform (GCP) account. If you don’t have one, you can create an account by following the steps below on Google Cloud. If you already have an account, sign in through the Google Cloud Console.
2. Creating a GCP Project and VM Instances
You’ll need to create a new project on GCP. Follow the steps below using the Google Cloud Console:
Google Cloud Console: Firstly, open Google Cloud Console in your browser.
Navigation Menu: Click on the navigation menu in the top left corner and select “Projects”.
Create Project: Click on the “Create Project” button to create a new project.
Project Details: Give your project a name and optionally specify a project ID. Then, click on the “Create” button.
Project Selection: Select the newly created project to proceed.
After following these steps, you have a new project.
Creating a VM (Virtual Machine)
If you didn’t see this page, you can click on the navigation menu in the top left corner and select “Compute Engine” under the “Compute” section.
To create two virtual machines (VMs), follow the steps below
Fill in the details for your first VM instance, such as name, region, zone, machine type, boot disk, and other configuration options according to your requirements. Then, click on the “Create” button to create the first VM.
First, let’s create the master node.
We’re performing the same steps for the worker-node as well.
3. Kubernetes installation
Now, let’s begin the Kubernetes installation on our machines. It’s worth noting that there are many different methods for installing Kubernetes. You can also choose a setup method that suits you best.
“Let’s Connect to Our Nodes via SSH”
Update the system and Install Prerequisites:
sudo apt update
sudo apt install curl -y
Download and Install kubectl
:
curl -LO https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl
sudo install -o root -g root -m 0755 kubectl /usr/local/bin/kubectl
kubectl version --client
Set Hostname for Master and Worker Nodes:
sudo hostnamectl set-hostname K8s-Master && bash
sudo hostnamectl set-hostname K8s-Worker && bash
Install Docker:
sudo apt-get install -y docker.io
sudo usermod -aG docker $USER
newgrp docker
sudo chmod 777 /var/run/docker.sock
Update Package Lists, Install Prerequisites, and Add Kubernetes Repository
sudo apt-get update -y
sudo apt-get upgrade -y
sudo apt-get install -y apt-transport-https ca-certificates curl gpg
sudo mkdir -p /etc/apt/keyrings/
curl -fsSL https://pkgs.k8s.io/core:/stable:/v1.29/deb/Release.key | sudo gpg --dearmor -o /etc/apt/keyrings/kubernetes-apt-keyring.gpg
echo 'deb [signed-by=/etc/apt/keyrings/kubernetes-apt-keyring.gpg] https://pkgs.k8s.io/core:/stable:/v1.29/deb/ /' | sudo tee /etc/apt/sources.list.d/kubernetes.list
Install Kubernetes Components
sudo apt-get update
sudo apt-get install -y kubelet=1.29.0-1.1 kubeadm=1.29.0-1.1 kubernetes-cni
sudo apt-mark hold kubelet kubeadm kubectl
Start and Enable Docker Service
sudo systemctl start docker
sudo systemctl enable docker
Configure Kernel Modules
sudo cat <<EOF | sudo tee /etc/modules-load.d/k8s.conf
overlay
br_netfilter
EOF
modprobe overlay
modprobe br_netfilter
Configure System Settings:
sudo cat <<EOF | sudo tee /etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-iptables = 1
net.bridge.bridge-nf-call-ip6tables = 1
net.ipv4.ip_forward = 1
EOF
sudo sysctl --system
Install and Configure Containerd
sudo apt update
sudo apt install -y containerd
sudo systemctl start containerd
sudo systemctl enable containerd
sudo mkdir -p /etc/containerd
sudo 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.toml
sudo systemctl restart containerd
sudo systemctl status containerd
Initialize Kubernetes on the Master Node:
sudo kubeadm config images pull
sudo kubeadm init --pod-network-cidr=10.244.0.0/16 --ignore-preflight-errors=All
Set Up kubeconfig for User
sudo mkdir -p /home/$USER/.kube
sudo cp -i /etc/kubernetes/admin.conf /home/$USER/.kube/config
sudo chown $USER:$USER /home/$USER/.kube/config
Deploy Networking and Storage Plugins
sudo su - $USER -c 'kubectl apply -f https://github.com/coreos/flannel/raw/master/Documentation/kube-flannel.yml'
sudo su - $USER -c 'kubectl apply -f https://raw.githubusercontent.com/rancher/local-path-provisioner/v0.0.26/deploy/local-path-storage.yaml'
sudo -i -u $USER kubectl patch storageclass local-path -p '{"metadata": {"annotations":{"storageclass.kubernetes.io/is-default-class":"true"}}}'
Now we will connect the Kubernetes worker node to the Kubernetes master node.
sudo kubeadm token create --print-join-command
Conclusion
“In conclusion, this guide provides a comprehensive walkthrough for setting up a Kubernetes cluster on both master and worker nodes. By following these step-by-step instructions, users can successfully deploy and manage Kubernetes clusters for their applications. From installing necessary dependencies, configuring Docker and Containerd, to initializing the master node and joining worker nodes to the cluster, this guide covers the essential tasks needed for Kubernetes deployment. With Kubernetes up and running, users can efficiently orchestrate containerized applications, streamline deployment processes, and scale their infrastructure as needed. By leveraging the power of Kubernetes, users can achieve greater flexibility, scalability, and resilience in their containerized environments.”