# Networking

## Pod ranges and peer reachability
Figure 1: A pod on node A calls a Service whose pod runs on node B. Node A's nftables rules for the Service pick the pod, and WireGuard carries the packet between the nodes. (drawn at https://runkite.io/docs/networking/)

The node agent does the work of kube-proxy, CoreDNS and a network add-on such
as flannel. Each node gets one `/24` of the pod range, or one `/64` for IPv6,
and containerd connects each pod to the node's `cni0` bridge. Choose ranges
that do not overlap your own networks.

## Open these ports

| Port                                             | Open                                              | For                                                            |
| ------------------------------------------------ | ------------------------------------------------- | -------------------------------------------------------------- |
| UDP `51820`, or `kite-agent join --network-port` | Between every pair of nodes                       | The WireGuard overlay that carries pods' traffic between nodes |
| UDP `51820`                                      | From anywhere, to each node with a public address | Nodes in different networks or clouds                          |
| TCP `443`                                        | Outbound, from each node to the cluster's address | The node's connection to the control plane                     |
| A NodePort or a host port                        | Inbound, to the host                              | Traffic from outside the cluster to a pod                      |

Open them in your firewalls, security groups and routes. A peer is another
node in the cluster. A node that cannot reach a peer still joins, becomes
Ready and runs pods, but its pods reach only pods on the nodes it can reach.
See [Check reachability between nodes](#check-reachability-between-nodes).

## Defaults
  <div class="tscroll net-t">
    <table class="tbl">
      <thead>
        <tr>
          <th style="width:20%">Parameter</th>
          <th style="width:22%">Default</th>
          <th>Notes</th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <td class="p">Pod range</td>
          <td class="mono">10.64.0.0/11</td>
          <td class="c">
            <code>KITE_POD_CIDR</code>. IPv6 example <code>fd12:3456:789a::/48</code>.
          </td>
        </tr>
        <tr>
          <td class="p">Prefix for each node</td>
          <td class="mono">/24</td>
          <td class="c">
            <code>/64</code> for IPv6. An IPv4 <code>/16</code> has room for 256 nodes.
          </td>
        </tr>
        <tr>
          <td class="p">Most nodes</td>
          <td class="mono">8,192</td>
          <td class="c">With the default range. 65,536 with the IPv6 example.</td>
        </tr>
        <tr>
          <td class="p">Service range</td>
          <td class="mono">10.96.0.0/16</td>
          <td class="c">
            <code>KITE_SERVICE_CIDR</code>. IPv6 example <code>fd12:3456:789b::/112</code>.
          </td>
        </tr>
        <tr>
          <td class="p">IP family</td>
          <td class="mono">IPv4 or IPv6</td>
          <td class="c">Both ranges in one family. Dual-stack is not supported.</td>
        </tr>
        <tr>
          <td class="p">Overlay</td>
          <td class="mono">UDP 51820</td>
          <td class="c">
            WireGuard on <code>kitewg0</code>, between every pair of nodes. In the kernel, or in
            userspace on a kernel without WireGuard.
          </td>
        </tr>
        <tr>
          <td class="p">Pod interface</td>
          <td class="mono">cni0</td>
          <td class="c">
            The CNI plugins <code>bridge</code> and <code>host-local</code>, which the agent
            configures in <code>/etc/cni/net.d/10-kite.conflist</code>. Do not install another
            network add-on, such as Calico or Cilium, because the agent manages this file and the
            bridge.
          </td>
        </tr>
        <tr>
          <td class="p">Peer probe</td>
          <td class="mono">every 10 s</td>
          <td class="c">
            One sweep probes every peer once, and takes 10 seconds, or 0.25 seconds for each peer
            when that is longer.
          </td>
        </tr>
        <tr>
          <td class="p">Cluster DNS</td>
          <td class="mono">cluster.local</td>
          <td class="c">The agent answers. It forwards other names to the host's resolver.</td>
        </tr>
      </tbody>
    </table>
  </div>

:::caution
Choose both ranges before the first deploy. You cannot change them on an
existing cluster: if you do, node joins and Service writes fail until you deploy
again with the original values. See
[Set the pod and Service networks](/docs/configure/#set-the-pod-and-service-networks).
:::

## Node addresses

<div class="tscroll net-t">
  <table class="tbl">
    <thead>
      <tr>
        <th style="width:12%">Address</th>
        <th style="width:44%">Where it comes from</th>
        <th>Connects</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td class="p">Private</td>
        <td>The source address of the node's default route</td>
        <td>Nodes in one VPC. You set nothing.</td>
      </tr>
      <tr>
        <td class="p">Public</td>
        <td>
          A node pool machine reads it from the cloud. On a host that you join yourself, pass{" "}
          <code>{"--network-public-endpoint"}</code> to <code>kite-agent join</code>
        </td>
        <td>
          Nodes in different networks or clouds, once the cluster sets{" "}
          <code>KITE_NETWORK_PUBLIC_ENDPOINTS=Allowed</code>. The Node lists it as an{" "}
          <code>ExternalIP</code> in <code>status.addresses</code>.
        </td>
      </tr>
    </tbody>
  </table>
</div>

A node tries a peer's private address first, then its public address, and
uses the first that responds. See
[Join nodes in different networks](/docs/configure/#join-nodes-in-different-networks).

:::note
The control plane refuses an address that a node pool machine's cloud did not
give it. A host that you join yourself is not checked.
:::

:::caution
Traffic between public addresses leaves the cloud, which bills it as egress.
:::

## DNS and traffic out of the cluster

Pods resolve `cluster.local` names through the agent, which forwards other
names to the host's resolver. If the host cannot resolve public names, its
pods cannot either.

:::note
Lookups keep getting answers while the agent restarts or updates. After
`kite-agent stop` or `kite-agent leave`, they fail until the agent starts again.
:::

Traffic to an address outside the pod range leaves through the host's routes.
To publish an HTTP service, see [Ingress and load balancing](/docs/ingress/).

### Traffic distribution

A Service sends each connection to any of its ready pods, wherever they run.
To keep a pod's connections in its zone, set `spec.trafficDistribution`:

```yaml
apiVersion: v1
kind: Service
metadata:
  name: api
spec:
  selector: { app: api }
  trafficDistribution: PreferClose
  ports: [{ port: 80, targetPort: 8080 }]
```

| `trafficDistribution`             | A pod's connections go to                                                                                       |
| --------------------------------- | --------------------------------------------------------------------------------------------------------------- |
| Not set                           | Every ready pod                                                                                                 |
| `PreferClose` or `PreferSameZone` | The ready pods in the node's zone, then those in its region, then those with its provider, then every ready pod |
| `PreferSameNode`                  | The ready pods on the node, then the pods that `PreferClose` picks                                              |

:::note
A node's zone, region and provider are its `topology.kubernetes.io/zone`,
`topology.kubernetes.io/region` and `kiteplane.dev/provider` labels. A node
pool sets all three on GCP and AWS. A node without one of them skips the step
that needs it.
:::

Each endpoint in the Service's EndpointSlices names its node's zone in `zone`,
and the zone or node that should prefer it in `hints`. A node leaves out the
pods on peers that it cannot reach before it applies the setting, so when every
pod in its zone is on an unreachable peer, the node takes the next step.

Upstream Kubernetes falls back from the zone straight to every ready pod. In a
cluster in one region, both pick the same pods.

:::caution
Connections follow the clients. A zone with many clients and one ready pod
sends every connection to that pod.
:::

## NetworkPolicy

The agent enforces NetworkPolicy ingress and egress rules. Policy matches
traffic by source address, so the agent binds each pod's port on `cni0` to
the pod's MAC and IP addresses, and the node drops from that port:

<div class="tscroll net-t">
  <table class="tbl">
    <thead>
      <tr>
        <th style="width:42%">Dropped</th>
        <th>When it</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td class="p">A frame</td>
        <td>has another source MAC address</td>
      </tr>
      <tr>
        <td class="p">A packet</td>
        <td>has another source IP address</td>
      </tr>
      <tr>
        <td class="p">An ARP message or IPv6 neighbor advertisement</td>
        <td>claims another pod's address</td>
      </tr>
    </tbody>
  </table>
</div>

A pod's containers start only after the agent binds the pod's port, and the
check holds while the agent restarts. The node does not check a pod that uses
the node's network.

:::note
Policy between pods on the same node requires bridge netfilter on the host. If
the agent cannot turn it on, the node stays NotReady.
:::

## Egress policy

An EgressPolicy limits the hosts outside the cluster that a selected pod can
reach. See [Egress policy](/docs/egress-policy/).

## When nodes join or leave

- A node gets no new pods while it is disconnected, or before it reports that
  its network is set up.
- A removed node's pod range stays reserved until every remaining node has
  applied the change.
- A disconnected node keeps its range until it reconnects, or until you delete
  its Node.

## Check reachability between nodes

Each agent probes every other node through WireGuard, and records the result
in a `NodeNetworkStatus` with the same name as the node:

```bash
kubectl get nodenetworkstatuses.kiteplane.dev
kubectl get nodenetworkstatuses.kiteplane.dev <node> -o yaml
```

<div class="tscroll net-t">
  <table class="tbl">
    <thead>
      <tr>
        <th style="width:22%">Field</th>
        <th>What it shows</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td>
          <code>peers</code>
        </td>
        <td>Counts of peers reached, unreachable, not yet probed, and with no path</td>
      </tr>
      <tr>
        <td>
          <code>unreachablePeers</code>
        </td>
        <td>Up to 32 peers this node cannot reach</td>
      </tr>
      <tr>
        <td>
          <code>unreachableFrom</code>
        </td>
        <td>Up to 32 peers that cannot reach this node</td>
      </tr>
      <tr>
        <td>
          <code>conditions</code>
        </td>
        <td>
          <code>OverlayReachable</code>, also shown on the Node
        </td>
      </tr>
      <tr>
        <td>
          <code>wireguard</code>
        </td>
        <td>
          <code>kernel</code>, or <code>userspace</code> on a kernel without WireGuard
        </td>
      </tr>
    </tbody>
  </table>
</div>

A peer in another network, one that this node has never reached and has no
public endpoint for, counts under `peers.noPath` and does not change the
condition. The probe tests only the WireGuard overlay, not pod ports,
NetworkPolicy, DNS or MTU.

:::tip
When many peers list one node in `unreachableFrom`, check the firewall or NAT
in front of that node first.
:::

### When a peer stops responding

<div class="tscroll net-t">
  <table class="tbl">
    <thead>
      <tr>
        <th style="width:30%">When</th>
        <th>What happens</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td class="p">Before a probe confirms it</td>
        <td>Requests to the peer's pods fail.</td>
      </tr>
      <tr>
        <td class="p">Once a probe confirms it</td>
        <td>
          Within one report interval, the node rewrites its rules and stops sending Service traffic
          to the peer's pods. So in a partitioned network, a Service keeps working within each part.
          The interval is 10 seconds in a cluster of up to 500 nodes, and grows with the cluster to
          at most 60 seconds.
        </td>
      </tr>
      <tr>
        <td class="p">After three failed probes</td>
        <td>
          <code>OverlayReachable</code> becomes <code>False</code> on the node's{" "}
          <code>NodeNetworkStatus</code> and on the Node. Ready and scheduling do not change.
        </td>
      </tr>
      <tr>
        <td class="p">Throughout</td>
        <td>
          <code>kubectl get endpointslices</code> lists every ready pod, and DNS for a headless
          Service returns every pod.
        </td>
      </tr>
    </tbody>
  </table>
</div>

:::caution
A Service whose pods are all on unreachable peers keeps sending traffic to
them.
:::

## Next

- [Nodes](/docs/nodes/): the host and its control connection.
- [Egress policy](/docs/egress-policy/): limit the hosts a pod can reach outside the cluster.
- [Troubleshoot](/docs/troubleshoot/#network-failures): steps for a pod that cannot reach another.
- [Expose a workload](/docs/expose/): publish an HTTP Service through Cloudflare Tunnel.
- [Compatibility and limitations](/docs/reference/compatibility/): known differences from upstream Kubernetes.
