Kubernetes & Jenkins – on-demand Selenium Grid

This is a fast-forward guide for testers using Kubernetes, Jenkins & Selenium Grid

Prerequisites:

  • A running Kubernetes cluster with internal DNS working
  • A running Jenkins container
  • A proper defined “selenium-hub:4444(TCP)” service inside the namespace

Define a variable “SELENIUM” as Boolean Parameter; default value is up to you.

 

Add the following to your build stage in Jenkins and make sure you are using parametrised builds.

#!/bin/bash

if [[ '$SELENIUM'='ENABLED' ]]
then
echo "Spinning Selenium Grid.."
SELENIUM_YAML=$(mktemp)
cat <<EOF >> $SELENIUM_YAML
---
apiVersion: v1
kind: Pod
metadata:
name: selenium-hub
labels:
app: selenium-hub
spec:
containers:
- name: selenium-hub
image: selenium/hub
ports:
- containerPort: 4444 
resources:
requests:
memory: "256Mi"
cpu: "250m"
limits:
memory: "512Mi"
cpu: "500m"
livenessProbe:
httpGet:
path: /grid/console
port: 4444
initialDelaySeconds: 30
timeoutSeconds: 5
readinessProbe:
httpGet:
path: /grid/console
port: 4444
initialDelaySeconds: 30
timeoutSeconds: 5
---
apiVersion: v1
kind: Pod
metadata:
name: selenium-node-chrome
labels:
app: selenium-node-chrome
spec:
containers:
- name: selenium-node-chrome
image: selenium/node-chrome
env:
- name: HUB_PORT_4444_TCP_ADDR
value: "selenium-hub"
- name: HUB_PORT_4444_TCP_PORT
value: "4444"
resources:
requests:
memory: "256Mi"
cpu: "100m"
limits:
memory: "512Mi"
cpu: "250m"
---
apiVersion: v1
kind: Pod
metadata:
name: selenium-node-firefox
labels:
app: selenium-node-firefox
spec:
containers:
- name: selenium-node-firefox
image: selenium/node-firefox
env:
- name: HUB_PORT_4444_TCP_ADDR
value: "selenium-hub"
- name: HUB_PORT_4444_TCP_PORT
value: "4444"
resources:
requests:
memory: "256Mi"
cpu: "100m"
limits:
memory: "512Mi"
cpu: "250m"
EOF
kubectl create -f $SELENIUM_YAML
rm -f $SELENIUM_YAML  
else
echo "Selenium Grid not enabled!"
fi

 

As Post-build step in Jenkins job add the following; it can be added also if you are using cascading jobs, sky’s the limit.

#!/bin/bash
if [[ '$SELENIUM'='ENABLED' ]]
then
echo "Spinning down Selenium Grid.. Testing is done"
kubectl delete pod selenium-hub selenium-node-chrome selenium-node-firefox
else
echo "Selenium Grid was not enabled - Nothing to be done.."
fi

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s