Google Kubernetes Engine (GKE) and Cloud Build

Introduction
I intend to document and share my GKE learning experiences, which form a crucial part of my certification preparation journey.
As part of learning process my demo includes below services
Google Cloud Kubernetes Engine is the managed Kubernetes service, It simplifies the deployment, management, and scaling of containerized applications using Kubernetes, an open-source container orchestration platform
Google Cloud Artifact Registry is a managed container registry service provided by Google Cloud Platform (GCP). It allows us to store, manage, and secure container images and other artifacts.
Google Cloud Build is the fully managed build and deploy service provided by GCP
Demo!
In google cloud console enable below API services or use gcloud command given below
Navigation -APIs & Services → Enable APIs and Services → Search for below services and enable service APIs
- Kubernetes Engine API
- Container Registry API
- Cloud build
gcloud services clouddeploy.googleapis.com artifactregistry.googleapis.com container.googleapis.com enable cloudbuild.googleapis.com
Creating Cluster
GKE offers different types of clusters like standard and autopilot and options with node pool, regional and zonal configurations.
Autopilot clusters are pre-configured with an optimized cluster configuration that is ready for production workloads. For Standard mode, we need determine to the configurations needed for your production workloads.
gcloud command to create cluster
gcloud container clusters create mygkecluster — zone us-central1 — num-nodes 2
once cluster is created we can the cluster in console

Containerizing an app with Cloud Build
In this demonstration, I utilized a straightforward Python code example named app.py that displays static content.
To produce an image, a Dockerfile is essential. It’s recommended to employ multi-stage build images over single stage build because they lead to smaller image sizes, making them the preferred choice for production environments. In multi-stage build each stage(build ,package stage) can have a different base image and set of instructions. Reduces the final image size by discarding unnecessary build artifacts and dependencies.
Dockerfile

then zip app.py and Dockerfile using below command
zip -r app.zip app.py Dockerfile
Create Artificat repository
gcloud artifacts repositories create mygke-repo \
— project=ProjectID-123\
— repository-format=docker \
— location=us-central1 \
— description=”Docker repository”
which will create repo to push our image

Then using code build we can build the image and push to above artifact repo
gcloud builds submit “app.zip” — tag us-central1-docker.pkg.dev/PROJECTID/mygke-repo



Deploying to GKE
Now we have image ready in Artifact repo which we can deploy to GKE cluster.
Creating a deployment and service of type LoadBalancer for this demo. Below is the sample deployment yaml and deploy it using below command
kubectl apply -f deploymenttest.yaml

verfication — kubctl get deployments will display the deployments and use kubectl get pods to verify the pods
This service provides a load-balancing proxy over test-app pods and when we speciify type as a ‘LoadBalancer’, Kubernetes Engine will
create an external HTTP load balancer.
kubectl apply -f service.yaml

Verify the services using below command
kubectl get services

then verify the application using external IP.
Observability
Console provides clear dashboard on cluster resources usage and it has few recommended alerts as well where metrics are provided by default.
