k8s Horizontal Scaling
The content of this article actually involves a k8s resource that I knew about a long time ago. But I used it again recently, so I’m making a note.
I won’t explain horizontal scaling (scaling up and down) here. People interested in reading this article should already know about it.
The best tutorials are actually from the official website. Here are the relevant links:
- A high - level introduction to horizontal scaling (HPA)
If you want to understand HPA more comprehensively, you can take a look at this article
https://kubernetes.io/zh/docs/tasks/run-application/horizontal-pod-autoscale/ - Practical examples
There are yaml sample files that can be directly modified and used
https://kubernetes.io/zh/docs/tasks/run-application/horizontal-pod-autoscale-walkthrough/
HPA Configuration Methods
There are two ways to configure HPA in k8s:
- Create directly through the command line, as follows,
1
kubectl autoscale deployment <deployment-name> --cpu-percent=75 --min=l --max=5
- Through a yaml configuration file
Write the detailed configuration in the yaml file and create the hpa through kubectl apply.
The second method is more recommended, especially in a production environment. If you use the first method, after a while, you may forget that you created such a thing, or you may not remember the specific details clearly. The second method allows you to store the yaml file through git, and use some methods (such as: PR triggering CICD) to always keep the yaml file in the repo consistent with the production environment.
HPA Configuration Instructions
1 | apiVersion: autoscaling/v2beta2 |
Dependencies for Configuring HPA
In the above configuration file, you can see that the two metrics I set are the utilization rates of Pods’ CPU and Memory. This means that k8s needs to provide an interface to collect this information, that is, the metric api. However, this is not deployed by default in k8s and needs to be deployed by yourself. For the specific deployment process, refer to the introduction on the official website.