k8s pod 自動水平擴展


1. 當前環境

kubernetes        v1.17
metrics-server    v0.3.6

要實現hpa,metrics-server 需要部署到集群中, 它可以通過 resource metrics API 對外提供度量數據,Horizontal Pod Autoscaler 正是根據此 API 來獲取度量數據。

2. 部署metrics-server

k8s部署參考網站 https://github.com/kubernetes-sigs/metrics-server/tree/master/deploy/kubernetes

  • metrics-server不能使用,報錯不能解析node節點的主機名
- --kubelet-preferred-address-types=InternalIP,Hostname,InternalDNS,ExternalDNS,ExternalIP
  • metrics-server報錯,x509,證書是非信任的
- --kubelet-insecure-tls

加在 args 的參數里面,args 片段如下

args:
    - --cert-dir=/tmp
    - --secure-port=4443
    - --kubelet-preferred-address-types=InternalIP,Hostname,InternalDNS,ExternalDNS,ExternalIP
    - --kubelet-insecure-tls

3. 部署一個應用與hpa

hpa 全拼 horizontal pod autoscaler,可以實現pod根據條件實現水平擴展,比如cpu、內存、訪問量等。
測試一個根據cpu的水平擴展,cpu限制的片段,限制cpu為0.02,100mCPU就是100 miliCPU,等價於0.1CPU

resources:
    limits:
        cpu: "20m"
    requests:
        cpu: "20m"

部署一個hpa

apiVersion: autoscaling/v2beta2
kind: HorizontalPodAutoscaler
metadata:
  name: spring-boot-demo-deployment
  namespace: default
spec:
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: spring-boot-demo-deployment
  minReplicas: 1
  maxReplicas: 2
  metrics:
  - type: Resource
    resource:
      name: cpu
      target:
        type: Utilization
        averageUtilization: 15
status:
  conditions: null
  observedGeneration: 1
  currentReplicas: 1
  desiredReplicas: 1
  currentMetrics:
  - type: Resource
    resource:
      name: cpu
      current:
        averageUtilization: 0
        averageValue: 0

當cpu利用率超過15%,pod會水平擴展成2個,當cpu降下來后pod又會收縮成1個。
當pod每秒超過服務1000個數據包請求時,觸發擴展,樣本如下。

- type: Pods
   pods:
      metric:
        name: packets-per-second
      targetAverageValue: 1k

參考網站 https://kubernetes.io/zh/docs/tasks/run-application/horizontal-pod-autoscale-walkthrough/


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM