Optimize Costs for Google Kubernetes Engine: Challenge Lab

Mayank Chourasia
5 min readMar 8, 2021

In this article, we will go through the lab to Optimize Costs for Google Kubernetes Engine: Challenge Lab. In the previous, lab you will get familiar with Managing a GKE Multi-tenant Cluster with Namespaces, Exploring Cost-optimization for GKE Virtual Machines, Understanding and Combining GKE Autoscaling Strategies, and GKE Workload Optimization

The challenge contains 4 required tasks

  1. Create a cluster and deploy an app.
  2. Migrate to an Optimized Nodepool.
  3. Apply a Frontend Update.
  4. Autoscale from Estimated Traffic.

Challenge Scenario

  • You are the lead Google Kubernetes Engine admin on a team that manages the online shop for OnlineBoutique.
  • You are ready to deploy your team’s site to Google Kubernetes Engine but you are still looking for ways to make sure that you’re able to keep costs down and performance up.
  • You will be responsible for deploying the OnlineBoutique app to GKE and making some configuration changes that have been recommended for cost optimization.

Here are some guidelines you’ve been requested to follow when deploying:

  • Create the cluster in the us-central1 region
  • The naming scheme is team-resource, e.g. a cluster could be named onlineboutique-cluster
  • For your initial cluster, start with machine size n1-standard-2 (2 vCPU, 8G memory)

1. Create our cluster and deploy our app

  • The First Step is to activate the cloud shell and type the following command to set a Zone for Future use.
ZONE=us-central1-b
  • Creating a Cluster with only two (2) nodes.
gcloud container clusters create onlineboutique-cluster --project=$DEVSHELL_PROJECT_ID --zone=$ZONE --machine-type=n1-standard-2 --num-nodes=2
  • Creating namespaces to separate resources on your cluster following the 2 environments — dev and prod.
kubectl create namespace dev

kubectl create namespace prod
  • Deploying the application to the dev namespace with the following command in cloud shell.
git clone https://github.com/GoogleCloudPlatform/microservices-demo.git &&
cd microservices-demo && kubectl apply -f ./release/kubernetes-manifests.yaml --namespace dev
  • In the Cloud Shell Copy and Paste the code.
kubectl get svc -w --namespace dev
  • In the GCP Console go to Navigation Menu >Kubernets Engine> Service and Ingress.
  • You can see all the namespaces are created.

2.Migrate to an Optimized Nodepool

  • Creating a new node pool named optimized-pool with custom-2–3584 as the machine type and number of nodes to 2.
gcloud container node-pools create optimized-pool --cluster=onlineboutique-cluster --machine-type=custom-2-3584 --num-nodes=2 --zone=$ZONE
  • Migrating your application’s deployments to the new nodepool by cordoning off and draining default-pool.
for node in $(kubectl get nodes -l cloud.google.com/gke-nodepool=default-pool -o=name); do  kubectl cordon "$node"; done
  • Deleting the default-pool.
for node in $(kubectl get nodes -l cloud.google.com/gke-nodepool=default-pool -o=name); do kubectl drain --force --ignore-daemonsets --delete-local-data --grace-period=10 "$node"; donekubectl get pods -o=wide --namespace=dev

gcloud container node-pools delete default-pool --cluster onlineboutique-cluster --zone $ZONE
  • Write Y.

3. Apply a Frontend Update

  • Setting a pod disruption budget for your frontend deployment. Naming it online boutique-frontend-pdb and set the min-availability of your deployment to 1.
kubectl create poddisruptionbudget onlineboutique-frontend-pdb --selector app=frontend --min-available 1 --namespace dev
  • Edit your frontend deployment and change its image to the updated one.
KUBE_EDITOR="nano" kubectl edit deployment/frontend --namespace dev
  • Find the image under spec replace.
image: gcr.io/qwiklabs-resources/onlineboutique-frontend:v2.1
  • Then Press ctrl+x then Y enter.
  • And Put Always in image pull policy.
  • Checking all modules are in Running State.
kubectl autoscale deployment frontend --cpu-percent=50 --min=1 --max=13 --namespace dev

kubectl get hpa --namespace dev

4. Autoscale from Estimated Traffic

  • Updating your cluster autoscaler to scale between 1 node minimum and 6 nodes maximum.
gcloud beta container clusters update onlineboutique-cluster --enable-autoscaling --min-nodes 1 --max-nodes 6 --zone $ZONE

Congratulations! Done with the challenge lab.

Stay tuned till the next blog

If you Want to Connect with Me:

Linkedin: https://www.linkedin.com/in/mayank-chourasia-38421a134/

Twitter: https://twitter.com/ChourasiaMayank.

Google Developers Google Cloud Qwiklabs Google News Lab Kubernetes Advocate

--

--

Mayank Chourasia

Hey, My name is Mayank Chourasia. Currently I am working on SAP Utilities as a SAP ABAP Developer. I had written a blogs on SAP ISU, SAP ABAP, Google Cloud .