hpa弹性扩缩容

弹性扩缩容是k8s中重要的功能之一,当服务负载大的时候,k8s会自动给给你扩容,当负载减低的时候,有自动给你缩容。

hpa通过metrics-server监控组件获取到服务负载监控数据,使用hpa需要先安装好metrics-server。

 

测试hpa弹性扩容容:

先部署deploy,svc;在部署hpa指定要控制的deploy信息;通过ab等压测工具来测试弹性扩缩容情况

#部署deploy
[root@k8s-master1 k8s]#kubectl apply -f  deploy.yaml 
[root@k8s-master1 k8s]# cat deploy.yaml 
apiVersion: apps/v1
kind: Deployment
metadata:
  creationTimestamp: null
  labels:
    app: kcxm-gc
  name: kcxm
  namespace: my-ns-kcxm
spec:
  replicas: 2
  selector:
    matchLabels:
      app: kcxm-gc
  strategy: {}
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: kcxm-gc
    spec:
      containers:
      - image: centos-nginx:1.23.1
        name: centos-nginx
        ports:
        - containerPort: 80
        resources: 
          limits: # 限制资源(上限) 
            cpu: "1" # CPU限制,单位是core数 
            memory: "100Mi" # 内存限制 
          requests: # 请求资源(下限) 
            cpu: "0.5" # CPU限制,单位是core数 
            memory: "10Mi" # 内存限制
status: {}

部署svc

也可以直接命令生成yaml文件,通过修改生成的yaml文件发布svc

[root@k8s-master1 k8s]# kubectl expose -n my-ns-kcxm deployment kcxm --port=80 --target-port=80 --type=NodePort --name=svc-kcmx --dry-run -o yaml >> svc-kcxm.yaml
W0908 16:35:40.609381   48091 helpers.go:553] --dry-run is deprecated and can be replaced with --dry-run=client. 

[root@k8s-master1 k8s]# cat svc-kcxm.yaml 
apiVersion: v1
kind: Service
metadata:
  creationTimestamp: null
  labels:
    app: kcxm-gc
  name: svc-kcmx
  namespace: my-ns-kcxm
spec:
  ports:
  - port: 80
    protocol: TCP
    targetPort: 80
  selector:
    app: kcxm-gc
  type: NodePort
status:
  loadBalancer: {}

创建hpa

通过命令生成yaml文件,添加namespace

 kubectl autoscale deployment -n my-ns-kcxm kcxm --min=1 --max=5 --cpu-percent=3 --name=hpa-kcxm  --dry-run -o yaml >>hpa-kcxm.yaml

[root@k8s-master1 k8s]# cat hpa-kcxm.yaml 
apiVersion: autoscaling/v1
kind: HorizontalPodAutoscaler
metadata:
  creationTimestamp: null
  name: hpa-kcxm
  namespace: my-ns-kcxm
spec:
  maxReplicas: 5   #最多扩展5个pod
  minReplicas: 1   #最少保留1个pod
  scaleTargetRef:   # 指定要控制的nginx信息
    apiVersion: apps/v1
    kind: Deployment
    name: kcxm
  targetCPUUtilizationPercentage: 3  # CPU使用率指标
status:
  currentReplicas: 0
  desiredReplicas: 0

查询目前创建的资源

# kubectl get all -n my-ns-kcxm

 

安装ab工具,进行压测

安装ab工具:yum install -y httpd

压测命令:ab -n 发送总并发量  -c  每次发送的量  http://压测网址

