6.2 Horizontal Pod autoscaling

View in the book. Buy the book.

Autoscaling.

Create a HorizontalPodAutoscaler:

Or imperatively

kubectl autoscale deployment timeserver --cpu-percent=20 --min=1 --max=10

To test the HPA we need some requests that consume more CPU. Here’s an updated one:

And a new Deployment manifest with version 4.

To update your existing deployment:

$ cd Chapter06/6.2_HPA
$ kubectl apply -f deploy.yaml

Or, create everything from scratch

$ cd Chapter06/6.2_HPA
$ kubectl create -f deploy.yaml -f service.yaml -f hpa.yaml
deployment.apps/timeserver created
service/timeserver created
horizontalpodautoscaler.autoscaling/timeserver created

Get the external IP

$ kubectl get svc -w
NAME         TYPE           CLUSTER-IP       EXTERNAL-IP     PORT(S)        AGE
kubernetes   ClusterIP      34.118.224.1     <none>          443/TCP        79m
timeserver   LoadBalancer   34.118.238.176   35.233.155.58   80:32534/TCP   74m

Start a new load test

EXTERNAL_IP=203.0.113.16
ab -n 100000 -c 5 http://$EXTERNAL_IP/pi

(See 5.2 if you need to install ab.)

That will generate some load. Open some tabs and run

watch -d kubectl get deploy,hpa,pods

watch kubectl get deploy

and

kubectl top pods

kubectl top pods

You should observe Pods being created. Once Apache bench has stopped sending load, the reverse should happen. Pods typically scale down slower than they scale up.