Selenium with kubernetes - unable to access from outside

Hi,

I try to install selenium hub with kubernetes on linux machine.
I take the files from the example
Below is describe of what I got .

I am able to access the hub using localhost/127.0.0.1 but I can’t access it using the ip of machine .

I also run the commands :
kubectl port-forward $PODNAME 4444:4444
nohup kubectl proxy --address=‘0.0.0.0’ --disable-filter=true &

What is missing ?
Thanks

[qa@qaomgutil5m root]$ kubectl describe service/selenium-hub
Name: selenium-hub
Namespace: default
Labels: app=selenium-hub
Annotations:
Selector: app=selenium-hub
Type: NodePort
IP Family Policy: SingleStack
IP Families: IPv4
IP: 10.97.86.63
IPs: 10.97.86.63
External IPs: 172.16.36.72
Port: port0 4444/TCP
TargetPort: 4444/TCP
NodePort: port0 31648/TCP
Endpoints: 172.17.0.3:4444
Session Affinity: None
External Traffic Policy: Cluster
Events:

describe deployment.apps/selenium-hub
Name: selenium-hub
Namespace: default
CreationTimestamp: Sun, 25 Jul 2021 11:22:10 +0000
Labels: app=selenium-hub
Annotations: deployment.kubernetes.io/revision: 1
Selector: app=selenium-hub
Replicas: 1 desired | 1 updated | 1 total | 1 available | 0 unavailable
StrategyType: RollingUpdate
MinReadySeconds: 0
RollingUpdateStrategy: 25% max unavailable, 25% max surge
Pod Template:
Labels: app=selenium-hub
Containers:
selenium-hub:
Image: selenium/hub:3.141
Port: 4444/TCP
Host Port: 0/TCP
Limits:
cpu: 500m
memory: 1000Mi
Liveness: http-get http://:4444/wd/hub/status delay=30s timeout=5s period=10s #success=1 #failure=3
Readiness: http-get http://:4444/wd/hub/status delay=30s timeout=5s period=10s #success=1 #failure=3
Environment:
Mounts:
Volumes:
Conditions:
Type Status Reason


Available True MinimumReplicasAvailable
Progressing True NewReplicaSetAvailable
OldReplicaSets:
NewReplicaSet: selenium-hub-6dd87dfcb6 (1/1 replicas created)
Events:

Is the selenium application in the pod only listening on the localhost interface perhaps? Install net tools inside the container and run netstat -plnt if you’re not sure, to see what interfaces are being listened over.

The port-forward is the correct way to proxy to the pod. You can do it by pod/${PODNAME} or by the service svc/${SERVICE_NAME}.

Hi,
The netstat should be in under the kubernetes docker?
How do I install it there ?

Needs to be done in the container running selenium.

Open a shell into the container

kubectl exec -it ${PODNAME} -- sh

Installing it depends on what the container is built from. It’s going to be one of these commands:

Alpine based images:

apk add -U net-tools

RHEL based images (if dnf doesn’t work, use yum instead):

dnf install net-tools

Debian based images, the first command updates the cache and 2nd installs:

apt update
apt install net-tools

The net-tools package comes with the netstat command.

Now when you run netstat -plnt, what you’re looking for is something like 0.0.0.0:4444 in the Local Address column to know it’s listening on all interfaces.

Though I’m guessing you’re using some stock image for your pod. Can you share the manifest for your deployment?

Hi,
Thanks for your reply
I got error install the netstat

[qa@qaomgutil5m /]$ kubectl exec -it selenium-hub-6dd87dfcb6-5sxxm – sh
$ apt install
Reading package lists… Done
Building dependency tree
Reading state information… Done
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
$ apt install net-tools
Reading package lists… Done
Building dependency tree
Reading state information… Done
E: Unable to locate package net-tools

Thanks

This is the file I use :
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

This container doesn’t run as root, I didn’t expect that… lol

Also for the future, some things to know:

  • You missed apt update, which is necessary in a lot of these containers because the cache is typically removed on build.
  • When sharing YAML and terminal output, use code blocks to preserve formatting.

Anyways, I pulled down the image and checked it out. It seems fine. This shows you exactly what I did and what I saw.

[protosam@nullhost]$ docker run --name selenium -d -u 0:0 selenium/hub:3.141 
03e642e3ba292bffa25102d6e312553c29839ee36b6afffc7f62e5a178dba183

