Spinning-up your own auto-scalable Selenium Grid in Kubernetes: Part 2

Selenium Grid Auto-Scalability Requirement

Scaling Up:

  • By default at-least one browser pod should always be running in our cluster. This long running Pod will be re-started once per day.
  • We have to continuously poll the Selenium Grid Console to know if there are any queued requests and then we have to increment the scale of our selenium-node deployment by number of queued requests.
  • We have to keep an upper limit to this auto-scaling so that our selenium grid should not end up consuming all the resources of our K8s cluster. So if we touch that max scale limit then we should stop auto-scaling and let all the new in-coming requests to be queued up.
New Scale = Present Scale + No.of Queued Requests

Scaling Down:

  • Lets’ say there are four pods running and only one pod is busy doing the execution so if we will apply the similar rule which we have applied for auto scaling then in case we have to bring down three of the pods.
New Scale = Present Scale - No. of free nodesP.S. This rule will only be applied when all the pods are free.
  • Here is one limitation of Kubernetes which is an opened bug , when we scale down the pods, k8s can bring down any pods randomly and we cannot say that I want these specific pods to be brought down. This is highly critical for our requirement as it can affect any running execution.
  • So, here is one caveat, for scaling down we have to wait for all the executions to be finished and then we can bring down our pods scale back to one.

Building our own Custom Auto-Scaler

kubectl get serviceaccountsThe output is similar to this:NAME      SECRETS    AGE
default 1 1d

Creating a New Service Account:

kubectl create -f auto-scale-robot-sa.yaml && \
kubectl create -f auto-scale-robot-role.yaml && \
kubectl create -f auto-scale-robot-rb.yaml

Generating the API Token for our service account:

kubectl get serviceaccounts/auto-scale-robot-sa -o yaml
kubectl describe secrets/auto-scale-robot-sa-token-7z9db
API_TOKEN=<token value extracted above>
curl -X PATCH \
--header "Authorization: Bearer $API_TOKEN" \
--header 'Accept: application/json' \
--header 'Content-Type: application/strategic-merge-patch+json' \
--insecure \
--data '{"spec": {"replicas": 2}}'

Packaging the auto-scaler Application:

kubectl create -f selenium-grid-k8s-autoscaler-deployment.yaml
kubectl logs -f selenium-grid-k8s-autoscaler-deployment-7ccc88dcd5-cnhsf



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store


I am a Civil Engineer by qualification, an Architect by profession and a Developer by passion. (amitrawat.tech)