Skip to content

Networking

VMs need to talk to each other and to the outside world. Here’s how SwarmCracker handles that.


Each VM gets a TAP device connected to a Linux bridge:

Host
├── swarm-br0 (192.168.127.1)
│ ├── tap0 ── VM1 (192.168.127.10)
│ └── tap1 ── VM2 (192.168.127.11)

VMs on the same bridge can talk directly. The host talks via the bridge IP. Internet access goes through NAT.


network:
bridge_name: "swarm-br0"
subnet: "192.168.127.0/24"
bridge_ip: "192.168.127.1/24"
ip_mode: "static" # static | dhcp
nat_enabled: true
Setting Default What It Does
bridge_name swarm-br0 The bridge name
subnet 192.168.127.0/24 IP range for VMs
bridge_ip 192.168.127.1/24 Host’s IP on the bridge
ip_mode static static (deterministic) or dhcp (dnsmasq)
nat_enabled true Let VMs reach internet

IPs come from hashing the VM ID. Same ID always gets the same IP. No DHCP needed, which makes startup faster.

The first host addresses .1–.16 are reserved for infrastructure: on a multi-node cluster every node shares one L2 overlay subnet and each node’s bridge takes a low address (.1, .2, …), so guests are only allocated from .17 upward to avoid colliding with a sibling node’s bridge.

Per-node blocks (cluster-wide uniqueness). On a multi-node cluster the guest range is additionally split into 16 disjoint per-node blocks — one per node slot. A node derives its slot from its own bridge address (bridge_ip: 192.168.127.2/24 → slot 2) and only hands out addresses from that block, so two nodes can never assign the same guest IP even though each node allocates locally (by hashing the task ID).

For the default 192.168.127.0/24 overlay, each slot holds 14 addresses:

Node (bridge) Guest block
…127.1 …127.17 – …127.30
…127.2 …127.31 – …127.44
…127.k block k of 16

A single node (no VXLAN peers) is not partitioned and keeps the whole subnet. For dense clusters use a larger overlay subnet (e.g. 192.168.0.0/16) so each node’s block is larger. Bridge addresses must be .1, .2, … for the slots to line up; cluster init / cluster join assign them that way. See decisions/ADR-007-cluster-wide-ip-allocation.md for the design.

If you want dynamic IPs, switch to dnsmasq-backed DHCP:

network:
ip_mode: "dhcp"

SwarmCracker starts a minimal dnsmasq instance bound to the bridge when ip_mode: "dhcp". If dnsmasq is not installed, the bridge is still created but DHCP allocation is unavailable.

The instance’s runtime files (pid, log, DHCP leases, and the service-name records described below) live in /var/lib/swarmcracker/dnsmasq/, a root-owned directory that only root and the unprivileged user dnsmasq drops to can write — never in world-writable /tmp, where a predictable file name would let any local user plant a symlink for the daemon to write through as root, or a pid file for it to signal as root.


If you have VMs on different workers, they need VXLAN to communicate.

Node 1 Node 2
swarm-br0 swarm-br0
┌───┐┌───┐ ┌───┐┌───┐
│VM1││VM2│ ← VXLAN UDP → │VM3││VM4│
└───┘└───┘ 4789 └───┘└───┘

When you start swarmd-firecracker with --vxlan-enabled, it creates a VXLAN interface named after the bridge (swarm-br0-vxlan for the default swarm-br0) and attaches it to the bridge:

Terminal window
swarmd-firecracker \
--vxlan-enabled \
--vxlan-peers 192.168.56.12,192.168.56.13 \
--bridge-name swarm-br0 \
--subnet 192.168.127.0/24

With a single static peer you can substitute --vxlan-peers <ip>; for dynamic discovery use --consul-enabled instead (see below).

Each node registers itself in Consul. When a new peer shows up, the VXLAN forwarding database gets updated automatically.

Terminal window
swarmd-firecracker \
--consul-enabled \
--consul-address 127.0.0.1:8500 \
--vxlan-enabled

VXLAN uses UDP port 4789:

Terminal window
sudo iptables -A INPUT -p udp --dport 4789 -j ACCEPT

SwarmCracker creates TAP devices automatically. Names follow the pattern tap-<8-char-task-hash>-<index>, where the hash is the first 8 hex characters of sha256(task-id):

