boxfork on Kubernetes
This page is for a contributor who changes the code in operator/. It describes boxfork on Kubernetes in three parts:
- The base: the
Sandboxresource, the operator, the Kubernetes engine and the API service. - Phase 2: snapshots, forks and pools, with the
SandboxSnapshotresource, the node agent and the registry. - Phase 4: the moves of sandboxes off a node that Kubernetes empties, the upgrades of the node agent, and GPU node pools.
After you read it, you know how a request becomes a pod, what each part must guarantee, and what is not proven.
On Kubernetes, the API service is the daemon of one machine with a different engine: the Kubernetes engine makes Sandbox objects in place of containers.
Thus the SDKs, the CLI, the MCP server and the tool kit work with no change.
A user can also create a sandbox with kubectl.
Decisions 17, 19 and 20 in decisions.md give the reasons for this design.
The steps to install the chart are in install.md.
This page uses more names:
- A Sandbox object is one object of the custom resource
Sandbox, in the API groupboxfork.dev, versionv1alpha1. - The operator is the mode
operatorof the binaryboxfork-operator. It makes one pod for each Sandbox object. - The API service is the mode
apiof the same binary. It is the daemon of one machine, with the Kubernetes engine in place of Docker. - The Kubernetes engine is the package
operator/internal/kubeengine. It implements the engine interface of engine.md with Sandbox objects. - A SandboxSnapshot object is one object of the custom resource
SandboxSnapshot. It holds the files of one sandbox, as an image in the registry. - The node agent is the mode
agentof the same binary, in a DaemonSet. On each node, it saves the files of the sandboxes of that node. - The registry is the OCI registry that keeps the images of the snapshots. The chart can run one.
In short
Section titled “In short”- The operator, the API service and the node agent are three modes of one binary,
boxfork-operator, in a second Go module. The core binary gets no Kubernetes libraries. - The operator makes one pod for each Sandbox object, under the RuntimeClass
gvisorby default. A phase moves only forward, and a lost pod givesFailed, not a new pod. - The Kubernetes engine runs the helper through
pods/exec. It givesErrUnsupported, status 501, for what a pod cannot do, for examplePause. - With
--snapshots, the node agent saves the files of a sandbox through containerd and pushes them as an image to the registry. A sandbox from a snapshot can then start on any node. - With
--move-on-drain, the operator moves each sandbox that runs off a node that Kubernetes empties. The files go with it, but its processes do not. - The API service has one replica and one state file, and it has no GPU allocator in this version.
The parts
Section titled “The parts”The code is in a second Go module, boxfork/operator, so that the core binary gets no Kubernetes libraries.
The module uses the core packages through replace boxfork => ../.
| File | Contents |
|---|---|
| operator/api/v1alpha1/sandbox_types.go | The types Sandbox, SandboxSpec and SandboxStatus, and the labels. |
| operator/api/v1alpha1/phase.go | The phases of a Sandbox object and the moves between them. |
| operator/api/v1alpha1/snapshot_types.go | The type SandboxSnapshot, its phases and the finalizer boxfork.dev/image. |
| operator/chart/crds | The two CRDs. controller-gen makes them from the types: run go generate ./... in operator/. |
| operator/internal/controller/sandbox.go | SandboxReconciler: the operator. |
| operator/internal/controller/pod.go | podFor: the pod of a Sandbox object. |
| operator/internal/controller/status.go | observe and apply: the status of a Sandbox object from its pod. |
| operator/internal/controller/move.go | The move of a sandbox off a node that Kubernetes empties, and the PodDisruptionBudget of each Sandbox object. |
| operator/api/v1alpha1/move.go | The steps of a move, and PodName: the name of the pod after each move. |
| operator/internal/controller/podsecurity.go | CheckPodSecurity: the check of the namespace at the start of the operator. |
| operator/internal/kubeengine/engine.go | The Kubernetes engine: create, start, remove, inspect, list. |
| operator/internal/kubeengine/exec.go | Exec through the subresource pods/exec. |
| operator/internal/kubeengine/network.go | The check that the network policy has an effect. |
| operator/internal/kubeengine/volume.go | Volumes as PersistentVolumeClaims. |
| operator/internal/kubeengine/found.go | Found: the Sandbox objects that kubectl made. |
| operator/internal/kubeengine/snapshot.go | Commit, LocalImage and RemoveImage with SandboxSnapshot objects. |
| operator/internal/agent/agent.go | The node agent: the reconciler of SandboxSnapshot objects on one node. |
| operator/internal/agent/containerd.go | Containerd.Save: the files of a container, through the socket of containerd. |
| operator/internal/agent/image.go | The new layer, its limit of size, the image with that layer, and the push. |
| operator/internal/agent/origin.go | NewOrigin: the fetch of a missing layer, only from public addresses. |
| operator/internal/registry/registry.go | A small client of the OCI distribution API, with basic and bearer logins. |
| internal/sandbox/adopt.go | Manager.Adopt: the manager takes in those Sandbox objects. |
| operator/cmd/boxfork-operator | The three modes and their flags. |
| operator/chart | The Helm chart. |
| operator/Dockerfile, operator/helper.Dockerfile | The image of boxfork-operator and the helper image. |
| scripts/kind-gvisor.sh | A kind cluster with gVisor on each node. WORKERS=2 gives two worker nodes. |
| scripts/proof/k8s-phase1.sh, scripts/proof/k8s-phase2.sh, scripts/proof/k8s-phase4.sh | The proof scripts of phases 1, 2 and 4. |
| scripts/proof/control-k8s.sh | The proof script of the join of two clusters to one control service. |
How the parts connect
Section titled “How the parts connect”A client calls the API service with the HTTP API of boxfork.
The manager in the API service does the same work as on one machine, and it calls the Kubernetes engine.
The engine creates a Sandbox object and waits until its status says Running.
The operator sees the new object and creates its pod.
Then the engine runs the helper in the pod through pods/exec, and the manager talks to the helper as on Docker.
Thus the SDKs, the CLI, the MCP server and the tool kit work with no change.
A user can also create a Sandbox object with kubectl.
The operator makes its pod in the same way, and the API service takes it in.
Then a client can use it through the HTTP API.
The Sandbox resource
Section titled “The Sandbox resource”The spec gives what the sandbox needs. The spec cannot change after the create: a CEL rule refuses a change. The reason is that a pod cannot change its image, its runtime or its resources.
| Field | Meaning | Default |
|---|---|---|
image |
The container image. | It is required. |
command |
The first process. | The helper in its init mode. |
env |
The environment of the first process. | None |
cpu |
The CPUs, as a Kubernetes quantity. The pod gets the same request and limit. | No limit |
memoryMB |
The memory in MiB. The pod gets the same request and limit. | No limit |
shmMB |
The size of /dev/shm in MiB, in memory. |
The default of the runtime |
pidsLimit |
The limit of processes. The pod does not apply it. See “Limits”. | None |
gpus |
The number of whole GPUs, as the resource nvidia.com/gpu. |
0 |
runtimeClassName |
The RuntimeClass of the pod. An empty value means the default runtime of the node. | gvisor |
network |
default or none. none puts the pod under the network policy that blocks all traffic. See “A sandbox without network”. |
default |
volumes |
PersistentVolumeClaims to mount: name, mountPath, readOnly. |
None |
labels |
The labels of the sandbox, as the manager gives them. | None |
The name of a Sandbox object has a maximum of 63 characters, because the pod gets the same name.
The status gives phase, podName, nodeName, podIP, runtime (the RuntimeClass) and message.
It also gives moves, saves, image and move for the moves in “Moves off a node that Kubernetes empties”.
It also gives the conditions Scheduled, Ready and NetworkPolicy. See “The network” for NetworkPolicy.
kubectl get sandboxes shows the phase, the node, the address and the runtime.
Phases
Section titled “Phases”A phase moves only forward. Phase.Next refuses a move back.
When the pod of a Sandbox object that ran is lost, the operator sets Failed and does not make a new pod.
The reason is that a new pod has no files of the old pod. A client that sees Running expects its files.
A move to a different node is the one exception. The node agent saved the files first, and the phase stays Running while the next pod starts from them.
The message tells why a sandbox does not run.
For a pod that the scheduler cannot place, the message starts with not scheduled: and gives the text of the scheduler.
For a container that waits, it gives the reason and the text of the kubelet, for example ErrImagePull.
The engine stops its wait at once for the reasons that do not end by themselves, in StuckReasons.
The operator
Section titled “The operator”The operator watches Sandbox objects and their pods in one namespace.
For each change, SandboxReconciler.Reconcile does these steps:
- It reads the Sandbox object. It ignores an object that Kubernetes deletes.
It also ignores an object with the label
boxfork.dev/releaseof a different release (--release). - If the object has no pod and its phase is empty or
Pending, it reads the object again. It reads from the API server, not from its cache. The cache can be late, and a pod that ran must not start again. If the phase is still empty orPending, it creates the pod. The pod has an owner reference to the object. - If the object has no pod after it ran, it sets
Failedwith the message “the sandbox’s pod was deleted”. - If a pod with the same name has a different owner, it does not touch that pod. The phase stays
Pending, and the message names the pod. - If the pod runs, and
--network-policy-checkis set, it checks that the network policy applies to the pod. See “The network”. - With
--move-on-drain, it makes the PodDisruptionBudget of the object. Without it, or when the sandbox ended, it deletes that budget. It does the next step of a move. See “Moves off a node that Kubernetes empties”. - It writes the status only when the status changed.
The pod has the name of the object. After a move, the name also has the number of moves: see PodName.
A second run with no change writes nothing. The tests in operator/internal/controller/sandbox_test.go prove this with the fake client of controller-runtime.
The operator can make pods in its namespace. A pod with a hostPath, privileges or the network of the node is root on that node.
Thus the namespace must admit only pods of the Pod Security level baseline. The level restricted refuses sandboxes, because they run as root.
At its start, with --pod-security-check (on in the chart), the operator does two dry runs of a pod create. A dry run makes nothing:
- The pod of a sandbox. The namespace must admit it.
- The same pod with a
hostPath. The namespace must refuse it.
If one of the two results is wrong, the operator stops with the reason, and does not start again until you correct the namespace. The check holds for each cause of the refusal: the label of the namespace, the default level of the cluster, or a different admission policy.
The operator deletes only the pods of a move. See “The steps of a move” and “A move that cannot end”.
When a client deletes a Sandbox object, the garbage collector of Kubernetes deletes the pod through the owner reference.
The owner reference has blockOwnerDeletion, so a deletion in the foreground waits for the pod.
The pod of a Sandbox object has these properties:
| Property | Value | Reason |
|---|---|---|
runtimeClassName |
From the spec, gvisor by default |
The isolation of decision 2. |
restartPolicy |
Never |
A sandbox that ended does not start again with empty processes. |
| Service account token | Not mounted | Code in a sandbox gets no access to the Kubernetes API. |
Init container boxfork-helper |
The helper image copies /boxfork-guest to /.boxfork/guest |
The helper is in each sandbox, whatever its image. |
Volume at /.boxfork |
An emptyDir |
The init container and the sandbox container share it. |
| Resources | The request is equal to the limit, for CPU, memory and nvidia.com/gpu |
A sandbox gets what it asks for, and no more. |
ephemeral-storage |
The limit and the request are --sandbox-ephemeral-storage, 10Gi in the chart |
The kubelet evicts a sandbox that writes more to its disk, and the node keeps that space for it. |
| Toleration | nvidia.com/gpu with the effect NoSchedule, only when gpus is more than 0 |
GPU nodes often have this taint. |
| Capabilities | NET_RAW, MKNOD and AUDIT_WRITE dropped, no privilege escalation |
The same as the containers of the daemon. |
| Seccomp profile | RuntimeDefault, for the pod |
The default filter of system calls, as on Docker. gVisor applies it in the sandbox too (--oci-seccomp of runsc, on by default). |
| Labels | boxfork.dev/sandbox, boxfork.dev/network, boxfork.dev/image-hash, and boxfork.dev/release when the object has it |
The network policies select pods by the first two labels. |
| Topology spread | maxSkew: 1 on kubernetes.io/hostname among the pods with the same boxfork.dev/image-hash, ScheduleAnyway |
The copies of a fork go to different nodes when the scheduler can put them there. |
imagePullSecrets |
The secret of --image-pull-secret, when the operator has one |
The kubelet logs in to the registry of the snapshots. |
The Kubernetes engine
Section titled “The Kubernetes engine”The engine keeps each guarantee of the engine interface that a sandbox on Kubernetes can keep.
For the others, it gives an error that matches engine.ErrUnsupported. The HTTP API gives the status 501 and the code unsupported for this error.
| Method | What it does on Kubernetes |
|---|---|
Info |
Product is kubernetes. Runtimes contains runsc only when the RuntimeClass of gVisor exists. |
EnsureImage |
It pulls nothing: the kubelet pulls the image when the pod starts. It gives linux and the architecture of the API service. |
Create |
For the image of a snapshot, the pod gets the image of its SandboxSnapshot object, by digest. It creates a Sandbox object with the labels boxfork.dev/created-by=api and boxfork.dev/release=<release>. A name that Kubernetes does not accept, such as boxfork-sbx_…, gets - in place of each bad character and a part of its hash. For a sandbox without network, see “A sandbox without network”. |
CopyIn |
It accepts only the helper, and only checks that the object exists. The init container puts the helper in place. |
Start |
It waits until the phase is Running. After the network check passed, it also waits for the condition NetworkPolicy. It stops at once on a phase that ended or on a reason in StuckReasons. After --start-timeout, the error gives the cause. |
Exec |
It runs the process in the pod with the name of the object, through pods/exec. It uses a WebSocket, or SPDY when the server has no WebSocket. |
Remove |
It deletes the object in the foreground and waits until the object and its pod are gone. |
Inspect, List |
They read Sandbox objects. List gives only the objects that the API service of this release made. |
EnsureNetwork |
It checks that the network policy has an effect. See “The network”. |
CreateVolume, RemoveVolume, Volumes |
A volume is a PersistentVolumeClaim with the annotation boxfork.dev/volume-name and the labels of Create. A claim that a Sandbox object mounts is in use. |
Commit |
With --snapshots, it makes a SandboxSnapshot object and waits until the node agent saved the files. See “Snapshots and forks”. Without --snapshots, it gives ErrUnsupported. |
LocalImage |
For the image of a snapshot, it gives the image of its SandboxSnapshot object in the phase Ready. For all other images, it gives ErrNotFound. |
RemoveImage |
It deletes the SandboxSnapshot object and waits until it is gone, after the agent deleted the image from the registry. A missing object is not an error. |
Pause, Unpause, NetworkSubnets |
ErrUnsupported. |
The helper gives the environment, the working directory and the user to each command of a client.
Thus the engine refuses an exec with its own environment or working directory, because pods/exec cannot set them.
It accepts the users "", 0 and root. pods/exec runs the process as the user of the container.
An exec continues when the context of its caller ends, as the engine interface requires.
The engine runs the process only in the pod with the name of the Sandbox object, and only when the object controls that pod. The status also gives the name of the pod, but a person who can write the status can change it.
Close ends the standard input first. Most processes then stop, and their stream ends without an error.
After 2 seconds (CloseGrace), the engine cuts the stream of a process that still runs.
The reason is that client-go logs errors when the engine cuts a stream that is still open.
The API service
Section titled “The API service”boxfork-operator api is the daemon of manager.md with a different engine.
At the start, it does these steps:
sandbox.Detectchecks gVisor with a test sandbox. With--isolation=gvisor, the default of the chart, the API service does not start without gVisor.Manager.CheckNetworkchecks the network policy.Manager.CheckSnapshotswrites a file in a test sandbox, saves its files, and starts a second sandbox from them. If the file is not there, or the agent fails, it switches off snapshot, fork and suspend. Without--snapshots, it always switches them off.Manager.Adopttakes in the Sandbox objects that kubectl made, then again each--adopt-every.Manager.Watchstarts the sweep, and the HTTP server starts on port 7070.
The state file is on a PersistentVolumeClaim, at /var/lib/boxfork/state.json.
The Deployment has one replica and the strategy Recreate: the old pod stops before the new pod starts.
The reason is decision 8: one state file, and one writer for it.
The API key comes from a Secret, through the variable BOXFORK_API_KEY. It must have a minimum of 16 characters.
If you do not give a Secret, the chart makes a new key and keeps it at each upgrade.
The API service has no GPU allocator in this phase.
A request for a GPU through the HTTP API gets the error “this host has no GPUs”.
A Sandbox object that kubectl makes can ask for GPUs with gpus.
One binary with three modes
Section titled “One binary with three modes”The operator, the API service and the node agent are three modes of one binary, in one image. The reason is one build, one image to release, and one copy of the types. Each mode has its own service account, so the rights of each mode are separate. See “Rights”. The cost is a larger image for the operator and the agent, which contain the code of the manager.
Join a control service
Section titled “Join a control service”The API service can join a control service, boxfork-control, as one cluster. It joins in the same way as a daemon on one machine.
Then one address serves the sandboxes of this cluster, of other clusters and of daemons.
The page clusters.md tells a user how to set it up. The page control.md tells how the join works.
boxfork-operator api takes the join flags of boxfork serve:
--join, the address of the control service--join-token, or the variableBOXFORK_JOIN_TOKEN--cluster-nameand--cluster-label--join-caand--join-insecure
The API service reads these flags before it does anything else.
Thus a wrong value stops the start at once, with the reason in the log.
An example is an http:// address of a different machine without --join-insecure.
The API service joins after the checks at its start, when it starts to serve. Thus the control service sends requests only to an API service that is ready.
serve in operator/cmd/boxfork-operator/main.go serves the API on port 7070, and also through the connections to the control service.
The connections get api.Server.TrustedHandler: the routes without the check of the API key. The join token made the connections trusted.
serve makes the two handlers before either of them serves, because the first handler fills in the defaults of the server.
The chart gives the flags from these values:
| Value | Flag | Meaning |
|---|---|---|
join.control |
--join |
The address of the control service, for example https://control.example.com:7443. Empty: the API service joins nothing. |
join.clusterName |
--cluster-name |
The name of the join token. It is required with join.control. |
join.labels |
--cluster-label |
The labels of the cluster, for example {cloud: aws, region: eu-west-1}. If the token fixed the labels, give exactly those labels. Each key and value must be a string. YAML reads 1000000, 1.10, 012, yes or on without quotes as a number or a boolean, and the chart then stops with an error. Put such a key or value in quotes, or give it with --set-string. |
join.tokenSecret.name, join.tokenSecret.key |
BOXFORK_JOIN_TOKEN |
The Secret with the join token, and its key, token by default. The Secret is required with join.control. |
join.caSecret.name, join.caSecret.key |
--join-ca |
A Secret with the PEM certificates of a certificate authority, and its key, ca.crt by default. The pod mounts it read-only in /etc/boxfork/join-ca. Empty: the authorities of the system. |
join.insecure |
--join-insecure |
Permit an http:// address of a different machine. |
No value of the chart holds the join token. Thus helm template and the history of the release do not contain it.
The chart does not see a change to a Secret. After you change the token or the certificate authority, restart the API service.
The tests in operator/chart/join_test.go check the flags, the Secrets, the mount, and the refusal of a join without a name or a token.
The GPUs that the API service reports
Section titled “The GPUs that the API service reports”The API service reports no GPUs to the control service, also on a cluster with GPU nodes. The control service uses the GPUs of a cluster for one decision: whether a sandbox with a GPU can go there. The API service has no GPU allocator in this version, so it refuses each sandbox with a GPU through the HTTP API. A report of the GPUs that the cluster can schedule thus sends GPU work to a cluster that refuses it. The report must give what the HTTP API of the cluster can give, and that is no GPU.
The other choice was the allocatable nvidia.com/gpu of the nodes, less the GPUs that the pods of Sandbox objects ask for. It was not selected for the reason above.
The cost: GET /v1/clusters of the control service shows no GPUs for a cluster with GPU nodes.
Also, the rule that keeps the clusters with GPUs for GPU work does not keep such a cluster.
When the API service gets a GPU allocator, the report will come from that allocator, as on a daemon.
The network of the join
Section titled “The network of the join”The API service opens the connections to the control service. A network policy lets in the packets of a connection that the pod opened.
The policy <release>-api limits only what comes in. Thus the API service reaches the control service at each address.
The join changes no network policy, and lets nothing more in.
A test in operator/chart/join_test.go renders the policies with and without a join, and finds them the same. The cost is the cost of the policy before the join: the API service can send traffic to all addresses. If a different policy blocks the traffic out of the namespace, permit the traffic of the API service to the control service.
When the control service stops
Section titled “When the control service stops”The API service and its sandboxes continue. Clients of port 7070 can use the API service with its own API key. The API service tries to connect again, first after 250 ms, then each 5 seconds at the most. It logs the same error only one time. When the API service stops, its connections close, and the control service waits for it. See control.md.
Snapshots and forks
Section titled “Snapshots and forks”The manager of the API service does snapshots, restores, forks and pools as on one machine.
It calls Commit, LocalImage, RemoveImage and Create of the engine, and the Kubernetes engine implements them with SandboxSnapshot objects.
The API service needs --snapshots for this. The chart sets it with snapshots.enabled, which is true by default.
The SandboxSnapshot resource
Section titled “The SandboxSnapshot resource”| Field | Meaning |
|---|---|
spec.sandbox |
The name of the Sandbox object to save, in the same namespace. It cannot change. |
status.phase |
Pending, then Ready or Failed. A phase moves only forward, and Ready and Failed are ends. |
status.image |
The image in the registry, by digest: <registry>/<namespace>/boxfork-snapshot:<name>@sha256:…. |
status.nodeName |
The node whose agent saved the files. That agent also deletes the image. |
status.attempts |
The number of saves that the agent started. It is 1 for each save that ended. |
status.sourceImage, status.layers, status.size |
The image of the sandbox, the number of layers of the new image, and the bytes of the new layer. |
status.message |
Why the save failed, or why the image is not deleted yet. |
The name has a maximum of 63 characters, because it is also the tag of the image.
The engine names the object snap- and the ID of the snapshot without snap_.
The object gets the labels boxfork.dev/created-by=api and boxfork.dev/release, as a Sandbox object does.
kubectl get sandboxsnapshots (short name sbxsnap) shows the phase, the sandbox and the node.
The node agent
Section titled “The node agent”Agent.Reconcile does these steps for each SandboxSnapshot object:
- It ignores these objects:
- an object with the label of a different release
- an object at an end
- an object that the agent of a different node took
- It reads the Sandbox object and its pod from the API server, not from a cache. The pod must have the name of the Sandbox object, and the Sandbox object must control it.
- If the files can never be saved, it sets
Failedwith the reason. Examples: no Sandbox object, a sandbox that ended, a pod with a different owner. If the pod does not run yet, it looks again after 2 seconds. If the pod is on a different node, it does nothing. - It adds the finalizer
boxfork.dev/image, and it writesPending, its node and one attempt. It does this before it saves anything. - It saves the files of the container
sandbox. See “The save”. - It pushes the image, and it writes
Readyand the image. If a step fails, it writesFailedand the reason.
A step can stop at any point, for example when the agent restarts.
The next run starts again from step 1. An object in Pending on its own node with one attempt is a save that did not end.
The agent was killed or evicted in it, and the same save can stop the next agent too.
Thus the agent does not save it again. It sets Failed with the reason, and the client can make a new snapshot.
At its start, before it saves anything, the agent resumes each container sandbox of its namespace on its node that is paused.
On Kubernetes nothing else pauses a sandbox: the API gives unsupported for a pause.
Thus such a container is one that an agent left paused when it stopped in a save.
The save
Section titled “The save”Containerd.Save uses the socket of containerd, in the namespace k8s.io. The agent itself mounts nothing.
- It takes a lease of containerd. All that the save writes in containerd lives only as long as the lease.
- It reads the container, and the manifest and the configuration of its image. It finds the image by the image ID that the kubelet gives for the container, not by its name. The tag can name a different image on the node by then. The chain ID of the layers of that image must be the parent of the snapshot of the container. If it is not, the save fails with the reason.
- It pauses the task of the container. It asks the diff service of containerd for the difference between the files of the container and its image. Then it resumes the task in all cases. It also resumes it when the diff failed or when the task was paused before. Two saves of one container at the same time take turns. A lock for each container holds the second save until the first save resumed the task.
- It reads the difference, a tar archive. It removes each entry at or below a mount point of the container.
Examples are
/etc/hosts,/etc/resolv.conf,/dev/shm,/.boxforkand the volumes. The kubelet gives these, and they are not files of the sandbox. - It compresses the rest with gzip into a file in
/tmp, and it calculates the two digests of the layer. The limit is--max-size, from the valuesnapshots.maxSize, 4 GiB by default. If the compressed layer gets larger, the save stops and fails with the reason. The file never gets larger than that limit. - It puts the new layer on top of the layers of the image. The new image has a new configuration and a new manifest. It keeps the media types of the image, Docker or OCI. The time in the new configuration is the time at which the SandboxSnapshot object was made. Thus a second save of the same files gives the same image.
With overlay2 = "none", gVisor writes the files of a sandbox to the snapshot of containerd, so the difference has them. See “gVisor on the nodes”.
The push sends only the layers that the registry does not have. It reads a layer of the image from containerd.
If containerd deleted that layer after the unpack (discard_unpacked_layers, as on kind), it gets the layer again from the registry of the image.
That registry must give the image without a login. If it does not, the save fails and the message tells why.
The name of that registry comes from the image of the sandbox, and the registry gives the address of its login (the realm).
Thus a user can point the agent to an address in the cluster. The agent connects only to public addresses for this fetch.
It refuses loopback, private, link-local and shared addresses, and the ranges of networkPolicy: the blocked addresses, the pods, the services and the nodes.
It checks the address that the name gives, before it connects, also for the realm.
For a registry in your network, give the name of its host in snapshots.agent.originAllow.
The deletion of a snapshot
Section titled “The deletion of a snapshot”RemoveImage deletes the SandboxSnapshot object and waits until it is gone.
The finalizer keeps the object until the agent deleted the manifest from the registry, by its digest and by its tag.
The agent of the node in status.nodeName does this at once.
If that agent does not do it, the agent of a different node does it after --grace, 2 minutes by default. This is for a node that is gone.
An object that failed before it had the finalizer goes at once.
The registry deletes the manifest. The layers stay on its disk until you run its garbage collection.
The registry
Section titled “The registry”With snapshots.registry.create, the chart runs a registry in the namespace of the agent.
The pod of the registry has two containers of the image registry:3 of the CNCF Distribution project, as user 65532.
The two use the same PersistentVolumeClaim of 10 GiB:
| Container | Port | Service | Login | Rights |
|---|---|---|---|---|
push |
5000 | <release>-registry, in the cluster only |
The push login, in the Secret <release>-registry-login |
Push and delete. Only the agent pods can reach it. |
pull |
5001 | <release>-registry-pull, node port 30500 |
The pull login, in the Secret <release>-registry-pull-login |
Read only. It has no method that changes or deletes an image (405), and it mounts the volume read-only. |
Each container knows only its own login. The kubelet has only the pull login, in the Secret <release>-registry-pull of the type kubernetes.io/dockerconfigjson.
The chart makes the two passwords once, at random, and keeps them at each upgrade.
The pull container reads each layer from the disk, with no cache, because the push container deletes.
The agent pushes to the Service <release>-registry. The nodes pull from localhost:30500.
The network policy of the registry lets in the agent pods at the port 5000, and the ranges of networkPolicy.nodeCIDRs at the port 5001.
With no nodeCIDRs, it lets in all addresses but the ranges of the pods at the port 5001.
Kubernetes always lets in the traffic from the node of the pod, so a pull from that node also works.
containerd uses HTTP for localhost and needs no change to its configuration.
The pull needs kube-proxy to send the traffic of a node port on localhost to the registry.
The iptables mode of kube-proxy does this by default. With a different mode, or a network plugin that replaces kube-proxy, use a registry of your own.
For a registry of your own, set snapshots.registry.create=false and these values:
| Value | Meaning |
|---|---|
snapshots.registry.pull |
The registry and a path in it, as the nodes pull from it. Example: registry.example.com/boxfork. |
snapshots.registry.endpoint |
The address that the agent pushes to. It is https:// and the host of pull by default. |
snapshots.registry.loginSecret |
A Secret with the keys username and password, for the agent, in the namespace of the agent. |
snapshots.registry.pullSecret |
A Secret of the type kubernetes.io/dockerconfigjson, for the kubelet, in the namespace of the release. |
The operator gives the pull Secret to each sandbox pod as imagePullSecrets. The Secret is not mounted in the pod.
helm template, as GitOps tools use it, cannot see the logins in the cluster. It makes new logins each time.
An annotation on the pods of the agent and the registry holds a sum of their logins. Thus a new login starts them again.
To keep the logins, make the three Secrets yourself and give all three names, with snapshots.registry.create:
| Value | Secret |
|---|---|
snapshots.registry.loginSecret |
The push login: username, password and htpasswd, in the namespace of the agent |
snapshots.registry.pullLoginSecret |
The pull login: htpasswd, in the namespace of the agent |
snapshots.registry.pullSecret |
The pull login for localhost:30500, of the type kubernetes.io/dockerconfigjson, in the namespace of the release |
After you change these Secrets, restart the agent and the registry. The chart cannot see the change.
Forks, pools and suspensions
Section titled “Forks, pools and suspensions”A fork is a snapshot and the sandboxes that start from it, as on one machine.
The copies of a fork have the same image, so the topology spread of their pods puts them on different nodes when the scheduler can.
The spread is ScheduleAnyway: a cluster with one node still gets all copies.
Pools are the pools of the manager, on the Kubernetes engine. There is no SandboxPool resource. Decision 19 gives the reason.
A pool of a snapshot works, because LocalImage finds the snapshot.
A suspension also saves the files through Commit, so --snapshots turns it on.
The suspension deletes the Sandbox object. The next call makes a new Sandbox object from the saved files, on any node.
Moves off a node that Kubernetes empties
Section titled “Moves off a node that Kubernetes empties”Kubernetes empties a node for an upgrade, a repair, or a smaller node pool.
With --move-on-drain, the operator moves each running sandbox off such a node to a different node.
The files of the sandbox go with it. Its processes do not, as with a snapshot. See decision 7.
The Sandbox object, and thus the ID of the sandbox, does not change. Decision 20 gives the reasons and the cost.
The chart sets --move-on-drain with snapshots.moveOnDrain, true by default, when snapshots.enabled is true.
The budget of each sandbox
Section titled “The budget of each sandbox”kubectl drain, the cluster autoscaler and Karpenter evict pods through the eviction API.
The operator gives each Sandbox object a PodDisruptionBudget with the same name and minAvailable: 1.
Its selector takes the pods with the label boxfork.dev/sandbox=<name> and without the label boxfork.dev/move.
Thus the eviction API refuses the eviction of a sandbox pod that runs, with the status 429.
kubectl drain tries again each 5 seconds, until the pod is gone.
The budget has an owner reference to the Sandbox object, and goes with it.
The next pod of a move has the label boxfork.dev/move until the move ends in it. Thus the budget counts only the pod that the sandbox runs in.
With the next pod in the budget, a drain can evict the paused pod that leaves before the move ends. The move then cannot go back.
The eviction API does not use the budget for a pod that waits or that ended. Such a pod has no files to save, and Kubernetes evicts it at once. The operator then makes its pod again with the same name, because the sandbox did not run yet. The scheduler puts it on a different node.
The marked nodes
Section titled “The marked nodes”The operator watches the nodes. A node is marked when one of these items is true:
| Item | Who sets it |
|---|---|
spec.unschedulable |
kubectl cordon and kubectl drain |
The taint node.kubernetes.io/unschedulable |
Kubernetes, for a cordon |
The taint ToBeDeletedByClusterAutoscaler |
The cluster autoscaler, before it removes the node |
The taint karpenter.sh/disrupted |
Karpenter v1, before it removes the node |
The taint karpenter.sh/disruption |
Karpenter before v1, before it removes the node |
The cluster autoscaler and Karpenter do not mark a node with a sandbox for a scale-down, a consolidation or a drift. In their plan, they first check that they can evict each pod of the node, and the budget of the sandbox refuses the eviction. Karpenter marks a node with a sandbox only for a forced disruption. For example, the node expired, or a person deleted the node or its NodeClaim. See “GPU node pools”.
Only a taint with the effect NoSchedule or NoExecute counts. The table is leavingTaints in move.go.
A node that does not answer is not marked: its agent cannot save the files.
The scheduler already keeps new pods off a marked node, so no new sandbox goes there.
The steps of a move
Section titled “The steps of a move”status.move holds the move that is under way:
step:Saving,StartingorFailedfrom,podandpodUID: the node that the sandbox leaves, and the pod that leavessnapshotandimage: the SandboxSnapshot of the move, and its image when it isReadyattempts,started,sinceandmessage.sinceis the time when the move got to its step.retries: how many times the move started again after it failed
The operator does one step in each run, from what exists, so a run after a crash comes to the same end:
- Saving. The operator writes the move in the status. In the next run, it makes a SandboxSnapshot of the sandbox with the name
<name>-move<saves>. The node agent of the old node saves the files as in “The save”. It keeps the sandbox paused: see “The pause of a move”. The SandboxSnapshot has the labelboxfork.dev/created-by=operatorand an owner reference to the Sandbox object, withoutblockOwnerDeletion. - When the SandboxSnapshot is
Ready, the step isStarting, andimageholds the image of the snapshot. - Starting. The operator makes the next pod with that image. The pod that leaves stays, paused, so the sandbox has two pods for a short time.
The next pod has the name
<name>-m<moves+1>, but the host name of the sandbox stays<name>. The next pod has the labelboxfork.dev/movewith the name of the SandboxSnapshot. - The next pod runs, and the operator sees the network policy in effect on it. Then the operator deletes the pod that leaves, with its UID as a precondition.
Then the operator adds 1 to
status.moves, writes the image instatus.image, and removesstatus.move. The drain then finds no pod of the sandbox on the node. In the same run, the operator removes the labelboxfork.dev/movefrom the next pod. The budget then counts the next pod.
Until step 4, status.podName, status.nodeName and status.moves are those of the pod that leaves.
The next pod gets a new name because kubectl drain evicts a pod by its name, with no UID.
A next pod with the old name on a different node gets the same eviction, and its budget refuses it for ever.
The engine, the node agent and the operator find the pod with Sandbox.Pod, from the name and status.moves. They also check that the Sandbox object controls the pod.
A move ends only in a next pod with the label and the image of its own SandboxSnapshot. A different pod with the name of the next pod has older files, for example the next pod of an earlier move. The move waits until that pod is gone. In each run, the operator deletes each pod that the Sandbox object controls and does not use. These are all pods except:
- the pod of the sandbox,
Sandbox.Pod - the next pod of the move in the step
Starting
The cache of the operator can be behind the end of a move. Thus the operator reads the Sandbox object again from the API server before it deletes such a pod.
A move that cannot end
Section titled “A move that cannot end”A move never leaves a sandbox without a pod: the operator deletes the pod that leaves only when the next pod runs.
A move that cannot end goes back. The operator deletes the next pod, if there is one, and the step is Failed, with the reason in message.
The node agent then lets the sandbox continue on its node, and the drain waits. These events make a move go back:
| Event | When |
|---|---|
| A save fails | After 3 tries. Each try makes a new SandboxSnapshot. |
| The save does not end | After --move-save-timeout: snapshots.saveTimeout in the chart, 5 minutes by default |
| No node agent runs on the node | 30 seconds after the start of the save, if no agent took the SandboxSnapshot. The operator lists the agent pods in --agent-namespace. |
| The API server refuses the next pod, for example for a quota, or a pod of a different owner has its name | At once |
| The API server does not make the next pod because of a different error, for example a timeout | After --move-start-timeout. Until then, the operator tries again, after 1 to 30 seconds. |
| The next pod does not run: it waits for a node, or the kubelet cannot pull its image | After --move-start-timeout: snapshots.moveStartTimeout in the chart, 5 minutes by default |
| The next pod ends before it runs | At once |
When the move goes back, the operator reads the next pod from the API server, not from its cache. The cache can be without a pod that the operator made a short time before.
A failed move starts again from a new save while the node stays marked: after 30 seconds, 1 minute and 2 minutes.
retries counts these tries. After 3 more tries, the move stays Failed, and the drain waits until a person acts.
The message of a failed move gives the time of the next try.
To keep a sandbox on its node after a failed move, uncordon the node. The operator then removes the failed move.
An uncordon also stops a move in the step Saving or Starting. The operator deletes the next pod, and the sandbox continues on its node.
saves counts each SandboxSnapshot of a move, so a later move never gets the name of a SandboxSnapshot that failed.
If the pod that leaves is gone before the move ends, the saved files are all that the sandbox has:
- In the step
Starting, the move continues, and the next pod starts from the saved files. This is also true when the next pod does not run in time. - In the step
Saving, the move continues to wait for the save. If the save fails, the sandbox fails, as with a pod that was deleted.
The phase stays Running through the move. A sandbox that ends while its files are saved is not moved.
The SandboxSnapshots of the moves stay until the Sandbox object is deleted, because a later move saves on top of their images.
The order of the pods
Section titled “The order of the pods”The next pod starts before the operator deletes the pod that leaves. This order has these results:
- A ResourceQuota of the namespace must have space for one more pod, with its CPU, memory and GPUs. If the quota refuses the next pod, the move fails, and the sandbox stays on its node.
- A volume with the access mode
ReadWriteOncestays attached to the old node while the pod that leaves uses it. The next pod on a different node cannot attach the volume, and waits. Thus a sandbox with such a volume does not move. The move fails after--move-start-timeout, and the drain waits. A volume with the access modeReadWriteManydoes not have this problem. - A volume on storage that only one node reaches, such as
local-path, keeps the next pod off the other nodes. The move fails after--move-start-timeout.
The other order, the delete first, puts the sandbox at risk: a next pod that cannot start leaves the sandbox without a pod.
Moves off
Section titled “Moves off”With snapshots.moveOnDrain=false, the operator deletes the PodDisruptionBudget of each Sandbox object, if the operator made it.
A drain then evicts the sandbox pods, and the sandboxes lose their files. The operator also ends each move under way:
- In the step
SavingorStarting, the operator deletes the next pod, and the sandbox continues on its node. - If the pod that leaves is gone and the files are saved, the next pod starts from them. The operator reads the SandboxSnapshot: if it is
Ready, the move continues, also in the stepSaving. - If the pod that leaves is gone and the files are not saved, the operator removes the move. The sandbox fails, as with a pod that was deleted.
- The operator removes a
Failedmove.
Thus the operator keeps the right to delete pods and PodDisruptionBudgets when moves are off. The operator also deletes the budget of a sandbox that ended.
The pause of a move
Section titled “The pause of a move”For a move, the node agent pauses the sandbox for the diff, as for a snapshot, and keeps it paused after the diff.
The agent reads the move in the status of the Sandbox object, so the operator writes the move before it makes the SandboxSnapshot.
The agent fails a SandboxSnapshot of a move when no move holds the pod, and saves nothing.
Thus no file changes after the save, and the next pod has each write that a client saw end.
The agent looks at each paused sandbox every 2 seconds (--thaw-every), and does one of these steps:
- It keeps the sandbox paused while a move in the step
SavingorStartingholds the pod. - It resumes the sandbox when no move holds the pod: the move failed, went back, or was let go.
- It kills the sandbox when its pod is deleted or gone. If containerd refuses to kill the paused task, the agent resumes it and kills it at once.
gVisor refuses to send a signal to a paused sandbox, so the kubelet cannot stop the pod of a paused sandbox. The agent does the kill for it.
A new agent does the same steps at its start, for each paused sandbox of the namespace, before it saves. Thus an agent that stops in a move does not leave a sandbox paused.
While no agent runs on the node, a paused sandbox stays paused. The move then fails after --move-save-timeout, and the sandbox continues when the next agent starts.
Caution: Do not remove the node agent while a move holds a sandbox paused, for example with snapshots.enabled=false. The sandbox then stays paused, and the operator cannot resume it.
If this occurs, install the node agent again. At its start, the agent resumes each paused sandbox that no move holds, and kills the sandbox of each deleted pod.
The API service during a move
Section titled “The API service during a move”Engine.Exec holds a new process while status.move is in the step Saving or Starting.
It waits until the move ends, for a maximum of --start-timeout. Then it starts the process in the next pod, or in the old pod if the move went back.
If the move does not end in that time, the error gives the node and the message of the move.
The manager sends most commands over the kept connection of the sandbox (decision 13): a process in the pod that leaves.
Engine.Exec stops each stream to a pod that a move holds. It also stops each stream to a pod that is not the pod of the sandbox now.
The command then fails with an error. The next command opens a new connection through Engine.Exec, which waits.
The process of a stopped command does not stop. It is paused with its pod, and it continues if the move fails. Thus the error tells the client that the command possibly ran, in part or to its end. The client must run the command again only if that is safe.
Each open stream reads its Sandbox object every 250 milliseconds (--poll). It reads the object from a cache that a watch of the API service keeps, not from the API server.
If the read fails, the stream stops too, because a move can hold its pod. The API service does not start if the watch does not start in one minute.
Thus a command that ends before the pause has its writes in the save. A command that does not end before the pause fails, and the client sees the failure. A write that the client saw end is never lost.
Engine.Inspect and Engine.List give the sandbox as running, so the manager does not remove it.
Engine.Found continues to give Manager.Adopt a Sandbox object that kubectl made while it moves, also before the policy applies to the next pod.
A snapshot that a client asks for during a move waits until the move ends.
Upgrades of the node agent
Section titled “Upgrades of the node agent”The DaemonSet of the agent has the strategy RollingUpdate with maxUnavailable: 1 and maxSurge: 0.
Thus an upgrade replaces the agent of one node at a time. The saves on the other nodes continue.
A save on that node waits for its next agent, because the SandboxSnapshot has no node until an agent takes it.
A stopped agent ends the save that it is in. The save does not use the context of the agent process, and the manager of controller-runtime waits for each run of Agent.Reconcile.
snapshots.agent.stopGracePeriodSeconds, 330 by default, is the grace period of the pod and --stop-grace of the agent.
The agent stops to wait 5 seconds before the end of that time, and the kubelet then kills it.
A save that the kubelet kills fails as in “The node agent”, and the move makes a new SandboxSnapshot.
The operator and the API service are Deployments with one replica. A move continues after a restart of the operator, from status.move.
The Deployment of the operator has the strategy Recreate: the old pod stops before the new pod starts.
Also, the operator reconciles only while it holds the lease <release>-boxfork-operator (--leader-elect), and it releases the lease when it stops.
Thus two operators never move one sandbox at the same time. Two operators can each delete one of the two pods of a move.
GPU node pools
Section titled “GPU node pools”The API service refuses a sandbox with a GPU in this version. See “The GPUs that the API service reports”. A Sandbox object that kubectl made can ask for GPUs with gpus.
These items are ready for GPU sandboxes:
- A sandbox that waits for a GPU has a pod in the phase
Pendingwith a request ofnvidia.com/gpu. The conditionPodScheduledof the pod isFalse, with the reasonUnschedulable. The cluster autoscaler adds a node to a node group for such a pod. boxfork needs nothing more for this. - The pod tolerates the taint
nvidia.com/gpuwith the effectNoSchedule, as GPU node pools often have. - For a node group that can scale from zero, the template of the node group must give
nvidia.com/gpu. Each cloud provider of the cluster autoscaler has a way to give it. The node also needs gVisor before the pod starts. See “gVisor on the nodes”. - The cluster autoscaler does not remove a node with a sandbox, because the budget blocks the eviction. Karpenter does not remove such a node for a consolidation or a drift.
- To remove a GPU node, cordon or drain it. The operator moves each GPU sandbox.
If no other GPU node is free, the next pod waits, and the cluster autoscaler adds one. The old pod stays, paused, while the next pod waits.
If the next pod does not run in
--move-start-timeout, the move fails, and the sandbox stays on the node. - Give a longer
snapshots.moveStartTimeoutif a new GPU node takes longer. When the node is empty, the cluster autoscaler or a person removes it. - Karpenter marks a node with a sandbox only for a forced disruption, with
karpenter.sh/disrupted, orkarpenter.sh/disruptionbefore Karpenter v1. The operator then moves the sandboxes. Karpenter waits for the budget only until theterminationGracePeriodof the NodePool, if it has one. A sandbox that did not move by then loses its files. A cloud provider gives a spot node only a short notice, for example 2 minutes. A move that takes longer fails, and the sandbox loses its files with the node. - The node agent must run on each GPU node. It always tolerates the taint
nvidia.com/gpu. Add the other taints of your GPU nodes tosnapshots.agent.tolerations.
The engine waits for a start for a maximum of --start-timeout, 3 minutes in the chart. A new GPU node can take longer.
When the API service accepts GPU sandboxes, it must wait longer for them.
Sandbox objects that kubectl made
Section titled “Sandbox objects that kubectl made”The API service takes in a Sandbox object that does not have the label boxfork.dev/created-by=api.
Each --adopt-every, 5 seconds by default, Engine.Found lists such objects in the phase Running.
After the network check passed, it lists only the objects with the condition NetworkPolicy.
It does not list an object with the label boxfork.dev/release of a different release.
With --isolation=gvisor, Manager.Adopt does not take in an object that does not run under gVisor.
An object runs under gVisor when its spec and its pod both have a RuntimeClass of gVisor.
Manager.Adopt checks that the helper answers in each new one, and then makes a record with Adopted set.
The ID of the sandbox is the name of the object, so Sandbox.connect("proof-kubectl") works.
A sandbox that the API service took in has these rules:
- It has no timeout. The sweep does not kill it.
- It does not count against
--max-sandboxes. - The start of the API service never removes it.
Listdoes not give it, andNewskips its record. Reconciledoes not remove it when its pod stops.- A kill through the HTTP API deletes the Sandbox object.
- When kubectl deletes the object, the next pass of
Adoptremoves the record.
The tests are in internal/sandbox/adopt_test.go and operator/internal/kubeengine/found_test.go.
The network
Section titled “The network”The chart installs six network policies:
| Policy | Pods | Ingress | Egress |
|---|---|---|---|
<release>-sandboxes |
Sandbox pods with a network | Only from the pods of the API service | All addresses, but not the metadata services of the clouds and not the address ranges of the cluster. DNS on port 53 stays open to the metadata addresses and to the DNS pods of the cluster. |
<release>-sandboxes-offline |
Sandbox pods with network: none |
None | None |
<release>-api |
The pods of the API service | Port 7070, from all pods that are not sandboxes, and from addresses that are not in podCIDRs |
All |
<release>-operator |
The pods of the operator | None from pods. The kubelet probes come from the node. | All |
<release>-agent |
The pods of the node agent | None | All |
<release>-registry |
The pod of the registry | Port 5000, from the pods of the agent, and from addresses that are not in podCIDRs: the nodes |
All |
The last two policies come only with snapshots.enabled, and the last one only with snapshots.registry.create.
The blocked addresses are the addresses of hostfw.Blocked and hostfw.Blocked6, the same as on one machine.
A test in operator/chart/chart_test.go keeps the two lists the same.
A sandbox can send to the pod of a different sandbox, but the ingress rule of that pod drops the traffic.
The address ranges of the cluster
Section titled “The address ranges of the cluster”The chart cannot find the address ranges of a cluster. You give them in three values:
| Value | Addresses | What a sandbox cannot reach |
|---|---|---|
networkPolicy.podCIDRs |
The pods | The API service, the operator, and the pods of other namespaces |
networkPolicy.serviceCIDRs |
The services | Each cluster IP, also the IP of the Kubernetes API server |
networkPolicy.nodeCIDRs |
The nodes | Each kubelet, and the address of the Kubernetes API server on the node |
A list can contain IPv4 and IPv6 ranges. The template puts each range in the except list of its family.
On kind, the pods use 10.244.0.0/16 and the services 10.96.0.0/16. The nodes use the IPv4 range of the network kind of Docker or Podman.
With empty values, a sandbox can reach all of these addresses. The egress check of EnsureNetwork then fails, and the API service tells this.
The check at the start of the API service
Section titled “The check at the start of the API service”A network policy has an effect only when the network plugin of the cluster enforces it.
Thus EnsureNetwork does a test at the start, with two test sandboxes A and B:
- It waits until the operator sees the policy in effect on the pods of A and B. See “The start of a pod” below.
- The helper in A listens on port 2:
guest listen 2. - The API service connects to port 2 of A. This proves that A is in reach and that the port takes connections.
- B connects to port 2 of A with the helper. B must get a timeout or “connection refused”.
- The API service connects to port 2 of A again. This proves that the listener in A did not stop during step 4.
- The API service and B do steps 3 to 5 again with the address of the Kubernetes API server. This shows that the policy blocks the traffic out of B.
Some network plugins drop the traffic that a policy blocks. Then B gets a timeout. Other network plugins reject it with an ICMP error, as kube-router in k3s does. Then B gets “connection refused”.
A listener that stopped also gives “connection refused”, on each network. The address took connections from the API service before and after the try of B. Thus the refusal comes from the network. If the second connection of the API service fails, the check fails. The check does not try again: restart the API service to do the check again.
Any other answer, such as “no route to host”, does not prove the policy. The check then fails.
The check uses only an IP address that names the same place in each pod. It refuses these addresses, and the check then fails:
- A loopback address, such as
127.0.0.1or::1 - A link-local address, such as
169.254.169.254orfe80::1 - The unspecified address,
0.0.0.0or:: - A name, such as
localhost, because a name can stand for one of the addresses above
In the pod of B, such an address can be the pod itself, and the pod refuses the connection.
For example, the kubeconfig of k3s gives the API server as https://127.0.0.1:6443.
An API service outside the cluster connects to that address, but B gets “connection refused” from its own pod.
In the cluster, client-go gets the address of the API server from KUBERNETES_SERVICE_HOST. That address is the cluster IP of the service kubernetes, and the check can use it.
The cost: an API service outside the cluster with such an address always reports network_isolated as false. No install of boxfork runs the API service outside the cluster.
The source is operator/internal/kubeengine/network.go.
The result goes to network_isolated and metadata_blocked in GET /v1/info.
If the check fails, the API service starts and logs a warning.
On kind version 0.33, kindnet enforces network policies. The proof script shows this.
On k3s 1.37, kube-router enforces them and rejects the blocked traffic. k3s.md gives the record.
A network plugin that does not enforce network policies
Section titled “A network plugin that does not enforce network policies”Some network plugins ignore network policies. On such a cluster, these items are true:
- A sandbox with a network can reach the other sandboxes, the metadata service of the cloud and the cluster.
- The check at the start fails.
GET /v1/infogivesnetwork_isolatedandmetadata_blockedasfalse. - The operator does not check each pod, because its probe is off. See “The start of a pod”.
- A sandbox without network gets no network only through the RuntimeClass
gvisor-offline. Without that RuntimeClass, the API service refuses such a sandbox. See the next section.
The same is true with networkPolicy.enabled=false.
A sandbox without network
Section titled “A sandbox without network”A sandbox without network must have no network, also when the network plugin ignores the policies.
Thus Create selects the RuntimeClass of the pod in this order:
- If the RuntimeClass
gvisor-offlineexists, the pod uses it. Its handlerrunsc-offlinehasnetwork = "none". The sandbox then has only the devicelo, and the network plugin has no effect on it. - If the check at the start passed, the pod uses
gvisorand the policy<release>-sandboxes-offline. - If not,
Creategives an error that matchesengine.ErrUnsupported. The API service does not make the sandbox.
Found reports a Sandbox object with network: none as without network only in the first two cases.
The test of gVisor at the start of the API service uses a sandbox with a network, through kubeengine.Trial.
That sandbox runs only the helper, and the API service removes it at once.
The start of a pod
Section titled “The start of a pod”A network plugin can start to apply the policy to a new pod some time after the pod runs. Until then, the policy does not limit the traffic of the pod. A sandbox without network can then reach the network. On kindnet, the proof script saw this in each of three tries, when the sandbox started just after a kill.
Thus the operator checks each pod that runs, with --network-policy-check:
- It connects to port 1 of the pod, with a timeout of
--network-probe-timeout(500 ms). The policy lets in only the API service, so the policy blocks the operator. - If the pod answers, or the connection fails for a different reason, the condition
NetworkPolicyisFalse. The operator tries again after 250 ms. - If the connection has a timeout, the condition is
True. The operator does not check that pod again.
A timeout proves the policy only when a pod that no policy covers answers the operator.
Thus at its start, the operator makes a control pod: <operator pod>-netcheck-<suffix>.
The control pod has no sandbox labels, and the operator pod owns it.
The operator connects to port 1 of the control pod, and it must get “connection refused”.
If not, the operator logs an error and does not check pods. The condition NetworkPolicy then does not come.
Thus the check at the start of the API service fails, because it waits for that condition.
The probe of the operator counts only a timeout. It does not count “connection refused” as the policy. A network plugin that rejects the blocked traffic with an ICMP error can give the operator “connection refused” from a pod that the policy covers.
The condition NetworkPolicy then stays False, and network_isolated is false.
This result is wrong, but it is not a security problem: the API service does not use the network as isolated.
On k3s, the operator got a timeout. See k3s.md.
After the network check passed, the engine hands out a sandbox only when the condition is True.
After 30 seconds without the policy, the operator tries again each 10 seconds.
The cost is one timeout of the probe, 500 ms, at the start of each sandbox.
The check assumes that the policy applies to the traffic out of the pod at the same time as to the traffic in. The plugin kindnet looks up the pod once for the two directions, and the proof script shows the result. For other network plugins, this is not proven.
Rights
Section titled “Rights”| Service account | Rights | Scope |
|---|---|---|
<release>-operator |
get, list, watch Sandbox objects. update their status and their finalizers. get, list, watch, create, patch, delete pods. get, list, watch, delete PodDisruptionBudgets. create leases, and get, update the lease <release>-boxfork-operator. create, patch events. It also connects to port 1 of sandbox pods and of its control pod. |
The namespace of the release |
<release>-operator, with snapshots.moveOnDrain |
Also create PodDisruptionBudgets. get, list, watch, create SandboxSnapshot objects. |
The namespace of the release |
<release>-operator, with snapshots.moveOnDrain |
list pods, through the Role <release>-operator-agents, to see the node agents |
The namespace of the agent |
<release>-operator, with snapshots.moveOnDrain |
get, list, watch nodes, through the ClusterRole <namespace>-<release>-operator-nodes |
The cluster |
<release>-api |
get, list, watch, create, delete Sandbox objects. get, list, create, delete claims. get, create, delete SandboxSnapshot objects. get pods. get, create on pods/exec. get the network policy of the sandboxes. |
The namespace of the release |
<release>-agent, in the namespace of the agent |
get, list, watch, update SandboxSnapshot objects. update their status. get Sandbox objects and pods. |
The namespace of the release |
<release>-api |
get the RuntimeClasses gvisor and gvisor-offline only |
The cluster |
The operator changes a pod only to remove the label boxfork.dev/move when a move ends in that pod.
It deletes only these pods:
- the pod that a sandbox leaves
- the next pod of a move that goes back
- a pod that the Sandbox object controls and does not use
Each delete has the UID of the pod as a precondition, and the Sandbox object must control the pod. It deletes only the PodDisruptionBudgets that a Sandbox object owns.
The API service cannot change a pod. It reads a pod only to check that a Sandbox object controls it, before it runs a process in it.
The node agent and the registry run in a namespace of their own: snapshots.namespace.name, by default <namespace>-<release>-system.
The chart makes it with the Pod Security level privileged, because the agent has a hostPath.
No right of the API service is in that namespace. Thus its right on pods/exec does not reach the agent, which has the socket of containerd.
The namespace of the release, where the sandboxes are, must have the level baseline. See “The operator”.
The node agent
Section titled “The node agent”The node agent has the socket of containerd. With that socket, a program can start any container as root on the node. Thus the agent is the part of boxfork with the most power, and this section gives its threat model.
What the agent has:
| Item | Value | Reason |
|---|---|---|
| From the node | The socket of containerd only, as a hostPath of the type Socket |
The save needs containerd. The agent mounts nothing more. |
| User | 65532, group 65532, and the group 0 | The socket lets the group 0 in. The agent is not root. |
| Process | No capabilities, no privilege escalation, a root file system that it cannot write, seccomp RuntimeDefault |
The same as the other parts. |
| Network | No hostNetwork, no port, no Service. The health and metrics servers are off. The policy <release>-agent lets nothing in. |
Nothing on the network can call the agent. |
| Kubernetes | The rights in the table above | It reads; it writes only SandboxSnapshot objects. |
| Secrets | The push login of the registry, through the environment | It pushes and deletes images. |
| Namespace | A namespace of its own, with the registry | No right of the API service reaches the agent. |
What the agent takes in, and the protection for each item:
| Input | Threat | Protection |
|---|---|---|
| A SandboxSnapshot object | A person who can create one tries to save a different container, such as a pod of kube-system. |
The agent saves only the container sandbox of the pod with the name of the Sandbox object, in the same namespace. The Sandbox object must control the pod. |
| The files of the sandbox | Code in the sandbox writes files to attack the agent. | containerd makes the difference. The agent reads the names in the archive and copies the data to one compressed file. It never runs or unpacks a file of the sandbox. A bad archive makes the save fail. |
| The mount points | The files that the kubelet puts in the pod, or the volumes, go to the registry. | The agent removes each entry at or below a mount point of the container. |
| The pause | A save stops with the sandbox paused. | For a snapshot, the agent resumes the task after the diff in all cases, with a context of its own. For a move, the agent keeps the task paused only while a move holds the pod. Every 2 seconds, and at its start, the agent resumes or kills each paused task that no move holds. See “The pause of a move”. Tests prove these, and the proof of phase 2 kills an agent in a pause. |
| The image of the sandbox | Its tag names a different image on the node at the time of the save. | The agent finds the image by the ID that the kubelet gives, and it checks the chain ID of its layers against the container. |
| The size of the files | A save fills the temporary folder of the agent. The kubelet evicts the agent in each save, and no snapshot on the node gets done. | The limit --max-size, and a save that did not end is not done again. The sandbox has its own limit of disk. |
| The registry of the image | The name of the registry, or the realm of its login, points the agent to an address in the cluster or on the node. | The fetch of a missing layer connects only to public addresses. See “The save”. |
| The registry | A different pod reads the files of a snapshot, or puts an image there. | Each registry needs its login. The node port gives only the read-only registry, with a login that only reads. The push registry lets in only the agent pods. A sandbox cannot reach the node port when the values give nodeCIDRs. |
| The API service | A person who controls the API service runs a process in the agent pod. | The agent is in a namespace of its own. The right of the API service on pods/exec is in the namespace of the release only. |
| A move | A SandboxSnapshot with the name of the next save of a move, made before the operator makes it, puts a different image in the next pod. | The operator uses a SandboxSnapshot for a move only when the Sandbox object owns it and its spec.sandbox names that object. Else the save counts as failed. The API service can still make such a SandboxSnapshot, but it can already run processes in each sandbox pod. |
| The status of a Sandbox object | A person who can write sandboxes/status writes a move in it. The operator then deletes a pod of that Sandbox object, or starts its next pod from a different image. |
Only the operator has this right in the chart. The operator deletes only pods and budgets that the Sandbox object controls, with the UID as a precondition. A person with this right can already change the sandbox in other ways, so we accept this. |
| The namespace of the release | The operator, or a person with its rights, makes a pod with a hostPath there. |
The namespace has the Pod Security level baseline. The chart refuses a namespace without that label, and the operator checks it at its start. |
What the protections do not cover:
- A person who controls the agent process controls containerd, and thus the node. The protections only make the agent hard to reach.
- Each agent has the same push login. One agent can change or delete the image of any snapshot in the registry.
- The two logins go over HTTP in the network of the cluster, also between nodes. A person who can read that network gets them. To prevent this, use a registry of your own with TLS, or a network plugin that encrypts the traffic between nodes.
- The node port of the pull registry is open on each address of each node. A network policy does not stop traffic from outside the cluster. Keep the port from outside the cluster with a firewall.
- A pod with the network of the node (
hostNetwork) can reach the pull registry. It still needs the pull login, which only reads. - The group 0 gives the agent the rights of that group on the files that it can see. It sees only the socket and its own image.
- A person who can create SandboxSnapshot objects can save each sandbox of the namespace. The API service has this right, and a cluster administrator can give it to others.
- The agent can send traffic to all addresses. It needs the API server, the registry, and the registries of the images. Only the fetch of a missing layer checks the address.
Two releases in one namespace
Section titled “Two releases in one namespace”Each release has its name in the label boxfork.dev/release.
The API service puts the label on each Sandbox object and claim that it makes, and it sees only these.
Thus the start of a second release does not remove the sandboxes of the first release.
The operator copies the label to the pod, and it does not reconcile an object with the label of a different release.
Each operator reconciles a Sandbox object without the label. In a namespace with two releases, give the label to each object that kubectl makes.
gVisor on the nodes
Section titled “gVisor on the nodes”Each node that runs sandboxes needs these items:
runsc,containerd-shim-runsc-v1and the foldergvisor-binof a gVisor release- the containerd runtime
runsc, withConfigPath = "/etc/containerd/runsc.toml" - the file
/etc/containerd/runsc.tomlwithoverlay2 = "none"in[runsc_config] - the RuntimeClass
gvisorwith the handlerrunsc - the containerd runtime
runsc-offline, withConfigPath = "/etc/containerd/runsc-offline.toml" - the file
/etc/containerd/runsc-offline.tomlwithoverlay2 = "none"andnetwork = "none"in[runsc_config] - the RuntimeClass
gvisor-offlinewith the handlerrunsc-offline
With --set runtimeClass.create=true, the chart makes the two RuntimeClasses. The nodes still need the runtimes.
With overlay2 = "none", the files that a sandbox writes go to the snapshot of containerd, not to the memory of gVisor.
Snapshots on Kubernetes need this, as on one machine. See snapshots-and-forks.md.
scripts/kind-gvisor.sh does these steps on each node of a kind cluster. You can run it again: it changes only what is not correct.
cd operator && go test ./...runs the unit tests. They check these items:- the CRD against the types, and the phases
- the pod, the status, and the idempotency of the operator
- each method of the engine, the exec and the network check
- the blocked addresses of the chart
- each network policy of the chart. The tests render the chart with Go
text/template, and also withhelm templatewhen Helm is on thePATH. - the rights and the security settings of the node agent, its namespace, and the two logins of the registry with their rights
- the check of Pod Security at the start of the operator, and the check of the chart
- the agent: which node saves, the finalizer, the deletion, the failures, the attempts, and the resume after a pause
- the save against a fake containerd:
- the resume of a sandbox that a killed agent left paused
- the image by its ID when its tag moved, and the refusal of an image that is not under the container
- two saves of one sandbox at the same time, and the same image from two saves
- the limit of size of a save, and the fetch of a missing layer, which refuses private addresses also through the realm
- the new layer: the mount points left out, the digests, the image with one more layer, and the push of the missing layers only
- the registry client against a fake registry, with no login, a basic login and a bearer token
Commit,LocalImage,RemoveImageand the create from a snapshot, with a fake agent- the join: the API service serves its API through the connections of a control service, without the API key, and reports no GPUs. Its start refuses a join over plain HTTP to a different machine.
- the join values of the chart: the flags, the token and the certificate authority from Secrets, and network policies that do not change
- the moves: the budget, the marked nodes, and each step of a move
- a move with saves that fail, with a sandbox that ends in the save, and with a cache that is late
- a move that goes back: a slow save or next pod, no agent, a quota, a pod in the way, an uncordon, and moves off
- a short error, which does not end a move, and a failed move that starts again 3 times
- the budget, which counts only the pod that the sandbox runs in
- a next pod of an earlier move: the operator deletes it, and no move ends in it, also with a cache that is late
- one operator at a time: the strategy
Recreate, the lease, and its rights - the pod that leaves, deleted only when the next pod runs, and a move that continues when that pod is gone
- the agent: a sandbox that a move holds stays paused, and the agent resumes or kills it when the move ends
- a command and a snapshot during a move, and a Sandbox from
kubectlthat the engine finds while it moves - a command that stops when its Sandbox object cannot be read, and open commands that read the cache of a watch
- the stop of an agent in a save
- the chart with and without
snapshots.moveOnDrain, and the strategy of the upgrade of the agent
- The CRD test fails when
go generatewas not run after a change to the types. - scripts/proof/k8s-phase1.sh installs the chart again on a kind cluster and does the checks of phase 1. It prints
PASSorFAILfor each check. - scripts/proof/k8s-phase2.sh does the same for phase 2, on a cluster with two worker nodes.
It checks a snapshot, a restore on the other node, a fork into 3 copies and the deletion of the image.
It also checks a pool, the registry and its two logins, a suspension, and the namespaces with
kubectl auth can-i. It kills an agent while a sandbox is paused for its save. It also moves the tag of the image of a sandbox on its node before a save. - scripts/proof/k8s-phase4.sh does the checks of phase 4, on a cluster with two worker nodes:
- a cordon, the budget of each sandbox, and an eviction that the budget refuses
kubectl drainof the node of 4 sandboxes, 3 from the SDK and 1 fromkubectl. Each sandbox must have its files and its ID on the other node, and no process of before the drain.- a command while a sandbox moves, and the stop of an agent in a save of 512 MB
- a command that writes in the pause of a move: its file is in the next pod, or the command fails
- a move off a node without an agent, and a next pod that cannot start. Each move fails, the sandbox continues on its node, and an uncordon removes the move.
- a drain while the next pod runs and the network policy does not apply to it yet. The budget counts 1 pod, and the drain does not evict the pod that leaves.
- a pod with the name of the next pod and older files. The operator deletes it, and the move ends in a next pod of its own save.
- the lease of the operator, and the strategy
Recreateof its Deployment - a rolling upgrade of the agents, and the pod of a sandbox that waits for a GPU
- scripts/proof/control-k8s.sh installs the chart with the join values on two kind clusters, and starts one control service and one test server on the computer.
Through the control service, the SDK puts a sandbox on each cluster by its labels, and
kubectlfinds it there. It checks commands, files, a snapshot and a fork on a cluster, the merged list, and a kill of the control service. It also checks that the image of the API service refuses a join overhttp://to a different machine.
Limits
Section titled “Limits”- Nothing ran on a node with an NVIDIA GPU. The tests check only the pod spec: the resource
nvidia.com/gpuand the toleration. The proof script checks only that a Sandbox object that asks for a GPU staysPendingon a cluster with no GPU. - The proofs ran only on kind clusters on Podman: arm64, Kubernetes 1.37.0, containerd 2.3.4, kindnet. Phase 1 ran on one node and on three nodes. Phase 2 ran on three nodes, two of them workers. Nothing ran on a cloud cluster.
- The check of the policy at the start of a pod is proven only with kindnet. See “The start of a pod”.
- The check of the traffic out of a sandbox uses one address, the Kubernetes API server. It does not prove the other blocked ranges.
pidsLimitis kept in the spec, but the pod does not apply it. Kubernetes sets the limit of processes for each node, withpodPidsLimitof the kubelet.- Pause and resume are not available. The API gives
501 unsupported. - A reset to a snapshot is not built. The engine has the method
NoReset, and the API gives501 unsupported. Kill the sandbox and create one from the snapshot. See reset.md. - The proof of phase 2 checks one suspension and its wake on the same cluster. It does not check a suspension with volumes or with a pool.
- The proof saved only small changes: one file written and one file deleted. It started a save of 1 GiB only to kill the agent in it. The time of a large save was not measured.
- The agent saves only with containerd. A node with CRI-O gets
Failed. - The registry does not free the space of deleted layers. Run its garbage collection.
- On a node that deletes the layers of an image after the unpack, the agent gets them from the registry of the image. A private image there makes the save fail.
- The chart’s registry needs the node port on
localhost. See “The registry”. - The paths that an image keeps in volumes of its own are not known to the Kubernetes engine. The manager thus does not list them as
excluded_paths. - An exec as root runs as the user of the container. Some images do not run as root. For these images, the manager cannot give new volumes to the user of the commands, and it logs a warning.
- The API service has one replica. While it restarts, calls fail, but the sandboxes continue to run.
- The join to a control service ran only with two kind clusters and the control service on one computer, through the network of Podman. It used a certificate authority of the proof, not a public one.
- The API service reports no GPUs to a control service. See “The GPUs that the API service reports”.
- A volume is a claim with the access mode
ReadWriteOnceby default. Two sandboxes on different nodes cannot mount it at the same time. - A move keeps the files, not the processes. A command that runs at the start of a move fails.
- A sandbox with a
ReadWriteOncevolume, or with a volume on storage that is bound to one node, such aslocal-path, cannot move. The move fails, and the drain waits. See “The order of the pods”. The proof of phase 4 did not move a sandbox with a volume. A move with storage that a different node can attach is not proven. - The moves ran only with
kubectl drainandkubectl cordon. The taints of the cluster autoscaler and of Karpenter are checked only in the unit tests. - While no node agent runs on a node, a sandbox that an agent paused there stays paused. The move fails after
--move-save-timeout, and the sandbox continues when an agent starts again. - The proof of phase 4 moved 4 sandboxes with 20 MB of files each. A move of a large sandbox, or of many sandboxes at one time, was not measured.
- Each release publishes the images of the operator and of the helper on GHCR, and the chart uses them by default.
The appVersion of the chart is the version of these images, so set it to the new version before each tag.
The chart’s registry uses the public image
registry:3.