Horizontal Pod Autoscaling

网友投稿 258 2022-10-27

Horizontal Pod Autoscaling

Horizontal Pod AutoscalingHorizontal Pod Autoscaling 可以根据 CPU 利用率自动伸缩一个 Replication Controller、Deployment 或者Replica Set 中的 Pod 数量用***下载hpa镜像[root@localhost ~]# docker pull gcr.io/google_containers/hpa-example[root@localhost ~]# docker save -o hpa-example.tar gcr.io/google_containers/hpa-example将测试hpa镜像hpa-example.tar导入所有节点[root@k8s-master ~]# docker load -i hpa-example.tar[root@k8s-master ~]# kubectl run php-apache --image=gcr.io/google_containers/hpa-example --requests=cpu=200m --expose --port=80kubectl run --generator=deployment/apps.v1 is DEPRECATED and will be removed in a future version. Use kubectl run --generator=run-pod/v1 or kubectl create instead.service/php-apache createddeployment.apps/php-apache created

[root@k8s-master ~]# kubectl get podNAME READY STATUS RESTARTS AGEphp-apache-bdb7d567b-ggrh2 0/1 ImagePullBackOff 0 78s

[root@k8s-master ~]# kubectl describe pod php-apache-bdb7d567b-ggrh2Events:Type Reason Age From Message

Normal Scheduled 32s default-scheduler Successfully assigned default/php-apache-bdb7d567b-ggrh2 to k8s-node2Warning Failed 15s kubelet, k8s-node2 Failed to pull image "gcr.io/google_containers/hpa-example:latest": rpc error: code = Unknown desc = Error response from daemon: Get net/request canceled while waiting for connection (Client.Timeout exceeded while awaiting headers)Warning Failed 15s kubelet, k8s-node2 Error: ErrImagePullNormal BackOff 15s kubelet, k8s-node2 Back-off pulling image "gcr.io/google_containers/hpa-example:latest"Warning Failed 15s kubelet, k8s-node2 Error: ImagePullBackOffNormal Pulling 4s (x2 over 30s) kubelet, k8s-node2 Pulling image "gcr.io/google_containers/hpa-example:latest"

镜像没问题,但是这里拉取镜像时报错,是因为镜像gcr.io/google_containers/hpa-example:latest,当k8s拉取镜像的时候看到latest的时候,总是认为本地的镜像不是latest的,所以总是从网络上去拉取镜像,而绕过了本地的latest镜像导致错误。解决办法就是将镜像打tag。比如:在所有节点打tag v1docker tag gcr.io/google_containers/hpa-example:latest gcr.io/google_containers/hpa-example:v1在调用的时候指定这个tagkubectl run php-apache --image=gcr.io/google_containers/hpa-example:v1 --requests=cpu=200m --expose --port=80

当时没想到镜像的latest的问题。当时由于上面的方法不行,我用了下面的方法

[root@k8s-master ~]# kubectl create -f php-dev.yamlreplicationcontroller/php-apache created

[root@k8s-master ~]# kubectl create -f php-svc.yamlservice/php-apache created

[root@k8s-master ~]# kubectl get podNAME READY STATUS RESTARTS AGEphp-apache-668b66d495-fhh2h 1/1 Running 0 117s

[root@k8s-master ~]# kubectl top pod php-apache-668b66d495-fhh2h NAME CPU(cores) MEMORY(bytes) php-apache-668b66d495-fhh2h 0m 10Mi

创建 HPA 控制器 [root@k8s-master ~]# kubectl autoscale deployment php-apache --cpu-percent=50 --min=1 --max=10 horizontalpodautoscaler.autoscaling/php-apache autoscaledkubectl autoscale:水平自动伸缩deployment:指定deployment,并创建已经定义好资源的自动伸缩器php-apache:设置伸缩的Pod服务--cpu-percent=50 CPU的利用率赫兹超过50M 则创建新的 Pod 副本--min:一次最小创建1一个--max:最多创建到10个封顶

[root@k8s-master ~]# kubectl get hpaNAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGEphp-apache ReplicationController/php-apache 0%/50% 1 10 1 32s

[root@k8s-master ~]# kubectl describe hpa php-apacheEvents:Type Reason Age From Message

Warning FailedGetResourceMetric 32s horizontal-pod-autoscaler unable to get metrics for resource cpu: no metrics returned from resource metrics APIWarning FailedComputeMetricsReplicas 32s horizontal-pod-autoscaler Invalid metrics (1 invalid out of 1), last error was: failed to get cpu utilization: unable to get metrics for resource cpu: no metrics returned from resource metrics API以上报错暂时不用理会。

