Search code examples
seleniumdockerkubernetesbrowser-automation

Connect Selenium Code to Selenium Hub in k8s


I want to run my automation test script on kubernetes via the selenium hub and the chrome node containers. My test script is also in the form of a container and running as a pod. My test script uses localhost:4444 to connect to grid. But the grid has the NodePort of 31376 and it keeps changing everytime i create a new selenium grid service.

Is there any way i can keep a constant NodePort for my selenium hub so that my script could run.

Selenium hub service:

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

I don't want to change the link to selenium hub every time I execute my command.

This is my service description :-

C:\KUBE>kubectl describe service selenium-hub
Name:                     selenium-hub
Namespace:                default
Labels:                   app=selenium-hub
Annotations:              <none>
Selector:                 app=selenium-hub
Type:                     NodePort
IP:                       10.106.49.182
Port:                     port0  4444/TCP
TargetPort:               4444/TCP
NodePort:                 port0  31376/TCP
Endpoints:
Session Affinity:         None
External Traffic Policy:  Cluster
Events:                   <none>

Thanks.


Solution

  • According to the documentation on NodePort services

    If you want a specific port number, you can specify a value in the nodePort field, and the system will allocate you that port or else the API transaction will fail (i.e. you need to take care about possible port collisions yourself). The value you specify must be in the configured range for node ports.

    Here is an example, which in your case would look like

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