Comprehensive Guide: Setting Up a Kubernetes(kubeadm) Cluster on Google Cloud Platform

Mehmet Denizli
6 min readMay 22, 2024

--

Kubernetes 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.

Type “Google Console” into the Google search bar and click on the Google Cloud Platform option.
Select the “free trial” option from the window that opens.
Choose the account type as “individual” as shown above. Enter your information and payment details.
Enter your payment information. It will ask for your payment details, but if you don’t approve when the free trial period ends, no charges will be deducted from your account.
Once you’ve entered your payment information, your free trial account will become active, and you can start using it immediately. Your account will be activated shortly.

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)

You can create a VM instance from here.

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.

Enable the Compute Engine service

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.

Name the VMs (e.g., “master-node” and “worker-node”) and select the Region and Zone (for example, “us-central1” region and “us-central1-a” zone).
Select the machine according to your needs.
Choose an appropriate machine type such as “e2-medium” (2 vCPUs, 4 GB RAM). If you prefer a slightly more powerful machine, you can adjust your machine preference based on your requirements.
Click on the “Change” button in the “Boot Disk” section and select “Ubuntu” as the operating system (for example, “Ubuntu 20.04 LTS”).
Click the “Create” button to create the VMs.

We’re performing the same steps for the worker-node as well.

We’ve created our worker node.
Our machines, which will serve as the master and worker nodes, are now ready.
In this section, we can check the firewall settings.
These are the default firewall settings that come with the platform.

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
You can check curl out your instance If `curl` is not installed, please install it.

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
These command download the kubectl binary.
These command install kubectl globally.
This command checks the version of kubectl client installed on the system.

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
This command update the package lists.
These commands install Docker and add the current user to the docker group, and set permissions for Docker socket.

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
These commands update package lists, upgrade existing packages, install necessary dependencies, create a directory for apt keyrings, download the Kubernetes repository signing key, and add the Kubernetes repository to the system.

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
These commands update package lists again and install specific versions of Kubernetes components: kubelet, kubeadm, and kubernetes-cni.
We have locked the packages to prevent them from being updated.”

Start and Enable Docker Service

sudo systemctl start docker
sudo systemctl enable docker
These commands start and enable the Docker service, ensuring that Docker is running and will start automatically on system boot.

Configure Kernel Modules

sudo cat <<EOF | sudo tee /etc/modules-load.d/k8s.conf
overlay
br_netfilter
EOF
modprobe overlay
modprobe br_netfilter
These commands configure kernel modules required by Kubernetes: overlay and 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
These commands configure system settings required by Kubernetes: enabling iptables filtering and IP forwarding.

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
These commands update package lists, install Containerd (a container runtime), start the Containerd service, and enable it to start on system boot. And then configure Containerd with systemd cgroup support, ensuring compatibility with Kubernetes. They also restart the Containerd service to apply the changes.

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
These commands pull necessary container images required by Kubernetes and initialize the Kubernetes control plane on the master node. The --pod-network-cidr flag specifies the CIDR range for pod IPs, and --ignore-preflight-errors=All ignores preflight checks to simplify setup.

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"}}}'
These commands configure the kubeconfig file for the current user, enabling them to interact with the Kubernetes cluster using kubectl. Additionally, they deploy networking (Flannel) and storage (Local Path Provisioner) plugins to the Kubernetes cluster and designate the local path storage class as the default.

Now we will connect the Kubernetes worker node to the Kubernetes master node.

sudo kubeadm token create --print-join-command
If you wish to use kubectl on the worker node, you can follow these steps.

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.”

--

--