Jenkinsfile Kubernetes expose container ports

3/14/2019

Hope someone can help me, I am trying to export a port on my kubernetes container in my Jenkins pipeline.

I have looked online, and I just can't work out how to get this to work..

I need to export port 4444 to the selenium-hub container.

def label = "selenium-hub-${UUID.randomUUID().toString()}"

podTemplate(label: label, yaml: """
apiVersion: v1
kind: Pod
spec:
  containers:
  - name: selenium-hub
    image: selenium/hub:3.4.0

  - name: selenium-chrome
    image: selenium/node-chrome:3.4.0
    env:
    - name: HUB_PORT_4444_TCP_ADDR
      value: localhost
    - name: HUB_PORT_4444_TCP_PORT
      value: 4444
    - name: DISPLAY
      value: :99.0
    - name: SE_OPTS
      value: -port 5556
  - name: selenium-firefox
    image: selenium/node-firefox:3.4.0
    env:
    - name: HUB_PORT_4444_TCP_ADDR
      value: localhost
    - name: HUB_PORT_4444_TCP_PORT
      value: 4444
    - name: DISPLAY
      value: :98.0
    - name: SE_OPTS
      value: -port 5557

I currently get the ip from the hub, and pass that onto my tests to run via the grid

  node(label) {
        containerLog('selenium-hub')
        sh('hostname -i')
        POD_IP = sh(script: "hostname -i", returnStdout: true).toString().replaceAll('\\n', '');
        node() {
            sh("POD_IP='${POD_IP}' UI_URL=https://EEEEEE.net/ SERVICE_URL=https://WWWWWW.net/ ./node_modules/webdriverio/bin/wdio ./tests/UI/helpers/configs/wdio.GRID.conf.js --nolazy")
        }

I would seem that at the point the tests run, it is unable to access the grid, so I believe it is due to port 4444 not being exposed, but I can't work out how to do it in kubernetes.

Hope someone can point me in the direction of what I need to do, I have tried some examples online, but don't seem to be getting any closer..

Thanks

Karl

-- Karl
jenkins
jenkins-pipeline
jenkins-plugins
kubernetes
selenium

2 Answers

3/26/2019

Service allows your applications to receive traffic. So you need one of those to expose access to your selenium deployment:

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

For more details https://github.com/kubernetes/examples/tree/master/staging/selenium

-- A_Suh
Source: StackOverflow

3/14/2019

Think of a Kubernetes pod as an abstraction of a traditional server. If a container in the pod is exposing a service like mysql or in this case selenium on a particular port then all the other containers in the pod can access that service on the selected port by default:

https://kubernetes.io/docs/concepts/workloads/pods/pod/#resource-sharing-and-communication

Rather than using the POD_IP just try using localhost:4444 (or whatever port you are needing) to access the service.

I use the same pattern for accessing mysql in a sidecar container during unit tests in various builds.

-- jjno91
Source: StackOverflow