Effective kubernetes auto-scaling requires coordination between two layers of scalability: (1) Pods layer autoscalers, this includes Horizontal Pod Autoscaler (HPA) and Vertical Pod Autoscaler (VPA)(aka. "rightsizing" or "autopilot"); both scale available resources for your containers, and (2) cluster level scalability, which managed by the Cluster Autoscaler (CA); it scales up or down the number of nodes inside your cluster.
Note this workshop focus only HPA.
The Horizontal Pod Autoscaler automatically scales the number of pods in a replication controller, deployment or replica set based on observed CPU utilization (or, with custom metrics support, on some other application-provided metrics).
The Horizontal Pod Autoscaler is implemented as a Kubernetes API resource and a controller. The resource determines the behavior of the controller. The controller periodically adjusts the number of replicas in a replication controller or deployment to match the observed average CPU utilization to the target specified by user.
- HPA continuously checks metrics values you configure during setup AT A DEFAULT 30 SEC intervals
- HPA attempts to increase the number of pods If the SPECIFIED threshold is met
- HPA mainly updates the number of replicas inside the deployment or replication controller
- The Deployment/Replication Controller WOULD THEN roll-out ANY additional needed pods
Objectives: Learn how to use the horizontal pod autoscaling feature and how to configure grafana to fetch pod/node metrics.
-
Start
minikube
with 4 cpu's and 4GB of memory Note the params specified to configure the behavior of the controller manager.Command
$ minikube start --cpus=4 --memory 4096 \ --extra-config=controller-manager.horizontal-pod-autoscaler-downscale-stabilization=5s \ --extra-config=controller-manager.horizontal-pod-autoscaler-sync-period=5s
-
Check heapster and metrics server are enabled
Command
$ minikube addons list | grep enabled
-
Check metrics pods are
Running
Command
$ kubectl get pods --namespace=kube-system | grep heapster $ kubectl get pods --namespace=kube-system | grep metrics-server
-
Check heapster is connected to
influxDB
Command
$ kubectl logs -n kube-system $(kubectl get -n kube-system pod --selector=k8s-app=heapster -o jsonpath='{.items..metadata.name}') | grep "k8s"
-
Check cpu and mem allocation
Command
$ kubectl describe nodes $ kubectl top node $ kubectl top pod -n kube-system
-
Open grafana
Command
$ minikube addons open heapster
-
configure grafana UI (optional)
-
Check previous values (request, usage) in Grafana
-
Deploy example app
Command
kubectl apply -Rf workshop-08/manifests/app
-
wait until pod is Running (1 replica is running)
Command
kubectl get pods --show-labels --watch
-
Deploy
HPA
Command
kubectl apply -f workshop-08/manifests/hpa/hpa.yaml
-
open new tab and run the following
Command
$ kubectl get hpa -w
-
open new tab and run the following (check 5 replicas running)
Command
$ kubectl get deployment php-apache -w
-
open new tab and run the following
Command
$ kubectl run --rm -i --tty load-generator --image=busybox /bin/sh # run this while true; do wget -q -O- http://php-apache.default.svc.cluster.local; done
-
Wait for the hpa to trigger (observe
hpa
anddeployment
) -
Stop the loader and check how the deployment scales down to 5