K8S水平自动扩、缩容实例

前提:K8S已经安装metric-server插件

  • 创建测试yaml
cat nginx-rc.yaml
apiVersion: v1
kind: ReplicationController
metadata:
  name: myweb2
spec:
  replicas: 2
  selector:
    app: myweb2
  template:
    metadata:
      labels:
        app: myweb2
    spec:
      containers:
      - name: myweb2
        image: nginx
        ports:
        - containerPort: 8012
        resources:
          limits:
            cpu: 100m
            memory: 50Mi
          requests:
            cpu: 100m
            memory: 50Mi
  • 执行yaml
kubectl create -f nginx-rc.yaml

replicationcontroller “myweb2” created

查看生成的pod

kubectl get pods -o wide --all-namespaces

yaml文件默认设置2
在这里插入图片描述
查看有多少副本

kubectl get rc -o wide --all-namespaces

在这里插入图片描述
创建自动扩容,设置最大8,最小1

kubectl autoscale replicationcontroller myweb2 --max=8 --min=1 --cpu-percent=10

等几分钟,pod由2变成1

kubectl get pods -o wide | grep myweb2

在这里插入图片描述

  • 压力测试
ab -n 50000 -c 100 http://10.36.0.3/
This is ApacheBench, Version 2.3 <$Revision: 1430300 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking 10.36.0.3 (be patient)
Completed 5000 requests
Completed 10000 requests
Completed 15000 requests
Completed 20000 requests
Completed 25000 requests
Completed 30000 requests
Completed 35000 requests
Completed 40000 requests
Completed 45000 requests
Completed 50000 requests
Finished 50000 requests


Server Software:        nginx/1.17.10
Server Hostname:        10.36.0.3
Server Port:            80

Document Path:          /
Document Length:        612 bytes

Concurrency Level:      100
Time taken for tests:   228.075 seconds
Complete requests:      50000
Failed requests:        0
Write errors:           0
Total transferred:      42300000 bytes
HTML transferred:       30600000 bytes
Requests per second:    219.23 [#/sec] (mean)
Time per request:       456.149 [ms] (mean)
Time per request:       4.561 [ms] (mean, across all concurrent requests)
Transfer rate:          181.12 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    1   2.5      1      93
Processing:    13  455 104.2    492    1000
Waiting:        1  452 104.1    491     995
Total:         20  456 103.8    493    1000

Percentage of the requests served within a certain time (ms)
  50%    493
  66%    499
  75%    501
  80%    502
  90%    594
  95%    601
  98%    696
  99%    703
 100%   1000 (longest request)

查看容器状态

开始压力测试…
pod逐渐创建
在这里插入图片描述
压力测试中…
创建最多8个pod
在这里插入图片描述
查看状态、日志

kubectl describe hpa myweb2

在这里插入图片描述
压力测试结束
pod自动缩容至1

在这里插入图片描述
最后查看pod
在这里插入图片描述
K8S水平自动扩、缩容测试完成。

Logo

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

更多推荐