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 | 172.16.1.110 | 10.0.0.110 |
k8s-node01 | CentOS7.7 | 2C/4G/20G | 172.16.1.111 | 10.0.0.111 |
k8s-node02 | CentOS7.7 | 2C/4G/20G | 172.16.1.112 | 10.0.0.112 |
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: https://github.com/kubernetes-retired/heapster
As 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:
https://github.com/kubernetes-sigs/metrics-server
Metrics-server-amd64 image download
1 # in the cluster, all nodes need to execute
2 docker pull registry.cn-beijing.aliyuncs.com/google_registry/metrics-server-amd64:v0.3.6
3 docker tag registry.cn-beijing.aliyuncs.com/google_registry/metrics-server-amd64:v0.3.6 k8s.gcr.io/metrics-server-amd64:v0.3.6
4 docker rmi registry.cn-beijing.aliyuncs.com/google_registry/metrics-server-amd64:v0.3.6
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 registry.cn-beijing.aliyuncs.com/google_registry/hpa-example
Start pod
kubectl run php-apache --image=registry.cn-beijing.aliyuncs.com/google_registry/hpa-example --requests=cpu=200m --expose --port=80
Create HPA controller. See the following address for relevant algorithms:
1 # official address: https://v1-17.docs.kubernetes.io/zh/docs/tasks/run-application/horizontal-pod-autoscale/
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 10.244.4.176 k8s-node01
Increase load
1 # start a pod and enter a terminal
2 kubectl run -i --tty load-generator --image=registry.cn-beijing.aliyuncs.com/google_registry/myapp:v1 /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
2 NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE
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
complete!
———END———
If you think it’s good, pay attention to it (- ^ o ^ -)!