# distributed-load-testing-using-k8s **Repository Path**: lldhsds/distributed-load-testing-using-k8s ## Basic Information - **Project Name**: distributed-load-testing-using-k8s - **Description**: 使用K8S集群执行分布式负载测试 - **Primary Language**: Python - **License**: Apache-2.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2024-06-25 - **Last Updated**: 2024-06-26 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # 使用K8S集群执行分布式负载测试 本教程介绍如何使用Kubernetes部署分布式负载测试框架,该框架使用分布式部署的locust 产生压测流量,对一个部署到 K8S集群的 Web 应用执行负载测试,该 Web 应用公开了 REST 格式的端点,以响应传入的 HTTP POST 请求。 关于分布式负载测试的更多资料请查看: 1. [Distributed Load Testing Using Kubernetes](https://cloud.google.com/solutions/distributed-load-testing-using-kubernetes) 2. [GoogleCloudPlatform/distributed-load-testing-using-kubernetes: Distributed load testing using Kubernetes on Google Container Engine (github.com)](https://github.com/GoogleCloudPlatform/distributed-load-testing-using-kubernetes) 本文借鉴了官方的框架,在官方基础上做了简化修改,支持在本地搭建的K8S集群上进行分布式负载测试。 ## 1. 测试使用的工作负载实例 下图展示了将客户端请求传送到应用的示例工作负载。 ![dl-client-app](./res/dl-client-app.svg) 为对该交互进行建模,您可以使用 [Locust](https://locust.io/) 这一基于 Python、可跨多个目标路径分发请求的分布式负载测试工具。例如,Locust 可以将请求分发到 `/login` 和 `/metrics` 目标路径。工作负载在 Locust 中建模为一组[任务](https://docs.locust.io/en/latest/writing-a-locustfile.html)。 ## 2. locust分布式负载测试架构 该架构涉及到两个主要组件: - Locust 容器映像。 - 容器编排和管理机制。 Locust 容器映像包含 Locust 软件,包含用于启动 Locust 服务和执行任务的脚本。为尽可能贴近真实客户端的情况,每个 Locust 任务都进行了加权。例如,每一千个客户端总请求发生一次注册。 Kubernetes提供容器编排和管理功能。使用 Kubernetes,您可以指定为负载测试框架奠定基础的容器节点的数量。此外,您还可以将负载测试工作器组织到 pod 中,并指定希望Kubernetes 持续运行的 pod 数量。 为了部署负载测试任务,请执行以下操作: 1. 部署负载测试主节点。 2. 部署一组负载测试工作器。您要使用这些负载测试工作器创建大量的流量,以便执行测试。 下图展示了使用示例应用进行负载测试的架构。主 Pod 提供用于操作和监控负载测试的网页界面。工作器 Pod 为接受测试的应用生成 REST 请求流量,并将指标发送到主 Pod。 ![dl-architecture](./res/dl-architecture.svg) ### 2.1 关于负载测试主节点 Locust 主节点是执行负载测试任务的入口点。Locust 主节点配置指定了数个元素,包括容器使用的默认端口: - `8089` 用于网页界面 - `5557` 和 `5558` 用于与工作器通信 ### 2.2 关于负载测试工作器 Locust 工作器执行负载测试任务,可以使用单个 Deployment 来创建多个 pod。这些 pod 分布在 Kubernetes 集群中。 下图显示了 Locust 主节点与 Locust 工作器之间的关系。 ![dl-locust-masters](./res/dl-locust-masters.svg) ## 3. 部署用于测试 Web 应用 `sample-webapp` 目录下包含一个简单的 web 测试应用,先构建为 docker 镜像。 ```shell $ git clone https://gitee.com/lldhsds/distributed-load-testing-using-k8s.git $ cd distributed-load-testing-using-k8s/sample-webapp/ # 构建镜像 $ docker build -t lldhsds/sample-webapp:20240625 . # 查看构建的容器镜像 $ docker images | grep sample-webapp lldhsds/sample-webapp 20240625 c4e7e59ac329 About a minute ago 928MB # 打包镜像,将镜像导入到其他节点。也可以推送到镜像仓库。 $ docker save -o sample-webapp.tar lldhsds/sample-webapp:20240625 ``` 在 kubernetes 上部署 web应用,名字保持默认,`sample-webapp` ```bash [root@k8s-master manifest-k8s]# pwd /root/distributed-load-testing-using-k8s/manifest-k8s # 镜像指向上面构建的镜像,其他信息如副本数根据自己的测试环境调整或者后续修改 [root@k8s-master manifest-k8s]# cat sample-webapp.yaml kind: Deployment apiVersion: apps/v1 metadata: name: sample-webapp spec: selector: matchLabels: name: sample-webapp replicas: 2 template: metadata: labels: name: sample-webapp spec: containers: - name: sample-webapp image: lldhsds/sample-webapp:20240625 ports: - name: web containerPort: 8000 --- kind: Service apiVersion: v1 metadata: name: sample-webapp labels: name: sample-webapp spec: ports: - port: 8000 selector: name: sample-webapp [root@k8s-master manifest-k8s]# kubectl create -f sample-webapp.yaml [root@k8s-master manifest-k8s]# kubectl get pod NAME READY STATUS RESTARTS AGE sample-webapp-65cdc749f7-hd8df 1/1 Running 0 61s sample-webapp-65cdc749f7-j65sr 1/1 Running 0 61s [root@k8s-master manifest-k8s]# kubectl get svc NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE kubernetes ClusterIP 10.96.0.1 443/TCP 14d sample-webapp ClusterIP 10.99.23.223 8000/TCP 9m52s # 测试应用正常响应 [root@k8s-master manifest-k8s]# curl http://10.99.23.223:8000 Welcome to the "Distributed Load Testing Using Kubernetes" sample web app ``` ## 4. 部署 Locust 分布式压测组件 `locust-master` 和 `locust-worker` 使用同样的容器镜像。 ### 4.1 构建 locust tasks 镜像 `locust-master` 和 `locust-worker` 使用的都是 `locust-tasks` 镜像,自定义locust测试脚本,构建为容器镜像: ```bash [root@k8s-master locust]# pwd /root/distributed-load-testing-using-k8s/locust [root@k8s-master locust]# cd .. [root@k8s-master distributed-load-testing-using-k8s]# cd locust/ [root@k8s-master locust]# ls Dockerfile locust-tasks [root@k8s-master locust]# docker build -t lldhsds/locust-tasks:20240625 . # 查看构建的镜像 [root@k8s-master locust]# docker images | grep locust-tasks lldhsds/locust-tasks 20240625 0b08d2e86b76 About a minute ago 983MB # 打包镜像导入到其他节点中 [root@k8s-master locust]# docker save -o locust-tasks.tar lldhsds/locust-tasks:20240625 ``` ### 4.2 部署 locust分布式测试环境 修改master cotnroller 和worker cotnroller 中 `spec.template.spec.containers.image` 字段,指向自己构建的镜像: ```ini [root@k8s-master manifest-k8s]# pwd /root/distributed-load-testing-using-k8s/manifest-k8s [root@k8s-master manifest-k8s]# ls locust-tasks.yaml sample-webapp.yaml [root@k8s-master manifest-k8s]# vim locust-tasks.yaml ... image: lldhsds/locust-tasks:20240625 ... ``` 部署`locust-master`和`locust worker`,根据需要修改worker节点的副本数: ```bash [root@k8s-master manifest-k8s]# ls locust-tasks.yaml sample-webapp.yaml [root@k8s-master manifest-k8s]# kubectl create -f locust-tasks.yaml deployment.apps/locust-master created service/locust-master created deployment.apps/locust-worker created # 查看部署的容器 [root@k8s-master manifest-k8s]# kubectl get pod NAME READY STATUS RESTARTS AGE locust-master-8b64d8b4c-fmjsz 1/1 Running 0 9m52s locust-worker-84fc79566c-2gjqr 1/1 Running 0 9m51s locust-worker-84fc79566c-6v8cz 1/1 Running 0 9m51s sample-webapp-65cdc749f7-hd8df 1/1 Running 0 18m sample-webapp-65cdc749f7-j65sr 1/1 Running 0 18m [root@k8s-master manifest-k8s]# kubectl get svc NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE kubernetes ClusterIP 10.96.0.1 443/TCP 14d locust-master NodePort 10.109.114.2 8089:30502/TCP,5557:31739/TCP,5558:32268/TCP 9m55s sample-webapp ClusterIP 10.99.23.223 8000/TCP 27m ``` ### 4.3 指向locust性能测试 使用`http://ip:30502`即可访问locust web界面,可以看到已经有两个workder节点连接到master。 ![dis-locust](./res/dist-locust.png) 配置参数进行测试: ![dist-locust-test](./res/dist-locust-test.png) ## 5. 总结 1. 基于该架构可以实现K8S环境下应用的分布式压测,借助与K8S易于扩展的能力,可以很容易的调整压测端、应用端的副本数,实现扩缩容; 2. 本文使用一套K8S集群承载locust分布式测试组件和业务应用,最佳实践情况下,可以将locust部署到单独的K8S集群中,对业务侧K8S进行压测。