$ k apply -f components.yaml serviceaccount/metrics-server created clusterrole.rbac.authorization.k8s.io/system:aggregated-metrics-reader created clusterrole.rbac.authorization.k8s.io/system:metrics-server created rolebinding.rbac.authorization.k8s.io/metrics-server-auth-reader created clusterrolebinding.rbac.authorization.k8s.io/metrics-server:system:auth-delegator created clusterrolebinding.rbac.authorization.k8s.io/system:metrics-server created service/metrics-server created deployment.apps/metrics-server created apiservice.apiregistration.k8s.io/v1beta1.metrics.k8s.io created
$ k apply -f nginx-hpa-deploy.yaml deployment.apps/nginx-hpa-deploy created
$ k get po -owide NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES nginx-hpa-deploy-866f5b85d-nzjxf 1/1 Running 0 29s 10.10.1.116 mac-worker <none> <none>
$ k apply -f nginx-hpa.yaml horizontalpodautoscaler.autoscaling/nginx-hpa created
$ k get hpa -owide NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE nginx-hpa Deployment/nginx-hpa-deploy 0%/5% 2 10 2 3m18s
$ k describe hpa nginx-hpa Warning: autoscaling/v2beta2 HorizontalPodAutoscaler is deprecated in v1.23+, unavailable in v1.26+; use autoscaling/v2 HorizontalPodAutoscaler Name: nginx-hpa Namespace: default Labels: <none> Annotations: <none> CreationTimestamp: Thu, 29 Jun 2022 14:59:41 +0000 Reference: Deployment/nginx-hpa-deploy Metrics: ( current / target ) resource cpu on pods (as a percentage of request): 0% (0) / 5% Min replicas: 2 Max replicas: 10 Deployment pods: 2 current / 2 desired Conditions: Type Status Reason Message ---- ------ ------ ------- AbleToScale True ScaleDownStabilized recent recommendations were higher than current one, applying the highest recent recommendation ScalingActive True ValidMetricFound the HPA was able to successfully calculate a replica count from cpu resource utilization (percentage of request) ScalingLimited False DesiredWithinRange the desired count is within the acceptable range Events: Type Reason Age From Message ---- ------ ---- ---- ------- Normal SuccessfulRescale 4m33s horizontal-pod-autoscaler New size: 2; reason: Current number of replicas below Spec.MinReplicas
$ k get deployments.apps nginx-hpa-deploy NAME READY UP-TO-DATE AVAILABLE AGE nginx-hpa-deploy 2/2 2 2 14m
$ k describe deployments.apps nginx-hpa-deploy Name: nginx-hpa-deploy Namespace: default CreationTimestamp: Thu, 29 Jun 2022 14:45:46 +0000 Labels: <none> Annotations: deployment.kubernetes.io/revision: 1 Selector: app=nginx-hpa-deploy Replicas: 2 desired | 2 updated | 2 total | 2 available | 0 unavailable StrategyType: RollingUpdate MinReadySeconds: 0 RollingUpdateStrategy: 25% max unavailable, 25% max surge Pod Template: Labels: app=nginx-hpa-deploy Containers: nginx: Image: nginx:alpine Port: 80/TCP Host Port: 0/TCP Limits: cpu: 100m memory: 20Mi Requests: cpu: 50m memory: 10Mi Environment: <none> Mounts: <none> Volumes: <none> Conditions: Type Status Reason ---- ------ ------ Progressing True NewReplicaSetAvailable Available True MinimumReplicasAvailable OldReplicaSets: <none> NewReplicaSet: nginx-hpa-deploy-866f5b85d (2/2 replicas created) Events: Type Reason Age From Message ---- ------ ---- ---- ------- Normal ScalingReplicaSet 15m deployment-controller Scaled up replica set nginx-hpa-deploy-866f5b85d to 1 Normal ScalingReplicaSet 67s deployment-controller Scaled up replica set nginx-hpa-deploy-866f5b85d to 2
$ k run ab -it --rm --image=httpd:alpine -- sh If you don't see a command prompt, try pressing enter. /usr/local/apache2 # /usr/local/apache2 # ab -V This is ApacheBench, Version 2.3 <$Revision: 1903618 $> Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/ Licensed to The Apache Software Foundation, http://www.apache.org/
/usr/local/apache2 # /usr/local/apache2 # ab -c 10 -t 60 -n 1000000 'http://nginx-hpa-svc/' This is ApacheBench, Version 2.3 <$Revision: 1903618 $> Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/ Licensed to The Apache Software Foundation, http://www.apache.org/
Server Software: nginx/1.25.1 Server Hostname: nginx-hpa-svc Server Port: 80
Document Path: / Document Length: 615 bytes
Concurrency Level: 10 Time taken for tests: 60.000 seconds Complete requests: 571159 Failed requests: 0 Total transferred: 484342832 bytes HTML transferred: 351262785 bytes Requests per second: 9519.29 [#/sec] (mean) Time per request: 1.050 [ms] (mean) Time per request: 0.105 [ms] (mean, across all concurrent requests) Transfer rate: 7883.16 [Kbytes/sec] received
Connection Times (ms) min mean[+/-sd] median max Connect: 0 0 0.5 0 85 Processing: 0 1 2.7 1 92 Waiting: 0 1 2.5 0 73 Total: 0 1 2.8 1 95
Percentage of the requests served within a certain time (ms) 50% 1 66% 1 75% 1 80% 1 90% 1 95% 1 98% 2 99% 4 100% 95 (longest request) /usr/local/apache2 # exit Session ended, resume using 'kubectl attach ab -c ab -i -t' command when the pod is running pod "ab" deleted
每 15 秒采集一次数据,HPA 发现目标的 CPU 使用率超过了阈值,会指数扩容(基为 2),直到上限
$ k describe hpa nginx-hpa Warning: autoscaling/v2beta2 HorizontalPodAutoscaler is deprecated in v1.23+, unavailable in v1.26+; use autoscaling/v2 HorizontalPodAutoscaler Name: nginx-hpa Namespace: default Labels: <none> Annotations: <none> CreationTimestamp: Thu, 29 Jun 2022 14:59:41 +0000 Reference: Deployment/nginx-hpa-deploy Metrics: ( current / target ) resource cpu on pods (as a percentage of request): 0% (0) / 5% Min replicas: 2 Max replicas: 10 Deployment pods: 2 current / 2 desired Conditions: Type Status Reason Message ---- ------ ------ ------- AbleToScale True ReadyForNewScale recommended size matches current size ScalingActive True ValidMetricFound the HPA was able to successfully calculate a replica count from cpu resource utilization (percentage of request) ScalingLimited True TooFewReplicas the desired replica count is less than the minimum replica count Events: Type Reason Age From Message ---- ------ ---- ---- ------- Normal SuccessfulRescale 23m horizontal-pod-autoscaler New size: 2; reason: Current number of replicas below Spec.MinReplicas Normal SuccessfulRescale 7m54s horizontal-pod-autoscaler New size: 4; reason: cpu resource utilization (percentage of request) above target Normal SuccessfulRescale 7m39s horizontal-pod-autoscaler New size: 8; reason: cpu resource utilization (percentage of request) above target Normal SuccessfulRescale 7m24s horizontal-pod-autoscaler New size: 10; reason: cpu resource utilization (percentage of request) above target Normal SuccessfulRescale 98s horizontal-pod-autoscaler New size: 2; reason: All metrics below target
$ k describe deployments.apps nginx-hpa-deploy Name: nginx-hpa-deploy Namespace: default CreationTimestamp: Thu, 29 Jun 2022 14:45:46 +0000 Labels: <none> Annotations: deployment.kubernetes.io/revision: 1 Selector: app=nginx-hpa-deploy Replicas: 2 desired | 2 updated | 2 total | 2 available | 0 unavailable StrategyType: RollingUpdate MinReadySeconds: 0 RollingUpdateStrategy: 25% max unavailable, 25% max surge Pod Template: Labels: app=nginx-hpa-deploy Containers: nginx: Image: nginx:alpine Port: 80/TCP Host Port: 0/TCP Limits: cpu: 100m memory: 20Mi Requests: cpu: 50m memory: 10Mi Environment: <none> Mounts: <none> Volumes: <none> Conditions: Type Status Reason ---- ------ ------ Progressing True NewReplicaSetAvailable Available True MinimumReplicasAvailable OldReplicaSets: <none> NewReplicaSet: nginx-hpa-deploy-866f5b85d (2/2 replicas created) Events: Type Reason Age From Message ---- ------ ---- ---- ------- Normal ScalingReplicaSet 37m deployment-controller Scaled up replica set nginx-hpa-deploy-866f5b85d to 1 Normal ScalingReplicaSet 23m deployment-controller Scaled up replica set nginx-hpa-deploy-866f5b85d to 2 Normal ScalingReplicaSet 7m11s deployment-controller Scaled up replica set nginx-hpa-deploy-866f5b85d to 4 Normal ScalingReplicaSet 6m56s deployment-controller Scaled up replica set nginx-hpa-deploy-866f5b85d to 8 Normal ScalingReplicaSet 6m41s deployment-controller Scaled up replica set nginx-hpa-deploy-866f5b85d to 10 Normal ScalingReplicaSet 55s deployment-controller Scaled down replica set nginx-hpa-deploy-866f5b85d to 2