如果看过测试十六到这里我们已经具备了去实验HPA的条件了,没看过的可以去看一下!

简介:

Horizontal Pod Autoscaling 可以根据 CPU 利用率自动伸缩一个 Replication Controller、Deployment 或者Replica Set 中的 Pod 数量。

运行一个比较耗费资源的pod:

kubectl run php-apache --image=gcr.io/google_containers/hpa-example --requests=cpu=200m --port=80 --image-pull-policy=IfNotPresent

 kubectl top pod 查看一下监控是否已经拿到数据:

 创建 HPA 控制器 最小1个 最大 10个 :

kubectl autoscale deployment php-apache --cpu-percent=50 --min=1 --max=10

帮助文档:

  --cpu-percent=-1: The target average CPU utilization (represented as a percent of requested CPU) over all the
pods. If it's not specified or negative, a default autoscaling policy will be used.

增加负载:

kubectl run -i --tty load-generator --image=busybox /bin/sh

创建无头服务:

apiVersion: v1
kind: Service
metadata:
  name: hpasvc
spec:
  ports:
  - port: 80
    name: web
  clusterIP: None
  selector:
    run: php-apache

循环调用 php页面将cpu指标提升到需要hpa介入增加pod数量:

while true; do wget -q -O- http://hpasvc.default.svc.cluster.local; done

开始创建pod:

 10个pod

当监控发现cpu资源不在紧张时会在等待一会才回收资源:

pod动态:

