Tag: load balancing

  • Patronus: I put two web servers behind the router and killed one

    Patronus: I put two web servers behind the router and killed one

    Full disclosure: I make Patronus, a router and firewall OS built on Gentoo. The reverse proxy page is one of the newer things in it. It went into main on November 25 and isn’t in a tagged release yet (the newest tag is v0.2.0).

    The idea is that the router itself can take requests from the internet on port 80 and hand them to servers on your LAN, picking the server by host name and skipping the ones that are down. It runs HAProxy underneath. I wanted to know what a server dying looks like from the outside, so on November 29 I put two web servers behind one name in a lab and killed one. Then I froze one instead of killing it.

    The lab

    A lab, not my house. One container with a handful of network namespaces: the router (the real patronus-web and the apply step, built from main at commit 2e57b8ae, 29 minutes 49 seconds cold in a capped container on a CI host that was already at a load average of 25 to 36, about nine of those minutes spent on package installs and crate downloads before the first compile), a LAN with three tiny Python web servers, a fake ISP and an “internet” host that runs curl against the router’s WAN address. I drove the admin page with a real browser from the LAN side.

    The servers are called wiki-a, wiki-b and notes. Each one answers with its own name and the client address the proxy passed along, and a plain ok on /health. Things that are fake: the host names are under example.test, there’s no real internet and no real load, and the lab’s HAProxy is Debian’s 2.6.12 because that’s what the container had. I didn’t check which version the Gentoo image carries, so timings could differ a little there. The page also shows a “This router has not been set up yet” banner because I never ran the setup wizard in the lab, and the sidebar footer says @PATRONUS_VERSION@ because my dev build never substituted it.

    Publishing one server

    The proxy is off until you use it. Before I touched anything, a request from the internet host to the router’s port 80 sat there until curl gave up (exit 28). The firewall drops it.

    The page has a button, “Publish a LAN service”, and it asks five questions through the browser’s own popup boxes: a name, the server’s LAN address, its port, a host name and the public port. I answered notes, 192.168.50.13, 8080, notes.example.test and 80. The page said published 1.8 seconds after the click in one run and 2.6 in another, and the next curl got an answer.

    The live status card of the reverse proxy page: enabled, installed and running all yes, and a statistics table with a frontend and one backend server, both up
    The status card after publishing. One frontend, one backend, one server.

    That one step made a listener, a backend with one server, a routing rule for the host name and a firewall rule. The rule is iifname "rtr-wan" tcp dport 80 accept with a comment naming the service. HAProxy itself listens on 0.0.0.0:80, so the firewall rule is what keeps it to the WAN side. The server saw the internet host’s address in X-Forwarded-For, so the proxy passes the client address along.

    One thing I didn’t expect. I sent a request for a name nobody published (whatever.example.test) straight to the router’s address, and the notes server answered. A frontend sends everything that matches no rule to its default backend, and publish makes the service you publish the default backend of its listener. The docs say the default backend takes the rest, so it’s behaving as written, but I’d have guessed a 404.

    Two servers, one name

    For the pool I used the forms on the page instead of the button: a backend called pool with an HTTP health check on /health, two servers in it, and a rule sending pool.example.test to it. The backend name in the server and rule forms is a plain text box. I typed it right, and I didn’t try a typo.

    The backend servers table listing notes-1 at 192.168.50.13 and the pool servers wiki-a and wiki-b at 192.168.50.11 and 192.168.50.12, port 8080, with Edit and Delete buttons and an empty add form below
    The servers table, with the add form under it.

    Six requests in a row went wiki-a, wiki-b, wiki-a, wiki-b, wiki-a, wiki-b. The generated config is short. Every server line is check inter 2s rise 2 fall 3: a check every two seconds, two good ones to come up, three bad ones to go down. The defaults block has timeout connect 5s, retries 2 and option redispatch.

    Terminal output: the first request to the published name times out with curl exit 28, then works, a made-up host name is answered by the same server, and six requests to the pool alternate between wiki-a and wiki-b
    From the top: the request before publishing, after publishing, the unknown host name, then the pool.

    Killing one, then freezing one

    I measured both with the same script. The internet host sends ten requests a second for 45 seconds, one curl process per request. At 8 seconds I break wiki-a, at 28 seconds I fix it. First test: kill -9 on the server process. Second test: SIGSTOP, which leaves the process there but silent, the way a hung app or a stuck disk looks. I poll the proxy’s own statistics every quarter second to see when it notices.

    FaultRequestsNo answerDown after
    killed42606.1 s
    frozen420510.4 s

    The killed server first, and no request failed. A killed process refuses connections at once, and HAProxy retries a refused connection on another server, so every one of the 426 requests came back 200 and the slowest took 56 milliseconds. That includes the six seconds before the health checks caught up. For the 20 seconds wiki-a was gone, all 194 requests went to wiki-b. After the restart it took four seconds to come back, and then traffic spread again: 58 requests to wiki-a and 96 to wiki-b in the last 17 seconds.

    Terminal output: the wiki-a process is killed and twelve requests over ten seconds are all answered by wiki-b
    The terminal log from the start of that session. The kill and the twelve requests are at the bottom, one request every half second. A separate run from the table.

    The frozen server went worse. The first five requests after the freeze got no answer at all before curl’s ten second limit. After that, 47 requests came back from wiki-b but each took 5.0 seconds, which is the backend connect timeout, a setting on the page that defaults to 5. It took 10.4 seconds for the checks to mark wiki-a down, and only then did the delays stop. Over the whole run the median was 10 milliseconds and the 95th percentile was 5.01 seconds.

    My guess at the mechanism: the kernel keeps accepting connections for a stopped process until its listen queue fills, and after that the connections just hang, so HAProxy waits out the connect timeout before trying the other server. The five that got nothing probably used up their retries on the frozen one. I didn’t confirm either in HAProxy’s logs.

    The live statistics table with be_pool wiki-a marked DOWN after 9 sessions in total and wiki-b UP after 15
    The statistics table after the kill. wiki-a is DOWN with 9 requests in total; wiki-b has 15.

    The statistics table is HAProxy’s own numbers. I pressed “Live statistics” to refresh it each time. Reading the page’s script I don’t see a timer, so I think it doesn’t update by itself, but I didn’t leave it open to check.

    The clip below is the kill and the restart from another run of the same lab, 44 seconds, real time. Router page on top, terminal underneath. The two panes were recorded separately, so don’t read anything into sub-second timing. After the loop ends I press the statistics button, which is when the table on top changes.

    wiki-a killed, requests keep working, wiki-a started again, traffic spreads. Top: router page. Bottom: terminal.

    Changes the page should refuse

    Two attempts. First I added a listener on port 8443, which is where the admin page lives in my lab. The page refused it and said why: the web UI already uses that port, move it or pick another. Then I added a TLS listener pointing at a certificate file that doesn’t exist. Patronus writes the new config as a candidate, runs haproxy -c on it and only swaps it in if that passes. The page showed HAProxy’s own error and said the running proxy was left alone. After the first refusal I checked that the pool still answered, and it did. After the second I didn’t, so for that one I only have the page’s word.

    A red message above the settings card reading Not applied: Invalid Frontend admin-clash wants port 8443 on 0.0.0.0, which the Patronus web UI already uses, with advice to move the web UI or pick another port
    A listener on the admin port is refused.
    A red message reading Not applied: Invalid reverse proxy configuration, haproxy -c refused it and the running proxy was left alone, followed by haproxy's own alert lines about a missing certificate file
    A missing certificate file. The red box is HAProxy’s raw output.

    What the docs say about the design

    From docs/REVERSE-PROXY.md: publishing creates the listener, the backend, the rule and the firewall rule together, a config that HAProxy refuses is undone, and the statistics come from HAProxy’s local admin socket with nothing exposed on the network. The page isn’t in the small embedded router images because of flash size, and the System page says so. The parity table in the repo lists the HAProxy packages that pfSense and OPNsense ship as the thing being matched, and lists what’s still missing: certificates from an ACME manager, TLS to the backend with verification, the PROXY protocol, rate limiting and a WAF.

    The docs don’t say why HAProxy and not nginx or Caddy, beyond that parity row.

    What doesn’t work (yet)

    The frozen server is the main one. With the defaults, a hung backend costs about ten seconds of five second waits, and some requests get nothing. The knobs are on the page (connect timeout, check interval, how many bad checks), but nothing on the page tells you the defaults behave like this, and I didn’t test other values. I’d start by trying a shorter connect timeout and see what it does to the numbers above.

    Publish gives its backend a plain TCP check, not an HTTP one. The generated config for notes has no httpchk line. So an app that accepts connections but answers every request with an error stays marked up. I read that in the config and didn’t test an app returning errors. The forms let you pick an HTTP check and a path, which is what I did for the pool.

    The catch-all from earlier matters more on a router that faces the internet: a scanner that sends an odd host name gets whatever you published first.

    Smaller things. After pressing “Live statistics” the Actions card fills with the raw JSON from HAProxy, a long block of it, above the table that is actually readable. The breadcrumb says “Home / Svc-revproxy”. The enable checkboxes float away from their labels. The five publish questions are browser popups. The missing certificate error is HAProxy’s alert lines pasted into one red box. On a 390 pixel phone the page itself doesn’t scroll sideways, but the statistics table is wider than the screen and scrolls inside its card, so the Sessions column and the ones after it are off to the right.

    The live status card on a 390 pixel wide phone with the statistics table cut off at the Sessions column
    The statistics table on a phone. It scrolls sideways inside the card.

    Not tested: a TLS listener that serves traffic, tcp mode, path rules, sticky cookies, other balancing modes, backup servers, a reboot, IPv6, real load, anything longer than a few minutes. I ran no unit tests and none of the repo’s netlab lanes this time. The docs list a netlab lane for the proxy and a Firefox test of the page, but I haven’t run either.

    Where it stands

    In the plain case it works in the lab: a dead server costs the visitor nothing. A hung one costs seconds and a few failed requests until the checks catch up, and I’d want that better before anything I care about sits behind it.

    Next: the same test with a shorter connect timeout and a faster check, to see what the best case is, and a TLS listener with a real certificate.

    Patronus lives at patronus.errant.solutions.