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进行弹性伸缩。
构建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以下命令表示当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创建后效果如下:
查看服务负载
可以看到随着服务器压力增加,会自动增加服务器节点
使用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_268033https://kubernetes.io/docs/tasks/run-application/horizontal-pod-autoscale/#support-for-cooldown-delayhttps://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.htmlhttps://blog.csdn.net/hello2mao/article/details/80418625