如何在分布式 Docker 配置中 运行 Locust?

How do I run Locust in a distributed Docker configuration?

我正在 Fargate 环境中与多个工作人员一起开发 运行ning Locust,但我想看看它在简单分布式 Docker 设置中的样子。我从 website 中提取了下面的 docker-compose.yml 并对其进行了修改,以便 运行 在 localhost 上。我可以用 docker-compose up --scale worker=4 很好地启动 Locust,然后出现四个 worker 和 master,但是当我尝试 运行 通过 Web UI 进行测试时,我得到:

Attaching to locust-distributed-docker-master-1, locust-distributed-docker-worker-1, locust-distributed-docker-worker-2, locust-distributed-docker-worker-3, locust-distributed-docker-worker-4
locust-distributed-docker-worker-1  | [2021-11-17 19:01:19,719] be1b465ae5c7/INFO/locust.main: Starting Locust 2.5.0
locust-distributed-docker-master-1  | [2021-11-17 19:01:19,956] 8769b6dcd3ed/INFO/locust.main: Starting web interface at http://0.0.0.0:8089 (accepting connections from all network interfaces)
locust-distributed-docker-master-1  | [2021-11-17 19:01:20,016] 8769b6dcd3ed/INFO/locust.main: Starting Locust 2.5.0
locust-distributed-docker-worker-4  | [2021-11-17 19:01:20,144] bd481d228ef6/INFO/locust.main: Starting Locust 2.5.0
locust-distributed-docker-worker-3  | [2021-11-17 19:01:20,716] 26af3d44e1c9/INFO/locust.main: Starting Locust 2.5.0
locust-distributed-docker-worker-2  | [2021-11-17 19:01:21,122] d536c752bdee/INFO/locust.main: Starting Locust 2.5.0
locust-distributed-docker-master-1  | [2021-11-17 19:01:42,998] 8769b6dcd3ed/WARNING/locust.runners: You are running in distributed mode but have no worker servers connected. Please connect workers prior to swarming.

这个练习的重点是观察控制台,看看 workers 如何与 master 交互,没有别的。

docker-compose.yml:

version: '3'

services:
  master:
    image: locustio/locust
    ports:
     - "8089:8089"
    volumes:
      - ./:/mnt/locust
    command: -f /mnt/locust/locustfile.py --master

  worker:
    image: locustio/locust
    volumes:
      - ./:/mnt/locust
    command: -f /mnt/locust/locustfile.py --worker --master-host 127.0.0.1

要以这种方式使用示例,工作人员 运行ning 与主机在同一台机器上,最简单的方法就是完全按照示例进行操作,只是不包括主机 IP 地址。问题是 Docker 本地容器之间的网络与主机之间的常规网络不同。您必须研究差异。

如果您没有为主机指定 IP 地址,那么如果 Locust worker 运行 在同一主机上,它们将自动发现主机。在示例中 docker-compose.yml --master-host master 指的是将 运行 按名称命名的主服务,创建 networking bridge between 工作容器和主容器以允许工人自动发现主人。当您实际部署工作人员时,如果他们 运行 在不同的主机上,您可能需要一个不同的设置来指定一个单独的 IP 地址进行通信。

所以直接按照这个例子让你的工人这样命令:

  worker:
    image: locustio/locust
    volumes:
      - ./:/mnt/locust
    command: -f /mnt/locust/locustfile.py --worker --master-host master

这应该会产生如下输出:

% docker compose up --scale worker=4
[+] Running 5/0
 ⠿ Container docker-compose_master_1  Created                                                       0.0s
 ⠿ Container docker-compose_worker_2  Created                                                       0.0s
 ⠿ Container docker-compose_worker_3  Created                                                       0.0s
 ⠿ Container docker-compose_worker_1  Created                                                       0.0s
 ⠿ Container docker-compose_worker_4  Created                                                       0.0s
Attaching to master_1, worker_1, worker_2, worker_3, worker_4
worker_3  | [2021-11-18 16:32:49,911] d90df67c6a69/INFO/locust.main: Starting Locust 2.5.0
worker_4  | [2021-11-18 16:32:50,062] 112a60412b1e/INFO/locust.main: Starting Locust 2.5.0
master_1  | [2021-11-18 16:32:50,224] 859d07f8570b/INFO/locust.main: Starting web interface at http://0.0.0.0:8089 (accepting connections from all network interfaces)
worker_2  | [2021-11-18 16:32:50,233] 56ffce9d4448/INFO/locust.main: Starting Locust 2.5.0
master_1  | [2021-11-18 16:32:50,238] 859d07f8570b/INFO/locust.main: Starting Locust 2.5.0
master_1  | [2021-11-18 16:32:50,239] 859d07f8570b/INFO/locust.runners: Client '56ffce9d4448_dfda9f3bcff742909af80b63d7866714' reported as ready. Currently 1 clients ready to swarm.
master_1  | [2021-11-18 16:32:50,249] 859d07f8570b/INFO/locust.runners: Client '112a60412b1e_49c9e2df265d4fd7bc0f6554a76e66c9' reported as ready. Currently 2 clients ready to swarm.
worker_1  | [2021-11-18 16:32:50,256] 988707b23133/INFO/locust.main: Starting Locust 2.5.0
master_1  | [2021-11-18 16:32:50,259] 859d07f8570b/INFO/locust.runners: Client '988707b23133_88ac7446afd843a5ae7a20dceaed9ea4' reported as ready. Currently 3 clients ready to swarm.
master_1  | [2021-11-18 16:32:50,336] 859d07f8570b/INFO/locust.runners: Client 'd90df67c6a69_e432779d02f94947abb992ff1043eb0e' reported as ready. Currently 4 clients ready to swarm.