天天看點

基于metric server的自動伸縮hpa

#(1)概念:

hpa功能: 能根據pod的cpu丶記憶體以及其它名額自動伸縮pod副本數量, 該名額由metrics-service和custom-metrics-apiserver提供
hpa版本:  通過kubectl api-versions檢視
autoscaling/v1
autoscaling/v2beta1   由metrics-service提供, 僅支援cpu名額來彈性伸縮
autoscaling/v2beta2   由custom-metrics-apiserver來提供, 支援記憶體網絡cpu等名額           
基于metric server的自動伸縮hpa

#(2)實驗目标: 建立一個pod,然後建立一個hpa監控pod的cpu使用率, 再對實施壓測, 檢視hpa是否根據pod的使用率伸縮pod副本

1)建立pod

kubectl run hpa-test --image=k8s.gcr.io/hpa-example --requests=cpu=200m --expose --port=80 -o yaml --dry-run
kubectl run hpa-test --image=k8s.gcr.io/hpa-example --requests=cpu=200m --expose --port=80            

2)建立hpa 超過cpu使用50% 最少建立一個pod, 最多建立10個pod

kubectl autoscale deployment hpa-test --cpu-percent=50 --min=1 --max=10 -o yaml --dry-run
kubectl autoscale deployment hpa-test --cpu-percent=50 --min=1 --max=10            
基于metric server的自動伸縮hpa

3)啟動一個程式進行壓力測試

kubectl run -i --tty load-generator --image=busybox /bin/sh
#while true; do wget -q -O- http://hpa-test.default.svc.cluster.local; done
![](https://s1.51cto.com/images/blog/201903/07/290af885aede8e6d6cbb647a6d30a25a.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=)           

4)過一段時間檢視 cpu超高, 自動擴充pod

基于metric server的自動伸縮hpa
基于metric server的自動伸縮hpa
apiVersion: autoscaling/v2beta1
kind: HorizontalPodAutoscaler
metadata:
    creationTimestamp: null
    name: hpa-test
spec:
    maxReplicas: 10
    minReplicas: 1
    scaleTargetRef:
        apiVersion: extensions/v1beta1
        kind: Deployment
        name: hpa-test
    metrics:
    - type: Resource
        resource:
            name: cpu
            targetAverageUtilization: 50

    - type: Resource
        resource:
            name: memory
            targetAverageValue: 50Mi           

繼續閱讀