[root@k8s-master1 metrics-server]# ab -n 50000 -c 200 http://192.168.198.144:32114/index.html
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 192.168.198.144 (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.23.1
Server Hostname:        192.168.198.144
Server Port:            32114

Document Path:          /index.html
Document Length:        5 bytes

Concurrency Level:      200
Time taken for tests:   15.538 seconds
Complete requests:      50000
Failed requests:        0
Write errors:           0
Total transferred:      11700000 bytes
HTML transferred:       250000 bytes
Requests per second:    3217.93 [#/sec] (mean)
Time per request:       62.152 [ms] (mean)
Time per request:       0.311 [ms] (mean, across all concurrent requests)
Transfer rate:          735.35 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0   28  65.0     14    1164
Processing:     0   34  41.0     21     519
Waiting:        0   30  39.5     17     511
Total:          1   62  84.2     38    1270

Percentage of the requests served within a certain time (ms)
  50%     38
  66%     51
  75%     67
  80%     82
  90%    129
  95%    180
  98%    249
  99%    364
 100%   1270 (longest request)

 

在压测过程观察hpa,deploy,pod的变化

可以看到当hpa显示使用率超过3%时候,pod数量开始增加,使用率减低后,pod数量又开始自动减少。

#pod变化
[root@k8s-master1 ~]# kubectl get pod -n my-ns-kcxm -w
NAME                    READY   STATUS    RESTARTS   AGE
kcxm-79c67944ff-pg2bw   1/1     Running   0          14m
kcxm-79c67944ff-xkglh   1/1     Running   0          14m
kcxm-79c67944ff-5656l   0/1     Pending   0          0s
kcxm-79c67944ff-5656l   0/1     Pending   0          0s
kcxm-79c67944ff-zwwl5   0/1     Pending   0          0s
kcxm-79c67944ff-zwwl5   0/1     Pending   0          0s
kcxm-79c67944ff-zwwl5   0/1     ContainerCreating   0          0s
kcxm-79c67944ff-5656l   0/1     ContainerCreating   0          0s
kcxm-79c67944ff-zwwl5   1/1     Running             0          1s
kcxm-79c67944ff-5656l   1/1     Running             0          2s
kcxm-79c67944ff-jghjr   0/1     Pending             0          0s
kcxm-79c67944ff-jghjr   0/1     Pending             0          0s
kcxm-79c67944ff-jghjr   0/1     ContainerCreating   0          0s
kcxm-79c67944ff-jghjr   1/1     Running             0          2s
kcxm-79c67944ff-5656l   1/1     Terminating         0          5m35s
kcxm-79c67944ff-jghjr   1/1     Terminating         0          5m20s
kcxm-79c67944ff-zwwl5   1/1     Terminating         0          5m35s
kcxm-79c67944ff-xkglh   1/1     Terminating         0          20m
kcxm-79c67944ff-zwwl5   0/1     Terminating         0          5m36s
kcxm-79c67944ff-xkglh   0/1     Terminating         0          20m
kcxm-79c67944ff-jghjr   0/1     Terminating         0          5m23s
kcxm-79c67944ff-5656l   0/1     Terminating         0          5m38s
kcxm-79c67944ff-zwwl5   0/1     Terminating         0          5m44s
kcxm-79c67944ff-zwwl5   0/1     Terminating         0          5m44s
kcxm-79c67944ff-xkglh   0/1     Terminating         0          21m
kcxm-79c67944ff-xkglh   0/1     Terminating         0          21m
kcxm-79c67944ff-5656l   0/1     Terminating         0          5m50s
kcxm-79c67944ff-5656l   0/1     Terminating         0          5m50s
kcxm-79c67944ff-jghjr   0/1     Terminating         0          5m35s
kcxm-79c67944ff-jghjr   0/1     Terminating         0          5m35s

#deploy变化
[root@k8s-master1 ~]# kubectl get deploy -n my-ns-kcxm  -w
NAME   READY   UP-TO-DATE   AVAILABLE   AGE
kcxm   5/5     5            5           15m
kcxm   5/1     5            5           20m
kcxm   5/1     5            5           20m
kcxm   1/1     1            1           20m

#hpa变化
[root@k8s-master1 k8s]# kubectl get hpa -n my-ns-kcxm -w
NAME       REFERENCE         TARGETS   MINPODS   MAXPODS   REPLICAS   AGE
hpa-kcxm   Deployment/kcxm   0%/3%     1         5         2          54s
hpa-kcxm   Deployment/kcxm   13%/3%    1         5         2          2m19s
hpa-kcxm   Deployment/kcxm   53%/3%    1         5         4          2m34s
hpa-kcxm   Deployment/kcxm   0%/3%     1         5         5          2m49s
hpa-kcxm   Deployment/kcxm   0%/3%     1         5         5          7m38s
hpa-kcxm   Deployment/kcxm   0%/3%     1         5         5          7m54s
hpa-kcxm   Deployment/kcxm   0%/3%     1         5         1          8m9s

 

posted @   IT运维成长笔记  阅读(270)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
点击右上角即可分享
微信分享提示