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
+141
View File
@@ -61,6 +61,31 @@ let
};
} overrides;
# A LAN with two reserved hosts, and a service on them.
lbLan = lan {
dhcp.reservations = {
hv1 = {
mac = "02:00:00:00:00:21";
address = "192.168.10.21";
};
hv2 = {
mac = "02:00:00:00:00:22";
address = "192.168.10.22";
};
};
};
lb =
overrides:
lib.recursiveUpdate {
listen.port = 8443;
certificate = "/var/lib/router/lb.pem";
backends = [
"hv1"
"hv2"
];
backendTls.enable = true;
} overrides;
failed = router: map (a: a.message) (lib.filter (a: !a.assertion) (evalRouter router).assertions);
cases = {
@@ -704,6 +729,121 @@ let
};
expect = [ ];
};
lbValid = {
router = {
lans.main = lbLan;
loadBalancers.api = lb { };
# Two services share a port when the host name tells them apart.
loadBalancers.a = lb {
listen.port = 9443;
hosts = [ "a.example.org" ];
};
loadBalancers.b = lb {
listen.port = 9443;
hosts = [ "b.example.org" ];
};
};
expect = [ ];
};
lbUnknownBackend = {
router = {
lans.main = lbLan;
loadBalancers.api = lb { backends = [ "hv1" "nothere" ]; };
};
expect = [ "`nothere` is neither a DHCP reservation nor an IPv4 address" ];
};
lbNoBackends = {
router = {
lans.main = lbLan;
loadBalancers.api = lb { backends = lib.mkForce [ ]; };
};
expect = [ "at least one backend" ];
};
lbUnknownZone = {
router = {
lans.main = lbLan;
loadBalancers.api = lb { listen.zones = [ "dmz" ]; };
};
expect = [ "listen.zones: unknown zone" ];
};
lbPortOfTheRouter = {
router = {
lans.main = lbLan;
loadBalancers.api = lb { listen.port = 22; };
};
expect = [ "is used by the router itself" ];
};
lbPortForwarded = {
router = {
lans.main = lbLan;
loadBalancers.api = lb { listen.port = 8443; };
firewall.nat = [
{
type = "dnat";
from = "wan";
to = "main";
protocol = "tcp";
ports = [ 8443 ];
toAddress = "192.168.10.50";
}
];
};
expect = [ "is also forwarded by router.firewall.nat" ];
};
lbSharedPortWithoutHosts = {
router = {
lans.main = lbLan;
loadBalancers.a = lb { };
loadBalancers.b = lb { hosts = [ "b.example.org" ]; };
};
expect = [
"port 8443 is shared with b: every service on it needs `hosts`"
"port 8443 is shared with a: every service on it needs `hosts`"
];
};
lbSharedPortDisagrees = {
router = {
lans.main = lbLan;
loadBalancers.a = lb { hosts = [ "a.example.org" ]; };
loadBalancers.b = lb {
hosts = [ "b.example.org" ];
tls = "none";
certificate = lib.mkForce null;
};
};
expect = [
"must agree on tls and protocol"
"must agree on tls and protocol"
];
};
lbPassthroughNeedsTcp = {
router = {
lans.main = lbLan;
loadBalancers.api = lb {
tls = "passthrough";
protocol = "http";
certificate = lib.mkForce null;
};
};
expect = [ "passthrough only forwards bytes" ];
};
lbTerminateWithoutCertificate = {
router = {
lans.main = lbLan;
loadBalancers.api = lb { certificate = lib.mkForce null; };
};
expect = [ "has no `certificate` and router.acme is not enabled" ];
};
lbVerifyNeedsTls = {
router = {
lans.main = lbLan;
loadBalancers.api = lb {
backendTls.enable = lib.mkForce false;
backendTls.verify = true;
};
};
expect = [ "backendTls.verify needs backendTls.enable" ];
};
wgNoPeers = {
router = {
lans.main = lan { };
@@ -1589,6 +1729,7 @@ let
"ulaValid"
"wgSourceOk"
"dnsValid"
"lbValid"
];
subnet =