tap-a1b2c3d4-0
tap-9f8e7d6c-0
Terminal window
# Create
sudo ip tuntap add dev tap0 mode tap
sudo ip link set tap0 up
sudo ip link set tap0 master swarm-br0
# Delete
sudo ip link del tap0

When nat_enabled: true, iptables masquerades outbound traffic:

Terminal window
iptables -t nat -A POSTROUTING -s 192.168.127.0/24 -j MASQUERADE
network:
nat_enabled: false

VMs can only talk to each other and the host.


Every microVM that boots gets an IP on the bridge, but where you read it back from depends on how the VM was created:

VM origin Source of the IP How to read it
swarmcracker vm create -d CLI state file (state.json) swarmcracker vm status <vm>
swarmcracker service create (daemon-managed) daemon network allocation swarmcracker vm status <task> / vm list --format json

The daemon records each running VM’s network details in a <task-id>.net.json file next to its Firecracker socket (default /var/run/firecracker). vm status and vm list --format json read that file, so the guest IP — including the fallback TAP/DHCP allocation, which is never written back into the SwarmKit task store — is visible for service VMs too.

The vm list table has no IP column, so parse --format json or use vm status. On the host you can also confirm an address directly:

Terminal window
ip neigh show dev swarm-br0

By default every service shares the single swarm-br0 bridge. To segment workloads (for example, a frontend tier and a database tier), create a user-defined network with its own subnet:

Terminal window
swarmcracker network create --name backend --subnet 10.10.0.0/24
swarmcracker network create --name frontend --subnet 10.20.0.0/24

Attach a service to one with --network (repeatable — the service gets one NIC per network):

Terminal window
# single network
swarmcracker service create --name api --image myapp --network backend
# several networks: eth0 -> backend, eth1 -> frontend
swarmcracker service create --name gateway --image myapp \
--network backend --network frontend

Each network is allocated its own subnet, a per-node bridge (br-<name>) and an overlay VXLAN ID. A service’s VM gets an IP from each attached network’s subnet, and services on different networks cannot reach each other — the node drops traffic between distinct network bridges. Services on the same network reach each other normally, including across nodes: every network gets its own per-network VXLAN fanned out to the other nodes, so the network’s L2 domain spans the cluster (the same mechanism as the default overlay).

Each node holds its own address on the network — <subnet>.1 on the first node, <subnet>.2 on the second, and so on, mirroring the default overlay’s bridge_ip — and a node’s guests use its address as their default gateway. Two nodes holding one address would both answer ARP for it on the network’s L2, and whichever answered first would become that guest’s gateway. Node 1 holds whatever the network records as its gateway, so a network created with --gateway keeps it (and keeps the guests’ default route with it), and that address stays reachable from every other node over the VXLAN.

The per-node addresses need what the per-node guest blocks need: a subnet of at least /27, and each node configured with its own bridge_ip (.1, .2, …) and --vxlan-peers — without those every node computes slot 1 and keeps the recorded gateway. The ingress and docker_gwbridge bridges are carved from the default overlay’s subnet, so each node’s holds that node’s single address on that segment, just like its default bridge.

Both the subnet and the VXLAN ID are assigned once and then kept: a daemon restart re-allocates the network’s resources, and renumbering a live network would move its guests’ addresses or leave its replicas without a tunnel. A newly assigned VXLAN ID is never handed to a network that already holds one — a node builds each network’s tunnel from it, so two networks cannot share one — and a network found holding a VXLAN ID that another network already has is renumbered once, with a warning naming both.

Terminal window
swarmcracker network ls
swarmcracker network inspect backend # subnet, gateway, bridge, VNI

Without --subnet, a network is carved from the overlay pool (--cni-overlay-subnet-pool, 10.0.0.0/8 by default), never from the flat L2 subnet in --subnet. That keeps a user-defined network off the default network’s addresses, which matters because the two would otherwise be indistinguishable to the IPAM pools and NAT rules that are keyed by subnet. The predefined networks (ingress, docker_gwbridge) are the exception: they stay on the flat L2 subnet, so ingress task IPs remain reachable cluster-wide.

An explicit --subnet is honored, but it has to be free — one that overlaps the flat L2 subnet or a network that already has one is refused before anything is created:

