GSP-343: Optimize Costs for Google Kubernetes Engine
Overview
Task 1 : Create cluster and deploy an app
1ZONE=us-central1-a
2
3gcloud container clusters create onlineboutique-cluster \
4 --project=${DEVSHELL_PROJECT_ID} --zone=${ZONE} \
5 --machine-type=n1-standard-2 --num-nodes=2
6
7kubectl create namespace dev
8kubectl create namespace prod
9
10kubectl config set-context --current --namespace dev
11
12git clone https://github.com/GoogleCloudPlatform/microservices-demo.git
13cd microservices-demo
14kubectl apply -f ./release/kubernetes-manifests.yaml --namespace dev
15
16kubectl get svc -w --namespace dev
- Open
http://<EXTERNAL_IP>
in a new tab. You should see the homepage of the Online Boutique application.
Task 2 : Migrate to an Optimized Nodepool
1gcloud container node-pools create optimized-pool \
2 --cluster=onlineboutique-cluster \
3 --machine-type=custom-2-3584 \
4 --num-nodes=2 \
5 --zone=$ZONE
6
7for node in $(kubectl get nodes -l cloud.google.com/gke-nodepool=default-pool -o=name); do
8 kubectl cordon "$node";
9done
10
11for node in $(kubectl get nodes -l cloud.google.com/gke-nodepool=default-pool -o=name); do
12 kubectl drain --force --ignore-daemonsets --delete-local-data --grace-period=10 "$node";
13done
14
15kubectl get pods -o=wide --namespace dev
16
17gcloud container node-pools delete default-pool \
18 --cluster onlineboutique-cluster --zone $ZONE
Task 3 : Apply a Frontend Update
1kubectl create poddisruptionbudget onlineboutique-frontend-pdb \
2--selector app=frontend --min-available 1 --namespace dev
3
4KUBE_EDITOR="nano" kubectl edit deployment/frontend --namespace dev
- Change the following in the configuration to
1 image to gcr.io/qwiklabs-resources/onlineboutique-frontend:v2.1
2 imagePullPolicy to Always
Save by: ctrl + o
-> Enter
-> ctrl + x
Task 4 : Autoscale from Estimated Traffic
1kubectl autoscale deployment frontend --cpu-percent=50 \
2 --min=1 --max=13 --namespace dev
3
4kubectl get hpa --namespace dev
5
6gcloud beta container clusters update onlineboutique-cluster \
7 --enable-autoscaling --min-nodes 1 --max-nodes 6 --zone $ZONE
8
9kubectl exec $(kubectl get pod --namespace=dev | grep 'loadgenerator' | cut -f1 -d ' ') \
10 -it --namespace=dev -- bash -c "export USERS=8000; sh ./loadgen.sh"
Congratulations, you're all done with the lab 😄