[root@k8s-master ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
php-apache-bdb7d567b-ggrh2 0/1 ImagePullBackOff 0 78s
[root@k8s-master ~]# kubectl describe pod php-apache-bdb7d567b-ggrh2
Events:
Type Reason Age From Message
Normal Scheduled 32s default-scheduler Successfully assigned default/php-apache-bdb7d567b-ggrh2 to k8s-node2
Warning 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 https://gcr.io/v2/: net/http: request canceled while waiting for connection (Client.Timeout exceeded while awaiting headers)
Warning Failed 15s kubelet, k8s-node2 Error: ErrImagePull
Normal BackOff 15s kubelet, k8s-node2 Back-off pulling image "gcr.io/google_containers/hpa-example:latest"
Warning Failed 15s kubelet, k8s-node2 Error: ImagePullBackOff
Normal Pulling 4s (x2 over 30s) kubelet, k8s-node2 Pulling image "gcr.io/google_containers/hpa-example:latest"
镜像没问题,但是这里报错,也不知道为什么。
由于上面的方法不行,我用了下面的方法
创建php-apache实例
[root@k8s-master ~]# vi php-dev.yaml
[root@k8s-master ~]# kubectl create -f php-dev.yaml
replicationcontroller/php-apache created
创建php-apache实例的SVC
[root@k8s-master ~]# vi php-svc.yaml
[root@k8s-master ~]# kubectl create -f php-svc.yaml
service/php-apache created
[root@k8s-master ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
php-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 autoscaled
kubectl autoscale:水平自动伸缩
deployment:指定deployment,并创建已经定义好资源的自动伸缩器
php-apache:设置伸缩的Pod服务
--cpu-percent=50 CPU的利用率赫兹超过50M 则创建新的 Pod 副本
--min:一次最小创建1一个
--max:最多创建到10个封顶
[root@k8s-master ~]# kubectl get hpa
NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE
php-apache ReplicationController/php-apache 0%/50% 1 10 1 32s
[root@k8s-master ~]# kubectl describe hpa php-apache
Events:
Type Reason Age From Message
Warning FailedGetResourceMetric 32s horizontal-pod-autoscaler unable to get metrics for resource cpu: no metrics returned from resource metrics API
Warning 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- http://php-apache.default.svc.cluster.local; done
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!OK!O
开个新窗口查看,可以查看到pod自动创建了
[root@k8s-master ~]# kubectl get hpa -w
NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE
php-apache Deployment/php-apache 44%/50% 1 10 1 6m13s
php-apache Deployment/php-apache 36%/50% 1 10 1 6m19s
php-apache Deployment/php-apache 405%/50% 1 10 1 6m34s
php-apache Deployment/php-apache 405%/50% 1 10 4 6m50s
php-apache Deployment/php-apache 348%/50% 1 10 4 7m5s
php-apache Deployment/php-apache 348%/50% 1 10 8 7m20s
php-apache Deployment/php-apache 97%/50% 1 10 8 7m35s
php-apache Deployment/php-apache 71%/50% 1 10 9 8m21s
可以查看到pod自动创建了(由于资源不够所以没有全部自动创建,有些是Pending,一共10个 )
[root@k8s-master ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
load-generator-7d549cd44-9z69l 1/1 Running 0 5m13s
php-apache-755b4877db-2sfdg 0/1 Pending 0 3m40s
php-apache-755b4877db-c6ddv 1/1 Running 0 12m
php-apache-755b4877db-gjc2g 1/1 Running 0 4m11s
php-apache-755b4877db-hn7c9 1/1 Running 0 3m40s
php-apache-755b4877db-mxgrc 1/1 Running 0 4m11s
php-apache-755b4877db-mxxw7 0/1 Pending 0 3m10s
php-apache-755b4877db-prxnt 1/1 Running 0 3m40s
php-apache-755b4877db-qtd69 1/1 Running 0 4m11s
php-apache-755b4877db-xkfdg 0/1 Pending 0 3m40s
php-apache-755b4877db-qtd29 /1 Running 0 4m11s
最后退出
/ # exit
当时遇到问题的解决如下:
[root@k8s-master ~]# kubectl describe hpa php-apache
Events:
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 API
Warning 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 AGE
default php-apache-54b78b9f49-5jlp5 0/1 Pending 0 5m14s
default php-apache-54b78b9f49-9rs62 1/1 Running 0 5m14s
[root@k8s-master ~]# kubectl describe pod php-apache-54b78b9f49-5jlp5
Events:
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 pod
NAME READY STATUS RESTARTS AGE
php-apache-54b78b9f49-5jlp5 1/1 Running 0 10m
php-apache-54b78b9f49-9rs62 1/1 Running 0 10m
原文:https://blog.51cto.com/2290153/2559779