k8s: the queue's client port is a loadbalancer service
The queue's client port was a hostPort on 0.0.0.0, which Pod Security baseline and restricted forbid. It is a LoadBalancer Service now, which k3s's service load balancer binds on every address of the node with a hostPort of its own in kube-system, so hives on other hosts keep their URL. No node port is allocated. The stt policy allows the load balancer's pods in place of the queue's, and the prune unit waits for them to go before the host unit starts. The egress published-port rule and the raw-table rules apply to the load balancer's hostPort as to the pod's.
This commit is contained in:
parent
c2d57df7f5
commit
796fd6201f
4 changed files with 200 additions and 75 deletions
|
|
@ -370,11 +370,11 @@ let
|
|||
};
|
||||
|
||||
# What the policy of a pod that reaches `reaches` allows: the cluster's DNS,
|
||||
# those host ports, and the queue's own pod where the queue runs in the
|
||||
# cluster, because its `hostPort` is forwarded to the pod before the policy
|
||||
# sees the packet. The policy selects every pod of the service for ingress
|
||||
# too and allows none, so only the node's own traffic (the kubelet's probes,
|
||||
# which the policy engine always admits) reaches it.
|
||||
# those host ports, and the pods of the queue's load balancer where the queue
|
||||
# runs in the cluster, because the `hostPort` they bind is forwarded to them
|
||||
# before the policy sees the packet. The policy selects every pod of the
|
||||
# service for ingress too and allows none, so only the node's own traffic
|
||||
# (the kubelet's probes, which the policy engine always admits) reaches it.
|
||||
networkPolicyOf =
|
||||
name: reaches:
|
||||
let
|
||||
|
|
@ -416,7 +416,15 @@ let
|
|||
}
|
||||
]
|
||||
++ lib.optional (queueOnCluster && lib.elem "queue" reaches) {
|
||||
to = [ { podSelector.matchLabels = labelsFor "swarm-nats"; } ];
|
||||
to = [
|
||||
{
|
||||
namespaceSelector.matchLabels."kubernetes.io/metadata.name" = "kube-system";
|
||||
podSelector.matchLabels = {
|
||||
"svccontroller.k3s.cattle.io/svcname" = "swarm-nats";
|
||||
"svccontroller.k3s.cattle.io/svcnamespace" = k8sCfg.namespace;
|
||||
};
|
||||
}
|
||||
];
|
||||
ports = [ (tcp hostPorts.queue) ];
|
||||
};
|
||||
};
|
||||
|
|
|
|||
|
|
@ -29,6 +29,15 @@ let
|
|||
'';
|
||||
apiReady = waitFor 600 "the cluster's API" "${kubectl} get --raw /readyz >/dev/null 2>&1";
|
||||
|
||||
# The service load balancer binds a Service's port on the node with a
|
||||
# `hostPort` in a pod of `kube-system`, which its controller deletes some time
|
||||
# after the Service. The rule that forwards the port to that pod stays until
|
||||
# the pod is gone, and would swallow what the host unit then listens on.
|
||||
lbPodsGone =
|
||||
name:
|
||||
waitFor 180 "the load balancer's pods of ${name}"
|
||||
"[ -z \"\$(${kubectl} --namespace kube-system get pods --selector svccontroller.k3s.cattle.io/svcname=${name},svccontroller.k3s.cattle.io/svcnamespace=${cfg.namespace} --output name)\" ]";
|
||||
|
||||
caFiles = lib.filterAttrs (_: path: path != null) rendered.caSources;
|
||||
|
||||
# The services on the cluster whose pods run in a user namespace.
|
||||
|
|
@ -148,6 +157,7 @@ in
|
|||
${kubectl} --namespace ${cfg.namespace} delete deployment,cronjob,serviceaccount,networkpolicy,service \
|
||||
--selector=app.kubernetes.io/name=${name} \
|
||||
--cascade=foreground --wait=true --timeout=180s --ignore-not-found
|
||||
${lbPodsGone name}
|
||||
${kubectl} --namespace kube-system delete addons.k3s.cattle.io ${name} --ignore-not-found
|
||||
rm -f "$pending"
|
||||
'';
|
||||
|
|
|
|||
|
|
@ -1,10 +1,11 @@
|
|||
# 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
|
||||
# cluster's own forwarding rules accept it. The raw table is read before the
|
||||
# DNAT and before any of those, so a rule there reads the original
|
||||
# destination, as INPUT does for the container's listener.
|
||||
# The iptables rules that give the `hostPort` the cluster's load balancer binds
|
||||
# for the queue's client port 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 cluster's own forwarding rules
|
||||
# accept it. The raw table is read before the DNAT and before any of those, so a
|
||||
# rule there reads the original destination, as INPUT does for the container's
|
||||
# listener.
|
||||
#
|
||||
# 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
|
||||
|
|
|
|||
|
|
@ -984,13 +984,11 @@ let
|
|||
image = ref server;
|
||||
imagePullPolicy = "Never";
|
||||
args = lib.tail (lib.splitString " " natsSystem.systemd.services.nats.serviceConfig.ExecStart);
|
||||
# Every address of the node, which is where the queue listens today.
|
||||
# The port the client Service targets.
|
||||
ports = [
|
||||
{
|
||||
name = "client";
|
||||
containerPort = cfg.port;
|
||||
hostPort = cfg.port;
|
||||
hostIP = "0.0.0.0";
|
||||
}
|
||||
];
|
||||
# The monitor is on the pod's loopback, so the probe runs in the
|
||||
|
|
@ -1108,6 +1106,34 @@ let
|
|||
];
|
||||
};
|
||||
};
|
||||
|
||||
# The client port, on every address of the node, which is where the
|
||||
# queue listens in its container, so the hives of other hosts keep their
|
||||
# URL. The cluster's service load balancer (k3s's servicelb) binds the
|
||||
# Service's port on each node with a `hostPort` of its own, in
|
||||
# `kube-system`, and forwards it to the pod through the Service. It does
|
||||
# that by the Service's cluster address, so no node port is needed, and
|
||||
# none is allocated: a node port would be a second way in, which the
|
||||
# raw-table rules (./lib/swarm-nats-raw-rules.nix) don't name. The server
|
||||
# sees the load balancer's pod as the client, not the client's address.
|
||||
objects."${machine}/loadbalancer" = {
|
||||
apiVersion = "v1";
|
||||
kind = "Service";
|
||||
metadata = metaFor machine;
|
||||
spec = {
|
||||
type = "LoadBalancer";
|
||||
allocateLoadBalancerNodePorts = false;
|
||||
selector = (metaFor machine).labels;
|
||||
ports = [
|
||||
{
|
||||
name = "client";
|
||||
protocol = "TCP";
|
||||
port = cfg.port;
|
||||
targetPort = "client";
|
||||
}
|
||||
];
|
||||
};
|
||||
};
|
||||
objects."${machine}/networkpolicy" = {
|
||||
apiVersion = "networking.k8s.io/v1";
|
||||
kind = "NetworkPolicy";
|
||||
|
|
@ -1615,13 +1641,15 @@ in
|
|||
# sets no listen host), so reachability is the firewall alone.
|
||||
networking.firewall.allowedTCPPorts = lib.mkIf deployCfg.nats.openFirewall [ cfg.port ];
|
||||
|
||||
# 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);
|
||||
|
||||
# An agent dials the queue at the bridge's address; the pod's `hostPort` is
|
||||
# forwarded to the cluster, which the egress forward chain would refuse.
|
||||
# An agent dials the queue at the bridge's address, and the load
|
||||
# balancer's `hostPort` forwards that to the cluster, which the egress
|
||||
# forward chain would refuse.
|
||||
services.hyperhive.deploy.hive-controller.egress.publishedPorts = lib.mkIf onK8s [ cfg.port ];
|
||||
|
||||
# The load balancer's `hostPort` is a DNAT rule, which the INPUT rules above
|
||||
# never see: ./lib/swarm-nats-raw-rules.nix gives it the container's reach.
|
||||
networking.firewall.extraCommands = lib.mkIf onK8s (
|
||||
import ./lib/swarm-nats-raw-rules.nix { inherit lib; } {
|
||||
chain = "nixos-fw-nats";
|
||||
|
|
|
|||
Loading…
Reference in a new issue