NAME                              READY   STATUS    RESTARTS   AGE
load-generator-5fb4fb465b-kjxl7   1/1     Running   0          31s
php-apache-69fcbf5fbc-5n82p       1/1     Running   0          15m
php-apache-69fcbf5fbc-lv7sh       0/1     Pending   0          0s
php-apache-69fcbf5fbc-8zk7w       0/1     Pending   0          0s
php-apache-69fcbf5fbc-wxmhd       0/1     Pending   0          0s
php-apache-69fcbf5fbc-lv7sh       0/1     Pending   0          0s
php-apache-69fcbf5fbc-8zk7w       0/1     Pending   0          0s
php-apache-69fcbf5fbc-wxmhd       0/1     Pending   0          0s
php-apache-69fcbf5fbc-lv7sh       0/1     ContainerCreating   0          0s
php-apache-69fcbf5fbc-8zk7w       0/1     ContainerCreating   0          0s
php-apache-69fcbf5fbc-wxmhd       0/1     ContainerCreating   0          1s
php-apache-69fcbf5fbc-wxmhd       1/1     Running             0          2s
php-apache-69fcbf5fbc-lv7sh       1/1     Running             0          2s
php-apache-69fcbf5fbc-8zk7w       1/1     Running             0          2s
php-apache-69fcbf5fbc-wb8wd       0/1     Pending             0          0s
php-apache-69fcbf5fbc-wb8wd       0/1     Pending             0          0s
php-apache-69fcbf5fbc-jtg7r       0/1     Pending             0          0s
php-apache-69fcbf5fbc-xfbbh       0/1     Pending             0          0s
php-apache-69fcbf5fbc-2tfcv       0/1     Pending             0          0s
php-apache-69fcbf5fbc-jtg7r       0/1     Pending             0          0s
php-apache-69fcbf5fbc-xfbbh       0/1     Pending             0          0s
php-apache-69fcbf5fbc-2tfcv       0/1     Pending             0          0s
php-apache-69fcbf5fbc-wb8wd       0/1     ContainerCreating   0          0s
php-apache-69fcbf5fbc-jtg7r       0/1     ContainerCreating   0          0s
php-apache-69fcbf5fbc-xfbbh       0/1     ContainerCreating   0          0s
php-apache-69fcbf5fbc-2tfcv       0/1     ContainerCreating   0          0s
php-apache-69fcbf5fbc-2tfcv       1/1     Running             0          1s
php-apache-69fcbf5fbc-xfbbh       1/1     Running             0          2s
php-apache-69fcbf5fbc-wb8wd       1/1     Running             0          2s
php-apache-69fcbf5fbc-jtg7r       1/1     Running             0          2s
php-apache-69fcbf5fbc-ngf2w       0/1     Pending             0          0s
php-apache-69fcbf5fbc-ngf2w       0/1     Pending             0          0s
php-apache-69fcbf5fbc-v8bhl       0/1     Pending             0          0s
php-apache-69fcbf5fbc-v8bhl       0/1     Pending             0          0s
php-apache-69fcbf5fbc-ngf2w       0/1     ContainerCreating   0          0s
php-apache-69fcbf5fbc-v8bhl       0/1     ContainerCreating   0          0s
php-apache-69fcbf5fbc-ngf2w       1/1     Running             0          2s
php-apache-69fcbf5fbc-v8bhl       1/1     Running             0          2s
load-generator-5fb4fb465b-kjxl7   0/1     Error               0          25m
load-generator-5fb4fb465b-kjxl7   1/1     Running             1          25m
load-generator-5fb4fb465b-kjxl7   1/1     Terminating         1          28m
php-apache-69fcbf5fbc-wxmhd       1/1     Terminating         0          6m20s
php-apache-69fcbf5fbc-v8bhl       1/1     Terminating         0          5m49s
php-apache-69fcbf5fbc-lv7sh       1/1     Terminating         0          6m20s
php-apache-69fcbf5fbc-ngf2w       1/1     Terminating         0          5m49s
php-apache-69fcbf5fbc-8zk7w       1/1     Terminating         0          6m20s
php-apache-69fcbf5fbc-2tfcv       1/1     Terminating         0          6m4s
php-apache-69fcbf5fbc-xfbbh       1/1     Terminating         0          6m4s
php-apache-69fcbf5fbc-jtg7r       1/1     Terminating         0          6m4s
php-apache-69fcbf5fbc-wb8wd       1/1     Terminating         0          6m4s
php-apache-69fcbf5fbc-8zk7w       0/1     Terminating         0          6m22s
php-apache-69fcbf5fbc-wb8wd       0/1     Terminating         0          6m6s
php-apache-69fcbf5fbc-ngf2w       0/1     Terminating         0          5m51s
php-apache-69fcbf5fbc-xfbbh       0/1     Terminating         0          6m6s
php-apache-69fcbf5fbc-v8bhl       0/1     Terminating         0          5m51s
php-apache-69fcbf5fbc-lv7sh       0/1     Terminating         0          6m22s
php-apache-69fcbf5fbc-jtg7r       0/1     Terminating         0          6m6s
php-apache-69fcbf5fbc-wxmhd       0/1     Terminating         0          6m22s
php-apache-69fcbf5fbc-wxmhd       0/1     Terminating         0          6m22s
php-apache-69fcbf5fbc-2tfcv       0/1     Terminating         0          6m7s
php-apache-69fcbf5fbc-ngf2w       0/1     Terminating         0          5m52s
php-apache-69fcbf5fbc-ngf2w       0/1     Terminating         0          5m52s
php-apache-69fcbf5fbc-v8bhl       0/1     Terminating         0          5m53s
php-apache-69fcbf5fbc-v8bhl       0/1     Terminating         0          5m53s
php-apache-69fcbf5fbc-jtg7r       0/1     Terminating         0          6m9s
php-apache-69fcbf5fbc-jtg7r       0/1     Terminating         0          6m9s
php-apache-69fcbf5fbc-wxmhd       0/1     Terminating         0          6m26s
php-apache-69fcbf5fbc-wxmhd       0/1     Terminating         0          6m26s
php-apache-69fcbf5fbc-2tfcv       0/1     Terminating         0          6m10s
php-apache-69fcbf5fbc-2tfcv       0/1     Terminating         0          6m10s
php-apache-69fcbf5fbc-lv7sh       0/1     Terminating         0          6m27s
php-apache-69fcbf5fbc-lv7sh       0/1     Terminating         0          6m27s
php-apache-69fcbf5fbc-8zk7w       0/1     Terminating         0          6m29s
php-apache-69fcbf5fbc-8zk7w       0/1     Terminating         0          6m29s
php-apache-69fcbf5fbc-xfbbh       0/1     Terminating         0          6m13s
php-apache-69fcbf5fbc-xfbbh       0/1     Terminating         0          6m13s
php-apache-69fcbf5fbc-wb8wd       0/1     Terminating         0          6m13s
php-apache-69fcbf5fbc-wb8wd       0/1     Terminating         0          6m13s
load-generator-5fb4fb465b-kjxl7   0/1     Terminating         1          29m
load-generator-5fb4fb465b-kjxl7   0/1     Terminating         1          29m
load-generator-5fb4fb465b-kjxl7   0/1     Terminating         1          29m

 监控下的数据:

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