(十一)基于metric server的自动伸缩hpa

概念

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等指标

实验目标: 创建一个pod,然后创建一个hpa监控pod的cpu使用率, 再对实施压测, 查看hpa是否根据pod的使用率伸缩pod副本

创建pod

kubectl run hpa-test --image=nginx:1.11 --requests=cpu=200m --expose --port=80 -o yaml --dry-run
kubectl run hpa-test --image=nginx:1.11   --requests=cpu=200m --expose --port=80 

创建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 

在这里插入图片描述
启动一个程序进行压力测试

kubectl run -i --rm --tty load-generator --image=busybox /bin/sh
#while true; do wget -q -O- http://hpa-test.default.svc.cluster.local; done

过一段时间查看 cpu超高, 自动扩展pod
在这里插入图片描述
在这里插入图片描述
停止压力测试,过一段时间自动减少 貌似我等了很久
在这里插入图片描述

扩展:同时针对cpu和内存设置, api版本: autoscaling/v2beta2

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

猜你喜欢

转载自blog.csdn.net/weixin_43342753/article/details/89791207
今日推荐