Autoscaling
Horizontal Pod Autoscaling (HPA) 可以根据 CPU 使用率或应 用自定义 metrics 自动扩展 Pod 数量(支持 replication controller、deployment 和 replica set )。
- 控制管理器每隔 15s(可以通过
--horizontal-pod-autoscaler-sync-period
修改)查询 metrics 的资源使用情况 - 支持三种 metrics 类型
- 预定义 metrics(比如 Pod 的 CPU)以利用率的方式计算
- 自定义的 Pod metrics,以原始值(raw value)的方 式计算
- 自定义的 object metrics
- 支持两种 metrics 查询方式:Heapster 和自定义的 REST API
- 支持多 metrics
注意:
Kubernetes 版本 | autoscaling API 版本 | 支持的 metrics |
---|---|---|
v1.5+ | autoscaling/v1 | CPU |
v1.6+ | autoscaling/v2beta1 | Memory及自定义 |
# 创建 pod 和 service
$ kubectl run php-apache --image=k8s.gcr.io/hpa-example --requests=cpu=200m --expose --port=80
service "php-apache" created
deployment "php-apache" created
# 创建 autoscaler
$ kubectl autoscale deployment php-apache --cpu-percent=50 --min=1 --max=10
deployment "php-apache" autoscaled
$ kubectl get hpa
NAME REFERENCE TARGET MINPODS MAXPODS REPLICAS AGE
php-apache Deployment/php-apache/scale 0% / 50% 1 10 1 18s
# 增加负载
$ kubectl run -i --tty load-generator --image=busybox /bin/sh
Hit enter for command prompt
$ while true; do wget -q -O- http://php-apache.default.svc.cluster.local; done
# 过一会就可以看到负载升高了
$ kubectl get hpa
NAME REFERENCE TARGET CURRENT MINPODS MAXPODS REPLICAS AGE
php-apache Deployment/php-apache/scale 305% / 50% 305% 1 10 1 3m
# autoscaler 将这个 deployment 扩展为 7 个 pod
$ kubectl get deployment php-apache
NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE
php-apache 7 7 7 7 19m