简体   繁体   English

在 docker 上使用 k6 测试本地站点

[英]Using k6 on docker to test a localhosted site

I've got a app running on my computer in localhost:1235, and I'm trying to load test it.我的计算机上有一个应用程序在 localhost:1235 中运行,我正在尝试对其进行负载测试。

I installed k6 container for docker to test it, but of course from the nature of docker, my container has a different localhost.我为 docker 安装了 k6 容器来测试它,但是当然从 docker 的性质来看,我的容器有一个不同的 localhost。 I'm trying to understand what do.我试图了解做什么。

I run the following command: docker run -it --rm --net=host -vc:/users/k6:/k6 loadimpact/k6 run /k6/script我运行以下命令: docker run -it --rm --net=host -vc:/users/k6:/k6 loadimpact/k6 run /k6/script

I read somewhere that --net=host doesn't work on windows, is that right?我在某处读到 --net=host 在 Windows 上不起作用,是吗? How would I find the host IP?我如何找到主机IP?

I've tried running by this tutorial: http://blog.michaelhamrah.com/2014/06/accessing-the-docker-host-server-within-a-container/我试过按本教程运行: http : //blog.michaelhamrah.com/2014/06/accessing-the-docker-host-server-within-a-container/

The IP I find 172.17.0.1 doesn't work in my test.我找到的 IP 172.17.0.1 在我的测试中不起作用。

I also tried adding -p 1235:1235 but it failed, I guess docker tries to bind this port and just forward to it.我也尝试添加-p 1235:1235但它失败了,我猜 docker 试图绑定这个端口并转发到它。

Thanks in advance, Chaim提前致谢,柴姆

Inside your k6 script use the url host.docker.internal to access something running on the host machine.在您的 k6 脚本中,使用 url host.docker.internal访问主机上运行的内容。

For example to access a service running on the host at http://localhost:8080例如访问在主机上运行的服务http://localhost:8080

// script.js
import http from "k6/http";
import { sleep } from "k6";

export default function () {
  http.get("http://host.docker.internal:8080");
  sleep(1);
}

Then on windows or mac this can be run with:然后在 Windows 或 Mac 上,这可以运行:

$ docker run -i loadimpact/k6 run - <script.js

for linux you need an extra flag对于 linux,你需要一个额外的标志

$ docker run --add-host=host.docker.internal:host-gateway -i loadimpact/k6 run - <script.js

References:参考资料:

k6 inside the docker instance should be able to connect to the "public" IP on your host machine - the IP that is configured on your ethernet or Wifi interface. docker 实例中的 k6 应该能够连接到主机上的“公共”IP - 在以太网或 Wifi 接口上配置的 IP。 You can do a ipconfig /all to see all your interfaces and their IPs.您可以执行ipconfig /all以查看所有接口及其 IP。

On my Mac I can do this: $ python httpserv.py & [1] 7824 serving at port 8000 $ ifconfig en1 en1: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 1500 ether b8:09:8a:bb:f7:ed inet6 fe80::148f:5671:5297:fc24%en1 prefixlen 64 secured scopeid 0x5 inet 192.168.0.107 netmask 0xffffff00 broadcast 192.168.0.255 nd6 options=201<PERFORMNUD,DAD> media: autoselect status: active $ echo 'import http from "k6/http"; export default function() { let res = http.get("http://192.168.0.107:8000"); console.log(res.status); };' |docker run -i loadimpact/k6 run -在我的 Mac 上,我可以这样做: $ python httpserv.py & [1] 7824 serving at port 8000 $ ifconfig en1 en1: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 1500 ether b8:09:8a:bb:f7:ed inet6 fe80::148f:5671:5297:fc24%en1 prefixlen 64 secured scopeid 0x5 inet 192.168.0.107 netmask 0xffffff00 broadcast 192.168.0.255 nd6 options=201<PERFORMNUD,DAD> media: autoselect status: active $ echo 'import http from "k6/http"; export default function() { let res = http.get("http://192.168.0.107:8000"); console.log(res.status); };' |docker run -i loadimpact/k6 run - $ python httpserv.py & [1] 7824 serving at port 8000 $ ifconfig en1 en1: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 1500 ether b8:09:8a:bb:f7:ed inet6 fe80::148f:5671:5297:fc24%en1 prefixlen 64 secured scopeid 0x5 inet 192.168.0.107 netmask 0xffffff00 broadcast 192.168.0.255 nd6 options=201<PERFORMNUD,DAD> media: autoselect status: active $ echo 'import http from "k6/http"; export default function() { let res = http.get("http://192.168.0.107:8000"); console.log(res.status); };' |docker run -i loadimpact/k6 run -

Ie I start a simple HTTP server on port 8000 of the host machine, then executes the k6 docker image and tells it to access a URL based on the IP address of the physical, outward-facing en1 interface on the host machine.即,我在主机的端口 8000 上启动一个简单的 HTTP 服务器,然后执行 k6 docker 映像并告诉它访问基于主机上物理的、向外的 en1 接口的 IP 地址的 URL。 In your case, on Windows, you can use ipconfig to find out your external-facing IP.在您的情况下,在 Windows 上,您可以使用ipconfig来找出您的面向外部的 IP。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM