Configure Kubernetes Horizontal Pod Autoscaler
Enable metrics plugin on Minikube
minikube addons enable metrics-server
Start a deployment and expose it as a service
kubectl apply -f https://k8s.io/examples/application/php-apache.yaml
Create Horizontal Pod Autoscaler
kubectl autoscale deployment php-apache --cpu-percent=50 --min=1 --max=10
This command creates an HPA with a minimum of one pod and maximum of ten pods running. It will try to keep the averga usage of the pods at 50% and if there is more usage, it will automatically create more pods to keep the usage at 50%.
To test it, run the following on a different terminal
kubectl run -it --rm load-generator --image=busybox /bin/sh
Then, to create the trafic neede for the HPA start working.
**while true; do wget -q -O- http://php-apache; done**
Back on the first terminal, monitor the HPA with
kubectl get hpa -w
To have more control over the HPA we can also create it with a mainfest file, the equivalent to the command kubectl autoscale deployment php-apache --cpu-percent=50 --min=1 --max=10
would be:
apiVersion: autoscaling/v2beta2
kind: HorizontalPodAutoscaler
metadata:
name: php-apache
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: php-apache
minReplicas: 1
maxReplicas: 10
metrics:
- type: Resource
resource:
name: cpu
target:
type: Utilization
HPA can also be configured depending on the cpu usage or requests recieved, it is a good idea save it as a file to keeep record of the configuration for the HPA.
Reference Links
Last updated