Skip to content

P6 on Kubernetes, slice 1: supervisor, relay, Redis, control plane and container sandboxes as K8s workloads #423

Closed

Description

@pdettori

Slice 1 of the P6-on-Kubernetes epic: the P6 process model on Kubernetes, container tier only

Part of epic #426. Written so a planner can start it in a fresh session.

Goal

Run the P6 runtime on Kubernetes as ordinary workloads: the supervisor with its forked worker pool, the relay, Redis, the MU1 control plane, and remote-worker container sandboxes. This adds a new kustomization (for example deploy/k8s-p6/) next to deploy/vm/ and deploy/compose/. It reuses the same harness image and the same environment variables. It does not revive the Knative path: no Knative Service, no KEDA, no KubectlTransport/pod-exec sandboxes. Execution goes through GrpcRelayTransport (SH_REMOTE_SANDBOX=1, SH_SANDBOX_DISCOVERY=records), as on the VM.

The closest precedent is deploy/compose/docker-compose.yml. It already runs these exact processes in containers. The VM path (deploy/vm/) carries the hardening and secret handling this slice has to translate.

Workload mapping

Component Workload Notes
Supervisor + W forked workers Deployment, one container fork() cannot cross containers (ADR-0034, deploy/compose/README.md). W comes from SH_WORKERS; capacity comes from replicas. Not a Knative Service.
Relay Deployment, replicas: 1, strategy: Recreate Presence-only: each Attach stream is held by one process. Start from deploy/knative/relay-deployment.yaml, but take tokens from Secrets and split the exec listener onto its own port (MOCA_RELAY_EXEC_ADDR).
Container sandboxes StatefulSet of remote-worker Derive a unique, stable SANDBOX_ID from the pod name (downward API). Plain replicas share one id and collide on one sh:sandbox:records entry.
Redis StatefulSet + PVC, with AUTH It holds sh:sandbox:records: whoever can write it chooses the executor for every turn. Today's deploy/knative/redis.yaml is a Deployment with no auth.
Control plane Deployment Start from deploy/knative/control-plane.yaml (K8sSecretStore). Check whether the sh-control-plane-pods Role is still needed without the pod-exec path.

Code changes

  • The supervisor's admin listener is bound to loopback (packages/supervisor/src/admin.ts:100, 127.0.0.1), so kubelet probes and Prometheus cannot reach it. It also serves only GET /metrics. Add a bind-address setting (for example SH_ADMIN_HOST, defaulting to loopback so the VM and Compose paths keep their posture), and add a readiness endpoint that is ready once at least one worker is healthy. Restrict access with a NetworkPolicy, not the bind.
  • Optional: an init process (tini) as PID 1, or a written argument for why Node as PID 1 is enough.

Supervisor gotchas the manifests must encode

  1. The front door is L4 only. Routing and 429 admission happen once per connection (packages/supervisor/src/routing.ts:21-25), never per request. An L7 proxy that pools upstream connections funnels many sessions' turns to one worker, which silently defeats the S cap and sticky routing. That includes an Ingress, an edge-terminated Route, an Istio/Ambient sidecar, and Knative's queue-proxy. Use a ClusterIP or LoadBalancer Service, or TLS passthrough, and keep the mesh off these pods. Document this prominently.
  2. W comes from the CPU limit, not the request. availableParallelism() reads cpu.max. With no limit, W describes the whole node. Set a limit or pin SH_WORKERS.
  3. Memory is roughly W × ~225 MiB at idle, because each worker compiles TypeScript with tsx at startup (see the measurement in relay-deployment.yaml). Size requests to match.
  4. Shutdown: terminationGracePeriodSeconds: 120, matching TimeoutStopSec in sh-supervisor.service. Kubernetes sends SIGTERM to PID 1 only, which is the KillMode=mixed behaviour main.ts::close() relies on. Add a short preStop sleep so the endpoint is removed before drainAll() stops accepting.
  5. More than one replica is allowed with leastInFlight. W workers are already separate processes sharing Redis, so pods add no new concurrency class. stickyBySession affinity holds only within a pod. Say so in the docs. An HPA on in-flight/S or the 429 rate is optional and out of scope for the first pass.
  6. HOME=/tmp on an emptyDir, plus readOnlyRootFilesystem, as the existing manifests do.

