---
title: "Kubernetes"
description: "Deploy TRUSTIVAN AppSec and NHI Security on Kubernetes: validate, preview, deploy with migrations gated, try it locally, and what each overlay changes."
keywords: ["Kubernetes", "Kustomize", "deployment", "overlay", "self-hosted", "infra/k8s"]
---

<Note>
**Self-hosting is by agreement.** The container images (`ghcr.io/trustivan/*`)
and the source repository are private: they are provided to customers who run
TRUSTIVAN on their own infrastructure. To evaluate TRUSTIVAN, use the hosted
service from the [quickstart](/getting-started/quickstart), or [contact
us](https://trustivan.com/contact/) about self-hosting.
</Note>

TRUSTIVAN ships Kubernetes manifests in `infra/k8s`: a Kustomize base that
describes what the product needs from any cluster, and one short overlay per
environment. Kustomize rather than Helm, so each environment is a readable
patch over a base that is itself valid YAML.

<Warning>
The **demo** overlay, NHI Security included, has been deployed end to end on a
real k3s cluster, and `make k8s-up-local` repeats that on your own machine. The
**production** overlay has never been applied: it pins placeholder image
digests until images are published, and `make deploy-prod` refuses to proceed
with them. Treat production on Kubernetes as unvalidated until you have run it
yourself. The **hetzner** overlay (production on single-node k3s, below) has
been applied on a local k3d cluster running the pinned k3s version, and has never
met a real Hetzner account.
</Warning>

## What the base contains

| File | What it is |
|---|---|
| `configmap.yaml` | Non-secret configuration |
| `secret.example.yaml` | The shape of the Secret, with no values. It is never applied |
| `migrate-job.yaml` | The schema migration, run as the schema owner before anything starts, then the login roles the API and worker connect as |
| `reencrypt.yaml` | The encryption-key rotation sweep, as a suspended CronJob you run by hand |
| `api.yaml` | The API: Deployment, Service and PodDisruptionBudget |
| `worker.yaml` | The scan worker and its volume. No Service, because nothing connects to it |
| `dast.yaml` | The dynamic-scan engine and the egress gateway that is its only route out |
| `dashboard.yaml` | The console |
| `ingress.yaml` | Hostname to Service routing |
| `networkpolicy.yaml` | Default deny, then only the flows each workload needs |

The base sets no namespace, so never apply it directly; always go through an
overlay.

## Components

Overlays add these to the base:

| Component | What it adds | Used by |
|---|---|---|
| `nhi` | NHI Security: its migration Job, API and console, and their network policies. The console is served at `/nhi` on the dashboard's address, so one sign-in covers both | demo, production, hetzner |
| `nhi-postgres` | NHI Security's own in-cluster Postgres | demo, hetzner |
| `datastores` | In-cluster Postgres and Redis for TRUSTIVAN | demo, play, hetzner |
| `backup` | A nightly CronJob that dumps both in-cluster databases, reads each dump back, and uploads it encrypted to S3-compatible object storage, keeping 30 days | hetzner |
| `docs` | This documentation site, served by nginx from a volume you publish to | hetzner |
| `k3s-traefik` | Admits k3s's bundled Traefik through the network policies, which otherwise admit only `ingress-nginx` | demo, hetzner |
| `ghcr-pull` | The image pull secret for the private registry, on every workload | production, hetzner |
| `monitoring` | A small Prometheus, Alertmanager and Grafana with the shipped alert rules and dashboard. Opt in by adding it to an overlay | local |

The dashboard image must be built with `NHI_WEB_URL=http://trustowl-nhi-web:3100`
for `/nhi` to reach the console.

## Choose an overlay

|  | `demo` | `play` | `production` | `hetzner` |
|---|---|---|---|---|
| Purpose | A disposable demonstration | A public, disposable sandbox | Your deployment, on managed databases | Your deployment, on one k3s node |
| NHI Security | Included, own database | Not included | Included, managed database | Included, own database |
| Postgres and Redis | In-cluster | In-cluster | Managed services you provide | In-cluster, with the nightly encrypted backup |
| Replicas | One each | One each | 3 API, 2 dashboard | 2 API, 2 dashboard |
| Images | Tag `:demo` | Tag | Digest | Digest |
| Ingress class | `traefik` (k3s) | `nginx` | `nginx` | `traefik` (k3s), certificates from Traefik's own ACME resolver |

Production deliberately has no in-cluster database: `make k8s-validate` fails
if a StatefulSet appears in it. The `hetzner` overlay is the one exception,
because Hetzner Cloud has no managed PostgreSQL, and validation fails it if any
of its databases is left out of the backup. A fourth overlay, `local`, is `demo` on your own
machine; see [Try it locally](#try-it-locally).

## Deploy

<Steps>
  <Step title="Create the Secret">
    Create the Secret from your secret manager, following the keys in
    `base/secret.example.yaml`. External Secrets Operator, Vault or a cloud CSI
    driver all work, because the application reads plain environment variables.
    With NHI Security included, also create `trustowl-nhi-secrets`: its three
    database role passwords and a random `CREDENTIAL_ENCRYPTION_KEY`. Optional
    keys such as `ANTHROPIC_API_KEY` (Ask AI) and mail credentials are listed in
    the example; leaving one out turns that feature off.

    <Warning>
    `DATABASE_URL` must be the unprivileged application role, never the schema
    owner. The owner can lift row-level security; the API refuses to start as a
    superuser or a `BYPASSRLS` role. The migration Job alone receives the
    owner's URL.
    </Warning>
  </Step>
  <Step title="Validate and preview">
    ```bash Terminal
    make k8s-validate            # render and schema-check every overlay; no cluster needed
    make k8s-build ENV=demo      # print exactly what would be applied
    make k8s-diff  ENV=demo      # what would change on the live cluster
    ```
  </Step>
  <Step title="Deploy with the migration gated">
    ```bash Terminal
    make deploy-demo             # migration first, then the rollout
    make deploy-prod             # the same, and you type the namespace to confirm
    ```

    Do not use `kubectl apply -k` for a first deployment. It starts the
    migration Job and the Deployments at the same time, so a failed migration
    shows up as pods crash-looping against a schema they do not expect. The make
    targets run `scripts/deploy-k8s.sh`, which waits for the migration to finish
    before rolling anything out.
  </Step>
</Steps>

## Production on single-node k3s (Hetzner Cloud)

`infra/terraform/providers/hetzner` provisions one server (default `cpx41`, 8
vCPU and 16 GB, amd64 only) with a firewall that opens 80 and 443 and SSH only
from the addresses you list, a private network, a data volume that every
PersistentVolume lands on, and k3s with secrets encrypted at rest. It never
deploys TRUSTIVAN: `scripts/deploy-k8s.sh hetzner` does that, with the same
migration gate, digest check and typed confirmation as production.

<Warning>
Apply-ready, never applied. The overlay, both migrations, the backup and a
restore from it have run on a local k3d cluster with the pinned k3s version.
The Hetzner API, cloud-init on Hetzner's image, the volume mount, a Let's
Encrypt certificate issuing and Hetzner Object Storage as the backup target
have not been tested anywhere yet.
</Warning>

- **Backups.** The `backup` component runs nightly at 02:17 UTC: `pg_dump` of
  both databases and their roles, each read back before upload, encrypted on
  the way out so the bucket holds only ciphertext, and runs older than 30 days
  deleted. The encryption password and salt are the backup: keep them in your
  password manager as well as the cluster Secret. Nothing alerts on a failed run
  yet; `kubectl -n trustivan get jobs -l app.kubernetes.io/component=backup`
  lists the last five.
- **Restore.** `scripts/k8s-restore-db.sh` restores one database from a
  downloaded dump, then `scripts/deploy-k8s.sh hetzner` brings the workloads
  back. Onto a new cluster, create the same Secrets first: a new
  `TOKEN_ENCRYPTION_KEY` or `CREDENTIAL_ENCRYPTION_KEY` cannot decrypt the
  stored credentials.
- **One node.** Traefik keeps its certificates on a single volume, so it stays
  at one replica; an operating-system reboot or a k3s upgrade is a few minutes
  of downtime. For more than one node, use cert-manager instead.

## Which workload gets which secret

No workload receives the whole Secret, and `make k8s-validate` fails if that
changes:

| Workload | Receives | Never receives |
|---|---|---|
| Migration Job | The schema owner's URL to migrate; then the two login roles' URLs, to create those roles | The signing and encryption keys |
| NHI Security API | Its own application roles' passwords and credential key | NHI's owner password, every TRUSTIVAN secret |
| API | `DATABASE_URL`, `JWT_SECRET`, `TOKEN_ENCRYPTION_KEY` | The owner's URL, `JOB_DATABASE_URL` |
| Worker | `DATABASE_URL`, `JOB_DATABASE_URL`, `TOKEN_ENCRYPTION_KEY` | The owner's URL, `JWT_SECRET` |

The worker handles untrusted images, so it never holds the key that signs
sessions. Key rotation variables reach the API and the worker when present; see
[Key rotation](/deployment/key-rotation).

## Try it locally

With Docker installed, one command builds the images, starts a local k3s
cluster with [k3d](https://k3d.io), deploys the `local` overlay and runs the
smoke checks: sign-up and sign-in, a scan finished by the worker, the NHI
Security console, and Ask AI refusing cleanly when no key is set.

```bash Terminal
make k8s-up-local      # your kubectl context is not changed
make k8s-down-local    # delete the cluster and everything in it
```

The `local` overlay is `demo` plus two differences: vulnerability data comes
from the public upstream source, because a workstation has no mirror, and it
includes the monitoring component.

## Related

- [Deployment topology](/deployment/topology): the process shapes and what deployment tooling does not exist yet.
- [Production Compose](/installation/production-compose): the single-host alternative.
- [Dynamic scanning](/configuration/dynamic-scanning): how the scan engine and its gateway are isolated.
- [NHI Security deployment](/nhi-security/deploy): NHI Security's own Kubernetes files.
