1.手動擴容機制
示例:對busybox-deployment手動擴縮容
apiVersion:apps/v1 kind: Deployment metadata: name: busybox-deployment spec: replicas: 3 template: metadata: labels: app: busybox spec: containers: - name: busybox image: busybox:latest # 此Pod已運行副本數量3個,使用kubectl scale命令擴容至5個 kubectl scale deploy busybox-deployment --replicas 5 注:如將replicas值設置小於當前副本數則系統會殺掉一些運行中的Pod已實現縮容
2.自動擴容機制(HPA)
Horizontal Pod Autoscaler(HPA)的控制器,用於實現基於CPU使用率進行自動Pod擴容的功能,HPA控制器基於Master的kube-controllere-manager服務啟動參數--horizontal-pod-autoscaler-sync-period定義的探測周期(默認值為15s),周期性監測目標Pod的資源性能指標,並與HPA資源對象中的擴容條件進行對比,在滿足條件時對Pod副本數量進行調整。
使用HPA功能需要在controller-manager啟動文件中加入的參數:
- --horizontal-pod-autoscaler-tolerance=0.1,設置擴縮容忍度,默認值0.1(10%),表示基於算法得到的結果在0.9-1.1(-10%-10%),控制器都不會進行擴縮容
- --horizontal-pod-autoscaler-initial-readiness-delay=30s,設置首次探測Pod是否Ready的延時時間,默認值30min
- --horizontal-pod-autoscaler-cpuinitialization-period=10s,設置首次采集Pod的CPU使用率的延遲時間
- --horizontal-pod-autoscaler-downscale-stabilization=1m0s,這個配置可讓系統更平滑的進行縮容操作,默認值5min
使用HorizontalPodAutoscaler配置自定義擴縮容的規則:
apiVersion: autoscaling/v2beta2 kind: HorizontalPodAutoscaler metadata: name: nginx spec: scaleTargetRef: # 定義目標對象,可以是deploy/RC/RS apiVersion: apps/v1 kind: Deployment name: nginx minReplicas: 1 # pod副本數量的最下值 maxReplicas: 10 # pod副本數量的最大值 metrics: # 目標指標值,系統在指標數據達到目標值時出發擴縮容操作 - type: Resource # 定義目標值 resources: name: cpu target: type: Utilization averageUtilization: 50
注:目標值類型可包括三項
- Resource:基於資源的指標,可設置CPU和內存,對於CPU使用率可在target參數中設置averageUtilization定義目標平均CPU使用率;對於內存使用率可在target參數中設置AverageValue定義目標平均內存使用率
- Pods:基於pod的指標,系統對全部Pod副本的指標進行計算平均值,數據來源於Pod對象本身,其target類型只能使用AverageValue
- Object:基於某種資源對象(如Ingress)的指標或應用系統的任意自定義指標,數據來源於其它資源對象或任意自定義指標,其target類型可以使用Value和AverageValue(根據Pod副本數計算平均值)進行設置
示例一:
apiVersion: autoscaling/v2beta2 kind: HorizontalPodAutoscaler metadata: name: nginx spec: scaleTargetRef: apiVersion: apps/v1 kind: Deployment name: nginx minReplicas: 1 maxReplicas: 10 metrics: - type: Object object: metrics: name: requests-per-second # 指標名稱 describedObject: apiVersion: extensions/v1beta1 kind: Ingress # 來源於ingress main-route name: main-route target: type: Value value: 2k # 目標值為2000,即在ingress的每秒請求數達到2000時觸發擴縮容操作
示例二:
apiVersion: autoscaling/v2beta2 kind: HorizontalPodAutoscaler metadata: name: nginx spec: scaleTargetRef: apiVersion: apps/v1 kind: Deployment name: nginx minReplicas: 1 maxReplicas: 10 metrics: - type: Object object: metrics: name: 'http_requests' # 指標名稱 selector: 'verb=GET' # 資源對象具有的標簽 target: type: AverageValue averageValue: 500 # 平均值到500觸發擴縮容操作
示例三:系統針對每種類型的指標都計算Pod副本的目標數量,以最大值為准進行擴縮容操作
apiVersion: autoscaling/v2beta2 kind: HorizontalPodAutoscaler metadata: name: nginx spec: scaleTargetRef: apiVersion: apps/v1 kind: Deployment name: nginx minReplicas: 1 maxReplicas: 10 metrics: - type: Resource resources: name: cpu targetAverageUtilization: 50 - type: Pods pods: metrics: name: packets-per-second targetAverageUtilization: 1k - type: Object object: metrics: name: requests-per-second describedObject: apiVersion: extensions/v1beta1 kind: Ingress name: main-route target: kind: Value value: 1k
示例四:使用外部服務的性能指標對自己部署的K8s中的服務進行HPA
....... metrics: - type: External external: metrics: name: queue-message-ready selector: 'queue=worker_tasks' targetAverageUtilization: 30
基於外部服務的性能指標實現HPA需要預先部署自定義Metries Server,目前可以基於Prometheus、MS Azure、Datadog Cluster和Google Stackdriver等系統的Adapter實現
使用外部性能指標,在K8s master的API Server啟動Aggregation層,需要在apierver啟動文件中加入的參數:
- --requestheader-client-ca-file,指定客戶端的CA證書
- --requestheader-allowed-names,允許訪問的客戶端common name列表,將其設置為空置時,表示任意客戶端都可以訪問
- --requestheader-extra-headers-prefix=X-Remote-Extra,請求頭中需要檢查的前綴名
- --requestheader-group-headers=X-Remote-Group,請求頭中需要檢查的組名
- --requestheader-username-headers=X-Remote-User,請求頭中需要檢查的用戶名
- --proxy-client-cert-file,在請求期間驗證Aggregator的客戶端CA證書
- --proxy-client-key-file,在請求期間驗證Aggreagator的客戶端私鑰
使用外部性能指標,在K8s master的API Server啟動Aggregation層,需要在controller-manager啟動文件中加入的參數:
--horizontal-pod-autoscaler-sync-period=10s,HPA控制器同步Pod副本數量的時間間隔,默認值15s
示例五、使用Prometheus作為外部性能指標收集器
# 部署Prometheus Operator apiVersion: apps/v1 kind: Deployment metadata: labels: k8s-app: prometheus-operator name: prometheus-operator spec: replicas: 1 selector: matchLabels: k8s-app: prometheus-operator template: metadata: labels: k8s-app: prometheus-operator spec: containers: - image: quay.io/coreos/prometheus-operator:v0.40.0 imagePullPolicy: IfNotPresent name: prometheus-operator ports: - containerPort: 8080 name: http resources: limits: cpu: 200M memory: 100Mi requests: cpu: 100m memory: 50Mi 這個prometheus-operatord會自動創建名為monitoring.coreos.com的CRD資源 # 通過Operator的配置部署Prometheus apiVersion: monitoring.coreos.com/v1 kind: Prometheus metadata: name: prometheus labels: app: promethus prometheus: prometheus spec: replicas: 2 baseImage: quay.io/prometheus/prometheus version: v2.10.0 serviceMonitorSelector: matchLabels: service-monitor: function resources: requests: memory: 300Mi --- apiVersion: v1 kind: Service metadata: name: prometheus labels: app: prometheus prometheus: prometheus spec: selector: prometheus: prometheus ports: - name: http port: 9090 # 確認prometheus operator和prometheus服務正常 kubectl get pods
d