2015-10-04 07:38:22 +00:00
## Selenium on Kubernetes
Selenium is a browser automation tool used primarily for testing web applications. However when Selenium is used in a CI pipeline to test applications, there is often contention around the use of Selenium resources. This example shows you how to deploy Selenium to Kubernetes in a scalable fashion.
### Prerequisites
2015-10-08 23:49:37 +00:00
2017-06-02 07:04:49 +00:00
This example assumes you have a working Kubernetes cluster and a properly configured kubectl client. See the [Getting Started Guides ](https://kubernetes.io/docs/getting-started-guides/ ) for details.
2015-10-04 07:38:22 +00:00
Google Container Engine is also a quick way to get Kubernetes up and running: https://cloud.google.com/container-engine/
Your cluster must have 4 CPU and 6 GB of RAM to complete the example up to the scaling portion.
### Deploy Selenium Grid Hub:
2015-10-08 23:49:37 +00:00
2015-10-04 07:38:22 +00:00
We will be using Selenium Grid Hub to make our Selenium install scalable via a master/worker model. The Selenium Hub is the master, and the Selenium Nodes are the workers(not to be confused with Kubernetes nodes). We only need one hub, but we're using a replication controller to ensure that the hub is always running:
2015-10-08 23:49:37 +00:00
2015-10-08 02:26:58 +00:00
```console
2015-10-08 23:49:37 +00:00
kubectl create --filename=examples/selenium/selenium-hub-rc.yaml
2015-10-04 07:38:22 +00:00
```
The Selenium Nodes will need to know how to get to the Hub, let's create a service for the nodes to connect to.
2015-10-08 23:49:37 +00:00
2015-10-08 02:26:58 +00:00
```console
2015-10-08 23:49:37 +00:00
kubectl create --filename=examples/selenium/selenium-hub-svc.yaml
2015-10-04 07:38:22 +00:00
```
### Verify Selenium Hub Deployment
2015-10-08 23:49:37 +00:00
2015-10-04 07:38:22 +00:00
Let's verify our deployment of Selenium hub by connecting to the web console.
#### Kubernetes Nodes Reachable
2015-10-08 23:49:37 +00:00
2015-10-08 02:26:58 +00:00
If your Kubernetes nodes are reachable from your network, you can verify the hub by hitting it on the nodeport. You can retrieve the nodeport by typing `kubectl describe svc selenium-hub` , however the snippet below automates that by using kubectl's template functionality:
2015-10-08 23:49:37 +00:00
2015-10-08 02:26:58 +00:00
```console
export NODEPORT=`kubectl get svc --selector='app=selenium-hub' --output=template --template="{{ with index .items 0}}{{with index .spec.ports 0 }}{{.nodePort}}{{end}}{{end}}"`
2015-10-04 07:38:22 +00:00
export NODE=`kubectl get nodes --output=template --template="{{with index .items 0 }}{{.metadata.name}}{{end}}"`
curl http://$NODE:$NODEPORT
```
#### Kubernetes Nodes Unreachable
2015-10-08 23:49:37 +00:00
2015-10-04 07:38:22 +00:00
If you cannot reach your Kubernetes nodes from your network, you can proxy via kubectl.
2015-10-08 23:49:37 +00:00
2015-10-08 02:26:58 +00:00
```console
export PODNAME=`kubectl get pods --selector="app=selenium-hub" --output=template --template="{{with index .items 0}}{{.metadata.name}}{{end}}"`
2017-02-06 19:50:41 +00:00
kubectl port-forward $PODNAME 4444:4444
2015-10-04 07:38:22 +00:00
```
2016-02-12 19:33:32 +00:00
In a separate terminal, you can now check the status.
2015-10-08 23:49:37 +00:00
2015-10-08 02:26:58 +00:00
```console
2015-10-04 07:38:22 +00:00
curl http://localhost:4444
```
#### Using Google Container Engine
2015-10-08 23:49:37 +00:00
2015-10-04 07:38:22 +00:00
If you are using Google Container Engine, you can expose your hub via the internet. This is a bad idea for many reasons, but you can do it as follows:
2015-10-08 23:49:37 +00:00
2015-10-08 02:26:58 +00:00
```console
2016-12-06 08:48:32 +00:00
kubectl expose rc selenium-hub --name=selenium-hub-external --labels="app=selenium-hub,external=true" --type=LoadBalancer
2015-10-04 07:38:22 +00:00
```
2015-10-08 02:26:58 +00:00
Then wait a few minutes, eventually your new `selenium-hub-external` service will be assigned a load balanced IP from gcloud. Once `kubectl get svc selenium-hub-external` shows two IPs, run this snippet.
2015-10-08 23:49:37 +00:00
2015-10-08 02:26:58 +00:00
```console
export INTERNET_IP=`kubectl get svc --selector="app=selenium-hub,external=true" --output=template --template="{{with index .items 0}}{{with index .status.loadBalancer.ingress 0}}{{.ip}}{{end}}{{end}}"`
2015-10-04 07:38:22 +00:00
curl http://$INTERNET_IP:4444/
```
2015-10-08 23:49:37 +00:00
2015-10-08 02:26:58 +00:00
You should now be able to hit `$INTERNET_IP` via your web browser, and so can everyone else on the Internet!
2015-10-04 07:38:22 +00:00
### Deploy Firefox and Chrome Nodes:
2015-10-08 23:49:37 +00:00
2015-10-04 07:38:22 +00:00
Now that the Hub is up, we can deploy workers.
2015-10-08 02:26:58 +00:00
This will deploy 2 Chrome nodes.
2015-10-08 23:49:37 +00:00
2015-10-08 02:26:58 +00:00
```console
2017-02-06 19:50:41 +00:00
kubectl create --filename=examples/selenium/selenium-node-chrome-rc.yaml
2015-10-04 07:38:22 +00:00
```
2015-10-08 02:26:58 +00:00
And 2 Firefox nodes to match.
2015-10-08 23:49:37 +00:00
2015-10-08 02:26:58 +00:00
```console
2017-02-06 19:50:41 +00:00
kubectl create --filename=examples/selenium/selenium-node-firefox-rc.yaml
2015-10-04 07:38:22 +00:00
```
Once the pods start, you will see them show up in the Selenium Hub interface.
### Run a Selenium Job
2015-10-08 23:49:37 +00:00
2015-10-04 07:38:22 +00:00
Let's run a quick Selenium job to validate our setup.
#### Setup Python Environment
2015-10-08 23:49:37 +00:00
2015-10-04 07:38:22 +00:00
First, we need to start a python container that we can attach to.
2015-10-08 23:49:37 +00:00
2015-10-08 02:26:58 +00:00
```console
2015-10-04 07:38:22 +00:00
kubectl run selenium-python --image=google/python-hello
```
Next, we need to get inside this container.
2015-10-08 23:49:37 +00:00
2015-10-08 02:26:58 +00:00
```console
2015-10-04 07:38:22 +00:00
export PODNAME=`kubectl get pods --selector="run=selenium-python" --output=template --template="{{with index .items 0}}{{.metadata.name}}{{end}}"`
kubectl exec --stdin=true --tty=true $PODNAME bash
```
Once inside, we need to install the Selenium library
2015-10-08 23:49:37 +00:00
2015-10-08 02:26:58 +00:00
```console
2015-10-04 07:38:22 +00:00
pip install selenium
```
#### Run Selenium Job with Python
2015-10-08 23:49:37 +00:00
2015-10-04 07:38:22 +00:00
We're all set up, start the python interpreter.
2015-10-08 23:49:37 +00:00
2015-10-08 02:26:58 +00:00
```console
2015-10-04 07:38:22 +00:00
python
```
And paste in the contents of selenium-test.py.
2015-10-08 23:49:37 +00:00
2015-10-04 07:38:22 +00:00
```python
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
def check_browser(browser):
driver = webdriver.Remote(
command_executor='http://selenium-hub:4444/wd/hub',
desired_capabilities=getattr(DesiredCapabilities, browser)
)
driver.get("http://google.com")
assert "google" in driver.page_source
driver.close()
print("Browser %s checks out!" % browser)
check_browser("FIREFOX")
check_browser("CHROME")
```
You should get
2015-10-08 23:49:37 +00:00
2015-10-04 07:38:22 +00:00
```
>>> check_browser("FIREFOX")
Browser FIREFOX checks out!
>>> check_browser("CHROME")
Browser CHROME checks out!
```
2015-10-08 23:49:37 +00:00
2015-10-04 07:38:22 +00:00
Congratulations, your Selenium Hub is up, with Firefox and Chrome nodes!
### Scale your Firefox and Chrome nodes.
If you need more Firefox or Chrome nodes, your hardware is the limit:
2015-10-08 23:49:37 +00:00
2015-10-08 02:26:58 +00:00
```console
2015-10-04 07:38:22 +00:00
kubectl scale rc selenium-node-firefox --replicas=10
kubectl scale rc selenium-node-chrome --replicas=10
```
You now have 10 Firefox and 10 Chrome nodes, happy Seleniuming!
### Debugging
2015-10-08 23:49:37 +00:00
2016-10-18 08:12:26 +00:00
Sometimes it is necessary to check on a hung test. Each pod is running VNC. To check on one of the browser nodes via VNC, it's recommended that you proxy, since we don't want to expose a service for every pod, and the containers have a weak VNC password. Replace POD_NAME with the name of the pod you want to connect to.
2015-10-08 23:49:37 +00:00
2015-10-08 02:26:58 +00:00
```console
2017-02-06 19:50:41 +00:00
kubectl port-forward $POD_NAME 5900:5900
2015-10-04 07:38:22 +00:00
```
2015-10-08 02:26:58 +00:00
Then connect to localhost:5900 with your VNC client using the password "secret"
2015-10-04 07:38:22 +00:00
Enjoy your scalable Selenium Grid!
Adapted from: https://github.com/SeleniumHQ/docker-selenium
### Teardown
To remove all created resources, run the following:
2015-10-08 02:26:58 +00:00
```console
2015-10-04 07:38:22 +00:00
kubectl delete rc selenium-hub
kubectl delete rc selenium-node-chrome
kubectl delete rc selenium-node-firefox
2016-03-22 18:43:28 +00:00
kubectl delete deployment selenium-python
2015-10-04 07:38:22 +00:00
kubectl delete svc selenium-hub
kubectl delete svc selenium-hub-external
```
2015-10-08 23:49:37 +00:00
<!-- BEGIN MUNGE: GENERATED_ANALYTICS -->
[![Analytics ](https://kubernetes-site.appspot.com/UA-36037335-10/GitHub/examples/selenium/README.md?pixel )]()
<!-- END MUNGE: GENERATED_ANALYTICS -->