Watch
0
0
Fork
You've already forked hyperhive
0

k8s: the queue's exporter is a ClusterIP service only the node reaches

The exporter's metrics port was a hostPort on the bridge's address, which Pod Security baseline and restricted forbid. It is a ClusterIP Service now, and the pod has a NetworkPolicy that admits the client port from any source and nothing else, so only the node reads the exporter, as the raw-table rules made it. The gateway's /metrics location dials the Service by name through nginx's resolver. The raw-table rules no longer name the exporter's port.
This commit is contained in:
atlas 2026-11-23 18:02:25 +01:00 • committed by mara
commit ead0ef8719
2 changed files with 228 additions and 63 deletions

View file

@ -1,4 +1,4 @@
# The iptables rules that give the queue pod's `hostPort`s the reach the
# The iptables rules that give the queue pod's client `hostPort` the reach the
# queue's container has. A `hostPort` is a DNAT rule, and what a DNAT rule
# rewrites is forwarded, never delivered to the host: the INPUT rules (the
# firewall's `allowedTCPPorts` and per-interface rules) never see it, and the
@ -9,8 +9,7 @@
# The client port is dropped for a packet addressed to the host unless it
# arrives on one of `trusted` (the host's loopback, the hive bridge and the
# pods' interfaces), or `openClient` is set, which is
# `deploy.nats.openFirewall`. The exporter's port answers with every client's
# name, so it is dropped for all but the host's own loopback.
# `deploy.nats.openFirewall`.
#
# Pure function — NOT a NixOS module. Returns the text for the firewall's
# `extraCommands`.
@ -18,7 +17,6 @@
{
chain,
clientPort,
metricsPort,
trusted,
openClient,
}:
@ -28,8 +26,6 @@ let
in
''
iptables -t raw -N ${chain} 2>/dev/null || iptables -t raw -F ${chain}
${rule} -p tcp --dport ${toString metricsPort} -i lo -j RETURN
${rule} -p tcp --dport ${toString metricsPort} ${local}
${lib.optionalString (!openClient) ''
${lib.concatMapStrings (i: ''
${rule} -p tcp --dport ${toString clientPort} -i ${i} -j RETURN

View file

@ -354,6 +354,11 @@ let
# host's cluster, which ./k8s/render.nix renders from the units below.
onK8s = deployCfg.nats.runtime == "k8s";
# The Service in front of the pod's exporter, and the name the host's resolver
# answers for it through the cluster's DNS.
metricsService = "${machine}-metrics";
metricsHost = "${metricsService}.${config.services.hyperhive.k8s.namespace}.svc.${deployCfg.k8s.clusterDomain}";
# The server's monitoring endpoint, which the exporter reads, and the
# collectors it serves.
monitorUrl = "http://127.0.0.1:${toString deployCfg.nats.monitorPort}";
@ -1027,15 +1032,11 @@ let
]
++ exporterFlags
++ [ monitorUrl ];
# On the bridge's address, for the host's gateway: the exporter
# answers a request with every client's name, so the host's firewall
# lets only the host's own processes reach it.
# The port the metrics Service targets.
ports = [
{
name = "metrics";
containerPort = deployCfg.nats.metricsPort;
hostPort = deployCfg.nats.metricsPort;
hostIP = networkCfg.bridgeIp;
}
];
inherit securityContext;
@ -1083,6 +1084,50 @@ let
};
};
# The exporter answers a request with every client's name, so only the
# node may read it: the Service is reached by name from the host's
# gateway, and the policy admits the node's own traffic (which the
# cluster's policy engine always lets in, ./k8s/render.nix) and, from
# anywhere else, the client port alone.
objects."${machine}/service" = {
apiVersion = "v1";
kind = "Service";
metadata = metaFor machine // {
name = metricsService;
};
spec = {
type = "ClusterIP";
selector = (metaFor machine).labels;
ports = [
{
name = "metrics";
protocol = "TCP";
port = deployCfg.nats.metricsPort;
targetPort = "metrics";
}
];
};
};
objects."${machine}/networkpolicy" = {
apiVersion = "networking.k8s.io/v1";
kind = "NetworkPolicy";
metadata = metaFor machine;
spec = {
podSelector.matchLabels = (metaFor machine).labels;
policyTypes = [ "Ingress" ];
ingress = [
{
ports = [
{
protocol = "TCP";
port = cfg.port;
}
];
}
];
};
};
images = [
init
server
@ -1138,6 +1183,10 @@ in
{option}`services.hyperhive.swarm.nats.domain` proxies it, behind
the swarm collector's authelia check.
With the queue in the cluster this is the exporter container's port in
the pod, which the Service `swarm-nats-metrics` publishes on the same
number and only the node reaches.
⚠️ This and {option}`services.hyperhive.deploy.nats.monitorPort` are two
more claims on a port
space every swarm container shares — they run in this container but
@ -1379,14 +1428,24 @@ in
# Authelia's rules for it admit the collector's client and deny
# every other subject (`metricsRules` in ./swarm-authelia.nix).
"= /metrics" = {
# In the cluster the exporter is a container of the pod, published on
# the bridge's address.
proxyPass = "http://${
if onK8s then networkCfg.bridgeIp else "127.0.0.1"
}:${toString deployCfg.nats.metricsPort}/metrics";
extraConfig = ''
auth_request /__nats_metrics_authz;
'';
# In the cluster the exporter is behind a Service, which the host's
# resolver answers through the cluster's DNS. The upstream is a
# variable, so nginx asks the resolver per request: a name in the
# `proxy_pass` gets one lookup when the configuration loads, which
# fails the load while the cluster is down.
proxyPass =
if onK8s then
"http://\$swarm_nats_metrics_upstream/metrics"
else
"http://127.0.0.1:${toString deployCfg.nats.metricsPort}/metrics";
extraConfig =
lib.optionalString onK8s ''
resolver ${networkCfg.bridgeIp} valid=10s ipv6=off;
set $swarm_nats_metrics_upstream ${metricsHost}:${toString deployCfg.nats.metricsPort};
''
+ ''
auth_request /__nats_metrics_authz;
'';
};
# The subrequest, same shape as the forge's `= /__forge_metrics_authz`
@ -1470,6 +1529,18 @@ in
this host, or leave the queue's runtime at "host".
'';
}
{
# The gateway's `/metrics` location dials the exporter's Service by
# name, which only the hive's resolver answers.
assertion = !onK8s || config.services.hyperhive.gateway.dns.enable;
message = ''
services.hyperhive.deploy.nats.runtime = "k8s" needs
services.hyperhive.gateway.dns.enable: the gateway dials the pod's
metrics Service by name, and the hive's resolver is what hands the
cluster's domain to its DNS. Set it, or leave the queue's runtime at
"host".
'';
}
{
# The callout xkey and the responder's OIDC client secret are read
# from the store under this identity; without either the server or
@ -1544,14 +1615,13 @@ in
# sets no listen host), so reachability is the firewall alone.
networking.firewall.allowedTCPPorts = lib.mkIf deployCfg.nats.openFirewall [ cfg.port ];
# The pod's `hostPort`s are DNAT rules, which the INPUT rules above never
# see: ./lib/swarm-nats-raw-rules.nix gives them the container's reach.
# The pod's client `hostPort` is a DNAT rule, which the INPUT rules above
# never see: ./lib/swarm-nats-raw-rules.nix gives it the container's reach.
services.hyperhive.network.enable = lib.mkIf onK8s (lib.mkDefault true);
networking.firewall.extraCommands = lib.mkIf onK8s (
import ./lib/swarm-nats-raw-rules.nix { inherit lib; } {
chain = "nixos-fw-nats";
clientPort = cfg.port;
metricsPort = deployCfg.nats.metricsPort;
trusted = [
"lo"
networkCfg.bridgeName