I have a k0s Kubernetes cluster on a single node. I am trying to run a selenium/standalone-chrome
to create a remote Selenium node. The trouble that I am having is that it responds if I port forward 4444
from the pod, but cannot seem to access it via a Service port. I get connection refused. I don't know if it's because it's ignore connections that non-localhost.
The Pod
definition for pod/standalone-chrome
is:
apiVersion: v1
kind: Pod
metadata:
name: standalone-chrome
spec:
containers:
- name: standalone-chrome
image: selenium/standalone-chrome
ports:
- containerPort: 4444
env:
- name: JAVA_OPTS
value: '-Dwebdriver.chrome.whitelistedIps=""'
The Service
definition I have for service/standalone-chrome-service
is:
apiVersion: v1
kind: Service
metadata:
name: standalone-chrome-service
labels:
app: standalone-chrome
spec:
ports:
- port: 4444
name: standalone-chrome
type: ClusterIP
selector:
app: standalone-chrome
This creates the following, along with a busybox
container I have just for testing connectivity.
NAME READY STATUS RESTARTS AGE
pod/busybox1 1/1 Running 70 2d22h
pod/standalone-chrome 1/1 Running 0 3m15s
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 18d
service/standalone-chrome-service ClusterIP 10.111.12.1 <none> 4444/TCP 3m5s
The issue I am having now is that I'm not able to access the remote Selenium service via standalone-chrome-service
. I get connection refused. For example, here is trying to reach it via the busybox1
container:
$ wget http://standalone-chrome-service:4444
Connecting to standalone-chrome-service:4444 (10.111.12.1:4444)
wget: can't connect to remote host (10.111.12.1): Connection refused
I am able to port forward from pod/standalone-chrome
to my host machine using kubectl port-forward
though and it works OK, which I think confirms a service is successfully running but not accessible via the Service
:
$ kubectl port-forward pod/standalone-chrome 4444:4444 &
Forwarding from 127.0.0.1:4444 -> 4444
Forwarding from [::1]:4444 -> 4444
$ wget http://localhost:4444
--2021-11-22 13:37:20-- http://localhost:4444/
Resolving localhost (localhost)... ::1, 127.0.0.1
Connecting to localhost (localhost)|::1|:4444... connected.
...
I'd greatly appreciate any help in figuring out how to get the Selenium remote server accessible via the Service
.
EDIT: Here is the updated Service definition with name
...
apiVersion: v1
kind: Service
metadata:
name: standalone-chrome-service
labels:
app: standalone-chrome
spec:
ports:
- port: 4444
name: standalone-chrome
type: ClusterIP
selector:
name: standalone-chrome
Here is the output of describe:
Name: standalone-chrome-service
Namespace: default
Labels: app=standalone-chrome
Annotations: <none>
Selector: name=standalone-chrome
Type: ClusterIP
IP Families: <none>
IP: 10.100.179.116
IPs: 10.100.179.116
Port: standalone-chrome 4444/TCP
TargetPort: 4444/TCP
Endpoints: <none>
Session Affinity: None
Events: <none>
CodePudding user response:
There are two problems that I've found from the above-posted YAML and console output.
- The selector for service, You've specified as
app: standalone-chrome
, it should bename: standalone-chrome
(if you are trying to access the service from inside kubernetes cluster you can ignore the next problem) - The service type you are trying to access is clusterIp which is only available in your cluster network. For accessing the service from the outside the cluster, use type NodePort. Note that the NodePort services have a predefined range of ports 30000 - 32767. Either you can manually set one port in this range and map it with your 4444 or let Kubernetes randomly pick it for you.
In addition to this, you can use the command kubectl describe service standalone-chrome-service
for verifying whether all your pods got attached with the service or not.
CodePudding user response:
Service's syntax with:
selector:
app: standalone-chrome
is correct, selector
should be matched by label
.
Services match a set of Pods using labels and selectors, a grouping primitive that allows logical operation on objects in Kubernetes. Labels are key/value pairs attached to objects
See for more details Using a Service to Expose Your App.
Now you need to add this label
(which is app: standalone-chrome
) to your pod.yaml
metadata:
apiVersion: v1
kind: Pod
metadata:
name: standalone-chrome
labels:
app: standalone-chrome # this label should match to selector in service
spec:
containers:
- name: standalone-chrome
image: selenium/standalone-chrome
ports:
- containerPort: 4444
env:
- name: JAVA_OPTS
value: '-Dwebdriver.chrome.whitelistedIps=""'