# 部署

在生产环境运行 rungent。本页讲 Docker 镜像、环境变量怎样交给它、数据库与密钥、反向代理和多副本。

嵌入式的 rungent 随你的 JavaScript 应用一起部署。在其他语言写的 API 旁边，rungent 作为独立服务 `rungent serve` 运行：用环境变量配置，状态存在 Postgres 里，放在已经为你的产品服务的反向代理后面。

```text
browser ─> reverse proxy (console.example.com)
             ├─ /rungent/v0/*, /mcp, /skill.md, /llms.txt, /.well-known/… ─> rungent serve ─> Postgres
             └─ everything else ──────────────────────────────────────> your API
rungent serve ─(the user's credential + Rungent-Assertion)─> your API
```

## 运行

```bash
docker run -d --name rungent -p 8080:8080 --env-file rungent.env ghcr.io/rungent/rungent-server:v0.4.0
```

镜像按每个发布版本打标签（`v0.4.0`），另有 `latest`。镜像以 `node` 用户在 8080 端口运行 `rungent serve`。不用 Docker 的话，`npx rungent serve` 在 Node.js 24 上运行同一个服务。

`GET /healthz` 返回 `ok`；即使 rungent 放在某个前缀下，它也在根路径上，镜像的健康检查就调用它。收到 `SIGTERM` 时，服务停止接收请求，等正在进行的运行结束后退出。要基于你自己的基础镜像构建，对 rungent 仓库 `packages/rungent` 下的 `Dockerfile` 传入 `--build-arg BASE_IMAGE=… --build-arg RUNGENT_VERSION=0.4.0`。

## 设置环境变量

`rungent serve` 从 `RUNGENT_*` 环境变量读取设置；每个变量的含义见[配置](configure.md#where-each-setting-lives)。必填的有五个：`RUNGENT_URL`、`RUNGENT_HOST_ORIGIN`、`RUNGENT_SIGNING_KEY`、`RUNGENT_SECRET` 和 `RUNGENT_DATABASE_URL`。大多数部署还会设置 `RUNGENT_HOST_BASE_URL`，让 rungent 走内网访问你的 API，以及 `RUNGENT_MODELS`。

```bash
# rungent.env
RUNGENT_URL=https://console.example.com
RUNGENT_HOST_ORIGIN=https://console.example.com
RUNGENT_HOST_BASE_URL=http://api.internal:8000
RUNGENT_SIGNING_KEY={"kty":"OKP","crv":"Ed25519","kid":"rungent-2026-11",…}
RUNGENT_SECRET=…
RUNGENT_DATABASE_URL=postgres://rungent:…@db.internal/rungent
RUNGENT_MODELS={"chain":[…]}
```

变量缺失、不是合法 JSON 或超出范围时，服务在启动时就停下，给出写明变量名的消息，退出码为 2。把 `RUNGENT_SIGNING_KEY`、`RUNGENT_SECRET`、`RUNGENT_PREVIOUS_SECRET`、`RUNGENT_DATABASE_URL` 和 `RUNGENT_MODELS`（里面有 API key）放在你的密钥存储里。

## 数据库与密钥

生产环境用 Postgres。rungent 把对话、确认、延迟任务、设置和知识索引都存在那里，放在自己的 `rungent_*` 表中，启动时自动创建和升级。请给它单独的数据库或 schema。SQLite 不能被多个副本共享。

签名密钥及其轮换见[以用户身份调用](acting-as-the-user.md#keys-and-rotation)。要轮换设置用的 secret，把新值设为 `RUNGENT_SECRET`、旧值设为 `RUNGENT_PREVIOUS_SECRET`，然后部署：新 secret 用于加密，两个都能解密；已保存的设置在第一次读取时用新 secret 重新加密。一旦有运行读取过设置，就可以去掉 `RUNGENT_PREVIOUS_SECRET`。如果设置是用 rungent 已经没有的 secret 加密的，请求会返回 `503 settings_unreadable`，直到管理员调用 `DELETE /rungent/v0/settings`。

## 路由 rungent 的路径

把你产品 origin 上的这些路径原样转给 rungent，其余的转给你的 API：

| 路径 | 提供什么 |
|---|---|
| `/rungent/v0/*` | 助手的 HTTP API 及其事件流 |
| `/mcp` | 基于 streamable HTTP 的 MCP |
| `/skill.md`、`/llms.txt` | 从你的目录生成的文档 |
| `/.well-known/rungent-keys.json` | rungent 的公钥 |
| `/.well-known/oauth-protected-resource/mcp` | 仅在设置了 `RUNGENT_MCP_AUTHORIZATION_SERVERS` 时 |

运行以 server-sent events 流式输出，所以要关掉响应缓冲，并允许长时间读取。nginx 的写法：

```nginx
location ~ ^/(rungent/v0/|mcp$|skill\.md$|llms\.txt$|\.well-known/(rungent-keys\.json|oauth-protected-resource/mcp)$) {
    proxy_pass http://rungent:8080;
    proxy_http_version 1.1;
    proxy_set_header Host $host;
    proxy_buffering off;
    proxy_read_timeout 1h;
}
```

`Authorization` 和 `Cookie` 请求头要原样转发：它们就是用户的凭据。要把 rungent 放在 `/assistant` 下，设置 `RUNGENT_BASE_PATH=/assistant` 和 `RUNGENT_URL=https://console.example.com/assistant`，代理 `/assistant/`，并给 Web 客户端传 `createClient({ baseUrl: '/assistant' })`。

## 运行多个副本

多个副本可以共用一个 Postgres：一次确认只执行一次，同一段对话不会同时被两个副本驱动。有两样东西只存在单个副本的内存里：

- **MCP 会话。** 把带同一个 `Mcp-Session-Id` 的请求都路由到同一个副本（粘性会话，或在 Kubernetes Service 上设 `sessionAffinity: ClientIP`）。落到别的副本会得到 `404 session_not_found`，客户端随即开一个新会话。
- **模型休息状态。** 每个副本各自发现某个模型端点在失败。

在 Kubernetes 上，用 Deployment 运行这个镜像，变量来自一个 Secret：

```yaml
containers:
  - name: rungent
    image: ghcr.io/rungent/rungent-server:v0.4.0
    ports: [{ containerPort: 8080 }]
    envFrom: [{ secretRef: { name: rungent-env } }]
    readinessProbe: { httpGet: { path: /healthz, port: 8080 } }
```

## 检查

- `npx rungent check --openapi https://console.example.com/api/openapi.json` 通过。
- 用某个用户的令牌请求 `GET /rungent/v0/catalog`，列出的是这个用户的操作。
- 通过面板发起的写操作会弹出确认卡，你的 API 日志记下 `actor = rungent` 和确认 id。
- `/skill.md` 能正常显示，编码 Agent 能连上 `/mcp`。

## 接下来

- [配置](configure.md#choose-models)：模型链，以及修改它的管理页面。
- [测试](testing.md)针对你的部署衡量 Agent 的表现。
