[英]Connection refused error (111) when using Locust for Performance Testing against docker-compose app
我正在使用性能測試工具 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
存在於當前工作目錄中:
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>
現在在同一個網絡中運行你的 locust 容器。 假設它正在偵聽端口 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
標志是關鍵!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.