Load balancers

router.loadBalancers: services on several hosts (HAProxy) with HTTP
checks, TLS terminated on the router or passed through, the client in
X-Forwarded-For or the PROXY protocol, host names that choose a service
on a shared port and are published in the local DNS. A status page lists
the backends.
This commit is contained in:
2026-11-27 20:54:45 +01:00
parent 2aa70630d4
commit 553361f811
16 changed files with 1349 additions and 3 deletions
+218
View File
@@ -426,6 +426,224 @@ in
);
};
loadBalancers = mkOption {
default = { };
example = {
api = {
listen.port = 8443;
listen.zones = [
"wan"
"lan"
];
tls = "terminate";
hosts = [ "api.example.org" ];
backends = [
"hv1"
"hv2"
];
backendTls.enable = true;
check.path = "/healthz";
};
};
description = ''
Services that spread their connections over several hosts (HAProxy),
keyed by name. A backend that fails its checks gets no connections
until it passes them again. The service listens on the router, in the
zones named in `listen.zones` (`wan` is the uplink): the router opens
the port there, so no firewall rule is needed. Clients inside reach it
through the router's address; `hosts` also publishes those names in the
local DNS (to the router's LAN addresses), so that inside and outside
use the same name. Backends are names of DHCP reservations or
IPv4 addresses.
A service either decrypts TLS on the router (`tls = "terminate"`) or
passes the connections through untouched (`"passthrough"`); the
backend then sees the router's address, unless `proxyProtocol` hands
it the client's. For HTTP the client's address is in `X-Forwarded-For`.
'';
type = types.attrsOf (
types.submodule (
{ config, ... }:
{
options = {
listen = {
port = mkOption {
type = types.port;
example = 443;
description = ''
TCP port the service listens on, in every zone of `zones`.
Services may share a port when each has `hosts` and they
agree on `tls` and `protocol` (the host name chooses).
'';
};
zones = mkOption {
type = types.listOf types.str;
default = [ "wan" ];
example = [
"wan"
"lan"
];
description = ''
Where the service is reachable: `wan` (the uplink) and the
zones of LANs and tunnels.
'';
};
};
tls = mkOption {
type = types.enum [
"terminate"
"passthrough"
"none"
];
default = "terminate";
description = ''
`terminate`: the router decrypts (its certificate: see
`certificate`) and sends on to the backends, encrypted again
if `backendTls.enable`. `passthrough`: the connection goes to
the backend as it is; the router only reads the requested
host name, and the backend has the certificate. `none`: no
TLS between the client and the router.
'';
};
protocol = mkOption {
type = types.enum [
"http"
"tcp"
];
default = if config.tls == "passthrough" then "tcp" else "http";
defaultText = ''"tcp" with passthrough, else "http"'';
description = ''
`http`: the router reads the requests (adds `X-Forwarded-For`,
routes by `Host`). `tcp`: bytes only. Passthrough is always `tcp`.
'';
};
certificate = mkOption {
type = types.nullOr types.path;
default = null;
example = "/var/lib/router/lb-api.pem";
description = ''
`terminate`: a file with the certificate chain and the
private key (PEM). null: the router's own certificate
(`router.acme`), which then has to cover `hosts`.
'';
};
hosts = mkOption {
type = types.listOf types.str;
default = [ ];
example = [ "api.example.org" ];
description = ''
The names the service answers to: the request's `Host` (or the
requested TLS name) chooses the service on a shared port, and
a name that is not on a shared port still has to match. The
names are published in the local DNS for the LANs the service
listens in. Empty: the service takes everything on its port.
'';
};
backends = mkOption {
type = types.listOf types.str;
example = [
"hv1"
"hv2"
];
description = "Names of DHCP reservations, or IPv4 addresses.";
};
backendPort = mkOption {
type = types.nullOr types.port;
default = null;
description = "The port on the backends; null: the same as `listen.port`.";
};
backendTls = {
enable = mkOption {
type = types.bool;
default = config.tls == "passthrough";
defaultText = "true with passthrough, else false";
description = ''
The backends speak TLS. With `terminate` or `none` the
router encrypts toward them; with `passthrough` it only
makes its health checks encrypted.
'';
};
verify = mkOption {
type = types.bool;
default = false;
description = ''
Check the backends' certificates (against `caFile`, or the
system's) when connecting to them and when checking them.
Off by default: backends often have a self-signed one.
'';
};
caFile = mkOption {
type = types.nullOr types.path;
default = null;
description = "Certificate authority of the backends, for `verify`.";
};
};
balance = mkOption {
type = types.enum [
"roundrobin"
"leastconn"
"source"
];
default = "roundrobin";
description = ''
How a new connection picks a backend: in turn, the one with
the fewest connections, or by the client's address (the same
client keeps its backend while the backends stay as they are).
'';
};
proxyProtocol = mkOption {
type = types.bool;
default = false;
description = ''
Send the client's address to the backends in a PROXY protocol
(v2) header, for passthrough or `tcp`. The backends must expect
it, or every connection fails.
'';
};
check = {
path = mkOption {
type = types.str;
default = "/";
example = "/healthz";
description = ''
An HTTP GET of this path decides whether a backend is up
(a 2xx or 3xx answer), also for TLS backends and in
passthrough. With the first of `hosts` as `Host`, else
`localhost`.
'';
};
useHttp = mkOption {
type = types.bool;
default = true;
description = "Check with an HTTP request; false: only that the port accepts connections.";
};
interval = mkOption {
type = types.strMatching "[0-9]+(ms|s|m)";
default = "2s";
description = "Time between two checks of a backend.";
};
timeout = mkOption {
type = types.strMatching "[0-9]+(ms|s|m)";
default = "2s";
description = "How long a check may take before it counts as failed.";
};
fall = mkOption {
type = types.ints.positive;
default = 3;
description = "Failed checks in a row that take a backend out.";
};
rise = mkOption {
type = types.ints.positive;
default = 2;
description = "Passed checks in a row that bring it back.";
};
};
};
}
)
);
};
wireguardEndpointRefresh = mkOption {
type = types.nullOr (types.strMatching "[0-9]+(s|min|h)");
default = "1min";