[protosam@nullhost]$ docker exec -it selenium bash

root@03e642e3ba29:/# apt update && apt install net-tools
Get:1 http://security.ubuntu.com/ubuntu focal-security InRelease [114 kB]
Get:2 http://archive.ubuntu.com/ubuntu focal InRelease [265 kB]                       
Get:3 http://security.ubuntu.com/ubuntu focal-security/universe amd64 Packages [784 kB]
Get:4 http://archive.ubuntu.com/ubuntu focal-updates InRelease [114 kB]
Get:5 http://archive.ubuntu.com/ubuntu focal/main amd64 Packages [1275 kB]
Get:6 http://security.ubuntu.com/ubuntu focal-security/main amd64 Packages [986 kB]
Get:7 http://archive.ubuntu.com/ubuntu focal/universe amd64 Packages [11.3 MB]
Get:8 http://archive.ubuntu.com/ubuntu focal-updates/main amd64 Packages [1417 kB]   
Get:9 http://archive.ubuntu.com/ubuntu focal-updates/universe amd64 Packages [1055 kB]
Fetched 17.4 MB in 3s (5900 kB/s)                     
Reading package lists... Done
Building dependency tree       
Reading state information... Done
33 packages can be upgraded. Run 'apt list --upgradable' to see them.
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following NEW packages will be installed:
  net-tools
0 upgraded, 1 newly installed, 0 to remove and 33 not upgraded.
Need to get 196 kB of archives.
After this operation, 864 kB of additional disk space will be used.
Get:1 http://archive.ubuntu.com/ubuntu focal/main amd64 net-tools amd64 1.60+git20180626.aebd88e-1ubuntu1 [196 kB]
Fetched 196 kB in 1s (262 kB/s)  
debconf: delaying package configuration, since apt-utils is not installed
Selecting previously unselected package net-tools.
(Reading database ... 8594 files and directories currently installed.)
Preparing to unpack .../net-tools_1.60+git20180626.aebd88e-1ubuntu1_amd64.deb ...
Unpacking net-tools (1.60+git20180626.aebd88e-1ubuntu1) ...
Setting up net-tools (1.60+git20180626.aebd88e-1ubuntu1) ...

root@03e642e3ba29:/# netstat -plnt
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:4444            0.0.0.0:*               LISTEN      14/java             

root@03e642e3ba29:/# exit

[protosam@nullhost]$ docker kill selenium 
selenium

[protosam@nullhost]$ docker rm selenium
selenium

The application also seems responsive when with your manifest over port-forward.

[protosam@nullhost]$ kubectl get pods -l app=selenium-hub
NAME                            READY   STATUS    RESTARTS   AGE
selenium-hub-6dd87dfcb6-4snff   0/1     Running   0          21s

[protosam@nullhost]$ kubectl get pods -l app=selenium-hub -oname
pod/selenium-hub-6dd87dfcb6-4snff

[protosam@nullhost]$ kubectl port-forward $(kubectl get pods -l app=selenium-hub -oname) 4444:4444
Forwarding from 127.0.0.1:4444 -> 4444
Forwarding from [::1]:4444 -> 4444
Handling connection for 4444
... I left this running and did the next step in another terminal...



[protosam@nullhost]$ curl -w'\n' -i localhost:4444/wd/hub/status
HTTP/1.1 200 OK
Date: Mon, 26 Jul 2021 15:57:46 GMT
Content-Type: application/json;charset=utf-8
Content-Length: 364
Server: Jetty(9.4.z-SNAPSHOT)

{
  "status": 0,
  "value": {
    "ready": false,
    "message": "No spare hub capacity",
    "build": {
      "revision": "e82be7d358",
      "time": "2018-11-14T08:25:53",
      "version": "3.141.59"
    },
    "os": {
      "arch": "amd64",
      "name": "Linux",
      "version": "5.10.25-linuxkit"
    },
    "java": {
      "version": "1.8.0_292"
    }
  }
}

Thanks,

with localhost it is working
The problem is when I try to access it from outside or even with the ip/hostname of the machine from inside the machine

I got connection refused error

Do you what should be done to be able to access the selenium from outside ?

AFter running the command :
minikube service --url selenium-hub

I can access the selenium hub from within the machine using command :curl http://192.168.49.2:31299/grid/console but I can’t access the selenium-hub from outside like I can access the minikube dashboard from outside