# Deployment steps (/self-hosting/deployment/deployment-steps)

The examples serve the app on `toucan.example.com`, these docs on `docs.toucan.example.com` and the
custom fronts on `*.usercontent.example.net`, and name the release `toucan-ai`.

## Before you start [#before-you-start]

These steps assume you know Helm values, ingresses or the Gateway API, TLS secrets and persistent
volumes. You need:

- `kubectl` and `helm` 3.8 or later, with access to a Kubernetes cluster that has an ingress
  controller or the Gateway API.
- PostgreSQL 16 or 17 with the `pgvector` and `pgcrypto` extensions, Redis, an S3-compatible bucket
  and an SMTP server.
- An LLM endpoint and an embeddings endpoint.
- DNS names and TLS certificates for the app, for these docs unless you
  [use ours](../authentication-and-networking/network-requirements.mdx#inbound), and for the custom
  fronts on a domain of their own.
- From us, an access to our registry and a license key.

[Required components](../architecture/required-components.mdx) details what each one needs. Without
internet access, also follow the [air-gapped installation](./air-gapped.mdx).

For an evaluation, the chart can run PostgreSQL, Redis and the bucket itself: uncheck those it runs,
and the steps below change to match.

<ProvidedComponents />

## 1. Create the database and its users [#1-create-the-database-and-its-users]

<When provided="postgresql">

```sql
CREATE USER toucan_admin WITH SUPERUSER PASSWORD '<admin password>';
CREATE USER toucan_app WITH PASSWORD '<app password>';
CREATE DATABASE toucan OWNER toucan_admin;
```

</When>

<When bundled="postgresql">

Nothing to do: the chart's PostgreSQL creates them.

</When>

## 2. Create the secrets [#2-create-the-secrets]

```sh
kubectl create secret generic toucan-ai \
  --from-literal=LICENSE_KEY='<license key>' \
  --from-literal=BETTER_AUTH_SECRET="$(openssl rand -hex 32)" \
  --from-literal=JWE_SECRET="$(openssl rand -hex 32)" \
  --from-literal=ENCRYPTION_KEYS="{\"v1\":\"$(openssl rand -hex 32)\"}" \
  --from-literal=CURRENT_ENCRYPTION_KEY_ID=v1 \
  --from-literal=ADMIN_DATABASE_URL='postgresql://toucan_admin:<admin password>@db.example.com:5432/toucan' \
  --from-literal=DATABASE_URL='postgresql://toucan_app:<app password>@db.example.com:5432/toucan' \
  --from-literal=REDIS_URL='redis://:<password>@redis.example.com:6379' \
  --from-literal=OBJECT_STORAGE_ACCESS_KEY_ID='<access key id>' \
  --from-literal=OBJECT_STORAGE_SECRET_ACCESS_KEY='<secret access key>' \
  --from-literal=SMTP_PASS='<SMTP password>' \
  --from-literal=OPENAI_COMPATIBLE_API_SECRET='<LLM API key>' \
  --from-literal=EMBEDDING_API_SECRET='<embeddings API key>'
```

<When provided="postgresql">

:::danger
`ENCRYPTION_KEYS` encrypts the credentials of your data sources: keep a copy of it, and never
recreate this secret, as a new key cannot read them back.
:::

</When>

<When bundled="objectStorage">

The chart's bucket takes its keys from a secret of its own:

```sh
kubectl create secret generic toucan-ai-bucket \
  --from-literal=object-storage-access-key-id="GK$(openssl rand -hex 12)" \
  --from-literal=object-storage-secret-access-key="$(openssl rand -hex 32)" \
  --from-literal=object-storage-rpc-secret="$(openssl rand -hex 32)"
```

</When>

You need to also provide a secret for the data service basic authentication:

```sh
kubectl create secret generic toucan-ai-data-service \
  --from-literal=data-service-auth-api-key="$(openssl rand -hex 32)"
```

To change a value, update the secret, for example with `kubectl patch`, then restart the pods: they
read it only when they start.

```sh
kubectl patch secret toucan-ai -p '{"stringData":{"SMTP_PASS":"<new password>"}}'
kubectl rollout restart deployment -l app.kubernetes.io/instance=toucan-ai
```

:::info
In production, have your secret manager create this secret, with the External Secrets Operator for
instance: the chart only reads it by its name.
:::

The examples use an OpenAI-compatible LLM endpoint. With another provider, replace its key here and
its values in step 3, as [LLM and embeddings](../architecture/required-components.mdx#llm-and-embeddings)
lists.

When the license expires, Toucan AI keeps working for 30 days, then blocks access until you set a new
key.

The cluster pulls our images with an access token: create one in the customer portal we invite you
to, then:

```sh
kubectl create secret docker-registry distr --docker-server=registry.distr.sh \
  --docker-username=- --docker-password="$DISTR_TOKEN"
```

The ingress serves TLS from two secrets: one whose certificate covers `toucan.example.com` and
`docs.toucan.example.com`, the other with the wildcard certificate of the custom fronts. Run the same
commands to renew them:

```sh
kubectl create secret tls toucan-ai-tls --cert=toucan.crt --key=toucan.key \
  --dry-run=client -o yaml | kubectl apply -f -
kubectl create secret tls toucan-ai-usercontent-tls --cert=usercontent.crt --key=usercontent.key \
  --dry-run=client -o yaml | kubectl apply -f -
```

## 3. Write your values [#3-write-your-values]

Generate the admin organization's ID with `uuidgen`, then:

```yaml
global:
  imagePullSecrets:
    - name: distr # created in step 2

# Accounts are created only from an invitation or an organization's SSO.
inviteOnlySignup: true

# The bundled PostgreSQL, Redis and bucket are for evaluation only: see Required components.
postgresql:
  enabled: false
redis:
  enabled: false
objectStorage:
  enabled: false
  auth:
    existingSecret: toucan-ai-bucket # created in step 2

ingress:
  enabled: true
  className: nginx
  hosts:
    - host: toucan.example.com
      paths:
        - path: /
          pathType: Prefix
  docs:
    hosts:
      - host: docs.toucan.example.com
        paths:
          - path: /
            pathType: Prefix
  tls:
    - secretName: toucan-ai-tls # created in step 2
      hosts:
        - toucan.example.com
        - docs.toucan.example.com

backend:
  envFrom:
    - secretRef:
        name: toucan-ai
  config:
    PUBLIC_URL: https://toucan.example.com/api
    FRONTEND_URL: https://toucan.example.com
    FRONTS_FILE_SERVER_ORIGIN: https://{orgId}.usercontent.example.net
    DOC_URL: https://docs.toucan.example.com
    ADMIN_ORGANIZATION_ID: <uuid>
    SMTP_HOST: smtp.example.com
    SMTP_PORT: "465"
    SMTP_SECURE: "true"
    SMTP_USER: toucan
    SMTP_FROM: toucan@example.com
    OBJECT_STORAGE_ENDPOINT: https://s3.example.com
    OBJECT_STORAGE_BUCKET: toucan-ai
    OBJECT_STORAGE_REGION: <region>
    AI_DEFAULT_PROVIDER: openai-compatible
    OPENAI_COMPATIBLE_API_URL: https://llm.example.com/v1
    OPENAI_COMPATIBLE_MODEL: <model>
    EMBEDDING_API_URL: https://embeddings.example.com/v1
    EMBEDDING_DEFAULT_MODEL: bge-m3
  # Serves the custom fronts, each organization on its own subdomain.
  frontsFileServer:
    enabled: true
    ingress:
      enabled: true
      className: nginx
      hosts:
        - host: "*.usercontent.example.net"
          paths:
            - path: /
              pathType: Prefix
      tls:
        - secretName: toucan-ai-usercontent-tls # created in step 2
          hosts:
            - "*.usercontent.example.net"

dataService:
  auth:
    existingSecret: toucan-ai-data-service

frontend:
  config:
    ADMIN_ORGANIZATION_ID: <uuid>
    DOC_URL: https://docs.toucan.example.com
    DOCS_PRODUCT_URL: https://toucan.example.com
    STATUS_PAGE_URL: ""
```

To use our docs instead, set `ingress.docs.hosts: []` and leave out `docs.toucan.example.com` and
both `DOC_URL`.

With the Gateway API, replace `ingress` and `backend.frontsFileServer.ingress` with routes attached
to your Gateway. It then terminates TLS, so skip the TLS secrets of step 2 and give its listeners the
certificates instead, the custom fronts' wildcard included, as the
[Gateway API guide](https://gateway-api.sigs.k8s.io/guides/user-guides/tls/) shows:

```yaml
httpRoute:
  enabled: true
  hostnames: [toucan.example.com]
  parentRefs:
    - name: my-gateway
      namespace: gateway-system
  docs:
    enabled: true
    hostnames: [docs.toucan.example.com]
    parentRefs:
      - name: my-gateway
        namespace: gateway-system

backend:
  frontsFileServer:
    httpRoute:
      enabled: true
      hostnames: ["*.usercontent.example.net"]
      parentRefs:
        - name: my-gateway
          namespace: gateway-system
```

To let your users try Toucan AI on demo data, add `sampleDatabase.enabled: true`: the chart then runs
a PostgreSQL filled with HR data, which they can connect when they add a data source.

## 4. Install the chart [#4-install-the-chart]

Log in to our registry with the same token, then install a [version](./upgrade-strategy.mdx):

```sh
helm registry login registry.distr.sh -u - --password-stdin <<< "$DISTR_TOKEN"
helm upgrade --install toucan-ai oci://registry.distr.sh/toucantoco/toucan-ai-chart --version <version> \
  -f values.yaml
```

The first install runs the database migrations before the app starts.

## 5. Check the deployment [#5-check-the-deployment]

Wait for every deployment to be ready, then call the health check, which answers `OK`:

```sh
kubectl wait --for=condition=available deployment -l app.kubernetes.io/instance=toucan-ai --timeout=15m
curl https://toucan.example.com/api/health
```

The backend pods start only once the migrations are done. If the wait times out, `kubectl get pods`
shows which pod is stuck, and the migration Job's logs say why:

```sh
kubectl logs -l app.kubernetes.io/subcomponent=migrate --tail=100
```

[Migrations](../architecture/helm-chart.mdx#migrations) says how to run the Job again once fixed.

## 6. Create the first admin [#6-create-the-first-admin]

```sh
kubectl exec deploy/toucan-ai-backend-web -- bun run --cwd apps/backend bootstrap-admin \
  --user-email you@example.com --organization-name "Example"
```

This creates the admin organization and its owner, then prints a link valid 24 hours. **Open the
link to set the owner's password**, then sign in with it. It needs no SMTP, and running the command
again prints a new link.

## 7. Check the instance [#7-check-the-instance]

Sign in, then open the [Instance page]($PRODUCT_URL/admin/instance): it shows the version, the
license, and whether each component answers. Test the LLM and the embeddings from there too.

## Roll back [#roll-back]

`helm uninstall toucan-ai` removes the release. The database, the bucket and the secrets you created
stay as they are, so installing again picks up where you left off. To start from scratch, also drop
the database and empty the bucket. The bundled PostgreSQL, Redis and bucket keep their data in
volumes that stay too: delete them with
`kubectl delete pvc -l app.kubernetes.io/instance=toucan-ai`, then the volumes themselves if your
storage class retains them (`reclaimPolicy: Retain`).

Once the instance is in use, roll an upgrade back as [Upgrade strategy](./upgrade-strategy.mdx#roll-back)
says.

## Next steps [#next-steps]

- Start using Toucan AI with [Getting started with Core](../../core/getting-started/index.mdx): your
  account already exists, so skip its first step.
- To give a team its own organization, create it from the organization menu and invite its owner.
- Connect organizations to your identity provider: see
  [Identity integration](../authentication-and-networking/identity-integration.mdx).
- Keep the instance up to date: see [Upgrade strategy](./upgrade-strategy.mdx).
