# Deploy on Kubernetes

> Generate a Kubernetes Deployment and Service with 10xgraph build --k8s, with grace periods and probes tuned so long agent runs finish during rolling deploys.

Source: https://10xgraph.com/docs/server/kubernetes
Last updated: 2026-11-25

## The problem: long runs and short timeouts

Agent runs can last minutes. In production the server cancels a run after 300 seconds unless you set `runs.timeout` in `10xgraph.json`.

Kubernetes' default pod lifecycle is hostile to this. When you trigger a rolling deploy, the kubelet gives a pod 30 seconds to shut down gracefully before sending SIGKILL. If the app is still in an LLM call, the pod dies mid-request, and the client's stream breaks.

On SIGTERM the generated image answers `503` on `/health/ready` (`draining`) so the Service stops routing to the pod, gives open runs up to 540 seconds to finish, and ends open streams with an "interrupted" chunk the client can resume from. Details are in [Graceful shutdown](/docs/guides/graceful-shutdown).

`10xgraph build --k8s` generates a manifest that fixes this: it sets the termination grace period, pre-termination delay, and probe timeouts so that in-flight runs finish normally, even during deploys.

## Generate the manifest

Create a `Dockerfile` and Kubernetes manifest in one command:

```bash
10xgraph build --k8s --service-name my-agent --port 8000
```

This produces three files:

- `Dockerfile` with an image that runs one Uvicorn process: `python -m tenxgraph_api.src.app.serve --host 0.0.0.0 --port <port>`. There is no Gunicorn and no `WEB_CONCURRENCY`; scale with replicas.
- `.dockerignore`
- `k8s.yaml` containing a Deployment and a Service

The `--force` flag overwrites existing files. Do not add `--docker-compose` for an image you will run on Kubernetes: with that flag the `Dockerfile` omits its `CMD` and relies on the compose file's `command`. Generate the compose file in a separate run if you need it.

### Why one process per pod

