Part 84: Increase the availability of your Selenium with HAProxy

What's up, home? part 84 cover image

Who wouldn't need a HAProxy in front of their Selenium instances at home? I guess nobody would NEED it but here we are.

I had my Zabbix Selenium tests running on my personal Mac. The trouble is/was that if I took my Mac away from home, or did take it offline for any reason, my tests broke. That. Is. Not. Good.

HAProxy to the rescue!

I noticed that HAProxy (or if you belong to The Suits and love the business lingo with pretty pictures, impressive slogans and lots of words but no actual content, here's your link) is available as an entware package for my Asus routers' Asuswrt-Merlin custom firmware.

What is HAProxy, other than "Introducing our next-generation HAProxy Enterprise WAF and HAProxy Enterprise Bot Management Module for unmatched security, performance, and reliability in a simple package. New UDP load balancing further extends HAProxy's legendary performance and flexibility." like it is described on its paid version site?

Simply put, it's a load-balancer sitting between your application and some backend. In this case, my Zabbix is pointed to my HAProxy, which then directs that traffic to any of my configured Selenium instances -- for now, my personal Mac and my Linux laptop. If any of the backend nodes would not be responding, it will be removed from the pool automatically.  So, I take my personal Mac with me when I go out, my Selenium tests would happily continue running through the Linux laptop.

HAProxy statistics

 

Awesome! But this must be hard to configure?

Not really. In case of Asus, ssh to router, and run

opkg install haproxy

Then the relevant config snippet in haproxy.cfg file for this experiment. As this is just one of my late-evening quick tries, there could be glaring issues but at least this seems to work.

# Selenium HTTP listener
listen selenium
       bind :8404
       mode http
       balance roundrobin 
       cookie SERVERID insert indirect nocache
       server personalmac 192.168.50.199:4444 check cookie personalmac 
       server linux 192.168.50.29:4444 check cookie linux
       stats enable
       stats uri /stats

What does it do, or I think it does? 

  • setup a new HAProxy listener called selenium
  • bind it to port :8404 and make it a http mode listener (instead of tcp or any other) -- that is where my zabbix_server.config file is pointing to
  • use round-robin load-balancing, so every other HTTP request goes to another Selenium instance
  • not to confuse Selenium though, inject a cookie with the backend server name; without that, the sessions would not be sticky, and every other HTTP request would go to different Selenium. That would confuse it, as Selenium would think that the page is broken, as it would need to attempt load page elements it thinks it should not load
  • enable some statistics for monitoring

At least it must be hard to monitor with Zabbix?

No, of course not, it's Zabbix. There's a HTTP based template and Zabbix agent based template for that. Add it, and soon enough you'll see all kinds of metrics about your HAProxy.

HAProxy Zabbix latest data

Next I need to build a fancy dashboard for this, but that's for another time. Zabbix HAProxy template actually comes with some pre-built dashboards, but the thing is that my HAProxy gets so little traffic that much of the metrics don't even get any worthwhile values... :D

Bundled HAProxy dashboards

And yes, I do acknowledge that Selenium itself would have some clustering features available, but this was very fast and easy to setup. 

Add new comment

Restricted HTML

  • Allowed HTML tags: <a href hreflang> <em> <strong> <cite> <blockquote cite> <code> <ul type> <ol start type> <li> <dl> <dt> <dd> <h2 id> <h3 id> <h4 id> <h5 id> <h6 id>
  • Lines and paragraphs break automatically.
  • Web page addresses and email addresses turn into links automatically.
Buy me a coffee

Like these posts? Support the project and Buy me a coffee