Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 18 additions & 5 deletions k8s/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ Kustomize + Helm-based configurations for deploying infrastructure management (`

| Stack | Components | Namespaces | Default Ports |
| :--- | :--- | :--- | :--- |
| **`infra`** *(Default)* | ArgoCD (backed by Valkey), Argo Workflows controller, Grafana k8s-monitoring (Alloy + kube-state-metrics + node-exporter), Prometheus server, Grafana, Alertmanager, Grafana Pyroscope, NVIDIA DCGM Exporter, OpenTelemetry Collector, Grafana Tempo | `argocd`, `argo-workflows`, `monitoring`, `otel` | `8080` (ArgoCD), `8030` (Grafana), `8090` (Prometheus), `4040` (Pyroscope), `3200` (Tempo) |
| **`llm`** | Ollama (per-VRAM-tier DaemonSets), LLM Gateway (LiteLLM), Open-WebUI, Qdrant Vector DB, Valkey Cache | `llm` | `11434` (Ollama), `3000` (WebUI), `6333` (Qdrant), `6379` (Valkey) |
| **`infra`** *(Default)* | ArgoCD (backed by Valkey), Argo Workflows controller, Grafana k8s-monitoring (Alloy + kube-state-metrics + node-exporter), Prometheus server, Grafana, Alertmanager, Grafana Pyroscope, NVIDIA DCGM Exporter, OpenTelemetry Collector, Grafana Tempo, CloudNativePG operator | `argocd`, `argo-workflows`, `monitoring`, `otel`, `cnpg-system` | `8080` (ArgoCD), `8030` (Grafana), `8090` (Prometheus), `4040` (Pyroscope), `3200` (Tempo) |
| **`llm`** | Ollama (per-VRAM-tier DaemonSets), LLM Gateway (LiteLLM) with its CloudNativePG database, Open-WebUI, Qdrant Vector DB, Valkey Cache | `llm` | `11434` (Ollama), `3000` (WebUI), `6333` (Qdrant), `6379` (Valkey) |
| **`logging`** | Loki (pod and journal logs shipped by the `infra` stack's Alloy `alloy-logs` DaemonSet) | `logging` | `3100` (Loki) |
| **`gitea`** | Gitea: the internal forge and container registry ([Gitea](#gitea)) | `gitea` | `3000` (Gitea HTTP, in the cluster) |
| **`chat`** | Mattermost Team Edition on CloudNativePG ([Mattermost](#mattermost)) | `mattermost`, `cnpg-system`, `mattermost-operator` | `8065` (Mattermost HTTP, in the cluster) |
| **`chat`** | Mattermost Team Edition on CloudNativePG ([Mattermost](#mattermost)) | `mattermost`, `mattermost-operator` | `8065` (Mattermost HTTP, in the cluster) |
| **`all`** | All components from the stacks above, and the `devops` stack | `argocd`, `argo-workflows`, `monitoring`, `otel`, `llm`, `logging`, `gitea`, `devops`, `mattermost`, `cnpg-system`, `mattermost-operator` | All ports above |

Register each manifest, Helm release and post-Helm step that runs in the cluster in `_MANIFESTS_BY_STACK`, `_HELM_RELEASES_BY_STACK` or `_POST_HELM_BY_STACK` (`src/devops_cli/commands/k8s/stack_lifecycle.py`), so that `devops k8s deploy-stack --stack all` deploys it with the `infra`, `llm`, `logging`, `devops`, `gitea` and `chat` stacks. deploy-stack applies a file of `_MANIFESTS_BY_STACK` with `kubectl apply -f` and a kustomization directory with `kubectl apply -k`.
Expand Down Expand Up @@ -108,6 +108,18 @@ The routing config is `llm/gateway/config.yaml`. LiteLLM reads it only at start.

A workstation reaches the gateway through the Cloudflare tunnel, and the Cloudflare edge cuts a request that has no data for 125 s (#2365). The CLI therefore streams every OpenAI-compatible chat call, and the gateway writes an SSE comment (`: ping`) into a streamed reply that has no data for 30 s (`litellm_settings.sse_keepalive_ping_interval_seconds`), also before its first token. Thus a long prefill, queue or reasoning does not end the call. A ping sends the status line before the gateway has routed the call, so a call whose first token comes after the first ping has no `x-litellm-*` response headers, and its spend-ledger row names no serving backend. A failure after the first ping comes as an SSE error frame under status 200, and the client reports it as the HTTP status that the frame names.

#### Gateway database and virtual keys
LiteLLM keeps its virtual keys (`/key/generate`: expiry, request and token limits, model list, alias) only in a Postgres database. The database is the CloudNativePG `Cluster` `llm-gateway-db` in `llm/gateway-db.yaml`, with one instance, 5 GiB on `local-path`, and Argo CD annotated `Prune=false,Delete=false`. The gateway keeps the master key as its admin key, and a virtual key does not replace it for the cluster Jobs yet.

- **No secret in git, no person step.** The `Cluster` names no `bootstrap.initdb.secret`. CloudNativePG therefore creates the user `litellm`, the database `litellm` and Secret `llm-gateway-db-app` with a generated password when it creates the database. The Deployment reads `DATABASE_URL` from the `uri` key of that Secret. `devops k8s push-secrets` has no row for it, and the secret guard of `tests/invariants/test_k8s_manifests.py` accepts a Secret that a `Cluster` without `initdb.secret` creates.
- **Network.** The gateway's policy gains an egress rule to the pods with label `cnpg.io/cluster: llm-gateway-db` on 5432. Policy `llm-gateway-db-perimeter` admits the gateway's pods and the operator in `cnpg-system` (8000 and 5432) and no other peer. Its egress is DNS and the Kubernetes API: the instance manager reads Secrets and updates status through the API, as the Mattermost database does. `llm-default-perimeter` leaves the database pods out.
- **Start-up.** LiteLLM applies its migrations with `prisma migrate deploy` before it opens port 4000. The root file system stays read-only: `LITELLM_MIGRATION_DIR` is an `emptyDir`, and the Prisma engines are in the image under `/opt/prisma`. A `startupProbe` gives a migrating pod ten minutes before the liveness probe starts. `ENFORCE_PRISMA_MIGRATION_CHECK=true` makes a failed migration end the start-up. Without it LiteLLM logs the failure and starts on an empty schema, and nothing runs the migration again.
- **When the database is down**, a virtual key that the gateway cannot check is refused: `allow_requests_on_db_unavailable` stays at its default `false`, because the gateway is reachable from outside the cluster. The master key needs no database, so the cluster Jobs, the CLI and Open WebUI keep working. The readiness probe uses `/health/liveliness`, because `/health/readiness` answers 503 while the database is down and Kubernetes would then remove every gateway pod from the Service. `/health/readiness` stays a manual check after a sync: its `db` field shows `connected`. Models stay in git (`store_model_in_db: false`).
- **No `LITELLM_SALT_KEY`.** LiteLLM uses the salt key only to encrypt values that it stores in the database, such as provider keys of models added at run time, and it falls back to the master key. It stores virtual keys as a SHA-256 hash of the key, so a salt key does not protect them. With `store_model_in_db: false` the database holds nothing to encrypt. Set a salt key, generated once and never rotated, before the first change that stores a credential in the database.
- **Order.** The operator is Helm release `cloudnative-pg` of stack `infra` (Argo CD Application `cloudnative-pg`). On a cluster without Argo CD, deploy-stack applies `llm/gateway-db.yaml` after the Helm releases, because the `Cluster` needs the operator's CRD. A gateway pod that starts before the database waits for Secret `llm-gateway-db-app`, and then restarts until the database accepts connections.
- **Rollout under Argo CD.** After the merge, Argo CD creates the policy and the `Cluster`, and starts a new gateway pod beside the old one (one replica, `maxSurge` 1, `maxUnavailable` 0). The new pod waits for the Secret, restarts a few times until the database accepts connections, migrates the empty database, and passes its readiness probe. Then Kubernetes stops the old pod. The old pod answers all requests meanwhile, so the gateway has no downtime. No person runs a command.
- **Backups.** The database has none yet. A lost node loses its virtual keys, and a person mints them again. #3741 adds the backup, in the form that #2144 chooses.

The Service is a NodePort that Kubernetes assigns; find it with `kubectl -n llm get svc llm-gateway`, then call the API from any node address:
```bash
KEY=$(kubectl -n llm get secret llm-gateway-secrets -o jsonpath='{.data.master-key}' | base64 -d)
Expand Down Expand Up @@ -569,6 +581,7 @@ Every Secret the stacks read comes from the workstation's OS keyring. `devops k8
| `mattermost/mattermost-db` | `password` | keyring `mattermost_db_password` | yes | adopt from `mattermost/mattermost-db password`; else generate `token_urlsafe(32)` | chat | none |
| `mattermost/mattermost-db` | `DB_CONNECTION_STRING` | derived from literal, keyring mattermost_db_password | yes | fail | chat | none |

- A Secret that a CloudNativePG `Cluster` without `bootstrap.initdb.secret` creates, such as `llm/llm-gateway-db-app`, has no row. The operator generates its password when it creates the database, and the Deployment reads it with `secretKeyRef`.
- The keyring must be unlocked (`devops devcontainer unlock-keyring`); a locked or missing keyring stops the push before anything is read or written, and stops deploy-stack before it applies anything.
- A value the keyring lacks but the cluster holds is adopted into the keyring, so a first push changes nothing live. Only values nobody types are generated, and each is stored in the keyring before it is pushed.
- Typed values are stored at a hidden prompt: `devops config set cloudflare.tunnel_token` or `devops config set gitea.admin_password`.
Expand Down Expand Up @@ -934,7 +947,7 @@ Mattermost Team Edition runs under Argo CD from the Mattermost Operator on Cloud
### What runs where

- **Operator**: `mattermost-operator` runs in namespace `mattermost-operator`.
- **Database Operator**: `cloudnative-pg` runs in namespace `cnpg-system`.
- **Database Operator**: `cloudnative-pg` runs in namespace `cnpg-system`. It also runs the gateway database of stack `llm`, so deploy-stack installs it with stack `infra`.
- **Workload**: `Mattermost` runs in namespace `mattermost`.
- **Database**: CloudNativePG `Cluster` named `mattermost-db` runs in namespace `mattermost`.
- **Volume**: PVC `mattermost-files` stores uploaded files on `local-path`.
Expand All @@ -947,7 +960,7 @@ Mattermost Team Edition runs under Argo CD from the Mattermost Operator on Cloud
### Rollout

1. Merge the pull request. Argo CD creates the namespaces and objects. The database waits for Secret `mattermost/mattermost-db`. Alert `MattermostDatabaseDown` fires until step 2 completes.
2. Run `uv run devops k8s deploy-stack --stack chat --context <context>`. Always keep the default `--wait`: CloudNativePG's admission webhook refuses the `Cluster` until its operator is ready. The deploy pushes Secret `mattermost/mattermost-db` and sets the host on Application `mattermost`.
2. Run `uv run devops k8s deploy-stack --stack chat --context <context>`. Always keep the default `--wait`: CloudNativePG's admission webhook refuses the `Cluster` until its operator is ready. Run `--stack infra` first on a cluster without Argo CD, because that stack installs the operator. The deploy pushes Secret `mattermost/mattermost-db` and sets the host on Application `mattermost`.
3. The owner signs up first from the bypassed network address. Cloudflare Access admits only the owner, so nobody else can claim the administrator account.
4. Set both persistent volumes to Retain:
```bash
Expand Down
98 changes: 98 additions & 0 deletions k8s/llm/gateway-db.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
# The database of the LiteLLM gateway: the store of its virtual keys (#1635).
#
# CloudNativePG runs it, as it runs the database of Mattermost. The Cluster names no secret under
# bootstrap.initdb, so the operator creates the user `litellm` and the database `litellm`, and
# writes Secret `llm-gateway-db-app` with a generated password and a ready connection string in
# its `uri` key. The gateway reads DATABASE_URL from that key (gateway/deployment.yaml). Git holds
# no password, and `devops k8s push-secrets` has no row for it.
#
# The operator is Helm release `cloudnative-pg` (Argo CD Application `cloudnative-pg`, stack
# `infra` of deploy-stack). The policy comes first, so it exists before the operator makes the pod.
---
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: llm-gateway-db-perimeter
namespace: llm
labels:
app.kubernetes.io/name: llm-gateway-db
app.kubernetes.io/part-of: devops-cli-stack
spec:
podSelector:
matchLabels:
cnpg.io/cluster: llm-gateway-db
policyTypes:
- Ingress
- Egress
ingress:
# The gateway is the only client of the database.
- from:
- podSelector:
matchLabels:
app.kubernetes.io/name: llm-gateway
ports:
- protocol: TCP
port: 5432
# The CloudNativePG operator reads the status of the instance (8000) and runs SQL (5432).
- from:
- namespaceSelector:
matchLabels:
kubernetes.io/metadata.name: cnpg-system
ports:
- protocol: TCP
port: 8000
- protocol: TCP
port: 5432
egress:
- to:
- namespaceSelector:
matchLabels:
kubernetes.io/metadata.name: kube-system
ports:
- protocol: UDP
port: 53
- protocol: TCP
port: 53
# The instance manager reads Secrets and updates status through the Kubernetes API server.
# Its address is a node address, which no pod selector names, so the rule has the form of the
# Mattermost database (k8s/mattermost/networkpolicy.yaml), and it excludes the metadata address.
- to:
- ipBlock:
cidr: 0.0.0.0/0
except:
- 169.254.169.254/32
ports:
- protocol: TCP
port: 443
- protocol: TCP
port: 6443
---
apiVersion: postgresql.cnpg.io/v1
kind: Cluster
metadata:
name: llm-gateway-db
namespace: llm
labels:
app.kubernetes.io/name: llm-gateway-db
app.kubernetes.io/part-of: devops-cli-stack
annotations:
# The data survives a removal of this file, a prune and a deletion of the Application.
argocd.argoproj.io/sync-options: Prune=false,Delete=false
spec:
instances: 1
imageName: ghcr.io/cloudnative-pg/postgresql:17.2
storage:
storageClass: local-path
size: 5Gi
bootstrap:
initdb:
database: litellm
owner: litellm
enableSuperuserAccess: false
resources:
requests:
cpu: 100m
memory: 256Mi
limits:
cpu: 1000m
memory: 1Gi
4 changes: 4 additions & 0 deletions k8s/llm/gateway/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -266,5 +266,9 @@ router_settings:
general_settings:
master_key: os.environ/LITELLM_MASTER_KEY
cancel_on_disconnect: true
# The models stay in git, so the database holds virtual keys and nothing LiteLLM must encrypt,
# and the gateway sets no LITELLM_SALT_KEY. Set one before this turns on (k8s/README.md).
store_model_in_db: false
# allow_requests_on_db_unavailable stays unset (false): the gateway is reachable from outside
# the cluster, so a key that it cannot check while the database is down is refused.
proxy_batch_write_at: 10
36 changes: 35 additions & 1 deletion k8s/llm/gateway/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,23 @@ spec:
name: llm-gateway-secrets
key: openrouter-api-key
optional: true
# The virtual keys live in the database that CloudNativePG runs (../gateway-db.yaml).
# The operator writes this connection string into its Secret, so git holds no
# password. The pod waits for the Secret and the database.
- name: DATABASE_URL
valueFrom:
secretKeyRef:
name: llm-gateway-db-app
key: uri
# A failed `prisma migrate deploy` ends the start-up. Without this variable LiteLLM
# logs the failure and goes on with an empty schema, and nothing runs the migration
# again.
- name: ENFORCE_PRISMA_MIGRATION_CHECK
value: "true"
# LiteLLM copies its migrations here before `prisma migrate deploy`, because the root
# file system is read-only.
- name: LITELLM_MIGRATION_DIR
value: /var/lib/litellm/migrations
# Use the bundled model cost map: the gateway never fetches it from the internet.
- name: LITELLM_LOCAL_MODEL_COST_MAP
value: "True"
Expand All @@ -89,22 +106,37 @@ spec:
readOnly: true
- name: tmp-volume
mountPath: /tmp
- name: migrations-volume
mountPath: /var/lib/litellm/migrations
resources:
requests:
cpu: 200m
memory: 512Mi
limits:
cpu: 1000m
memory: 2048Mi
# The proxy opens its port only after it has applied the migrations, which take longer
# on an empty database. This probe holds the other two back for up to ten minutes, so
# the kubelet does not kill a pod that is still migrating.
startupProbe:
httpGet:
path: /health/liveliness
port: 4000
periodSeconds: 10
failureThreshold: 60
livenessProbe:
httpGet:
path: /health/liveliness
port: 4000
initialDelaySeconds: 15
periodSeconds: 15
# Readiness does not depend on the database: /health/readiness answers 503 while the
# database is down, and Kubernetes would then remove every gateway pod from the Service,
# also for clients of the master key. The liveliness path keeps the gateway in the
# Service. A virtual key that the gateway cannot check is refused by the gateway itself.
readinessProbe:
httpGet:
path: /health/readiness
path: /health/liveliness
port: 4000
initialDelaySeconds: 10
periodSeconds: 10
Expand All @@ -117,3 +149,5 @@ spec:
name: llm-gateway-config
- name: tmp-volume
emptyDir: {}
- name: migrations-volume
emptyDir: {}
9 changes: 9 additions & 0 deletions k8s/llm/gateway/networkpolicy.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,15 @@ spec:
ports:
- protocol: TCP
port: 11434
# The database of the virtual keys (gateway-db.yaml), named by the label that CloudNativePG
# puts on its pods.
- to:
- podSelector:
matchLabels:
cnpg.io/cluster: llm-gateway-db
ports:
- protocol: TCP
port: 5432
# The hosted model groups (#3113) call the providers over HTTPS. A NetworkPolicy cannot name
# a host, so the rule admits TCP 443 to any public address. It excludes every private range,
# which kube-router matches against pod addresses (#795), and the cloud metadata address.
Expand Down
1 change: 1 addition & 0 deletions k8s/llm/kustomization.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ resources:
- valkey-runs.yaml
- valkey-devops.yaml
- profiles
- gateway-db.yaml
- gateway
- gateway/servicemonitor.yaml
- portkey/deployment.yaml
Expand Down
5 changes: 5 additions & 0 deletions k8s/llm/networkpolicy.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ spec:
- valkey
- valkey-runs
- valkey-devops
# The gateway database has its own policy (gateway-db.yaml).
- key: cnpg.io/cluster
operator: NotIn
values:
- llm-gateway-db
policyTypes:
- Ingress
- Egress
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
schema: spec-driven
created: 2026-11-06
Loading
Loading