Terminal window
swarmcracker network create --name clash --subnet 192.168.127.0/24
# subnet 192.168.127.0/24 overlaps network "ingress" (192.168.127.0/24); choose a free subnet

The daemon refuses the same overlap when it allocates a network, so a client that does not go through this CLI (the API directly, swarmctl) cannot end up with two networks on the same addresses either. There the refusal is recorded in the daemon’s log and the network is left unallocated, because SwarmKit commits a network before allocating it.

A network cannot be removed while a service is still attached:

Terminal window
swarmcracker network rm backend
# network is in use by service(s) api; remove or update them first

Removing the last service that uses a network tears down its per-node bridge (br-<name>), its VXLAN interface and its NAT rule, so network rm leaves nothing behind. The network is re-materialized on demand the next time a task attaches to it.

Outbound NAT for a user-defined network is installed with a physdev guard, so it applies only to traffic the node routes off the network — never to frames it is merely bridging between two guests. That distinction matters: with bridge-nf-call-iptables enabled (the Docker/CNI default) bridged frames also traverse the NAT table, and a masquerade rule that catches them rewrites the source address of guest-to-guest traffic, which breaks the return path across nodes.

The default network’s masquerade rule carries the same guard, because an auto-allocated user-defined network can be given the default network’s subnet (network create without --subnet): an unguarded default rule would then masquerade that network’s guest-to-guest frames instead.


service create --publish (alias -p) exposes a port inside the microVM, so you do not need to look up the guest IP:

Terminal window
swarmcracker service create --name web --image nginx:alpine --publish 8080:80

The syntax is [host:]container[/tcp|udp]:

Terminal window
# TCP (default): host 8080 -> guest 80
swarmcracker service create --name web --image nginx --publish 8080:80
# UDP
swarmcracker service create --name dns --image coredns/coredns --publish 53:53/udp
# Several mappings at once
swarmcracker service create --name app --image myapp --publish 8080:80 --publish 8443:443

--publish-mode selects how the port is exposed:

Mode Behavior
ingress (default) Cluster-wide: one entry point that load-balances across the service’s healthy replicas, including replicas on other nodes.
host Per-replica: forwards the host port on the node running each replica.
Terminal window
# Load-balanced across replicas (default)
swarmcracker service create --name web --image nginx --replicas 3 --publish 8080:80
# Pin the host port to each node running a replica
swarmcracker service create --name web --image nginx --replicas 3 --publish 8080:80 --publish-mode host

service ls shows the mapping, and service inspect prints the published ports and, for ingress services, the service VIP:

Terminal window
$ swarmcracker service ls
ID NAME REPLICAS MODE PORTS IMAGE
web web 3 replicated 8080:80/tcp nginx:alpine

For an ingress service the manager allocates a per-service VIP (from the top of the overlay subnet) and attaches every replica to the ingress network, so each replica gets a cluster-unique address on the shared L2 overlay. A reconciler on manager nodes programs L4 (TCP/UDP) load balancing: new connections to the published host port are distributed across the healthy replicas and forwarded to the target port inside the chosen replica — including replicas running on worker nodes.

  • One entry point: reach the published port on any node — manager or worker. Each node balances locally from the same routing table.
  • Balancing is L4 and per-connection; sticky sessions are not provided (front the service with a proxy if you need them).
  • Replicas that are not RUNNING are removed from rotation automatically, and a service with no healthy replica stops being forwarded.

A manager computes the authoritative routing table and serves it to the other nodes over mutual TLS (port 4243 by default); every node then programs its own load balancing. The mesh is enabled by default and can be tuned on the daemon with --ingress-mesh and --ingress-port. That channel is cluster-internal and not an API: see Internal (Private) Endpoints.

In host mode the daemon programs DNAT rules on the node running the task, so host:8080 (on 127.0.0.1, the LAN IP, or any interface) is forwarded to <guest-ip>:80. The rules are tagged with the task ID and are removed automatically when the task is removed or the service is scaled down.

Because each replica is published on its own node’s host port, two replicas on the same node cannot bind the same host port. If a host port is already in use, the task fails with an explicit error (for example host port 8080/tcp is already published by task <id>) instead of silently being ignored. --replicas 2 across two nodes makes both nodes listen on the host port, but there is no single entry point — use ingress mode for that.

Terminal window
# Host-mode rules are tagged with the task ID; ingress rules with the service ID.
iptables -t nat -S PREROUTING | grep swarmcracker
iptables -t nat -S OUTPUT | grep swarmcracker

PREROUTING handles traffic from other hosts; OUTPUT handles traffic that originates on the node itself (so curl 127.0.0.1:8080 works, like Docker).


A microVM whose resolver points at its node’s bridge gateway — the default for a service on the default network — resolves the cluster’s services by name, so you do not have to look up guest IPs:

Terminal window
$ swarmcracker vm exec <task> -- getent hosts web
192.168.127.5 web
$ swarmcracker vm exec <task> -- getent hosts tasks.web
192.168.127.5 tasks.web
192.168.127.6 tasks.web
Name Answers with
<service> every running replica’s IP
tasks.<service> every running replica’s IP

A name with several replicas becomes several addresses, so the client picks one — the equivalent of Docker’s dnsrr endpoint mode. Records follow the replicas: a task that stops is dropped from the answer within a few seconds (the table is recomputed and polled every 3s), and a service with no running replica stops resolving instead of pointing at a dead address. Anything the node does not know (an external hostname) is forwarded to the host’s own resolver, so apk/apt and friends keep working.

Every node reports the guest addresses of the tasks it runs to the manager, over the same mutual-TLS channel as the table (port 4243). That report is what makes this work: the address of a task on the default bridge is allocated on the node and never written back into the SwarmKit task store, so the control API alone cannot say where a plain service’s replicas live. The manager joins the reports with the control API — a task is only resolved while it is RUNNING — and serves the resulting records to every node in the routing table. Only the node’s reported address is used; the addresses the control API knows are on the ingress network or a user-defined one, which a guest asking this resolver cannot reach. Each node writes the records into its bridge dnsmasq’s hosts file and reloads it, and each guest’s /etc/resolv.conf points at that bridge gateway.

A record disappears when the node running the task stops reporting (crash, partition, drain — within a few seconds) or when SwarmKit stops considering the task RUNNING, the same liveness signal service ps uses. Service DNS is part of the ingress mesh, so --ingress-mesh=false turns it off too; reports go to the same endpoint that serves the table (a manager reports to itself), so a cluster with several managers uses the one serving the mesh.

An explicit --dns on the service always wins over the bridge resolver:

Terminal window
swarmcracker service create --name app --image myapp --dns 1.1.1.1
  • Published and user-defined networks are not resolved by name. Records only cover tasks on the default bridge, because that is the bridge this node’s resolver serves: a guest asking it cannot reach an address on the ingress network or on a user-defined one, and answering with one would send it somewhere it cannot go. Reach a published service through its published port. (User-defined overlays get no dnsmasq of their own, so a guest attached only to one keeps the image’s resolver.)
  • Replica addresses, not the VIP. The service VIP is allocated for ingress services but is not a routable address on this datapath, so <service> answers with the replicas.
  • Golden images boot their own init and never read the daemon’s resolver override, so the image’s own /etc/resolv.conf applies.
  • Names are cluster-wide. A guest resolves any service on the default network, not only the ones on its own node; per-network scoping is not implemented.
  • dnsmasq must be installed (it is part of the standard install). The daemon starts one per bridge when it prepares a task, whatever ip_mode is, because it also serves the service-name records. Without it no resolver runs on the bridge, so a guest keeps the image’s own resolver and service names do not resolve.

Terminal window
ip link show swarm-br0 # Bridge exists?
ip link show | grep tap # TAP devices attached?

Inside the VM, check ip addr show eth0.

Terminal window
iptables -t nat -L POSTROUTING # NAT rule there?
sysctl net.ipv4.ip_forward # Should be 1

Enable forwarding if needed:

Terminal window
sudo sysctl -w net.ipv4.ip_forward=1
Terminal window
ip link show swarm-br0-vxlan # VXLAN interface up?
iptables -L INPUT | grep 4789 # Port open?
ping <other-node-ip> # Underlay reachable?

If FDB entries are missing, check Consul:

Terminal window
curl http://127.0.0.1:8500/v1/catalog/service/swarmcracker-vxlan