一、概述
? ? ? ? Metrics-Server組件目的:獲取集群中pod、節(jié)點等負載信息;
? ? ? ? hpa資源目的:通過metrics-server獲取的pod負載信息,自動伸縮創(chuàng)建pod;
參考鏈接:
資源指標管道 | Kubernetes
https://github.com/kubernetes/kubernetes/tree/master/cluster/addons/metrics-server
GitHub - kubernetes-sigs/metrics-server: Scalable and efficient source of container resource metrics for Kubernetes built-in autoscaling pipelines.
二、安裝部署Metrics-Server組件
? ? ? ? 就是給k8s集群安裝top命令的意思;
1,下載Metrics-Server資源清單
· 第一種方式:github下載
[root@k8s231 metricsserver]# wget https://github.com/kubernetes-sigs/metrics-server/releases/latest/download/high-availability-1.21+.yaml
· 第二種方式:本地上傳安裝包
百度云盤;
鏈接:https://pan.baidu.com/s/1axn44_AsbHQxIMw9nuNVMw?pwd=jtqb?
提取碼:jtqb
2,編輯Metrics-Server的資源清單
[root@k8s231 metricsserver]# vim high-availability-1.21+.yaml
? ? spec:
? ? ? affinity:
? ? ? ? podAntiAffinity:
? ? ? ? ? requiredDuringSchedulingIgnoredDuringExecution:
? ? ? ? ? - labelSelector:
? ? ? ? ? ? ? matchLabels:
? ? ? ? ? ? ? ? k8s-app: metrics-server
? ? ? ? ? ? namespaces:
? ? ? ? ? ? - kube-system
? ? ? ? ? ? topologyKey: kubernetes.io/hostname
? ? ? containers:
? ? ? - args:
? ? ? ? #啟動允許使用不安全的證書
? ? ? ? - --kubelet-insecure-tls
? ? ? ? - --cert-dir=/tmp
? ? ? ? - --secure-port=10250
? ? ? ? - --kubelet-preferred-address-types=InternalIP,ExternalIP,Hostname
? ? ? ? - --kubelet-use-node-status-port
? ? ? ? - --metric-resolution=15s
? ? ? ? #image: registry.k8s.io/metrics-server/metrics-server:v0.7.0
? ? ? ? image: registry.aliyuncs.com/google_containers/metrics-server:v0.6.3
3,創(chuàng)建Metrics-Server資源
[root@k8s231 metricsserver]# kubectl apply -f high-availability-1.21+.yaml
4,驗證Metrics-Server是否成功安裝
· 查看pod
[root@k8s231 metricsserver]# kubectl get pods -A?
· 使用top命令測試是否管用
查節(jié)點的top值
[root@k8s231 metricsserver]# kubectl top node
查看pod的top值
[root@k8s231 metricsserver]# kubectl top ?pods -A
三、hpa資源實現(xiàn)pod水平伸縮(自動擴縮容)
? ? ? ? 1,當資源使用超一定的范圍,會自動擴容,但是擴容數(shù)量不會超過最大pod數(shù)量;
? ? ? ? 2,擴容時無延遲,只要監(jiān)控資源使用超過闊值,則會直接創(chuàng)建pod;
? ? ? ? 3,當資源使用率恢復(fù)到闊值以下時,需要等待一段時間才會釋放,大概時5分鐘;
1,編輯deployment資源
[root@k8s231 hpa]# cat deploy.yaml?
apiVersion: apps/v1
kind: Deployment
metadata:
? name: dm-hpa
spec:
? replicas: 1
? selector:
? ? matchLabels:
? ? ? k8s: xinjizhiwa
? template:
? ? metadata:
? ? ? labels:
? ? ? ? k8s: xinjizhiwa
? ? spec:
? ? ? containers:
? ? ? - name: c1
? ? ? ? image: centos:7
? ? ? ? command:
? ? ? ? - tail
? ? ? ? - -f
? ? ? ? - /etc/hosts
? ? ? ? resources:
? ? ? ? ? requests:
? ? ? ? ? ? cpu: "50m"
? ? ? ? ? limits:
? ? ? ? ? ? cpu: "150m"
2,編寫hpa資源清單
[root@k8s231 hpa]# cat hpa.yaml?
apiVersion: autoscaling/v1
kind: HorizontalPodAutoscaler
metadata:
? name: hpa-tools
spec:
? #指定pod最大的數(shù)量是多少(自動擴容的上限)
? maxReplicas: 10
? #指定pod最小的pod數(shù)量是多少(自動縮容的下限)
? minReplicas: 2
? #彈性伸縮引用的目標是誰?
? scaleTargetRef:
? ? #目標資源的api
? ? apiVersion: "apps/v1"
? ? #目標資源的類型kind
? ? kind: Deployment
? ? #目標資源的名稱metadata-name是什么
? ? name: dm-hpa
? #使用cpu閾值(使用到達多少,開始擴容、縮容)
? #95%
? targetCPUUtilizationPercentage: 95
3,創(chuàng)建hpa和deploy資源
[root@k8s231 hpa]# kubectl apply -f? .
4,查看hpa資源
[root@k8s231 hpa]# kubectl get hpa -o wide
到這里,就已經(jīng)實現(xiàn)了自動擴縮容的pod副本了;
至此,咱們的metrics-server組件和hpa資源,就學習完畢了;
四、壓測測試
1,進入pod,安裝stress工具
· 進入pod容器
[root@k8s231 hpa]# kubectl exec dm-hpa-5bb4dd448d-ks2rt -it -- sh
· 安裝aili源和epel源
sh-4.2#?yum -y install wget
sh-4.2#?wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
sh-4.2#?wget -O /etc/yum.repos.d/epel.repo https://mirrors.aliyun.com/repo/epel-7.repo
· 安裝壓測工具
sh-4.2#? yum -y install stress
2,開始使用命令壓測pod
sh-4.2#?stress --cpu 8 --io 4 --vm 2 --vm-bytes 128M --timeout 20m
3,查看hpa資源的負載情況
[root@k8s231 ~]# kubectl get hpa -o wide
可以看到:
1,我們創(chuàng)建的deploy資源只有一個副本;
2,我們創(chuàng)建的hpa資源之后,設(shè)置最小值是2,最大值是10 ;
3,我們在查看pod,可以看見,pod變成了2個;
4,我們進入容器,開始壓測,將負載壓測到超過95%;
5,再次查看pod,發(fā)現(xiàn)變成了3個,自動創(chuàng)建了一個;
6,關(guān)閉壓測,5分鐘后,pod有回歸到了2個;文章來源:http://www.zghlxwxcb.cn/news/detail-831535.html
7,至此,hpa的pod自動伸縮,測試完畢;文章來源地址http://www.zghlxwxcb.cn/news/detail-831535.html
到了這里,關(guān)于24-k8s的附件組件-Metrics-server組件與hpa資源pod水平伸縮的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!