ab压力测试模拟实现kubernetes Pod水平自动伸缩

Horizontal Pod Autoscaler 可以根据CPU利用率自动伸缩 replication controllerdeployment 和 replica set 中的Pod数量(除了 CPU 利用率)也可以 基于其他应程序提供的度量指标custom metrics。pod 自动缩放不适用于无法缩放的对象,比如 DaemonSets

Pod 水平自动伸缩特性由 Kubernetes API 资源和控制器实现。资源决定了控制器的行为。控制器会周期性的获取平均 CPU 利用率,并与目标值相比较后来调整 replication controller 或 deployment 中的副本数量。

通过实际的Demo来

体验一下HPA基于资源实现的水平自动伸缩,为了演示 Horizontal Pod Autoscaler,我们将使用tomcat镜像作为测试对象,以下为配置tomcat的deployment的配置清单:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: dp-tomcat
  namespace: learning
spec:
  revisionHistoryLimit: 15
  replicas: 1
  selector:
    matchLabels:
      app: dp-tomcat
    matchExpressions:
      - key: app
        operator: In
        values: [dp-tomcat]
  template:
    metadata:
      labels:
        app: dp-tomcat
    spec:
      nodeSelector:
        kubernetes.io/hostname: dev-k8s-05.xsl.link
      containers:
      - name: dp-tomcat
        image: tomcat:9.0
        resources:
          limits:
            memory: "1024Mi"
            cpu: "500m"
        ports:
        - containerPort: 8080
          name: web

上面的yaml说明一下:

  • 运行了一个副本数为3的deployment
  • 暴露出一个名为nginxsvc的service
  • 指定了revisionHistoryLimit表示保留历史版本的个数
  • 因为要通过资源负载模拟HPA功能的场景,必须依赖template中对pod做资源限制
  • 集群环境需要安装metrics-server,方便观察集群资源负载
➜  00-template.resources.yaml git:(master) ✗ (☸ kubernetes-admin@kubernetes:default) k get pods -n learning        
NAME                               READY   STATUS    RESTARTS   AGE
abcontainer-58c67cfb87-g4j8q       1/1     Running   0          25d
dp-tomcat-5b4465b6bf-82llc         1/1     Running   0          125m
dp-tomcat-5b4465b6bf-zsttg         1/1     Running   0          126m
nginx-deployment-8558b4659-6dkmr   1/1     Running   0          17s
nginx-deployment-8558b4659-v8fzf   1/1     Running   0          17s
nginx-deployment-8558b4659-w5tlq   1/1     Running   0          8m56s

现在,dp-tomcat服务已经运行,我们将通过 定义一个HPA资源对象来创建 Horizontal Pod Autoscaler。以下配置清单将创建一个Horizontal Pod Autoscaler用于控制我们上一步骤中创建的deployment,使 Pod 的副本数量在维持在1到10之间。大致来说,HPA 将通过增加或者减少 Pod 副本的数量(通过 Deployment )以保持所有 Pod 的平均CPU利用率在50%以内

定义一个HPA资源对象

apiVersion: autoscaling/v1
kind: HorizontalPodAutoscaler
metadata:
  name: dp-tomcat
  namespace: learning
spec:
  minReplicas: 1
  maxReplicas: 10
  scaleTargetRef:
    kind: Deployment
    name: dp-tomcat
    apiVersion: apps/v1
  targetCPUUtilizationPercentage: 10

 接下来,我们通过部署一个含有ab命令的httpd镜像来模拟负载

apiVersion: apps/v1
kind: Deployment
metadata:
  name: abcontainer
  namespace: abcontainer
spec:
  selector:
    matchLabels:
      app: abcontainer
  template:
    metadata:
      labels:
        app: abcontainer
    spec:
      containers:
      - name: abcontainer
        image: httpd
        ports:
        - containerPort: 80

在部署完成httpd之后,进入到abcontainer通过ab增加负载

k exec abcontainer-58c67cfb87-g4j8q -n learning -it -- bash

 使用ab命令进行压力测试

ab -c 5000 -n 2000000 http://tomcat-svc:8080/

查看HPA资源变化

NAME        REFERENCE              TARGETS   MINPODS   MAXPODS   REPLICAS   AGE
dp-tomcat   Deployment/dp-tomcat   0%/10%    1         10        1          16m
NAME        REFERENCE              TARGETS   MINPODS   MAXPODS   REPLICAS   AGE
dp-tomcat   Deployment/dp-tomcat   98%/10%   1         10        1          18m
# 中断ab测试
NAME        REFERENCE              TARGETS   MINPODS   MAXPODS   REPLICAS   AGE
dp-tomcat   Deployment/dp-tomcat   36%/10%   1         10        10         19m
# pods的变化
NAME                           READY   STATUS              RESTARTS   AGE
abcontainer-58c67cfb87-g4j8q   1/1     Running             0          25d
dp-tomcat-96b7b9f6f-54v84      0/1     Pending             0          13s
dp-tomcat-96b7b9f6f-55hsf      0/1     ContainerCreating   0          13s
dp-tomcat-96b7b9f6f-btbzr      0/1     ContainerCreating   0          13s
dp-tomcat-96b7b9f6f-dpppj      0/1     ContainerCreating   0          13s
dp-tomcat-96b7b9f6f-k4jqq      1/1     Running             0          12m
dp-tomcat-96b7b9f6f-mxd4f      1/1     Running             0          28s
dp-tomcat-96b7b9f6f-tjpj6      1/1     Running             0          28s
dp-tomcat-96b7b9f6f-zztqm      1/1     Running             0          28s

 默认情况下,当ab模拟完成之后,当流量开始下降,5分钟后pod的数量会慢慢恢复到replicas的值。

 https://mp.weixin.qq.com/s/ykyVZU7Bw1Qg3xfLFaTr5Q

 

posted @ 2020-07-27 17:19  凡人半睁眼  阅读(385)  评论(0编辑  收藏  举报