Kubernetes: Horizontal Pod Autoscaler using GKE

Clone git repository

https://github.com/itsmetommy/kubernetes-hpa-example-gke

Create namespace

kubectl create -n itsmetommy

Create

kubectl create -f .

Get

kubectl -n itsmetommy get all -l app=hpa-example
NAME READY STATUS RESTARTS AGE
pod/hpa-example-6cd7bf9947-q62vh 1/1 Running 0 5m
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/hpa-example LoadBalancer 10.43.29.175 35.230.80.192 80:30636/TCP 5m
NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE
deployment.apps/hpa-example 1 1 1 1 5m
NAME DESIRED CURRENT READY AGE
replicaset.apps/hpa-example-6cd7bf9947 1 1 1 5m
NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE
horizontalpodautoscaler.autoscaling/hpa-example Deployment/hpa-example 10539008/1000Mi, 0%/10% 1 10 1 3m

View website

kubectl -n itsmetommy get service hpa-example
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
hpa-example LoadBalancer 10.43.29.175 35.230.80.192 80:30636/TCP 7m

Open in browser

open http://35.230.80.192

Watch

Open a terminal window and watch pods, service, deployment, replicaset and the horizontalpodautoscaler.

watch kubectl -n itsmetommy get all -l app=hpa-example

Add load

I ran three while loops in three different terminal windows.

You can also note the targetAverageUtilization within hpa.yaml is set to 10 also to trigger the autoscale faster.

Terminal window 1

service=$(kubectl -n itsmetommy get service hpa-example | tail -1 | awk '{print $4}')
while true; do curl $service; done

Terminal window 2

service=$(kubectl -n itsmetommy get service hpa-example | tail -1 | awk '{print $4}')
while true; do curl $service; done

Terminal window 3

service=$(kubectl -n itsmetommy get service hpa-example | tail -1 | awk '{print $4}')
while true; do curl $service; done

After you’ve added load to the service, you should see a few hpa-example pods created automatically to handle the load.

Every 2.0s: kubectl -n itsmetommy get all -l app=hpa-example                                                                           TELMESEW-MBP: Thu Nov  1 19:01:06 2018
NAME READY STATUS RESTARTS AGE
pod/hpa-example-6cd7bf9947-22skk 1/1 Running 0 51s
pod/hpa-example-6cd7bf9947-8jkjv 1/1 Running 0 51s
pod/hpa-example-6cd7bf9947-crg77 1/1 Running 0 6m
pod/hpa-example-6cd7bf9947-q62vh 1/1 Running 0 18m
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/hpa-example LoadBalancer 10.43.29.175 35.230.80.192 80:30636/TCP 18m
NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE
deployment.apps/hpa-example 4 4 4 4 18m
NAME DESIRED CURRENT READY AGE
replicaset.apps/hpa-example-6cd7bf9947 4 4 4 18m
NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE
horizontalpodautoscaler.autoscaling/hpa-example Deployment/hpa-example 21764096/1000Mi, 238%/10% 1 10 4 16m

Stop load

In each terminal, terminate the while loop by typing control + C.

You should eventually see the additinal pods terminating.

Every 2.0s: kubectl -n itsmetommy get all -l app=hpa-example                                                                           TELMESEW-MBP: Thu Nov  1 19:09:01 2018
NAME READY STATUS RESTARTS AGE
pod/hpa-example-6cd7bf9947-q62vh 1/1 Running 0 26m
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/hpa-example LoadBalancer 10.43.29.175 35.230.80.192 80:30636/TCP 26m
NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE
deployment.apps/hpa-example 1 1 1 1 26m
NAME DESIRED CURRENT READY AGE
replicaset.apps/hpa-example-6cd7bf9947 1 1 1 26m
NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE
horizontalpodautoscaler.autoscaling/hpa-example Deployment/hpa-example 15917056/1000Mi, 0%/10% 1 10 1 24m

Clean up

kubectl delete -f .