增加负载,查看负载节点数目[root@k8s-master ~]# kubectl run -i --tty load-generator --image=busybox /bin/sh/ # while true; do wget -q -O- doneOK!OK!OK!OK!OK!OK!OK!OK!OK!OK!OK!OK!OK!OK!OK!OK!OK!OK!OK!OK!OK!OK!OK!OK!OK!OK!OK!OK!OK!OK!OK!O

开个新窗口查看,可以查看到pod自动创建了[root@k8s-master ~]# kubectl get hpa -wNAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGEphp-apache Deployment/php-apache 44%/50% 1 10 1 6m13sphp-apache Deployment/php-apache 36%/50% 1 10 1 6m19sphp-apache Deployment/php-apache 405%/50% 1 10 1 6m34sphp-apache Deployment/php-apache 405%/50% 1 10 4 6m50sphp-apache Deployment/php-apache 348%/50% 1 10 4 7m5sphp-apache Deployment/php-apache 348%/50% 1 10 8 7m20sphp-apache Deployment/php-apache 97%/50% 1 10 8 7m35sphp-apache Deployment/php-apache 71%/50% 1 10 9 8m21s

可以查看到pod自动创建了(由于资源不够所以没有全部自动创建,有些是Pending,一共10个 )[root@k8s-master ~]# kubectl get podNAME READY STATUS RESTARTS AGEload-generator-7d549cd44-9z69l 1/1 Running 0 5m13sphp-apache-755b4877db-2sfdg 0/1 Pending 0 3m40sphp-apache-755b4877db-c6ddv 1/1 Running 0 12mphp-apache-755b4877db-gjc2g 1/1 Running 0 4m11sphp-apache-755b4877db-hn7c9 1/1 Running 0 3m40sphp-apache-755b4877db-mxgrc 1/1 Running 0 4m11sphp-apache-755b4877db-mxxw7 0/1 Pending 0 3m10sphp-apache-755b4877db-prxnt 1/1 Running 0 3m40sphp-apache-755b4877db-qtd69 1/1 Running 0 4m11sphp-apache-755b4877db-xkfdg 0/1 Pending 0 3m40sphp-apache-755b4877db-qtd29 /1 Running 0 4m11s

最后退出/ # exit

当时遇到问题的解决如下:[root@k8s-master ~]# kubectl describe hpa php-apacheEvents:Type Reason Age From Message

Warning FailedGetResourceMetric 19s (x7 over 4m51s) horizontal-pod-autoscaler unable to get metrics for resource cpu: no metrics returned from resource metrics APIWarning FailedComputeMetricsReplicas 19s (x7 over 4m51s) horizontal-pod-autoscaler Invalid metrics (1 invalid out of 1), last error was: failed to get cpu utilization: unable to get metrics for resource cpu: no metrics returned from resource metrics API

[root@k8s-master ~]# kubectl get pod --all-namespaces\NAMESPACE NAME READY STATUS RESTARTS AGEdefault php-apache-54b78b9f49-5jlp5 0/1 Pending 0 5m14sdefault php-apache-54b78b9f49-9rs62 1/1 Running 0 5m14s

[root@k8s-master ~]# kubectl describe pod php-apache-54b78b9f49-5jlp5Events:Type Reason Age From Message

Warning FailedScheduling 6s (x9 over 5m47s) default-scheduler 0/3 nodes are available: 1 node(s) had taints that the pod didn't tolerate, 2 Insufficient cpu.1 node(s) had taints that the pod didn't tolerate. 直译意思是节点有了污点无法容忍,执行 kubectl get no -o yaml | grep taint -A 5 之后发现该节点是不可调度的。这是因为kubernetes出于安全考虑默认情况下无法在master节点上部署pod,于是用下面方法解决:[root@k8s-master ~]# kubectl taint nodes --all node-role.kubernetes.io/master-

[root@k8s-master ~]# kubectl get podNAME READY STATUS RESTARTS AGEphp-apache-54b78b9f49-5jlp5 1/1 Running 0 10mphp-apache-54b78b9f49-9rs62 1/1 Running 0 10m

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:TYPE-C接口的工作原理图文详解
下一篇:springboot多个service互相调用的事务处理方式
相关文章

 发表评论

暂时没有评论,来抢沙发吧~