日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

kubernetes HPA使用及测试

發布時間:2024/3/12 编程问答 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 kubernetes HPA使用及测试 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

一、安裝metrics server

Metrics Server是Kubernetes內置的容器資源指標來源。
Metrics Server從node節點上的Kubelet收集資源指標,并通過Metrics API在 Kubernetes apiserver中公開指標數據,以供Horizontal Pod Autoscaler和Vertical Pod Autoscaler使用,也可以通過訪問kubectl top node/pod查看指標數據。
使用Metrics-Server監控node和pod計算資源使用情況,并提供給第三方使用
下載地址:
gituhub地址

修改資源定義文件

root@master:~/.kube# vim components.yaml

修改鏡像位置:

image: k8s.gcr.io/metrics-server/metrics-server:v0.6.1 修改為: registry.cn-hangzhou.aliyuncs.com/liangxiaohui/metrics-server:0.6.1

部署:

kubectl apply -f components.yaml serviceaccount/metrics-server created clusterrole.rbac.authorization.k8s.io/system:aggregated-metrics-reader created clusterrole.rbac.authorization.k8s.io/system:metrics-server created rolebinding.rbac.authorization.k8s.io/metrics-server-auth-reader created clusterrolebinding.rbac.authorization.k8s.io/metrics-server:system:auth-delegator created clusterrolebinding.rbac.authorization.k8s.io/system:metrics-server created service/metrics-server created deployment.apps/metrics-server created apiservice.apiregistration.k8s.io/v1beta1.metrics.k8s.io created

驗證:

root@deploy:~/yaml# kubectl get pods -n kube-system NAME READY STATUS RESTARTS AGE calico-kube-controllers-7ff47c8649-mnwjh 1/1 Running 31 (69m ago) 21d calico-node-8pqgz 1/1 Running 21 (69m ago) 21d calico-node-bqbj8 1/1 Running 21 (69m ago) 21d calico-node-zjrwp 1/1 Running 21 (69m ago) 21d coredns-778797657-zct55 1/1 Running 18 (69m ago) 21d coredns-778797657-zlgfg 1/1 Running 14 (69m ago) 21d metrics-server-597c6f68ff-68hpv 1/1 Running 0 63s root@deploy:~/yaml# kubectl top nodes NAME CPU(cores) CPU% MEMORY(bytes) MEMORY% 192.168.100.2 76m 3% 1073Mi 64% 192.168.100.5 141m 7% 1543Mi 20% 192.168.100.6 50m 2% 652Mi 8%

驗證:查看可以采集pods和nodes節點資源

root@deploy:~/yaml# kubectl top pod -n test NAME CPU(cores) MEMORY(bytes) dubbo-consumer-deployment-b55c7cdff-k8lm7 1m 3Mi dubbo-provider-deployment-5cc994c8b4-sd72z 1m 6Mi dubboadmin-deployment-d98765c7-mwt5v 71m 717Mi zookeeper1-654c4f44c-9cbqb 1m 80Mi zookeeper2-645fdbd686-rmwrl 2m 81Mi zookeeper3-b879d8d8f-prhmb 2m 74Mi

二、HPA自動伸縮

HorizontalPodAutoscaler(簡稱HPA)自動更新工作負載資源(例如Deployment或者StatefulSet),基于pod資源利用率橫向調整pod副本數量。目的是自動擴縮工作負載以滿足需求。

根據當前pod的負載,動態調整pod副本數量,業務高峰期自動擴容pod的副本數以盡快相應pod的請求。

水平擴縮意味著對增加的負載的響應是部署更多的Pods。這與“垂直(Vertical)”擴縮不同,對于Kubernetes,垂直擴縮意味著將更多資源(例如:內存或CPU)分配給已經為工作負載運行的Pod。

如果負載減少,并且Pod的數量高于配置的最小值, HorizontalPodAutoscaler會指示工作負載資源(Deployment、StatefulSet或其他類似資源)縮減。

相關的其他動態伸縮控制器類型

垂直pod自動縮放器(VPA):基于pod資源利用率,調整對單個pod的最大資源限制,不能與HPA同時使用
集群伸縮(Cluster Autoscaler,CA):基于集群中node資源使用情況,動態伸縮node節點,從而保證有CPU和內存資源用于創建pod。

