How to Create a Horizontal Pod Autoscaler in Kubernetes


If you could like to a create a Horizontal Pod Autoscaler (hpa) in Kubernetes, then you could run the following:

Step 1 – Create a Deployment

If you already have a deployment, then ignore this step, otherwise:

kubectl create deployment php-apache --image=us.gcr.io/k8s-artifacts-prod/hpa-example
kubectl set resources deploy php-apache --requests=cpu=200m
kubectl expose deploy php-apache --port 80

kubectl get pod -l app=php-apache

Step 2 – Create a Horizontal Pod Autoscaler

kubectl autoscale deployment php-apache `#The target average CPU utilization` \
    --cpu-percent=50 \
    --min=1 `#The lower limit for the number of pods that can be set by the autoscaler` \
    --max=10 `#The upper limit for the number of pods that can be set by the autoscaler`

Step 3 – Get your hpa

kubectl get hpa