kubernetes Horizontal Pod Autoscaling
- 自動擴縮ReplicationController赠涮、Deployment啤它、ReplicaSet 和 StatefulSet 中的 Pod 數量
- 度量指標可以是:CPU指標 和 自定義
HPA 控制過程
-
實現自動擴縮容的原理官方文檔
-
--horizontal-pod-autoscaler-sync-period=15s
控制器的自動檢測周期驯用。 - metrics-server 提供 metrics.k8s.io API 為pod資源的使用提供支持鉴裹。
- 15s/周期 -> 查詢metrics.k8s.io API -> 算法計算 -> 調用scale 調度 -> 特定的擴縮容策略執(zhí)行喂很。
-
-
HPA擴縮容算法 期望副本數 = ceil[當前副本數 * (度量指標 / 期望指標)]
- 擴容
- 如果計算出的擴縮比例接近 1.0, 將會放棄本次擴縮, 度量指標 / 期望指標接近1.0同衣。
- 縮容
- 冷卻/延遲: 如果延遲(冷卻)時間設置的太短,那么副本數量有可能跟以前一樣出現抖動缸逃。 默認值是 5 分鐘(5m0s)
--horizontal-pod-autoscaler-downscale-stabilization
- 冷卻/延遲: 如果延遲(冷卻)時間設置的太短,那么副本數量有可能跟以前一樣出現抖動缸逃。 默認值是 5 分鐘(5m0s)
- 特殊處理
- 丟失度量值:縮小時假設這些 Pod 消耗了目標值的 100%, 在需要放大時假設這些 Pod 消耗了 0% 目標值厂抽。 這可以在一定程度上抑制擴縮的幅度需频。
- 存在未就緒的pod的時候:我們保守地假設尚未就緒的 Pod 消耗了期望指標的 0%,從而進一步降低了擴縮的幅度筷凤。
- 未就緒的 Pod 和缺少指標的 Pod 考慮進來再次計算使用率昭殉。 如果新的比率與擴縮方向相反,或者在容忍范圍內藐守,則跳過擴縮挪丢。 否則,我們使用新的擴縮比例卢厂。
- 指定了多個指標乾蓬, 那么會按照每個指標分別計算擴縮副本數,取最大值進行擴縮慎恒。
- 擴容
HPA 對象定義
apiVersion: autoscaling/v2beta2
kind: HorizontalPodAutoscaler
metadata:
name: nginx
spec:
behavior:
scaleDown:
policies:
- type: Pods
value: 4
periodSeconds: 60
- type: Percent
value: 10
periodSeconds: 60
stabilizationWindowSeconds: 300
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: nginx
minReplicas: 1
maxReplicas: 10
metrics:
- type: Resource
resource:
name: cpu
target:
type: Utilization
averageUtilization: 50
HPA對象默認行為
behavior:
scaleDown:
stabilizationWindowSeconds: 300
policies:
- type: Percent
value: 100
periodSeconds: 15
scaleUp:
stabilizationWindowSeconds: 0
policies:
- type: Percent
value: 100
periodSeconds: 15
- type: Pods
value: 4
periodSeconds: 15
selectPolicy: Max
metrics-server api github
為集群提供監(jiān)控性能參考是必須的任内。
kubectl apply -f https://github.com/kubernetes-sigs/metrics-server/releases/latest/download/components.yaml
遇到問題請參考 issue
一個nginx的演示實例
---
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: nginx
namespace: lidj
spec:
maxReplicas: 10
metrics:
- resource:
name: cpu
target:
averageUtilization: 40
type: Utilization
type: Resource
minReplicas: 1
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: nginx
---
apiVersion: v1
kind: Service
metadata:
name: nginx
namespace: lidj
spec:
type: NodePort
ports:
- name: "http"
port: 80
targetPort: 80
selector:
service: nginx
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx
namespace: lidj
spec:
replicas: 1
selector:
matchLabels:
service: nginx
template:
metadata:
labels:
service: nginx
spec:
containers:
- name: nginx
image: nginx:latest
resources:
requests:
cpu: 100m
memory: 100Mi
limits:
cpu: 200m
memory: 200Mi
使用壓測工具ab進行
ab -n 100000 -c 800 http://10.22.112.223:43434
查看現象
[root@shifeinode5 ~]# kubectl get pods,hpa -n lidj | grep nginx
pod/nginx-866f647cbc-97qp2 0/1 ContainerCreating 0 11s
pod/nginx-866f647cbc-hf65d 0/1 ContainerCreating 0 11s
pod/nginx-866f647cbc-hq6b7 1/1 Running 0 19h
pod/nginx-866f647cbc-kp6jq 1/1 Running 0 23h
horizontalpodautoscaler.autoscaling/nginx Deployment/nginx 97%/40% 2 20 2 23h
[root@shifeinode5 ~]# kubectl get pods,hpa -n lidj | grep nginx
pod/nginx-866f647cbc-5tlft 1/1 Running 0 22s
pod/nginx-866f647cbc-97qp2 1/1 Running 0 37s
pod/nginx-866f647cbc-bfc5f 0/1 ContainerCreating 0 22s
pod/nginx-866f647cbc-chl48 0/1 ContainerCreating 0 7s
pod/nginx-866f647cbc-dl4v8 1/1 Running 0 22s
pod/nginx-866f647cbc-hf65d 1/1 Running 0 37s
pod/nginx-866f647cbc-hq6b7 1/1 Running 0 19h
pod/nginx-866f647cbc-j5275 1/1 Running 0 22s
pod/nginx-866f647cbc-kp6jq 1/1 Running 0 23h
pod/nginx-866f647cbc-v5gpc 0/1 ContainerCreating 0 7s
horizontalpodautoscaler.autoscaling/nginx Deployment/nginx 91%/40% 2 20 8 23h