The image runs one Uvicorn process through the serve module, which drains and interrupts open streams the same way the Gunicorn image does. In our load test, one Uvicorn process gave the same throughput as Gunicorn with one worker and used about 50 MiB less memory, because there is no master process. Kubernetes already restarts the container and spreads load across replicas, so the Gunicorn master adds nothing here. See [Choosing the server process](/docs/production/scaling#choosing-the-server-process).

### Worker and scheduler

If your `10xgraph.json` has a `worker` key with a broker other than `memory`, `build --k8s` can also write a `<service>-worker` Deployment and, with `--scheduler`, a `<service>-scheduler` Deployment. They run `10xgraph worker` and `10xgraph scheduler` in the same image, have no Service and no HTTP probes, and use the same security context and Secret as the API. The scheduler has one replica and `strategy: Recreate`, so a rollout never runs two. See [Background tasks](/docs/server/background-tasks).

### What the generated manifest includes

The manifest sets these values:

| Setting | Value | Purpose |
| --- | --- | --- |
| `terminationGracePeriodSeconds` | 675 | Exceeds the app's 540-second graceful deadline (`TENXGRAPH_GRACEFUL_SHUTDOWN_SECONDS`) plus the 15-second `preStop` sleep, which runs inside the same period. Ensures SIGKILL does not arrive until the app has fully drained. |
| `preStop` sleep | 15 seconds | Gives the load balancer time to notice the pod is terminating and stop routing new requests. Without it, new traffic arrives while the app shuts down. |
| `readinessProbe` | 10s interval, 5s initial delay | Removes unhealthy pods from the load balancer quickly. Checked via `GET /health/ready`. |
| `livenessProbe` | 30s interval, 30s initial delay, 5 failures | Deliberately slack. A worker handling a long run must not be restarted. Checked via `GET /ping`. |

The readiness probe is strict (10-second intervals) because false positives only temporarily remove a pod from rotation. The liveness probe is slack (30-second intervals, 5 consecutive failures required) because a false positive kills the pod mid-run. Readiness hits `/health/ready` (checkpointer, store and cache store reachable) and liveness hits `/ping`; neither requires authentication.

Default resource requests and limits are also set (500m CPU and 512 Mi memory requested; 2 CPU and 2 Gi memory as limits), but you should tune these based on your agents and deployment environment.

The manifest holds a Deployment with 2 replicas and a Service on port 80 that targets your container port.

### Security defaults

The pod is locked down out of the box:

| Setting | Value | Purpose |
| --- | --- | --- |
| `automountServiceAccountToken` | `false` | The server never calls the Kubernetes API, so a compromised pod gets no cluster token. |
| `runAsNonRoot`, `runAsUser`, `runAsGroup`, `fsGroup` | `true`, 10001 | Matches the image's numeric `USER 10001:10001`. |
| `seccompProfile` | `RuntimeDefault` | Blocks syscalls a web server never needs. |
| `readOnlyRootFilesystem` | `true` | Code and dependencies cannot be modified at runtime. |
| `allowPrivilegeEscalation` | `false` | No setuid binaries can raise privileges. |
| `capabilities.drop` | `ALL` | The server binds a port above 1024 and needs no Linux capabilities. |
| `emptyDir` volumes | `/tmp`, `/app/uploads` | The only writable paths: scratch files and local media uploads. |
| `envFrom.secretRef` | `<service>-secrets` | Every secret comes from a Kubernetes Secret. |

If your graph writes to another path (a SQLite checkpointer file, for example), add a volume for it.

## Customize before applying

The manifest is not production-ready as-is. You must edit these values:

```yaml
containers:
  - name: my-agent
    image: registry.example.com/my-agent:1.0.0   # Replace with your own pinned image reference
    env:
      - name: MODE
        value: "production"             # Already set, but verify
```

### Pin the image

The manifest uses `registry.example.com/<service>:1.0.0` as a placeholder image name. Build your own image from the generated `Dockerfile`, push it to your registry, and reference it immutably. A moving `latest` tag makes rollbacks ambiguous and makes image-related issues hard to debug.

### Set CORS origins explicitly

The manifest does not set `ORIGINS` or `ALLOWED_HOST` as env entries. Both are read from the Secret (see [Mount secrets](#mount-secrets-do-not-bake-them)), because an env entry in the manifest would override the Secret. Put your actual frontend domain(s) in the Secret, comma-separated if there are multiple:

```bash
kubectl create secret generic my-agent-secrets \
  --from-literal=ORIGINS="https://app.example.com,https://app-staging.example.com" \
  --from-literal=ALLOWED_HOST="app.example.com"
```

With `MODE=production`, the server refuses to start if `ORIGINS` or `ALLOWED_HOST` is the wildcard `*`. The check is `STRICT_PRODUCTION_CHECKS`, which is on by default in production. Production refuses to start while either is missing, so add both to the Secret. If you truly need wildcard origins, set `CORS_ALLOW_CREDENTIALS=false` and `STRICT_PRODUCTION_CHECKS=false`, which is rare.

### Mount secrets, do not bake them

API keys, database passwords, and JWT secrets must come from Kubernetes Secrets, never from plain values in the manifest or baked into the image. The generated manifest already reads every key of a Secret named after the service, and the pod does not start until that Secret exists:

```yaml
envFrom:
  - secretRef:
      name: my-agent-secrets
```

Create the secret in your cluster before deploying. The server reads `JWT_SECRET_KEY`, `REDIS_URL`, `ORIGINS` and `ALLOWED_HOST` itself; names such as the Postgres DSN variable below are whatever your graph module reads when it builds the checkpointer:

```bash
kubectl create secret generic my-agent-secrets \
  --from-literal=GOOGLE_API_KEY="..." \
  --from-literal=OPENAI_API_KEY="..." \
  --from-literal=JWT_SECRET_KEY="..." \
  --from-literal=ORIGINS="https://app.example.com" \
  --from-literal=ALLOWED_HOST="app.example.com" \
  --from-literal=POSTGRES_DSN="postgresql://..." \
  --from-literal=REDIS_URL="redis://..."
```

Use `--from-literal`, not `--from-env-file=.env`: `kubectl` keeps the quotes from a `.env` file as part of each value.

## Apply and verify

Once you have customized the manifest, apply it:

```bash
kubectl apply -f k8s.yaml
```

Monitor the rollout:

```bash
kubectl rollout status deployment/my-agent
kubectl get pods -l app=my-agent
```

Verify the service is reachable:

```bash
kubectl port-forward svc/my-agent 8000:80
curl http://127.0.0.1:8000/ping
```

If `/ping` returns a JSON body with `"data": "pong"`, the deployment is live.

### Test graceful termination

The true test is that a long-running request survives a deploy. Start a streaming request:

```bash
# Terminal 1: Stream a request (this will take minutes)
curl -X POST http://127.0.0.1:8000/v1/graph/stream \
  -H "Content-Type: application/json" \
  -d '{"messages": [{"role": "user", "content": [{"type": "text", "text": "explain quantum physics in detail"}]}]}'
```

While that request is in flight, trigger a rolling restart in another terminal:

```bash
# Terminal 2: Rolling restart
kubectl rollout restart deployment/my-agent
```

The stream should continue and complete normally. If the connection closes or times out, one of these is true:

- `terminationGracePeriodSeconds` is shorter than your longest run
- The container is not running as PID 1 and not receiving SIGTERM
- Your load balancer's idle timeout is shorter than the grace period

## Scaling horizontally

The manifest defaults to 2 replicas. Scale up for higher concurrency:

```bash
kubectl scale deployment my-agent --replicas=5
```

Before scaling, understand how 10xgraph handles distributed state:

1. **Shared checkpointer is mandatory.** All replicas must read and write to the same Postgres and Redis. If they do not, the same thread will resolve differently depending on which pod answers, causing data loss. Use `PgCheckpointer` in production (see [Set up checkpointing](/docs/guides/set-up-checkpointing)).

   Set `"cache_store": {"type": "redis"}` (and `REDIS_URL`, or a `cache_store.url`) as well so the API's cache store is shared: the run locks, caps, idempotency state, token revocations and rate-limit counters all live there. The in-memory store is per pod.

2. **Threads are not sticky.** State lives in the checkpointer, not in the pod. You do not need (and should not use) session affinity. A client can invoke a thread on any replica.

3. **Streaming connections are long-lived.** If your load balancer (or ingress) has an idle timeout shorter than your longest run, the connection will close mid-stream even though the pod is healthy. Set idle timeouts to at least 15 minutes. For AWS ALB/NLB, increase `deregistration_delay`. For Nginx, increase `proxy_read_timeout`.

4. **Concurrent writes conflict.** With optimistic concurrency, two overlapping writes to the same thread raise a `StaleStateError`, and the API returns 409. Clients that fan out against a single `thread_id` should retry or serialize. Most clients do not do this; it only matters if you deliberately parallelize writes to the same thread.

## Troubleshooting deployments

| Symptom | Cause | Fix |
| --- | --- | --- |
| Runs truncate on every deploy | `terminationGracePeriodSeconds` is too short, or the process is not PID 1 | Increase to 675+ seconds. Ensure your Dockerfile uses `CMD` or `ENTRYPOINT`, not a shell wrapper. |
| Requests fail for a few seconds after a deploy | `preStop` sleep removed or load balancer does not respect endpoint removal | Restore the `preStop` sleep in the manifest. Increase your load balancer's connection drain time. |
| Pods restart during long runs | Liveness probe is too strict (failing too quickly) | Loosen the liveness probe (`periodSeconds: 30`, `failureThreshold: 5`). The readiness probe should be strict; the liveness probe should not. |
| Thread history disappears or threads resolve differently | Replicas do not share a checkpointer | Ensure the Postgres DSN and `REDIS_URL` your graph module uses point to shared instances. Do not use `InMemoryCheckpointer` in production. |
| Random 409 errors under high load | Two requests wrote to the same thread concurrently | This is expected with optimistic concurrency. Retry with exponential backoff, or serialize writes to the same thread. |
| Server exits at startup with a CORS or host error | `ORIGINS` or `ALLOWED_HOST` is `*` | Set both to your real values. If you need wildcard origins, set `CORS_ALLOW_CREDENTIALS=false` and `STRICT_PRODUCTION_CHECKS=false` (unusual). |
| Browser requests fail CORS checks | `ORIGINS` is missing from the Secret or holds the wrong domain | Set `ORIGINS` in the Secret to your real domain. |

## Related

- [Deployment](/docs/server/deploy) for the general container strategy and production decisions
- [Backup and restore](/docs/server/backup-and-restore) for Postgres and Redis backup procedures
- [Observability](/docs/server/observability) for logging, metrics, and traces in production

## Frequently asked questions

### Why does the termination grace period need to be so long?

Agent runs are not typical web requests. An LLM call alone can take minutes, and tools add more time. Kubernetes default 30-second termination grace period kills pods mid-run. The server waits up to 540 seconds for open runs on shutdown, so the termination window must exceed that.

### Do I need to scale horizontally?

Start with 2 replicas for redundancy. Scale up for concurrent demand. All replicas share Postgres and Redis, so scaling is straightforward. Configure your load balancer idle timeout for long streaming connections.

### What if I want to use the generated manifest as-is?

You must pin the container image tag and set a real CORS origin. Production mode refuses to start with wildcard CORS and credentials enabled. API keys and secrets must come from Kubernetes Secrets.
