Google Kubernetes Engine (GKE) Cluster : Step by Step Procedure

This is a very simple and easy tutorial to deploy your first GKE Cluster. In this tutorial, we will perform the following actions :

  1. Deploy a Kubernetes Cluster with 2 nodes
  2. Create a simple docker image and push it to a google container registry (gcr.io)
  3. Deploy the docker image to the Kubernetes Cluster

Pre-requisites

1. Deploy a Kubernetes Cluster with 2 nodes

Once you’re logged in the Google Cloud console, open a Cloud Shell to execute the following commands

We start by setting the right project on the Cloud Shell .
As we created a project with the id “gke-demo-cluster-cm”, we will run this command :

gcloud config set project gke-demo-cluster-cm

We need to create a default network on which our Kubernetes cluster will be deployed, to create a default network, run the command

gcloud compute networks create default

We will enable the container apis, which is essential to create the Kubernetes cluster (more details)

gcloud services enable container.googleapis.com

The next step, is to set in which zone the cluster will be deployed by running the command :

gcloud config set compute/zone europe-west2-a

Note : you can find a complete list of all the available zones here

Now that we have set up all the pre-requisites, we can now create our Kubernetes cluster, in our example we will create a Kubernetes cluster named “gke-cluster” with “2 nodes”

gcloud container clusters create gke-cluster --num-nodes=2

Once the cluster is created, before accessing the cluster we need to run the command gcloud container clusters get-credentials, that will update the kubeconfig file with the right credentials and endpoint information to point kubectl to the newly created cluster “gke-cluster”

gcloud container clusters get-credentials gke-cluster

Note : kubectl is a CLI tool that helps you manage your Kubernetes clusters

A good way to validate that you can access your cluster and see how many nodes you have, is to run the command “kubectl get nodes”

kubectl get nodesOutputNAME                                         STATUS   ROLES    AGE     VERSION
gke-gke-cluster-default-pool-ef6c15c6-323p Ready <none> 2m48s v1.16.15-gke.6000
gke-gke-cluster-default-pool-ef6c15c6-p89r Ready <none> 2m58s v1.16.15-gke.6000

Congratulations you’ve successully deployed your first GKE cluster !!!

Alternatively you can use this shell script to automate the deployment of your cluster:

#!/bin/bash
# This script will deploy a new GKE cluster in a default network
# Please provide the 6 arguments in this order PROJECT_ID, FOLDER_ID, BILLING, REGION, CLUSTER_NAME and NUMBER_OF_NODES

# Example : ./deploy-gke.sh gke-project-demo 422615538687 3461CD-G9A478-AF75C9 europe-west2-a gke-cluster1 2


# Variables
PROJECT_ID=${1} # The Google Project ID (example "gke-project-demo")
FOLDER_ID=${2} # the folder id of the parent folder
BILLING=${3} # the billing account number you want to associate the project
REGION=${4} # The Region where the cluster is deployed (example "europe-west2-a")
CLUSTER_NAME=${5} # The name of the cluster (example "gke-cluster1")
NUMBER_OF_NODES=${6} # The number of nodes (example "2")

usage() {
echo "Usage: ${0} [PROJECT_NAME] [FOLDER_ID] [BILLING_ACCOUNT] [REGION] [CLUSTER_NAME] [NUMBER_OF_NODES] " >&2
echo "Example :"
echo
echo " ./deploy-gke.sh gke-project-demo 422615538687 3461CD-G9A478-AF75C9 europe-west2-a gke-cluster1 2"
echo
exit 1
}# Check if the number of arguments provided is less than 6
if [[ "${#}" -lt 6 ]]
then
usage
fi

# Create a new project
gcloud projects create ${PROJECT_ID} --folder=${FOLDER_ID} |& cat -n

# Link new project to existing billing account
gcloud alpha billing projects link ${PROJECT_ID} --billing-account ${BILLING}

# Set the configuration of your project
gcloud config set project ${PROJECT_ID}

# Enabble container apis
gcloud services enable compute.googleapis.com

# Enabble container apis
gcloud services enable container.googleapis.com

# Create a new network
gcloud compute networks create default

# Set the region for your GKE Cluster
gcloud config set compute/zone ${REGION}

# Create the GKE Cluster
gcloud container clusters create ${CLUSTER_NAME} --num-nodes=${NUMBER_OF_NODES}

# Configure kubectl to use the cluster
gcloud container clusters get-credentials ${CLUSTER_NAME}

# Display the nodes
kubectl get nodes

exit 0

Note : Please clone this repository before performing the next actions

2. Create a simple docker image and push it to a google container registry

Our first task is to run this command to create a new Dockerfile

cat > Dockerfile <<EOF
FROM ubuntu:18.04
RUN apt-get update
RUN apt-get install nginx -y
COPY index.html /var/www/html/
EXPOSE 80
CMD ["nginx","-g","daemon off;"]
EOF

Once the Dockerfile is created, we then run this command to build a new image, in our example we will create new image “nginxdemo:1.0”

docker build -t nginxdemo:1.0 .

You can validate that the image has been successfully (locally) by running the command

docker images

Now that we have our image, we then need to tag that image to match the gcloud container registry (grc.io) and then push it

docker tag nginxdemo:1.0 gcr.io/gke-demo-cluster-cm/nginxdemo:1.0
docker push gcr.io/gke-demo-cluster-cm/nginxdemo:1.0

gke-demo-cluster-cm : make sure to replace with your project id

3. Deploy the docker image to the Kubernetes Cluster

The next step is to deploy our image from gcr.io to our kubernetes cluster.
We will create a new deployment by running the command:

kubectl create deployment web-server --image=gcr.io/gke-demo-cluster-cm/nginxdemo:1.0

Once the image deployed, to allow external access to our web server, we need to expose our deployment.
This will create a new service with a ClusterIP and a ExternalIP

kubectl expose deployment web-server --type LoadBalancer --port 80 --target-port 80

We can retrieve the external ip address for the service by running this command

kubectl get service web-serverOutputNAME         TYPE           CLUSTER-IP     EXTERNAL-IP   PORT(S)        AGE
web-server LoadBalancer 10.3.249.196 34.89.89.83 80:32593/TCP 18s

Note : if the status of the external ip is pending, refresh a few time, it might take 2-3 min to provision an ip

Open a browser and navigate to the external ip

Conclusion:

In this project, we learned how to deploy a GKE Cluster with simple steps. Then we built a docker image that we pushed to the google container registry gcr.io, and finally we deployed this image to GKE Cluster.
I hope you enjoyed this tutorial.

Leave a Comment

MFH IT Solutions (Regd No -LIN : AP-03-46-003-03147775)

Consultation & project support organization.

Contact

MFH IT Solutions (Regd)
NAD Kotha Road, Opp Bashyam School, Butchurajupalem, Jaya Prakash Nagar Visakhapatnam, Andhra Pradesh – 530027