Create a deployment with the it’s service, i will set a CPU limit for this pod, basically it is a web server and i will hammer it with siege utility from another machin client
This is the content of the hpa_examle.yml manifest file
touk@k8smaster:~$ k top pods
NAME CPU(cores) MEMORY(bytes)
nignxwebsite-6d4bd7c9b4-smx77 0m 6Mi
The output of the k top pods command shows that the nignxwebsite pod is currently using 0m CPU, which means it's not being utilized at all. This is a good opportunity to test the HorizontalPodAutoscaler by increasing the load on the server and observing if the number of replicas increases accordingly.
After collecting the metrics, i may take some secondes:
touk@k8smaster:~$ k top pods
NAME CPU(cores) MEMORY(bytes)
nignxwebsite-6d4bd7c9b4-smx77 380m 6Mi
And it exceeds the limit i have set for it
Let’s Configure Autoscaler resource
From the command line utility we can use kbectl to create an autoscale
touk@k8smaster:~$ k get all
NAME READY STATUS RESTARTS AGE
pod/nignxwebsite-8594759588-xkjr2 1/1 Running 0 4m6s
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 45h
service/php-apache NodePort 10.107.246.64 <none> 80:30007/TCP 32m
NAME READY UP-TO-DATE AVAILABLE AGE
deployment.apps/nignxwebsite 1/1 1 1 32m
NAME DESIRED CURRENT READY AGE
replicaset.apps/nignxwebsite-6d4bd7c9b4 0 0 0 32m
replicaset.apps/nignxwebsite-8594759588 1 1 1 4m6s
NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE
horizontalpodautoscaler.autoscaling/nignxwebsite-hpa Deployment/nignxwebsite 0%/50% 1 3 1 62s
After using the siege utility to hammer the website
touk@k8snode3:~$ siege http://192.168.1.13:30007
** SIEGE 4.0.4
** Preparing 25 concurrent users for battle.
The server is now under siege...
touk@k8smaster:~$ k get all
NAME READY STATUS RESTARTS AGE
pod/nignxwebsite-8594759588-ft44q 0/1 ContainerCreating 0 2s
pod/nignxwebsite-8594759588-x2wwd 0/1 ContainerCreating 0 2s
pod/nignxwebsite-8594759588-xkjr2 1/1 Running 0 4m22s
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 45h
service/php-apache NodePort 10.107.246.64 <none> 80:30007/TCP 32m
NAME READY UP-TO-DATE AVAILABLE AGE
deployment.apps/nignxwebsite 1/3 3 1 32m
NAME DESIRED CURRENT READY AGE
replicaset.apps/nignxwebsite-6d4bd7c9b4 0 0 0 32m
replicaset.apps/nignxwebsite-8594759588 3 3 1 4m22s
NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE
horizontalpodautoscaler.autoscaling/nignxwebsite-hpa Deployment/nignxwebsite 275%/50% 1 3 1 78s
It started creating 3 pods, so the deployment tells the Replicaset spins up extra pods
touk@k8smaster:~$ k top pods
NAME CPU(cores) MEMORY(bytes)
nignxwebsite-8594759588-xkjr2 407m 7Mi
touk@k8smaster:~$ k top pods
NAME CPU(cores) MEMORY(bytes)
nignxwebsite-8594759588-ft44q 149m 3Mi
nignxwebsite-8594759588-x2wwd 156m 6Mi
nignxwebsite-8594759588-xkjr2 171m 8Mi