image.png
    **
    HPA是kubernetes里面pod弹性伸缩的实现方法,它能根据设置的监控阀值进行pod的弹性缩的,HPA默认只能支持cpu的阀值检测扩缩容,但也可以通过custom metric api 调用prometheus实现自定义metric 来更加灵活的监控指标实现弹性伸缩。但hpa不能用于伸缩一些无法进行缩放的控制器如DaemonSet。


    部署Metric-Server

    1.1.部署带有资源请求的Deployment

    1. apiVersion: apps/v1
    2. kind: Deployment
    3. metadata:
    4. name: damon
    5. labels:
    6. app: nginx
    7. spec:
    8. replicas: 1
    9. selector:
    10. matchLabels:
    11. app: nginx-pod
    12. template:
    13. metadata:
    14. labels:
    15. app: nginx-pod
    16. spec:
    17. containers:
    18. - name: nginx
    19. image: nginx:1.9.0
    20. imagePullPolicy: IfNotPresent
    21. ports:
    22. - containerPort: 80
    23. resources:
    24. requests:
    25. cpu: "100m"


    kubectl expose deployment damon

    1.2 创建HPA
    kubectl autoscale deployment damon —max=10 —min=1 —cpu-percent=80

    1.3 增加cpu负载验证pod是否扩容
    watch -n1 ‘kubectl top pod && kubectl get hpa && kubectl get pod’
    # 安装压力测试工具WebBench
    yum -y install gcc
    git clone https://github.com/EZLippi/WebBench.git
    cd WebBench
    make ; make install
    # 压力测试,递增的扩容,并不是一次扩容到最大值
    webbench -c 1000 -t 60 http://10.43.77.199/
    watch -n1 ‘kubectl get hpa && kubectl top pod && kubectl get pod’