我正在使用性能测试工具Locust对安装在docker-compose中运行的应用程序进行负载测试。对于每个请求,我都会收到以下错误(连接被拒绝,错误111):
Error report
# occurrences Error
--------------------------------------------------------------------------------------------------------------------------------------------
5 GET /parties/: 'ConnectionError(MaxRetryError("HTTPConnectionPool(host=\'localhost\', port=8080): Max retries exceeded with url: /parties/ (Caused by NewConnectionError(\'<urllib3.connection.HTTPConnection object at 0x7fa6f294df28>: Failed to establish a new connection: [Errno 111] Connection refused\',))",),)'
我正在从Docker容器运行Locust,如下所示:
docker run --volume /mydir/locustfile:/mnt/locust -e LOCUSTFILE_PATH=/mnt/locust/locustfile.py -e TARGET_URL=https://localhost:8080/parties -e LOCUST_OPTS="--clients=1 --no-web --run-time=600" locustio/locust
奇怪的是,当我使用curl来访问完全相同的URL时,它可以正常工作。
curl http://localhost:8080/parties
任何帮助表示赞赏!
最佳答案
localhost
可能是您在上下文中使用的错误主机名。使用容器时,主机名必须与容器的名称匹配。因此,请使用您容器的名称而不是localhost
。
参考资料:
https://github.com/elastic/elasticsearch-py/issues/715
https://docs.locust.io/en/latest/running-locust-docker.html
https://docs.locust.io/en/stable/configuration.html
一般假设:locustfile.py
存在于当前工作目录中
带有docker compose的分布式示例:
例如,以下docker-compose.yml文件配置将起作用:
services:
web:
build: .
command: python manage.py runserver 0:8000
volumes:
- .:/code/
ports:
- "8000:8000"
master:
image: locustio/locust
ports:
- "8089:8089"
volumes:
- ./:/mnt/locust
command: -f /mnt/locust/locustfile.py --master -H http://web:8000
worker:
image: locustio/locust
volumes:
- ./:/mnt/locust
command: -f /mnt/locust/locustfile.py --worker --master-host master
下一行的-H标志(--host的别名)可以解决问题:
command: -f /mnt/locust/locustfile.py --master -H http://web:8000
使用docker compose的非分布式示例:
services:
web:
build: .
command: python manage.py runserver 0:8000
volumes:
- .:/code/
ports:
- "8000:8000"
locust:
image: locustio/locust
ports:
- "8089:8089"
volumes:
- ./:/mnt/locust
command: -f /mnt/locust/locustfile.py --host=http://web:8000
您也可以指定配置文件,而不是在命令行上定义标志。假设文件
locust.conf
存在于当前工作目录中:locust.conf:
host: http://web:8000
因此,您可以在
docker-compose.yml
中执行以下操作:command: -f /mnt/locust/locustfile.py --config=/mnt/locust/locust.conf
代替:
command: -f /mnt/locust/locustfile.py --host=http://web:8000
一个没有docker compose的非分布式示例:
这些容器必须位于同一网络上,以便它们可以相互通信。为此,我们创建一个称为
locustnw
的通用桥接网络:docker network create --driver bridge locustnw
现在在此网络中运行您的应用容器。假设它正在侦听端口8000并命名为web:
docker run -p 8000:8000 --network=locustnw --name web <my_image>
现在也可以在同一网络中运行蝗虫容器。假设它正在侦听端口8089:
docker run -p 8089:8089 --network=locustnw -v $PWD:/mnt/locust locustio/locust -f /mnt/locust/locustfile.py --host=http://web:8000
--network
,--name
和--host
标志是键!关于python - 使用Locust对docker-compose应用进行性能测试时出现连接拒绝错误(111),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/60550111/