【问题标题】:pointing selenium tests through nodePort in a service通过服务中的 nodePort 指向 selenium 测试
【发布时间】:2021-10-12 17:49:23
【问题描述】:

我在selenium-hub-service.yml 文件中有这个:

apiVersion: v1
kind: Service
metadata:
  name: selenium-srv
spec:
  selector:
    app: selenium-hub
  ports:
  - port: 4444
    nodePort: 30001
  type: NodePort
  sessionAffinity: None

当我在终端上执行kubectl describe service 时,我将 kubernetes 服务的端点设为192.168.49.2:8443。然后,我将浏览器指向192.168.49.2:30001,但浏览器无法到达该端点。我期待到达 selenium hub。

当我执行minikube service selenium-srv --url,这给了我http://127.0.0.1:56498 并将浏览器指向它时,我可以到达中心。

我的问题是:为什么我无法通过nodePort 联系?

我想通过nodePort 方式来做,因为我事先知道端口,如果 kubernetes 服务端点保持不变,那么当我将它与 azure 管道集成时,可能很容易将我的测试指向一个已知端点。

编辑:kubectl get service 的输出:

NAME           TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)          AGE
kubernetes     ClusterIP   10.96.0.1      <none>        443/TCP          4d
selenium-srv   NodePort    10.96.34.117   <none>        4444:30001/TCP   2d2h

【问题讨论】:

  • 你会添加kubectl get svc的o/p吗?
  • 我在上面进行了编辑以显示
  • 你在哪里设置了你的 minikube 集群? Linux、Mac 还是 Windows?你用的是哪个minikube driver?如果是 linux 主机,你有没有像 br-42319e616ec5 这样的接口,地址是 inet 192.168.49.1/24 ? (ip a 命令)?您可以从您的主机 ping 192.168.42.2 吗?
  • 我在mac上设置的。我通过这个 curl -LO https://storage.googleapis.com/minikube/releases/latest/minikube-darwin-amd64 安装了 minikube。对192.168.42.2 的请求超时
  • 好的,谢谢。检查this topic on GitHub - 似乎在 Mac 上您只能使用 localhost 地址来访问服务。你还好吗?

标签: kubernetes kubernetes-service


【解决方案1】:

基于this Github topic 发布的社区维基。随意扩展它。

以下信息假设您使用的是the default driver docker


macOS 上的 Minikube 与 Linux 上的行为有些不同。在 Linux 上,您有用于 docker 和连接到 minikube 节点端口的特殊接口,如下所示:

3: docker0:
...
inet 172.17.0.1/16

还有这个:

4: br-42319e616ec5:
...
inet 192.168.49.1/24 brd 192.168.49.255 scope global br-42319e616ec5

在 macOS 上没有实现这样的解决方案。 Check this:

这是一个已知问题,Docker Desktop 网络不支持端口。您将不得不使用 minikube 隧道。

Also:

Macos 上没有 bridge0,导致容器 IP 无法从主机访问。

这意味着您无法使用 IP 地址192.168.49.2 连接到您的服务。

也请查看这篇文章:Known limitations, use cases, and workarounds - Docker Desktop for Mac:

ma​​cOS 上没有 docker0 网桥 由于在 Docker Desktop for Mac 中实现网络的方式,您无法在主机上看到 docker0 接口。这个接口其实是在虚拟机里面的。

我无法 ping 我的容器 Docker Desktop for Mac 无法将流量路由到容器。

无法按容器 IP 寻址 无法从 macOS 主机访问 docker (Linux) 桥接网络。

setup minikube to use NodePort at the localhost address on Mac, like this one有几种方法:

minikube start --driver=docker --extra-config=apiserver.service-node-port-range=32760-32767 --ports=127.0.0.1:32760-32767:32760-32767`

您也可以使用minikube service 命令,该命令将返回一个 URL 以连接到服务。

【讨论】:

    【解决方案2】:

    您的部署是否在端口 4444 上运行?

    试试这个

    deployment.yaml

    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: selenium-hub
      labels:
        app: selenium-hub
    spec:
      replicas: 1
      selector:
        matchLabels:
          app: selenium-hub
      template:
        metadata:
          labels:
            app: selenium-hub
        spec:
          containers:
          - name: selenium-hub
            image: selenium/hub:3.141
            ports:
              - containerPort: 4444
            resources:
              limits:
                memory: "1000Mi"
                cpu: ".5"
            livenessProbe:
              httpGet:
                path: /wd/hub/status
                port: 4444
              initialDelaySeconds: 30
              timeoutSeconds: 5
            readinessProbe:
              httpGet:
                path: /wd/hub/status
                port: 4444
              initialDelaySeconds: 30
              timeoutSeconds: 5
    

    service.yaml

    apiVersion: v1
    kind: Service
    metadata:
      name: selenium-hub
      labels:
        app: selenium-hub
    spec:
      ports:
      - port: 4444
        targetPort: 4444
        name: port0
      selector:
        app: selenium-hub
      type: NodePort
      sessionAffinity: None
    

    如果你想使用 chrome

    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: selenium-node-chrome
      labels:
        app: selenium-node-chrome
    spec:
      replicas: 2
      selector:
        matchLabels:
          app: selenium-node-chrome
      template:
        metadata:
          labels:
            app: selenium-node-chrome
        spec:
          volumes:
          - name: dshm
            emptyDir:
              medium: Memory
          containers:
          - name: selenium-node-chrome
            image: selenium/node-chrome-debug:3.141
            ports:
              - containerPort: 5555
            volumeMounts:
              - mountPath: /dev/shm
                name: dshm
            env:
              - name: HUB_HOST
                value: "selenium-hub"
              - name: HUB_PORT
                value: "4444"
    

    测试python代码

    from selenium import webdriver
    from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
    
    def check_browser(browser):
      driver = webdriver.Remote(
        command_executor='http://<IP>:<PORT>/wd/hub',
        desired_capabilities=getattr(DesiredCapabilities, browser)
      )
      driver.get("http://google.com")
      assert "google" in driver.page_source
      driver.quit()
      print("Browser %s checks out!" % browser)
    
    check_browser("CHROME")
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-12-26
      • 2019-07-26
      • 2015-11-05
      • 1970-01-01
      • 1970-01-01
      • 2013-04-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多