Skip to content

远程与多服务器

一份配置可以包含多台本地或 SSH 服务器,但每次命令都应明确本次操作范围

完整示例

ts
import { caddy } from "@cratenaut/caddy";
import { defineConfig } from "@cratenaut/core";

const createWebsite = (message: string, port: number) =>
  caddy({
    id: "website",
    description: message,
    options: {
      ports: { http: port, https: false, http3: false },
      config: {
        format: "structured",
        sites: [
          {
            addresses: [`http://:${port}`],
            tls: "off",
            routes: [{ kind: "respond", body: `${message}\n` }],
          },
        ],
      },
    },
  });

export default defineConfig({
  project: "multi-server-example",
  servers: [
    {
      id: "local",
      description: "在当前计算机验证配置",
      connection: { kind: "local" },
      crates: [createWebsite("Local environment", 8080)],
    },
    {
      id: "production",
      description: "通过 SSH 管理的生产服务器",
      connection: {
        kind: "ssh",
        host: process.env.CRATENAUT_EXAMPLE_HOST ?? "server.example.com",
        user: process.env.CRATENAUT_EXAMPLE_USER ?? "deploy",
        identityFile: process.env.CRATENAUT_EXAMPLE_IDENTITY_FILE,
      },
      crates: [createWebsite("Production environment", 80)],
    },
  ],
});

SSH 前置条件

远程服务器需要:

  • 已安装并运行 Docker
  • 登录用户能够执行 Docker 命令
  • 登录用户能够读写 Cratenaut 管理根目录
  • 客户端能够通过非交互 SSH 连接

先在终端验证连接:

bash
ssh deploy@server.example.com docker version

然后执行:

bash
naut doctor --server production --all
naut plan --server production --all

不依赖当前目录的全局配置

需要从任意位置管理一组固定服务器时,可以使用:

bash
naut init --global
naut plan --global

全局模式不会改变服务器目录规范,只改变本地配置与本地元数据的查找位置

分开配置也是合理选择

多台服务器不必写在同一个配置文件中。如果环境由不同团队管理、权限边界不同,或者发布周期互不相关,使用多个配置项目通常更清晰:

bash
naut plan --config ./deploy/staging.config.ts --all
naut plan --config ./deploy/production.config.ts --all

Cratenaut 不要求所有服务或服务器组成一个全局依赖图

以 MIT 许可证发布