Detailed explanation of kubernetes k8s deploying metrics server and HPA through Helm



Detailed explanation of kubernetes k8s deploying metrics server and horizontal pod autoscaling (HPA) through Helm


Host configuration planning

Server name (hostname) System version to configure Intranet IP External IP (Analog)
k8s-master CentOS7.7 2C/4G/20G
k8s-node01 CentOS7.7 2C/4G/20G
k8s-node02 CentOS7.7 2C/4G/20G


Deploying metrics server using Helm

Under normal circumstances, if metrics server is not deployed, we cannot collect information by using the following command

1 kubectl top node
2 kubectl top pod -A

First complete theHelm deployment, use and example of kubernetes k8s」。

From heapsterGitHub address: can be seen in, heapster has been retired. Starting with kubernetes 1.12, it will be removed from various kubernetes installation scripts. Kubernetes recommends using metrics server. Here we use helm to deploy metrics server.

GitHub address:


Metrics-server-amd64 image download

1 # in the cluster, all nodes need to execute
2 docker pull
3 docker tag
4 docker rmi


metrics-server. Yaml file

1 [[email protected] helm]# pwd
2 /root/k8s_practice/helm
3 [[email protected] helm]# 
4 [[email protected] helm]# cat metrics-server.yaml 
5 args:
6 - --logtostderr
7 - --kubelet-insecure-tls
8 - --kubelet-preferred-address-types=InternalIP


Deploying metrics server through Helm

1 # query the version information of metrics server
2 helm search stable/metrics-server -l
3 # deploy metrics server through helm and specify the version
4 helm install stable/metrics-server --version 2.11.1 -n metrics-server --namespace kube-system -f metrics-server.yaml


View helm and pod information

1 [[email protected] ~]# helm list
2 NAME              REVISION    UPDATED                     STATUS      CHART                    APP VERSION    NAMESPACE  
3 metrics-server    1           Mon Jul 20 10:06:58 2020    DEPLOYED    metrics-server-2.11.1    0.3.6          kube-system
4 [[email protected] ~]# 
5 [[email protected] ~]# kubectl get deploy -A | grep 'metrics-server'
6 kube-system            metrics-server              1/1     1            1           94s
7 [[email protected] ~]# 
8 [[email protected] ~]# kubectl get pod -A | grep 'metrics-server'
9 kube-system            metrics-server-6796d97d6b-wvd48              1/1     Running   0          18s


Now you can use the following command to obtain the indicator information about the cluster node and Pod:

 1 [[email protected] ~]# kubectl top node
 2 NAME         CPU(cores)   CPU%   MEMORY(bytes)   MEMORY%   
 3 k8s-master   205m         10%    2122Mi          57%       
 4 k8s-node01   130m         6%     1060Mi          61%       
 5 k8s-node02   193m         9%     1093Mi          63%       
 6 [[email protected] ~]# 
 7 [[email protected] ~]# kubectl top pod -A
 8 NAMESPACE              NAME                                         CPU(cores)   MEMORY(bytes)   
 9 default                load-generator-57d5fb67c7-xx2wj              0m           0Mi             
10 default                php-apache-5db466758-k2vxj                   1m           20Mi            
11 kube-system            coredns-6955765f44-c9zfh                     5m           20Mi            
12 kube-system            coredns-6955765f44-lrz5q                     5m           20Mi            
13 kube-system            etcd-k8s-master                              21m          88Mi   
14 ………………


Horizontal Pod Autoscaling(HPA)

Horizontal pod autoscaling (HPA) automatically scales the number of pods in a replication controller, deployment, or replicaset based on CPU utilization.

Case image download

1 # gcr. io/google_ Containers / HPA example is abroad. Here we download the domestic image
2 # in the cluster, all nodes need to execute [mainly node nodes]
3 docker pull


Start pod

kubectl run php-apache --requests=cpu=200m --expose --port=80


Create HPA controller. See the following address for relevant algorithms:

1 # official address:
2 # when the CPU utilization in the pod reaches 50%, the capacity is expanded. Minimum 1, maximum 10
3 kubectl autoscale deployment php-apache --cpu-percent=50 --min=1 --max=10


Query relevant information at this time

