Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Dynamic expansion and reduction of K8s Cluster-- HPA

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

Outline:

I. Introduction to HPA

II. Examples of pod expansion and reduction

I. Introduction to HPA

The full name of HPA is Horizontal Pod Autoscaling, which can dynamically expand and shrink the number of copies according to the utilization rate of current pod resources (such as CPU, disk, memory, etc.), so as to reduce the pressure on each pod. When the pod load reaches a certain threshold, more new pods will be generated to share the pressure according to the expansion and contraction strategy. When the pod is relatively idle, the number of copies of the pod will be automatically reduced after a stable idle period.

To realize the automatic scaling function, it is also necessary to deploy the heapster service, which is used to collect and count the utilization rate of resources. The kubectl top command is supported. The heapster service is integrated into the prometheus MertricServer service. Therefore, for convenience, I deploy the HPA (Dynamic Scaling) service on the environment based on the prometheus service here.

You can refer to the blog post: k8s cluster of three Web-UI deployment of the third deployment Prometheus service, to run prometheus service, if you do not want to deploy prometheus, then you can refer to github to deploy heapster service separately,

In summary, in order to achieve an increase or decrease in the number of pod copies, you must ensure that you can execute the following command on master:

[root@master ~]# kubectl top node #View node resource usage NAME CPU(cores) CPU% MEMORY(bytes) MEMORY% master 218m 10% 1362Mi 79% node01 113m 5% 1116Mi 64% node02 127m 6% 1287Mi 74% 2. Example of implementing pod expansion and reduction 1. Generate HPA controller #Run this hpa-example, request CPU resources of 200m, expose an 80 port [root@master ~]# kubectl run php-apache --image=mirrorgooglecontainers/hpa-example --requests=cpu= 200m--expose -port=80#When the CPU utilization rate of deployment resource object reaches 50%, expand it. Up to 10 [root@master ~]# kubectl autoscale deployment php-apache --cpu-percent=50 --min=1 --max=10#The above command is used to generate an HPA controller for controlling automatic expansion and contraction [root@master ~]# kubectl get svc| grep php-apache #View svc cluster IPphp-apache for php-apache ClusterIP 10.99.60.48 80/TCP 15m[root@master ~]# kubectl get pod | grep php-apa #Determine the current number of pods php-apache-867f97c8cb-vw74k 1/1 Running 0 17m2. Simulate the consumption of php-apache resources and verify whether pod will automatically expand and shrink.

Open multiple terminals and make endless loop requests to pods, as follows (if your system resources are sufficient, you can choose to open multiple terminals and make endless loop requests to pods. I have opened three terminals here and requested php-apache pods at the same time):

[root@master ~]# while true; do wget -q -O- 10.99.60.48; done #execute this command on each terminal [root@master ~]# kubectl top pod #You can check the pod load at any time with this command NAME CPU(cores) MEMORY(bytes) load-generator-7d549cd44-xm98c 0m 1Mi php-apache-867f97c8cb-vw74k 208m 20Mi #You can also check whether the number of pods has changed from time to time [root@master ~]# kubectl get pod #After running the endless loop request for a while, check the number of pods #You can see that the number of pods in php-apache has become 10, and will not increase any more, because the command above limits the maximum number of NAME READY STATUS RESTARTS AGEload-generator-7d549cd44-xm98c 1/1 Running 1 25mphp-apache-867f97c8cb-4r6sk 1/1 Running 0 19mphp-apache-867f97c8cb-4rcpk 1/1 Running 0 13mphp-apache-867f97c8cb-5pbxf 1/1 Running 0 16mphp-apache-867f97c8cb-8htth 1/1 Running 0 13mphp-apache-867f97c8cb-d94h9 0/1 ContainerCreating 0 13mphp-apache-867f97c8cb-drh62 1/1 Running 0 18mphp-apache-867f97c8cb-f67bs 0/1 ContainerCreating 0 17mphp-apache-867f97c8cb-nxc2r 1/1 Running 0 19mphp-apache-867f97c8cb-vw74k 1/1 Running 0 39mphp-apache-867f97c8cb-wb6l5 0/1 ContainerCreating 0 15m

When the endless loop request is stopped, the number of pods will not be reduced immediately, but will be reduced after a period of time to prevent traffic from surging again.

At this point, the automatic expansion and contraction of the number of pod copies is realized.

--------This article ends here, thanks for reading-------

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report