Skip to content

第 12 章 · Kubernetes Operator

本章目标:使用 Locust Kubernetes Operator 在 K8s 集群中以声明式方式管理分布式压测,掌握 Helm 安装、LocustTest CRD 配置与监控集成。

12.1 为什么需要 Operator

上一章用 Docker Compose 编排了 master + workers。但在 K8s 环境中,你希望:

  • 压测配置声明式管理(YAML 即代码,可入 Git);
  • Pod 故障自动重启、资源配额自动调度;
  • 与集群内其他服务共享网络和监控体系。

Locust Kubernetes Operator 是一个 CRD + Controller,把压测定义为 K8s 资源(LocustTest),自动创建 master/worker Job、挂载 locustfile、暴露 Web UI。

12.2 安装 Operator

方式一:Helm Chart(推荐)

bash
# 添加仓库
helm repo add locust-operator https://locustio.github.io/k8s-operator
helm repo update

# 安装到独立 namespace
helm install locust-operator locust-operator/locust-operator \
  --namespace locust-operator --create-namespace

验证安装:

bash
# 检查 operator pod 运行状态
kubectl get pods -A -l app.kubernetes.io/name=locust-operator
# 输出示例:
# NAMESPACE         NAME                                    READY   STATUS    AGE
# locust-operator   locust-operator-xxxxxxxxx-xxxxx         1/1     Running   18s

# 检查 CRD 已注册
kubectl get crd
# NAME                    CREATED AT
# locusttests.locust.io   2024-01-15T08:30:00Z

方式二:Manifest 文件

bash
kubectl apply -f https://raw.githubusercontent.com/locustio/k8s-operator/main/dist/install.yaml

12.3 创建第一个 LocustTest 资源

yaml
# locust-test.yaml
apiVersion: locust.io/v1
kind: LocustTest
metadata:
  name: my-load-test
spec:
  master:
    configMapName: "my-locustfile"       # 引用包含 locustfile 的 ConfigMap
    image: "locustio/locust:latest"
    replicas: 1
  worker:
    configMapName: "my-locustfile"
    image: "locustio/locust:latest"
    replicas: 5                          # 5 个 worker pod
  experiment_target: "http://my-app.default.svc.cluster.local:8000"

先创建 ConfigMap 存放 locustfile:

bash
kubectl create configmap my-locustfile --from-file=locustfile.py

然后应用压测资源:

bash
kubectl apply -f locust-test.yaml

# 查看 LocustTest 状态
kubectl get locusttest
# NAME           STATUS     AGE
# my-load-test   Running    45s

Operator 会自动创建:

  • 1 个 master Deployment + Service(暴露 Web UI)
  • N 个 worker Deployment
  • 对应的 ConfigMap 挂载

12.4 内联 locustfile 与自定义配置

小型测试可以直接把 locustfile 写在 CRD 中(无需 ConfigMap):

yaml
apiVersion: locust.io/v1
kind: LocustTest
metadata:
  name: inline-test
spec:
  locustfile: |
    from locust import HttpUser, task, between

    class QuickTest(HttpUser):
        wait_time = between(1, 3)
        host = "http://target-service:8000"

        @task
        def health(self):
            self.client.get("/health")

  worker:
    replicas: 3

适用场景

内联适合快速验证和小脚本;正式项目建议 ConfigMap 或 Git 仓库引用以便版本管理。

12.5 监控与清理

访问 Web UI(通过 port-forward):

bash
kubectl port-forward svc/my-load-test-master 8089:8089

打开 http://localhost:8089 即可看到实时图表。

删除压测资源(自动清理所有 Pod):

bash
kubectl delete locusttest my-load-test

卸载 Operator:

bash
helm uninstall locust-operator -n locust-operator

本章小结

  • Operator 通过 CRD locusttests.locust.io 把压测变成 K8s 原生资源;
  • Helm 一行安装:helm install locust-operator locust-operator/locust-operator
  • LocustTest YAML 声明 master/worker 副本数与 locustfile 来源;
  • 支持内联 locustfile 和 ConfigMap 引用两种模式;
  • 删除 CR 即自动清理所有关联 Pod。

🧪 随堂测验

点击你认为正确的选项。答错时会展示正确答案与原因解析。

1. Locust Kubernetes Operator 的 CRD 名称是什么?

2. 在 LocustTest CRD 中如何指定 worker 数量?

3. LocustTest 支持哪两种方式提供 locustfile?

4. 删除 LocustTest 资源后会发生什么?

🛠️ 动手实践

  1. 在本地 minikube/kind 集群中安装 Locust Operator 并验证 CRD 注册。
  2. 用 ConfigMap 方式创建一个 LocustTest,设置 3 个 worker,观察 Pod 自动创建。
  3. 尝试修改 LocustTest 的 worker replicas 从 3 到 6,观察滚动更新行为。

下一章学习突破 HTTP 限制,用 Locust 测试非 HTTP 协议