1 [[email protected] ~]# kubectl get hpa
 2 NAME         REFERENCE               TARGETS         MINPODS   MAXPODS   REPLICAS   AGE
 3 php-apache   Deployment/php-apache   0%/50%          1         10        1          3m16s
 4 [[email protected] ~]# 
 5 [[email protected] ~]# kubectl get deploy -A | grep 'php-apache'
 6 default                php-apache                  1/1     1            1           10h
 7 [[email protected] ~]# 
 8 [[email protected] ~]# kubectl get pod -o wide
 9 NAME                         READY   STATUS    RESTARTS   AGE    IP             NODE         NOMINATED NODE   READINESS GATES
10 php-apache-5db466758-k2vxj   1/1     Running   0          8m8s   k8s-node01


Increase load

1 # start a pod and enter a terminal
2 kubectl run -i --tty load-generator /bin/sh
3 # re enter the pod [note the name of the pod and modify it according to the actual situation]
4 kubectl exec -it load-generator-57d5fb67c7-xx2wj -- /bin/sh
5 # cyclic access in pod
6 while true; do wget -q -O- http://php-apache.default.svc.cluster.local; done

If multiple terminals are required to access, you only need to modify the TTY terminal name [e.g. load-generator01]. After entering, you can cycle again.

After the load is increased [it may take a while], the number of pods in PHP Apache reaches a maximum of 10. At this time, the pod information is:

 1 [[email protected] ~]# kubectl get hpa
 3 php-apache   Deployment/php-apache   465%/50%   1         10        10         10h
 4 [[email protected] ~]# 
 5 [[email protected] ~]# kubectl get deploy -A | grep 'php-apache'
 6 default                php-apache                  10/10   10           10          10h
 7 [[email protected] ~]# 
 8 [[email protected] ~]# kubectl get pod
 9 NAME                                READY   STATUS    RESTARTS   AGE
10 load-generator-57d5fb67c7-xx2wj     1/1     Running   0          48m
11 load-generator02-6c857bf9b5-cfmhj   1/1     Running   1          22s
12 php-apache-5db466758-2jv4x          1/1     Running   0          5m13s
13 php-apache-5db466758-4cxxm          1/1     Running   0          5m28s
14 php-apache-5db466758-6vz2b          1/1     Running   0          5m13s
15 php-apache-5db466758-98sqk          1/1     Running   0          4m58s
16 php-apache-5db466758-hdvrs          1/1     Running   0          5m13s
17 php-apache-5db466758-k2vxj          1/1     Running   0          62m
18 php-apache-5db466758-srctq          1/1     Running   0          4m58s
19 php-apache-5db466758-vkr5d          1/1     Running   0          5m28s
20 php-apache-5db466758-vmhlv          1/1     Running   0          5m13s
21 php-apache-5db466758-x8kms          1/1     Running   0          5m28s

At this time, if we stop the load access pressure test, the number of pods will not drop immediately. But it will slow down after a period of time.

This is also for the sake of safety to prevent too fast recovery of pods due to network reasons or sudden increase and drop of intermittent traffic, and the number of pods is not enough after the traffic comes up.

Related reading

1、Helm deployment, use and example of kubernetes k8s

2、Pod horizontal automatic expansion





If you think it’s good, pay attention to it (- ^ o ^ -)!


Recommended Today

Summary of import and export usage in JavaScript

import import 和 require 的区别 import 和js的发展历史息息相关,历史上 js没有模块(module)体系,无法将一个大程序拆分成互相依赖的小文件,再用简单的方法拼装起来。这对开发大型工程非常不方便。在 ES6 之前,社区制定了一些模块加载方案,最主要的有 CommonJS 和 AMD 两种。前者用于服务器,后者用于浏览器。ES6 在语言标准的层面上,实现了模块功能,而且实现得相当简单,完全可以取代 CommonJS 和 AMD 规范,成为浏览器和服务器通用的模块解决方案。也就是我们常见的 require 方法。 比如 `let { stat, exists, readFile } = require(‘fs’);` 。ES6 在语言标准的层面上,实现了模块功能。ES6 模块不是对象,而是通过export命令显式指定输出的代码,再通过import命令输入。 import 的几种用法: 1. import defaultName from ‘modules.js’; 2. import { export } from ‘modules’; 3. import { export as ex1 } from […]