HPA控制器簡介:

Horizontal Pod Autoscaling (HPA)控制器,根據預定義好的閾值及pod當前的資源利用率,自動控制在k8s集群中運行的pod數量(自動彈性水平自動伸縮).

HPA重要參數:

使用kube-controller-manager --help|grep <options>,進行查詢 ##默認每隔15s,可以通過此選項修改查詢metrics的資源使用 --horizontal-pod-autoscaler-sync-period#縮容間隔周期,默認5分鐘。 --horizontal-pod-autoscaler-downscale-stabilization#HPA控制器同步pod副本數的間隔周期 --horizontal-pod-autoscaler-sync-period#初始化延遲時間,在此時間內pod的CPU資源指標將不會生效,默認為5分鐘。 --horizontal-pod-autoscaler-cpu-initialization-period#用于設置pod準備時間,在此時間內的pod統統被認為未就緒及不采集數據,默認為30秒。 --horizontal-pod-autoscaler-initial-readiness-delay#HPA控制器能容忍的數據差異(浮點數,默認為0.1),即新的指標要與當前的閾值差異在0.1或以上,即要大于1+0.1=1.1,或小于1-0.1=0.9,比如閾值為CPU利用率50%,當前為80%,那么80/50=1.6 > 1.1則會觸發擴容,反之會縮容。 --horizontal-pod-autoscaler-tolerance

即觸發條件:avg(CurrentPodsConsumption) / Target >1.1 或 <0.9=把N個pod的數據相加后根據pod的數量計算出平均數除以閾值,大于1.1就擴容,小于0.9就縮容。
計算公式:TargetNumOfPods = ceil(sum(CurrentPodsCPUUtilization) / Target) #ceil是一個向上取整的目的pod整數。

指標數據需要部署metrics-server,即HPA使用metrics-server作為數據源。 https://github.com/kubernetes-sigs/metrics-server

在k8s 1.1引入HPA控制器,早期使用Heapster組件采集pod指標數據,在k8s 1.11版本開始使用Metrices Server完成數據采集,然后將采集到的數據通過API(Aggregated API,匯總API),例如metrics.k8s.io、custom.metrics.k8s.io、external.metrics.k8s.io,然后再把數據提供給HPA控制器進行查詢,以實現基于某個資源利用率對pod進行擴縮容的目的。

根據cpu利用率伸縮

創建deployment

apiVersion: apps/v1 kind: Deployment metadata:labels:app: nginxname: nginx spec:replicas: 2selector:matchLabels:app: nginxtemplate:metadata:labels:app: nginxspec:containers:- image: nginxname: nginximagePullPolicy: IfNotPresent

創建hpa

[root@master10 hpa]# kubectl autoscale deployment nginx --max=5 --min=2 --cpu-percent=80 --dry-run=client -o yaml > hpa.yaml[root@master10 hpa]# vim hpa.yaml apiVersion: autoscaling/v1 kind: HorizontalPodAutoscaler metadata:creationTimestamp: nullname: nginx spec:maxReplicas: 5minReplicas: 2scaleTargetRef:apiVersion: apps/v1kind: Deploymentname: nginxtargetCPUUtilizationPercentage: 80 status:currentReplicas: 0desiredReplicas: 0

要想看到HPA的TARGETS值必須滿足2個條件:

  • 安裝metric server
  • 為pod設定資源限制。
  • 配置deployment 限制cpu和內存
    添加resource資源限制

    kubectl edit deployments.apps nginxspec:containers:- image: nginximagePullPolicy: IfNotPresentname: nginxresources:limits:memory: "200m"cpu: "100m"

    修改后,自動創建兩個新的pod來替換舊的pod資源限制
    暴露deployment端口,創建svc

    kubectl expose deployment nginx --port=80 --target-port=80


    apt-install httpd-tools 安裝ab工具進行壓力測試

    [root@master10 hpa]# ab -n 300000 -c 100 http://10.102.118.93/


    容器cpu達到80%則hpa自動水平伸縮,擴容pods,直到擴容到cpu負載均勻到各個pods中,或者hpa擴容到最大pods數量。

    總結

    以上是生活随笔為你收集整理的kubernetes HPA使用及测试的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。