pointing selenium tests through nodePort in a service

10/12/2021

I have this in a selenium-hub-service.yml file:

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

When I do kubectl describe service on terminal, I get the endpoint of kubernetes service as 192.168.49.2:8443. I then take that and point the browser to 192.168.49.2:30001 but browser is not able to reach that endpoint. I was expecting to reach selenium hub.

When I do minikube service selenium-srv --url, which gives me http://127.0.0.1:56498 and point browser to it, I can reach the hub.

My question is: why am I not able to reach through nodePort?

I would like to do it through nodePort way because I know the port beforehand and if kubernetes service end point remains constant then it may be easy to point my tests to a known endpoint when I integrate it with azure pipeline.

EDIT: output of 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
-- user1207289
kubernetes
kubernetes-service

2 Answers

10/12/2021

is your deployment running on port 4444 ?

try this

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

if you want to use to 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"

testing python code

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")
-- Harsh Manvar
Source: StackOverflow

10/14/2021

Posted community wiki based on this Github topic. Feel free to expand it.

The information below assumes that you are using the default driver docker.


Minikube on macOS behaves a bit differently than on Linux. While on Linux, you have special interfaces used for docker and for connecting to the minikube node port, like this one:

3: docker0:
...
inet 172.17.0.1/16

And this one:

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

There is no such solution implemented on macOS. Check this:

This is a known issue, Docker Desktop networking doesn't support ports. You will have to use minikube tunnel.

Also:

there is no bridge0 on Macos, and it makes container IP unreachable from host.

That means you can't connect to your service using IP address 192.168.49.2.

Check also this article: Known limitations, use cases, and workarounds - Docker Desktop for Mac:

There is no docker0 bridge on macOS Because of the way networking is implemented in Docker Desktop for Mac, you cannot see a docker0 interface on the host. This interface is actually within the virtual machine.

I cannot ping my containers Docker Desktop for Mac can’t route traffic to containers.

Per-container IP addressing is not possible The docker (Linux) bridge network is not reachable from the macOS host.

There are few ways to 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`

You can also use minikube service command which will return a URL to connect to a service.

-- Mikolaj S.
Source: StackOverflow