Scott Tompkins

Edge Proxy and Load Balancing

Two edge nodes behind one virtual IP received every request from the internet. If the primary failed, the backup took over the address within 3 to 5 seconds.

HAProxy · Nginx · Keepalived · Ansible

A single HAProxy instance would have routed every request correctly. It would also have been a single point of failure: if that one machine died, everything behind it became unreachable. The hard part of this layer was making a node failure fix itself in seconds, without anyone being called.

The edge layer is the set of machines that receive every request from the internet and pass it to the right service. I ran this one on our own hardware, with no cloud load balancer in front of it. I wanted two things from it. Failover had to be automatic and take seconds, not minutes. And only HTTPS was to be exposed publicly.

This page follows one request inward, then follows it again with the primary node dead.

A request arrives at one address

Clients connected to a single IP address. That address did not belong to either machine. It was a virtual IP, shared by a pair of edge nodes and held by one of them at a time.

Keepalived managed the address using VRRP, a protocol that lets two machines agree on which of them answers for a shared address. I chose VRRP because it is simple, well understood and needs no outside coordinator. It requires both nodes to sit on the same local network segment.

Nginx decrypts and HAProxy routes

Inside the node, the request met two programs in turn. Nginx handled TLS termination, which means it decrypted the HTTPS connection. HAProxy then did the load balancing and routing, choosing which backend should receive the request. I split the work this way because each tool is strong at its own job.

Only HTTPS was exposed publicly. Plain HTTP was open for one purpose: Let’s Encrypt, the certificate authority, checks that you control a domain by fetching a file over HTTP, a method called the HTTP-01 challenge.

Some services needed the encrypted connection passed through untouched, so that they could decrypt it themselves. For those I routed on SNI (Server Name Indication), the hostname a client sends in the clear at the start of a TLS connection. That let several services share port 443 by hostname, with no extra ports opened.

A backend has to pass a real check

HAProxy only sent the request to a backend that had passed its health check. The checks tested that the service was available, not only that its port answered, so an unhealthy backend was removed from rotation quickly.

When some backends failed, traffic concentrated on the healthy ones that were left, and alerts fired so that someone could look at capacity.

The same request with the primary node dead

Suppose the primary node had failed its health checks. Keepalived on the backup node took over the virtual IP within 3 to 5 seconds. The backup ran the same Nginx and HAProxy pair, so from there the request took the same path as before.

The client kept connecting to the same IP address throughout. Nothing on the client’s side had to change, and nobody had to step in.

What every request left behind

All edge traffic was logged and forwarded to the central log pipeline, for audit trails and for investigating incidents. When something did happen, the logs showed what traffic had reached the infrastructure and when.

The configuration of both nodes was managed through Ansible, a tool that applies configuration from files kept in version control. Changes were versioned and could be reproduced.

One of the main backends behind this layer was the on-premise Kubernetes platform, and the edge logs were part of what the multi-tenant SIEM monitored.