Blue-green deployment at HAProxy is two backends — be_blue and be_green — and a frontend that splits traffic between them by weight. You stand up the green release, send 1% of traffic to it, watch metrics, ramp to 10%, 50%, 100%, then decommission blue. This article covers the config patterns, the runtime API choreography, and the rollback we always wire before the first cutover.
How to verify
For an existing blue-green setup, the runtime API tells you the current weights and which backend a request landed on:
echo "show servers state be_app" | sudo socat /run/haproxy/admin.sock -
echo "show stat" | sudo socat /run/haproxy/admin.sock - | grep -E 'be_(blue|green)'
for i in $(seq 1 100); do curl -s https://api.example.com/version; done | sort | uniq -c
The curl ... uniq -c loop hits the /version endpoint 100 times. Server identity in the response (or a X-Version header you wire from each backend) tells you the actual split observed by clients.
The procedure
-
Two backends, one frontend. The blue and green backends share an identical shape; only the server addresses differ:
backend be_blue balance roundrobin option httpchk http-check send meth GET uri /healthz http-check expect status 200 server blue1 10.0.1.11:8080 check inter 2s fall 3 rise 2 server blue2 10.0.1.12:8080 check inter 2s fall 3 rise 2 backend be_green balance roundrobin option httpchk http-check send meth GET uri /healthz http-check expect status 200 server green1 10.0.2.11:8080 check inter 2s fall 3 rise 2 server green2 10.0.2.12:8080 check inter 2s fall 3 rise 2The frontend uses
use_backendwith a weighted condition or a simplerbalanceapproach via a meta-backend. -
Split with a fanout backend. The cleanest pattern: a thin “router” backend whose
serverentries point at the two real backends:frontend fe_http bind *:443 ssl crt /etc/haproxy/certs/api.example.com.pem default_backend be_router backend be_router balance roundrobin server blue 127.0.0.1:8001 weight 100 server green 127.0.0.1:8002 weight 0Then run two
listenblocks that internally proxy to the real backends:listen lis_blue bind 127.0.0.1:8001 server-template blue 2 be_blue/_ resolvers haproxy_resolvers init-addr none listen lis_green bind 127.0.0.1:8002 server-template green 2 be_green/_ resolvers haproxy_resolvers init-addr noneThis adds one local hop but makes weight changes a single runtime command.
-
Simpler pattern with
use_backendand a stick-table sample. Use an ACL based on a stick-table that returns a pseudo-random server index per request, weighted:frontend fe_http bind *:443 ssl crt /etc/haproxy/certs/api.example.com.pem acl is_green rand(100) lt 10 use_backend be_green if is_green default_backend be_bluerand(100) lt 10means 10% of requests go to green. Change10to50to do a 50/50 split. This requires a config reload to change weights — for runtime change, prefer the fanout pattern above. -
Runtime weight shift. With the fanout pattern, ramping is a series of runtime commands:
echo "set server be_router/green weight 10" | sudo socat /run/haproxy/admin.sock - sleep 300 echo "set server be_router/green weight 50" | sudo socat /run/haproxy/admin.sock - sleep 600 echo "set server be_router/green weight 100" | sudo socat /run/haproxy/admin.sock - echo "set server be_router/blue weight 0" | sudo socat /run/haproxy/admin.sock -At weight 100 for green and 0 for blue, all new requests go to green. Existing connections to blue finish normally. The rollback is one command:
set server be_router/blue weight 100; set server be_router/green weight 0. -
Health-gated promotion. Tie the next weight bump to a metric query:
ramp_to () { new=$1 err=$(curl -s 'http://prometheus/api/v1/query?query=...' | jq -r '.data.result[0].value[1]') if [ "$err" \< "0.001" ]; then echo "set server be_router/green weight $new" | sudo socat /run/haproxy/admin.sock - else echo "set server be_router/green weight 0" | sudo socat /run/haproxy/admin.sock - exit 1 fi } ramp_to 10 && sleep 300 && ramp_to 50 && sleep 600 && ramp_to 100If error rate exceeds the threshold, the script rolls back to 0 and exits non-zero.
-
Header-based opt-in for staff. Before the percentage rollout, let internal users opt in by header:
frontend fe_http bind *:443 ssl crt /etc/haproxy/certs/api.example.com.pem acl is_staff hdr(x-staff-build) -i green use_backend be_green if is_staff default_backend be_blueStaff hit a browser extension or curl alias setting
X-Staff-Build: green; they get the green release while production stays on blue. -
Decommission blue. Once green is at 100% and stable for a defined window (a day, a week), tear blue down. We keep blue’s config in the file with
weight 0and the servers in MAINT until the watch period ends, then remove thebe_bluesection in a config commit and reload.
Common pitfalls
- A blue-green setup with stateful sessions (uploaded files, session affinity) breaks if the user lands on green for one request and blue for the next. Use sticky sessions inside the router backend, or use a shared session store.
set server weightdoes not migrate existing connections — only new connections are affected. Long-poll and WebSocket clients on blue stay on blue until they reconnect.- Database schema migrations under blue-green: green’s schema must be backward-compatible with blue’s code for the duration of the ramp. Plan additive-only migrations during the split.
- Weight changes via runtime API are not persisted. A reload returns weights to what the config says. Either edit the config (
weight 100on green,weight 0on blue) before reloading, or use thedata-plane APIwhich writes to disk. - Runtime weight 0 means “no new sessions”; weight 1 still routes some traffic. Use 0, not 1, to fully drain blue.
Stack Harbor wires blue-green into the deployment pipeline for clients running zero-downtime services — fanout backend, runtime API ramping gated by metric queries, automatic rollback on threshold breach, decommission step in the next maintenance window. This is part of how we operate Clustered Environments and what makes lunchtime deploys boring rather than scary.