Deployment in Kubernetes for Selenium Grid
By: Kumar Rishabh
Tags: Selenium, Cloud, Test Automation, Selenium Grid, Kubernetes

Now, since we have gain enough knowledge on Kubernetes working, It is a right time to deploy the setup to run selenium grid.

Pre-Requisite

VS Code with yml and kubernetes add-on. A selenium code with remote driver using Selenium Grid. Understanding of Kubernetes Basics. A Kubernetes setup and good to have some hands on as described in the linked article.

Kubernetes deployments

A Deployment provides declarative updates for Pods ReplicaSets.

You describe a desired state in a Deployment, and the Deployment Controller changes the actual state to the desired state at a controlled rate. You can define Deployments to create new ReplicaSets, or to remove existing Deployments and adopt all their resources with new Deployments.

So to reduce our maintenance time on upgrading each pods/scripts whenever a new selenium hb or node version comes up, we opt for Deployments and Replication sets.

So lets open a VS code and go ahead with a Deployment template by creating a new yml file and adding “Kubernetes deployments”

Following is the Deployment we need to run for hub:-

apiVersion: apps/v1

kind: Deployment

metadata:

name: selenium-hub

spec:

selector:

matchLabels:

app: selenium-hub

template:

metadata:

labels:

app: selenium-hub

spec:

containers:

- name: selenium-hub

image: selenium/hub:3.141.59-20200326

resources:

limits:

memory: "128Mi"

cpu: "500m"

ports:

- containerPort: 4444

livenessProbe:

httpGet:

path: /wd/hub/status

port: 4444

initialDelaySeconds: 30

timeoutSeconds: 5

Now to create a deployment we need to run command “kubectl create -f <Path and yml filename>”

Now our selenium hun is up, but is not exposed yet and there is no nodes to run the test cases. Let us run the nodes by Replication controller which soes as :-

apiVersion: v1

kind: ReplicationController

metadata:

name: selenium-rep

spec:

replicas: 2

selector:

app: selenium-rep

template:

metadata:

name: selenium-chrome

labels:

app: selenium-rep

spec:

containers:

- name: selenium-node

image: selenium/node-chrome

ports:

- containerPort: 5555

env:

- name: HUB_HOST

value: "selenium-srv"

- name: HUB_PORT

value: "4444"

Presently it runs 2 replicas, you may increase it when needed. Now we need a service to control these, service yml goes like :-

apiVersion: v1

kind: Service

metadata:

name: selenium-srv

labels:

app: selenium-srv

spec:

selector:

app: selenium-hub

ports:

- port: 4444

nodePort: 40001

type: NodePort

Now this service exposes the port 40001 for usage. When we say nodePort, it actually talks on Kubernetes node(Please do not confuse it with selenium node here).

Now we need node IP which will act as a hub’s IP and port is 40001 as described in service above.

Lets execute a command, “kubectl describe node” and search for internal IP. This IP should be entered in you Selenium code and the code is executed.

Ta da.. Test passes on Pods..!!

Now how can we see run time logs?? Lets see Visual code and the Kubernetes extension.

Kubernetes Extension

So the logs will be visible in the nodes description as :-

Logs in Kubernetes Extensions

So We have executed our tests on Kubernetes..!! Now you can take this deployment to cloud using Kubernetes.