> ## Documentation Index
> Fetch the complete documentation index at: https://cndoc-langchain.site/llms.txt
> Use this file to discover all available pages before exploring further.

# 自托管独立服务器

> 使用 Docker、Docker Compose 或 Kubernetes 部署独立的 Agent 服务器，无需 LangSmith 控制平面。

本指南向您展示如何直接部署**独立的 <Tooltip tip="运行您的 LangGraph 应用程序的服务器。">Agent 服务器</Tooltip>**，而无需使用 [LangSmith 控制平面](/langsmith/deploy-with-control-plane)。您可以独立部署服务器，同时仍可单独使用 LangSmith 进行跟踪和评估。独立服务器已具备生产就绪能力，并为运行代理提供了最轻量级的自托管选项。

<Note>
  有关自托管部署选项的概述，请参阅[自托管选项](/langsmith/self-hosted)。
</Note>

## 前提条件

1. 使用 [LangGraph CLI](/langsmith/cli) [在本地测试您的应用程序](/langsmith/local-dev-testing)。
2. 使用 [LangGraph CLI](/langsmith/cli) 构建 Docker 镜像（即 `langgraph build`）。
3. 数据平面部署需要以下环境变量。
4. `REDIS_URI`：Redis 实例的连接详情。Redis 将用作发布-订阅代理，以实现从后台运行中流式传输实时输出。`REDIS_URI` 的值必须是有效的 [Redis 连接 URI](https://redis-py.readthedocs.io/en/stable/connections.html#redis.Redis.from_url)。

   <Note>
     **共享 Redis 实例**
     多个自托管部署可以共享同一个 Redis 实例。例如，对于 `Deployment A`，`REDIS_URI` 可以设置为 `redis://<hostname_1>:<port>/1`，对于 `Deployment B`，`REDIS_URI` 可以设置为 `redis://<hostname_1>:<port>/2`。

     `1` 和 `2` 是同一实例内的不同数据库编号，但 `<hostname_1>` 是共享的。**不能为不同的部署使用相同的数据库编号**。
   </Note>
5. `DATABASE_URI`：Postgres 连接详情。Postgres 将用于存储助手、线程、运行，持久化线程状态和长期记忆，并以“恰好一次”语义管理后台任务队列的状态。`DATABASE_URI` 的值必须是有效的 [Postgres 连接 URI](https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-CONNSTRING-URIS)。

   <Note>
     **共享 Postgres 实例**
     多个自托管部署可以共享同一个 Postgres 实例。例如，对于 `Deployment A`，`DATABASE_URI` 可以设置为 `postgres://<user>:<password>@/<database_name_1>?host=<hostname_1>`，对于 `Deployment B`，`DATABASE_URI` 可以设置为 `postgres://<user>:<password>@/<database_name_2>?host=<hostname_1>`。

     `<database_name_1>` 和 `database_name_2` 是同一实例内的不同数据库，但 `<hostname_1>` 是共享的。**不能为不同的部署使用相同的数据库**。
   </Note>

   <Tip>
     您可以选择将检查点数据存储在 MongoDB 中，而不是 PostgreSQL。所有其他服务器数据仍需要 PostgreSQL。有关详细信息，请参阅[配置检查点后端](/langsmith/configure-checkpointer)。
   </Tip>
6. `LANGSMITH_API_KEY`：LangSmith API 密钥。
7. `LANGGRAPH_CLOUD_LICENSE_KEY`：LangSmith 许可证密钥。这将用于在服务器启动时进行一次性身份验证。
8. `LANGSMITH_ENDPOINT`：要将跟踪发送到[自托管 LangSmith](/langsmith/self-hosted) 实例，请将 `LANGSMITH_ENDPOINT` 设置为自托管 LangSmith 实例的主机名。
9. 从您的网络出站到 `https://beacon.langchain.com`。如果不在气隙模式下运行，这是许可证验证和使用情况报告所必需的。有关更多详细信息，请参阅[出站文档](/langsmith/self-host-egress)。

<a id="helm" />

## Kubernetes

使用此 [Helm chart](https://github.com/langchain-ai/helm/blob/main/charts/langgraph-cloud/README.md) 将 Agent 服务器部署到 Kubernetes 集群。这是生产环境独立服务器部署的推荐设置。

Helm chart (v0.2.6+) 支持使用捆绑实例（开发/测试）或外部部署（生产）进行 MongoDB 检查点存储。在您的 values 文件中设置 `mongo.enabled: true`。有关完整的配置详细信息，请参阅[配置检查点后端](/langsmith/configure-checkpointer#deploy-by-environment)。

## Docker

此 `docker` 示例适用于本地开发和测试。

运行以下 `docker` 命令：

```shell theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
docker run \
    --env-file .env \
    -p 8123:8000 \
    -e REDIS_URI="foo" \
    -e DATABASE_URI="bar" \
    -e LANGSMITH_API_KEY="baz" \
    my-image
```

<Note>
  * 您需要将 `my-image` 替换为您在前提条件步骤中构建的镜像名称（来自 `langgraph build`），
    并且应为 `REDIS_URI`、`DATABASE_URI` 和 `LANGSMITH_API_KEY` 提供适当的值。

  * 如果您的应用程序需要额外的环境变量，您可以以类似的方式传递它们。
</Note>

## Docker Compose

此 Docker Compose 示例适用于本地开发和测试。

使用以下 Docker Compose 文件：

```yml theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
volumes:
    langgraph-data:
        driver: local
services:
    langgraph-redis:
        image: redis:6
        healthcheck:
            test: redis-cli ping
            interval: 5s
            timeout: 1s
            retries: 5
    langgraph-postgres:
        image: postgres:16
        ports:
            - "5432:5432"
        environment:
            POSTGRES_DB: postgres
            POSTGRES_USER: postgres
            POSTGRES_PASSWORD: postgres
        volumes:
            - langgraph-data:/var/lib/postgresql/data
        healthcheck:
            test: pg_isready -U postgres
            start_period: 10s
            timeout: 1s
            retries: 5
            interval: 5s
    langgraph-api:
        image: ${IMAGE_NAME}
        ports:
            - "8123:8000"
        depends_on:
            langgraph-redis:
                condition: service_healthy
            langgraph-postgres:
                condition: service_healthy
        env_file:
            - .env
        environment:
            REDIS_URI: redis://langgraph-redis:6379
            LANGSMITH_API_KEY: ${LANGSMITH_API_KEY}
            DATABASE_URI: postgres://postgres:postgres@langgraph-postgres:5432/postgres?sslmode=disable
```

在包含此文件的文件夹中运行 `docker compose up`。

<Accordion title="使用 MongoDB 检查点存储">
  要将检查点存储在 MongoDB 中而不是 PostgreSQL 中，请添加一个 MongoDB 服务并配置检查点后端。在您的 `langgraph.json` 中将后端设置为 `"mongo"`，或使用 `LS_DEFAULT_CHECKPOINTER_BACKEND` 环境变量。所有其他服务器数据仍需要 PostgreSQL。

  ```yml theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
  volumes:
      langgraph-data:
          driver: local
      langgraph-mongo-data:
          driver: local
  services:
      langgraph-redis:
          image: redis:6
          healthcheck:
              test: redis-cli ping
              interval: 5s
              timeout: 1s
              retries: 5
      langgraph-postgres:
          image: postgres:16
          ports:
              - "5432:5432"
          environment:
              POSTGRES_DB: postgres
              POSTGRES_USER: postgres
              POSTGRES_PASSWORD: postgres
          volumes:
              - langgraph-data:/var/lib/postgresql/data
          healthcheck:
              test: pg_isready -U postgres
              start_period: 10s
              timeout: 1s
              retries: 5
              interval: 5s
      langgraph-mongo:
          image: mongo:7
          command: ["mongod", "--replSet", "rs0"]
          ports:
              - "27017:27017"
          volumes:
              - langgraph-mongo-data:/data/db
          healthcheck:
              test: mongosh --eval "try { rs.status().ok } catch(e) { rs.initiate({_id:'rs0',members:[{_id:0,host:'langgraph-mongo:27017'}]}).ok }" --quiet
              interval: 5s
              timeout: 10s
              retries: 10
              start_period: 10s
      langgraph-api:
          image: ${IMAGE_NAME}
          ports:
              - "8123:8000"
          depends_on:
              langgraph-redis:
                  condition: service_healthy
              langgraph-postgres:
                  condition: service_healthy
              langgraph-mongo:
                  condition: service_healthy
          env_file:
              - .env
          environment:
              REDIS_URI: redis://langgraph-redis:6379
              LANGSMITH_API_KEY: ${LANGSMITH_API_KEY}
              DATABASE_URI: postgres://postgres:postgres@langgraph-postgres:5432/postgres?sslmode=disable
              LS_DEFAULT_CHECKPOINTER_BACKEND: mongo
              LS_MONGODB_URI: mongodb://langgraph-mongo:27017/langgraph?replicaSet=rs0
  ```

  有关 MongoDB 配置选项的更多详细信息，请参阅[配置检查点后端](/langsmith/configure-checkpointer)。
</Accordion>

这将在端口 `8123` 上启动一个 Agent 服务器（如果需要，可以更改 `langgraph-api` 中的端口映射）。测试应用程序是否健康：

```shell theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
curl --request GET --url 0.0.0.0:8123/ok
```

假设一切运行正常，您应该看到如下响应：

```shell theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
{"ok":true}
```

***

<div className="source-links">
  <Callout icon="terminal-2">
    [将这些文档连接](/use-these-docs)到 Claude、VSCode 等，通过 MCP 获取实时答案。
  </Callout>

  <Callout icon="edit">
    [在 GitHub 上编辑此页面](https://github.com/langchain-ai/docs/edit/main/src/langsmith/deploy-standalone-server.mdx) 或 [提交问题](https://github.com/langchain-ai/docs/issues/new/choose)。
  </Callout>
</div>