Isolation: NetworkPolicy replaces nftables and isolate=strict (MI1 §5 R5/R8)

A pod has a single IP, so the VM's loopback bind for the exec listener does not carry over. Enforce by port, which needs a CNI that enforces NetworkPolicy:

  • Relay exec port (9444): from supervisor pods only.
  • Relay attach port (9443): from sandbox pods (and from the P4 CIDR in slice 2).
  • Redis: from the supervisor, relay and control plane only. Never from sandboxes.
  • Sandbox egress: the relay's attach port, plus whatever deploy/knative/harness-egress-policy.yaml's successor permits.
  • Every token comes from a Secret: SH_RELAY_TOKEN, MOCA_RELAY_EXEC_TOKEN, the MU1 keypair, the KEK, and the exchange token. A setup script, analogous to setup-vm.sh and to install.sh's use of genkeys.ts, generates them once and never rotates them on a re-run.

Acceptance

  • On Kind (and on one real cluster with an enforcing CNI): a setup script brings the stack up. An authenticated mocactl run turn executes in a sandbox pod, and an unauthenticated /turn is refused with SH_REQUIRE_AUTH=true.
  • A smoke test adapted from deploy/compose/smoke.sh passes.
  • Negative tests: a sandbox pod cannot reach Redis or the relay's exec port.
  • Rolling the supervisor Deployment lets in-flight turns finish (§3.9).
  • A static test, in the spirit of deploy/compose/tests/compose.test.sh, keeps the env vars aligned with deploy/vm/env/*.env.example.

Out of scope

P4 (slice 2), tier selection and affinity (slice 3), autoscaling, and Vault.

Activity

  1. added
    enhancement
    on Oct 2, 2026
  2. pdettori commented on Oct 4, 2026

    @pdettori
    MemberAuthor

    Slice 1 acceptance on OpenShift: #370 demo parity on the container tier

    The acceptance run, deploy/k8s/README.md §9, ran against PR #427 at f3b4f62. Every step passed. No fallback was needed.

    Environment

    Cluster OpenShift 4.20.8 (Kubernetes v1.33.6, CRI-O 1.33.6, OVN-Kubernetes), 3 control-plane and 3 worker nodes, amd64, AWS us-east-1. Apps domain apps.moca1.kubestellar.org. Default StorageClass gp3-csi.
    Harness image Built in-cluster from f3b4f62 (OpenShift binary Docker build, pi-fork included) as image-registry.openshift-image-registry.svc:5000/moca-build/moca-harness@sha256:ba53ffca6e28c52f072c7882bd2e61df7a15ab143383b5e46b1ab7e3c1412694. Not published outside the cluster.
    Sandbox image ghcr.io/rossoctl/moca-remote-worker:latest; this branch does not change remote-worker.
    TLS No --tls-cert, so setup.sh generated a self-signed certificate for the supervisor Route. The cluster's default ingress CA is not publicly trusted either, so NODE_EXTRA_CA_CERTS holds both.
    Users User 1 github:6678093 (Paolo Dettori) and user 2 github:228572680 (ingpaolodettori-dev): one laptop, two XDG_CONFIG_HOMEs, a private browser window. Each user stored the same Anthropic key under its own credential name.

    9.1 Bring-up

    SH_GITHUB_CLIENT_ID=… deploy/k8s/setup.sh --target ocp --image <in-cluster image>@sha256:ba53ffca…
    EXIT:0
    ==> 2 sandbox(es) attached to the relay
      harness:        https://moca-moca.apps.moca1.kubestellar.org   (TLS passthrough to the supervisor's L4 sidecar)
      control plane:  https://moca-control-plane-moca.apps.moca1.kubestellar.org
    
    • Pods: all Running with 0 restarts. That includes redis-0 under nonroot-v2 with UID 999 and GID 1000, so the Redis SCC fallback was not needed.
    • Probes: every Deployment went Ready under OVN-Kubernetes, so the node-CIDR probe fallback was not needed.
    • Routes:
      • control-plane /readyz answers 200 through the edge Route;
      • the harness answers 401 token_required through the passthrough Route and the ghostunnel sidecar, with TLS verified both times.

    9.2 Live smoke, with a real model (api.anthropic.com, kind api-key)

    PASS=11 FAIL=0. On OpenShift, claim 7 also proves the kube-API carve-out that Kind cannot: redis, relay exec, kube API and metadata BLOCKED; relay attach OPEN. Claim 9 drained a turn across a supervisor pod deletion, and claim 10 survived a redis-0 restart.

    Re-checks Kind could not prove, probed from moca-sandbox-0:

    Target Result
    API server endpoint 10.0.112.96:6443 BLOCKED
    kubelet on the sandbox's own node 10.0.64.91:10250 BLOCKED
    kubelet on another node 10.0.112.96:10250 BLOCKED
    machine-config server 10.0.64.91:22623 BLOCKED
    https://example.com 200

    9.3 The acts

    • Act 1, login and doctor:

      • both logged in with the GitHub device flow: logged in as Paolo Dettori and logged in as ingpaolodettori-dev;
      • mocactl doctor gave 7/7 ✓ for both, including "harness reachable" and "harness trusts this control plane" through the passthrough Route.
    • Act 2, own credentials: each user ran credentials add and then a turn (uname -a, which ran in moca-sandbox-1 on the RHCOS kernel 5.14.0-570.73.1.el9_6.x86_64). The audit sh:cp:audit shows:

      • github:6678093: session_created and credential_issued with credential anthropic-user1;
      • github:228572680: session_created and credential_issued with credential anthropic-user2;
      • names only, never values.
    • Act 3, disjoint sessions:

      • each mocactl sessions lists only that user's own session;
      • user 2 on user 1's session fec4362e-… gets that session no longer exists, or is not yours, exit 1;
      • user 2 on sess-does-not-exist gets the same text, exit 1;
      • user 1 resumes its session: resumed, exit 0.
    • Act 4, research from the sandbox: both users ran the VM demo's 2a prompt at the same time. Both clones and both fetches ran in the sandbox:

      The agents' answers (both users) The world
      COMMIT d94eb3047631 git ls-remote … HEAD → d94eb3047631
      NODE_VERSION / NODE_DATE v26.10.0 / 2026-09-21 index.json[0] → v26.10.0 2026-09-21

      The honest beat: moca-sandbox-0 holds research-user1 (and the smoke's proof.txt); moca-sandbox-1 holds research-user2. Both users lease the same pool, with no per-user sandbox isolation, as README §7 states (MI1 S5).

    L4 check, spec §10 risk 3

    These are established connections inside the supervisor pod, counted from /proc/net/tcp and /proc/net/tcp6:

    When 8443 (router → ghostunnel) 8080 (ghostunnel → supervisor)
    Baseline, idle 0 0
    During the two concurrent research turns (t+4 s, t+8 s) 2 2
    After 0 0

    The connections are 1:1 for two concurrent clients, so the passthrough Route does not pool.

    Counters, as context: restarts 0, handoff_retries 0, handoff_failures 0, over_admission 0 → 7. over_admission rises once per turn, as it did in the Kind spike.

    Fallbacks applied

    None: no Redis SCC patch, no node-CIDR policy, no LoadBalancer.

    Notes for follow-ups

    • The device-flow code expires in a few minutes. Two codes expired here while waiting for the user. mocactl login should either say how long the code is valid, or re-issue a code by itself.
    • The cluster's default ingress certificate is self-signed. NODE_EXTRA_CA_CERTS therefore needs the ingress CA as well as the supervisor CA. README §9.1 says this already; setup.sh could write the combined file.
  3. 21 remaining items

  4. pdettori commented on Oct 4, 2026

    @pdettori
    MemberAuthor

    Done: merged in #427 (0573a82) and accepted on OpenShift 4.20.8 (results). Follow-ups #428–#435; the epic is #426.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancement

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions