Skip to content
grepjavaPublic

About

Container orchestration for FreeBSD: Swarm's operating model plus selected Kubernetes features, running workloads as jails

Resources

Stars

2 stars

Watchers

1 watching

Forks

Repository files navigation

Legion

Legion of BSD Daemons

A container orchestrator for FreeBSD: Docker Swarm's operating model plus selected Kubernetes features, running workloads as jails from OCI images, which it pulls into ZFS and runs itself. The plan and its rationale are in docs/DESIGN.md; how it compares with Docker Swarm, Kubernetes, CBSD, BastilleBSD and Sylve is in docs/COMPARISON.md, and how to build and ship images for it in docs/IMAGES.md. What it takes to lose any one machine is in docs/HA.md. What changed in each version is in CHANGELOG.md.

Images are built with legion build: it builds FreeBSD jail images from Containerfiles with nothing but the base system, and legion local runs them in a jail set up as a Legion node sets one up (the same runtime and image store) and publishes them to any OCI registry. These were the separate tool jailer until it was merged into Legion. Legion also runs images made by buildah or Podman. examples/ has a small native base (FreeBSD's runtime image with pkg, 35 MiB) and recipes on it for PostgreSQL, Redis, nginx, Node.js and Python, each a template in the console; a Service running a Linux image is pointed to the recipe that runs the same natively.

Status: phases 1 (single node), 2 (cluster), 3 (K8s-lite: namespaces, RBAC, network policy, quotas, jobs, security profiles, metrics), 4 (stateful services and operations) and 5 (Legion's own jail runtime, Linux images, IPv6, network volumes) are done, and so is the hardening in ROADMAP.MD: every test qualified on disposable FreeBSD VMs, partitions and drains with stated guarantees, deployments pinned to digests, recovery drills, image signatures, revocation and an audit trail. A web console (legion daemon --console-listen, CONSOLE.md) runs the cluster from a browser as the signed-in user. PHASE.md tracks what is built and what is next, including the work on main since v0.2.0 (Volume failover, alerts sent, checks before disruption, 30 days of usage, native recipes).

Sizes measured: the planner up to 200 nodes, 1,000 services and 5,000 tasks (78 ms a pass); a node's pf up to 2,000 services (a service added then takes 4.2 s on each node); the WireGuard mesh to 24 nodes; the whole qualification on three nodes. docs/MEASURE.md has the figures. Larger clusters are untested; if you run one, please open an issue saying how it goes.

Install

On FreeBSD 15, from a release tarball (amd64; or one made with packaging/freebsd/release.sh, which also builds on arm64, though arm64 is not qualified yet; a port for both is in packaging/freebsd/port), as root:

tar -xf legion-<version>-freebsd15-amd64.tar.xz --strip-components 1 -C /usr/local
sysrc -f /boot/loader.conf kern.racct.enable=1   # rctl limits; reboot once
zfs create -o mountpoint=/zroot/legion zroot/legion   # images, containers, volumes
sysrc legion_enable=YES legion_args="--zfs-root zroot/legion"
service legion start
legion init                   # on the first manager; other nodes join (below)

pf must be enabled and /etc/pf.conf must call Legion's anchors (see below): legion doctor --fix pf, as root, does both, after showing what it changes and asking; legion init and legion join offer it. The daemon logs to /var/log/legion.log, which newsyslog rotates; service legion stop leaves the pods running, and the next start takes them over.

Build

Rust 1.90 or newer. No protoc is needed.

cargo build --release
cargo test --workspace

Try it

legion daemon --data-dir /tmp/legion --no-agent &
export LEGION_IDENTITY=/tmp/legion/admin.pem   # written by the daemon
legion init
cat <<'EOF' | legion apply -f -
apiVersion: legion/v1
kind: Service
metadata: { name: web, namespace: shop, labels: { app: web } }
spec:
  replicas: 3
  pod:
    containers: [{ name: web, image: nginx:1.27 }]
EOF
legion list svc -A
legion get svc web -n shop
legion watch Service
legion top

legion top shows the cluster live in the terminal: nodes, workloads, tasks, volumes, routes and policies, namespaces and their quotas, Secrets and Configs, with graphs of what tasks use (live, or over up to 30 days as the managers keep it), each node's load, host and ZFS pools, each object's details and YAML, and a log of what changes. Keys drain, pause or label nodes, schedule their scrubs, scale or roll back services, suspend cron jobs, move volumes or promote their replicas, delete objects, and follow logs or open a shell in a task. It shows one panel at a time, as ISPF does: a menu of the lists and the graphs, Enter to open a list, a row or a node's tasks, F3 to go back, a Command ===> line, an action bar pulled down with F10, rows marked with Space for one letter to act on all, and the function keys on the last line; F1 lists the keys (legion top --once prints it as text). For dashboards, Prometheus can scrape each daemon's --metrics-listen endpoint; packaging/grafana has a Grafana dashboard for it.

The daemon serves the API on port 7470 of every address, with mutual TLS: every call but joining needs a certificate of the cluster's, as admin.pem holds. Point the CLI at another manager with --server or LEGION_SERVER. Without --no-agent, on FreeBSD and as root, it also runs the agent, so applied services run as pods of jails on the node (legion list tasks -n shop shows each pod's address on the node's legion0 bridge). The agent runs them itself: it pulls OCI images into ZFS and runs each container as a jail on a clone of its image, so it needs --zfs-root <pool>/legion, where images, containers and volumes live (legion image load adds images no registry serves). Pods reach each other and the host, and find services by name (web, web.shop or web.shop.legion) through the node's resolver on the bridge. Service VIPs, published ports and NAT for pods need the host's /etc/pf.conf to call Legion's anchors (legion doctor, as root, checks this and the rest of what a host needs, and changes nothing; legion doctor --fix pf adds the lines below after showing them and asking, keeping a copy of the old file). Each node's agent runs the same checks as it starts and every minute after: while one the host requires is unmet, the node takes no new tasks (legion list nodes says ready, lacks pf.anchors), keeping what runs there, and takes them again once it is put right. legion node require n1 forwarding makes an advised check count so on that node:

nat-anchor "legion/*"
rdr-anchor "legion/*"
anchor "legion/*"

The anchors decide only what network policy names; the rest of the ruleset must pass the cluster's traffic (WireGuard on UDP 51820, and legion0, wg0 and the pods' lp* interfaces) keeping state with pf's default floating state policy, as a final pass all does. A ruleset that blocks by default, or sets state-policy if-bound, is not supported.

More nodes join with a token. A manager advertises the address of its default route to the others (or --listen's, when that names one), and a worker the address it reaches its managers from; --advertise chooses another. A worker runs only the agent, which follows its work through any manager and fails over to the others. Pods on different nodes reach each other over a WireGuard mesh (UDP port 51820, which the hosts must let through), sent to each node's advertised address:

legion join-token manager            # or worker; on a member
legion join <token> --manager 10.0.0.1:7470    # on the new node, then
service legion start                           # or legion daemon

Only managers serve the API, so on a worker the CLI needs a manager's address: --server 10.0.0.1:7470, or LEGION_SERVER in the shell's profile. Without it the CLI tries 127.0.0.1:7470 and the connection is refused.

The cluster runs while a majority of its managers is up. One manager is a single point of control: while it is down, pods keep running and serving, but nothing can be deployed, scaled, rescheduled or renewed, and the CLI fails, until it is back. Three managers survive losing any one (the others elect a new leader in 5 to 10 s, and agents fail over), five survive two. Two are worse than one, as losing either stops the cluster. Managers exchange heartbeats every second, so they belong near each other (round trips well under a second). A worker becomes a manager by being removed and joining again with a manager token. Whatever the count, keep a legion backup and each manager's <data-dir>/pki off the cluster.

A worker may sit behind NAT, such as a FreeBSD virtual machine on a desktop: it reaches the managers and keeps its WireGuard path open with keepalives. Two nodes behind different NATs cannot reach each other, so give such a VM a bridged network on the LAN of the other local nodes rather than the hypervisor's user-mode NAT. Managers must reach each other directly.

amd64 and arm64 nodes may share a cluster, though only amd64 is qualified so far. The manager does not read images: a task placed on a node whose architecture the image does not offer is rejected, and the service's tasks then stay off nodes of that architecture. A constraint such as node.platform.arch == amd64 keeps them off from the start.

Compose files deploy as stacks. Each stack gets a namespace of its name, in which services find each other by their compose names; what Legion cannot honour (bind mounts, depends_on, privileged, ...) is reported as a warning, or refused with --strict:

legion stack deploy shop -c compose.yaml
legion stack convert shop -c compose.yaml   # the Legion YAML it becomes
legion rollback web -n shop                 # undo the latest update
legion restart web -n shop                  # new tasks, same spec
legion stop web -n shop                     # no tasks; `legion start` brings them back
legion pause web -n shop                    # tasks frozen in place; `legion resume` thaws them
legion stack ls                             # each stack, its services ready or stopped
legion stack rm shop                        # volumes and their data stay

A task's files, an image's packages and the nodes themselves can be looked into from the CLI as from the console:

legion files web -n shop /usr/local/www     # a directory in a running task
legion cp web:/etc/nginx.conf . -n shop     # out of it (or into it)
legion image scan nginx:1.27 --node n1     # its packages against FreeBSD's VuXML
legion diagnose                             # legion doctor, on every node
legion support-bundle                       # to send with a report, Secrets left out

A namespace's tasks are reached only by its own tasks, the nodes and published ports, unless its isolation is open (or locked, not even its own); NetworkPolicy objects let more in by namespace, service labels and ports. With egress, a policy also limits where the tasks it selects may send (DNS always works; egress: [] allows nothing else):

kind: NetworkPolicy
metadata: { name: api-out, namespace: shop }
spec:
  services: { app: api }
  egress:
    - to: [{ services: { app: db } }]
      ports: [{ port: 5432 }]
    - to: [{ cidr: 192.0.2.0/24 }]   # a payment provider
      ports: [{ port: 443 }]

A namespace's quota, which only the cluster admin sets, caps what its tasks request, how many run, and its volumes; tasks beyond it wait as pending:

kind: Namespace
metadata: { name: shop }
spec:
  quota: { cpu: 4, memory: 8Gi, tasks: 20, volumes: 5, storage: 100Gi }

Containers' jails get no raw sockets, chflags, mlock, SysV IPC or mounts, at securelevel 2 and enforce_statfs 2. A container's security loosens that only as far as its namespace allows, which again only the cluster admin sets:

kind: Namespace
metadata: { name: shop }
spec:
  security: { allow: [rawSockets] }   # also chflags, mlock, sysvipc,
                                      # securelevel, enforceStatfs
---
# in a Service's pod
containers:
  - name: probe
    image: "registry.example/shop/netcheck:1"
    security: { allow: [rawSockets] }   # ping works here

Jobs run pods to completion, retrying failures; CronJobs make Jobs on a schedule (UTC):

kind: CronJob
metadata: { name: backup, namespace: shop }
spec:
  schedule: "30 2 * * *"
  concurrencyPolicy: forbid
  job:
    completions: 1
    backoffLimit: 3
    activeDeadline: 1h
    pod:
      containers: [{ name: dump, image: "registry.example/shop/tools:1" }]

Into the tasks themselves, through any manager; a service name picks one of its running tasks (a Job's name works too). A finished task's output is kept until the task is deleted, so logs works on a completed Job:

legion logs web -n shop -f --tail 20
legion exec -it web -n shop -- /bin/sh       # exits with the command's code
legion port-forward web -n shop 8080:80      # localhost:8080 to the pod's 80

admin.pem is the cluster admin's. People get their own credentials and a role (view, edit or admin) in a namespace; the key is made where the command runs:

legion user create alice --role edit -n shop   # writes alice.pem; hand it over
                                               # (view may read logs; exec needs edit)
legion --identity alice.pem list services -n shop
legion delete RoleBinding alice -n shop        # takes her rights away

Nodes started with --ingress-listen 0.0.0.0:80 (and --ingress-tls-listen 0.0.0.0:443) serve Route objects: HTTP by host and path to a service's ready tasks, HTTPS with a certificate and key from a Secret the route names, which the cluster can keep filled from Let's Encrypt (tls: { secret: shop-cert, acme: { email: ops@example.org } }, validated on port 80). Any node may read those Secrets until one node is labelled as serving HTTPS; then only labelled nodes may, so label every node that serves it:

legion node label n1 legion.ingress=true    # key- removes a label

--metrics-listen 0.0.0.0:7472 on the daemon serves Prometheus metrics at /metrics: Raft, objects, tasks (each one's state, node, image and start), services, nodes, quotas, each node's ZFS space and each Volume's used, available and size from the leading manager, and each container's CPU, memory and I/O from rctl on every node. It is plain HTTP without authentication, and shows every namespace's names, so keep it on an admin network, or on the node's pod gateway (10.200.<n>.1:7472) for the node's own pods to read.

Layout

Crate Role
legion-api Object model: envelope, typed specs, admission
legion-store openraft state machine on redb, watches
legion-proto gRPC definitions (compiled with protox)
legion-manager API server, Raft transport
legion-image OCI images: registry client, ZFS layer store
legion-jail The jail runtime: containers as jails on ZFS clones, each under a supervisor
legion-sys FreeBSD operations: jails, interfaces, routes, WireGuard, rctl, pf, ZFS
legion-agent Registers the node; runs assigned tasks as pod jails
legion-net Pod bridge and interfaces, the WireGuard mesh, the node resolver, pf load balancing
legion-pki Cluster CA, node and user certificates, identities
legion Daemon and CLI

Licence

BSD-2-Clause; see LICENSE.

About

Container orchestration for FreeBSD: Swarm's operating model plus selected Kubernetes features, running workloads as jails

Resources

Stars

2 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages