github.com/qsunny/k8s@v0.0.0-20220101153623-e6dca256d5bf/examples-master/staging/selenium/README.md (about) 1 ## Selenium on Kubernetes 2 3 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. 4 5 ### Prerequisites 6 7 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. 8 9 Google Container Engine is also a quick way to get Kubernetes up and running: https://cloud.google.com/container-engine/ 10 11 Your cluster must have 4 CPU and 6 GB of RAM to complete the example up to the scaling portion. 12 13 ### Deploy Selenium Grid Hub: 14 15 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: 16 17 ```console 18 kubectl create --filename=staging/selenium/selenium-hub-deployment.yaml 19 ``` 20 21 The Selenium Nodes will need to know how to get to the Hub, let's create a service for the nodes to connect to. 22 23 ```console 24 kubectl create --filename=staging/selenium/selenium-hub-svc.yaml 25 ``` 26 27 ### Verify Selenium Hub Deployment 28 29 Let's verify our deployment of Selenium hub by connecting to the web console. 30 31 #### Kubernetes Nodes Reachable 32 33 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: 34 35 ```console 36 export NODEPORT=`kubectl get svc --selector='app=selenium-hub' --output=template --template="{{ with index .items 0}}{{with index .spec.ports 0 }}{{.nodePort}}{{end}}{{end}}"` 37 export NODE=`kubectl get nodes --output=template --template="{{with index .items 0 }}{{.metadata.name}}{{end}}"` 38 39 curl http://$NODE:$NODEPORT 40 ``` 41 42 #### Kubernetes Nodes Unreachable 43 44 If you cannot reach your Kubernetes nodes from your network, you can proxy via kubectl. 45 46 ```console 47 export PODNAME=`kubectl get pods --selector="app=selenium-hub" --output=template --template="{{with index .items 0}}{{.metadata.name}}{{end}}"` 48 kubectl port-forward $PODNAME 4444:4444 49 ``` 50 51 In a separate terminal, you can now check the status. 52 53 ```console 54 curl http://localhost:4444 55 ``` 56 57 #### Using Google Container Engine 58 59 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: 60 61 ```console 62 kubectl expose deployment selenium-hub --name=selenium-hub-external --labels="app=selenium-hub,external=true" --type=LoadBalancer 63 ``` 64 65 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. 66 67 ```console 68 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}}"` 69 70 curl http://$INTERNET_IP:4444/ 71 ``` 72 73 You should now be able to hit `$INTERNET_IP` via your web browser, and so can everyone else on the Internet! 74 75 ### Deploy Firefox and Chrome Nodes: 76 77 Now that the Hub is up, we can deploy workers. 78 79 This will deploy 2 Chrome nodes. 80 81 ```console 82 kubectl create --filename=staging/selenium/selenium-node-chrome-deployment.yaml 83 ``` 84 85 And 2 Firefox nodes to match. 86 87 ```console 88 kubectl create --filename=staging/selenium/selenium-node-firefox-deployment.yaml 89 ``` 90 91 Once the pods start, you will see them show up in the Selenium Hub interface. 92 93 ### Run a Selenium Job 94 95 Let's run a quick Selenium job to validate our setup. 96 97 #### Setup Python Environment 98 99 First, we need to start a python container that we can attach to. 100 101 ```console 102 kubectl run selenium-python --image=google/python-hello 103 ``` 104 105 Next, we need to get inside this container. 106 107 ```console 108 export PODNAME=`kubectl get pods --selector="run=selenium-python" --output=template --template="{{with index .items 0}}{{.metadata.name}}{{end}}"` 109 kubectl exec --stdin=true --tty=true $PODNAME bash 110 ``` 111 112 Once inside, we need to install the Selenium library 113 114 ```console 115 pip install selenium 116 ``` 117 118 #### Run Selenium Job with Python 119 120 We're all set up, start the python interpreter. 121 122 ```console 123 python 124 ``` 125 126 And paste in the contents of selenium-test.py. 127 128 ```python 129 from selenium import webdriver 130 from selenium.webdriver.common.desired_capabilities import DesiredCapabilities 131 132 def check_browser(browser): 133 driver = webdriver.Remote( 134 command_executor='http://selenium-hub:4444/wd/hub', 135 desired_capabilities=getattr(DesiredCapabilities, browser) 136 ) 137 driver.get("http://google.com") 138 assert "google" in driver.page_source 139 driver.quit() 140 print("Browser %s checks out!" % browser) 141 142 143 check_browser("FIREFOX") 144 check_browser("CHROME") 145 ``` 146 147 You should get 148 149 ``` 150 >>> check_browser("FIREFOX") 151 Browser FIREFOX checks out! 152 >>> check_browser("CHROME") 153 Browser CHROME checks out! 154 ``` 155 156 Congratulations, your Selenium Hub is up, with Firefox and Chrome nodes! 157 158 ### Scale your Firefox and Chrome nodes. 159 160 If you need more Firefox or Chrome nodes, your hardware is the limit: 161 162 ```console 163 kubectl scale deployment selenium-node-firefox --replicas=10 164 kubectl scale deployment selenium-node-chrome --replicas=10 165 ``` 166 167 You now have 10 Firefox and 10 Chrome nodes, happy Seleniuming! 168 169 ### Debugging 170 171 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. 172 173 ```console 174 kubectl port-forward $POD_NAME 5900:5900 175 ``` 176 177 Then connect to localhost:5900 with your VNC client using the password "secret" 178 179 Enjoy your scalable Selenium Grid! 180 181 Adapted from: https://github.com/SeleniumHQ/docker-selenium 182 183 ### Teardown 184 185 To remove all created resources, run the following: 186 187 ```console 188 kubectl delete deployment selenium-hub 189 kubectl delete deployment selenium-node-chrome 190 kubectl delete deployment selenium-node-firefox 191 kubectl delete deployment selenium-python 192 kubectl delete svc selenium-hub 193 kubectl delete svc selenium-hub-external 194 ``` 195 196 197 <!-- BEGIN MUNGE: GENERATED_ANALYTICS --> 198 []() 199 <!-- END MUNGE: GENERATED_ANALYTICS -->