Kubernetes HPA (Horizontal Pod Autoscaler)
HPA功能概述
Pod水平自动扩缩,可以基于CPU/Memroy的使用率或者当前使用值进行自动扩缩容(kubernetes 1.6+可支持对内存扩缩),当然也基于其它自定义metrcis name进行扩缩容(kubernetes 1.6+)支持kubernetes的副本集包括deployment replicaSet ReplicationController
Pod 水平自动扩缩特性由 Kubernetes API 资源和控制器实现。资源决定了控制器的行为。 控制器会周期性的调整副本控制器或 Deployment 中的副本数量,以使得 Pod 的平均 CPU 利用率与用户所设定的目标值匹配,由控制器管理器的 --horizontal-pod-autoscaler-sync-period 参数指定周期(默认值为 15 秒)
对于按 Pod 统计的资源指标(如 CPU),控制器从资源指标 API 中获取每一个 HorizontalPodAutoscaler 指定的 Pod 的度量值,如果设置了目标使用率, 控制器获取每个 Pod 中的容器资源使用情况,并计算资源使用率。 如果设置了 target 值,将直接使用原始数据(不再计算百分比)。 接下来,控制器根据平均的资源使用率或原始值计算出扩缩的比例,进而计算出目标副本数
如果pod 使用opject指标和external指标(每个指标描述一个对象信息)。 这个指标将直接跟据目标设定值相比较,并生成一个上面提到的扩缩比例。 在 autoscaling/v2beta2 版本API中,这个指标也可以根据 Pod 数量平分后再计算
HPA计算方式
期望副本数 = ceil[当前副本数 * (当前指标 / 期望指标)]
如果 HorizontalPodAutoscaler 指定的是targetAverageValue 或 targetAverageUtilization, 那么将会把指定 Pod 度量值的平均值做为 currentMetricValue。 然而,在检查容忍度和决定最终扩缩值前,仍然会把那些无法获取指标的 Pod 统计进去
当使用CPU指标进行HPA时,任何还未就绪(例如还在初始化)状态的 Pod 或 最近的指标 度量值采集于就绪状态前的 Pod,该 Pod 也会被搁置。
-
--horizontal-pod-autoscaler-initial-readiness-delay参数(默认为 30s)用于设置 Pod 准备时间, 在此时间内的 Pod 统统被认为未就绪 -
--horizontal-pod-autoscaler-cpu-initialization-period参数(默认为5分钟) 用于设置 Pod 的初始化时间, 在此时间内的 Pod,CPU 资源度量值将不会被采纳 -
--horizontal-pod-autoscaler-downscale-stabilization:kube-controller-manager的这个参数表示缩容冷却时间。 即自从上次缩容执行结束后,多久可以再次执行缩容,默认时间是 5 分钟(5m0s),防止抖动
测试案例
-
创建一个测试deployment副本集,如下
kubectl apply -f php-apache.yaml
apiVersion: apps/v1 kind: Deployment metadata: name: php-apache spec: selector: matchLabels: run: php-apache replicas: 1 template: metadata: labels: run: php-apache spec: containers: - name: php-apache image: k8s.gcr.io/hpa-example ports: - containerPort: 80 resources: limits: cpu: 500m memory: 200M requests: cpu: 200m memory: 100M --- apiVersion: v1 kind: Service metadata: name: php-apache labels: run: php-apache spec: ports: - port: 80 selector: run: php-apache
-
创建一个HPA策略,如下
kubectl apply -f hpa-v2.yaml
piVersion: autoscaling/v2beta2 kind: HorizontalPodAutoscaler metadata: name: php-apache namespace: default spec: minReplicas: 1 maxReplicas: 10 scaleTargetRef: apiVersion: apps/v1 kind: Deployment name: php-apache metrics: - type: Resource resource: name: cpu target: averageUtilization: 50 type: Utilization - type: Resource resource: name: memory target: averageUtilization: 80 type: Utilization
-
memroy压力测试,如下
<root@HK-K8S-CP ~># kubectl exec -it php-apache-69475dc47c-9snfm -- bash # 安装stress root@php-apache-69475dc47c-9snfm:/var/www/html# apt-get update Ign http://httpredir.debian.org jessie InRelease Get:1 http://security.debian.org jessie/updates InRelease [44.9 kB] Get:2 http://httpredir.debian.org jessie-updates InRelease [16.3 kB] Get:3 http://httpredir.debian.org jessie Release.gpg [1652 B] Get:4 http://httpredir.debian.org jessie Release [77.3 kB] Get:5 http://security.debian.org jessie/updates/main amd64 Packages [992 kB] Get:6 http://httpredir.debian.org jessie-updates/main amd64 Packages [20 B] Get:7 http://httpredir.debian.org jessie/main amd64 Packages [9098 kB] Fetched 10.2 MB in 37s (274 kB/s) Reading package lists... Done W: There is no public key available for the following key IDs: AA8E81B4331F7F50 root@php-apache-69475dc47c-9snfm:/var/www/html# apt-get install stress Reading package lists... Done Building dependency tree Reading state information... Done The following NEW packages will be installed: stress 0 upgraded, 1 newly installed, 0 to remove and 102 not upgraded. Need to get 18.5 kB of archives. After this operation, 44.0 kB of additional disk space will be used. Get:1 http://httpredir.debian.org/debian/ jessie/main stress amd64 1.0.1-1+deb8u1 [18.5 kB] Fetched 18.5 kB in 0s (0 B/s) debconf: delaying package configuration, since apt-utils is not installed Selecting previously unselected package stress. (Reading database ... 12878 files and directories currently installed.) Preparing to unpack .../stress_1.0.1-1+deb8u1_amd64.deb ... Unpacking stress (1.0.1-1+deb8u1) ... Setting up stress (1.0.1-1+deb8u1) ... # stress 压力测试,参数解释,新建3个IO进程,3个写进程,每次写入分配50M的内存,分配后不释放,测试100秒 root@php-apache-69475dc47c-9snfm:/var/www/html# stress -i 3 --vm 3 --vm-bytes 50M --vm-hang 100 --timeout 100s stress: info: [99] dispatching hogs: 0 cpu, 3 io, 3 vm, 0 hdd stress: info: [99] successful run completed in 100s #观察HPA策略,如下 <root@HK-K8S-CP ~># kubectl get hpa NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE php-apache Deployment/php-apache 168%/80% 1 10 3 105m <root@HK-K8S-CP ~># kubectl get hpa NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE php-apache Deployment/php-apache 168%/80% 1 10 3 105m
-
CPU压力测试,如下
# 增加4个cpu进程,处理sqrt()函数函数,以提高系统CPU负荷 root@php-apache-69475dc47c-9snfm:/var/www/html# stress -c 4 stress: info: [107] dispatching hogs: 4 cpu, 0 io, 0 vm, 0 hdd <root@HK-K8S-CP ~># kubectl get hpa NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE php-apache Deployment/php-apache 9%/80%, 1%/50% 1 10 1 4h46m <root@HK-K8S-CP ~># kubectl get hpa NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE php-apache Deployment/php-apache 10%/80%, 194%/50% 1 10 4 4h47m

浙公网安备 33010602011771号