Kubernetes Horizontal Pod Autoscaler

Kubernetes的自动弹性伸缩有两个维度:

  • 处理node缩放操作的Cluster Autoscaler
  • 自动弹性伸缩部署副本集Pod数量的Horizontal Pod Autoscaler(HPA)

Cluster Autoscaler 需要依赖云服务功能。
HPA在K8S版本1.8以下默认以heapster作为性能指标采集来源。在Kubernetes 1.8中,只有启用horizontal-pod-autoscaler-use-rest-clients时才需要Metrics Server。 Kubernetes 1.9默认启用HPA rest 客户端。 Metrics Server 为heapster替代项目,heapster在未来会慢慢废弃掉。
除heapster、Metrics Server采集CPU、内容等性能指标外,K8S还支持Prometheus 进行采集性能指标,代码地址:https://github.com/DirectXMan12/k8s-prometheus-adapter。

本文主要讲解HPA与heapster进行弹性伸缩,HPA主要根据采集的性能指标自动的控制RC/Deployment进行弹性伸缩。

image

HPA 测试

前期准备

  • Kubernetes集群
  • kubernetes 1.8以下需要安装heapster,1.8以上建议使用metrics server

构建 php-apache server

构建php-apache server 镜像,docker file脚本如下

FROM php:5-apache
ADD index.php /var/www/html/index.php
RUN chmod a+rx index.php

其中index.php代码如下,会频频占用CPU

<?php
  $x = 0.0001;
  for ($i = 0; $i <= 1000000; $i++) {
    $x += sqrt($x);
  }
  echo "OK!";
?>

执行构建镜像命令,项目代码可参考 https://github.com/xjune123/hpa-example.git

docker build -t registry.cn-hangzhou.aliyuncs.com/xjune/content-reveal/hpa-example .

启动php-apache server

kubectl run php-apache --image=registry.cn-hangzhou.aliyuncs.com/xjune/hpa:1.0 --requests=cpu=200m --expose --port=80

创建HPA进行弹性伸缩

以下命令表示当CPU平均负载大于50%后,将自动伸缩,最大伸缩Pod数为10个,最小1个。
方式一 直接执行kubectl命令

 kubectl autoscale deployment php-apache --cpu-percent=50 --min=1 --max=10

方式二 使用yaml方式

application/hpa/php-apache.yaml  
apiVersion: autoscaling/v1
kind: HorizontalPodAutoscaler
metadata:
  name: php-apache
  namespace: default
spec:
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: php-apache
  minReplicas: 1
  maxReplicas: 10
  targetCPUUtilizationPercentage: 50

创建后效果如下: 

增加CPU负载

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

查看服务负载

可以看到随着服务器压力增加,会自动增加服务器节点

停止增加CPU负载

使用ctrl+c 停止 busibox请求 查看服务节点,随着服务器压力越来越低,自动减少节点

指标说明

标准指标

目前heapster 所支持的指标有如下:

  • CPU
  • 内存
  • 并发数
  • 包传输大小 样例如下:
apiVersion: autoscaling/v2beta1
kind: HorizontalPodAutoscaler
metadata:
  name: hpa-name
  namespace: my-namespace
spec:
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment  # 基于Deployment进行扩缩
    name: deployment-name  # Deployment名
  minReplicas: 1   # 最大实例数
  maxReplicas: 10   # 最小实例数
  metrics:
  - type: Resource
    resource:
      name: cpu
      targetAverageUtilization: 50  # CPU阈值设定50%
  - type: Resource
    resource:
      name: memory
      targetAverageValue: 200Mi  # 内存设定200M
  - type: Pods
    pods:
      metricName: packets-per-second
      targetAverageValue: 1k   # 每秒数据量
  - type: Object
    object:
      metricName: requests-per-second
      target:
        apiVersion: extensions/v1beta1
        kind: Ingress
        name: main-route
      targetValue: 10k   # 每秒请求量

若需要指标生效,需要一定注明该pod的request cpu和memory,样例如下:

containers:
- image: nginx
  imagePullPolicy: Always
  name: default-mem-demo-ctr
  resources:
    requests:
      memory: 256Mi
      cpu: 2

计算逻辑 参考 https://github.com/kubernetes/community/blob/master/contributors/design-proposals/autoscaling/horizontal-pod-autoscaler.md#autoscaling-algorithm

TargetNumOfPods = ceil(sum(CurrentPodsCPUUtilization) / Target)

自定义指标

除标准指标外,kubernetes 还支持自定义客户化指标,可参考 https://kubernetes.io/docs/tasks/run-application/horizontal-pod-autoscale/#support-for-custom-metrics

弹性伸缩容错处理

由于有可能业务不稳定,突然有服务压力但过几分钟就结束了,kubernetes 增加了弹性增加节点和减少节点的延迟时间。

  • --horizontal-pod-autoscaler-downscale-delay: 弹性减少节点延迟时间,默认5分钟
  • --horizontal-pod-autoscaler-upscale-delay: 弹性增加节点延迟时间,默认3分钟

参考资料

https://www.sohu.com/a/231155920_268033
https://kubernetes.io/docs/tasks/run-application/horizontal-pod-autoscale/#support-for-cooldown-delay
https://kubernetes.io/docs/tasks/run-application/horizontal-pod-autoscale-walkthrough/
https://kubernetes.io/docs/tasks/run-application/horizontal-pod-autoscale/
https://jimmysong.io/kubernetes-handbook/concepts/horizontal-pod-autoscaling.html
https://blog.csdn.net/hello2mao/article/details/80418625

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值