dstackai/dstack · 上手攻略
- 仓库:dstackai/dstack
- 链接:https://github.com/dstackai/dstack
- 分类:ai / engineering(AI 基础设施 / GPU 编排)
- 作者:Jay
- 更新:2026-07-13
是什么
dstack 是一个厂商无关的 GPU 供给与编排控制平面,支持在任意 GPU 云、Kubernetes 集群和裸机服务器上统一运行训练、推理和 Agentic 工作负载。它用一套简洁的 YAML 配置(CLI / API / UI)替代了多套云控制台和运维脚本,让 GPU 资源的管理从「到处点鼠标」变成「声明式配置 + 一键部署」。
dstack 原生支持 NVIDIA、AMD、Google TPU、Tenstorrent 四种硬件生态,并可连接 AWS、Crusoe、Lambda Labs、Azure、GCP 等主流 GPU 云,以及现有 Kubernetes 集群。
一句话总结:GPU 集群的万能编排层——不管 GPU 在哪、是什么型号,dstack 统一管起来。
Stars:2173 ⭐ | 语言:Python | 许可:MPL 2.0
解决什么问题
| 痛点 | 传统做法 | dstack 解法 |
|---|---|---|
| 多云 GPU 管理 | 各自云控制台 + Terraform 脚本 | 统一 YAML + dstack apply |
| GPU 资源浪费 | 手动登录机器操作 | 按需申请,自动回收 |
| 分布式训练编排 | 写 SLURM / 自己写调度 | tasks 配置声明分布式 job |
| 模型服务部署 | 写 Dockerfile + K8s YAML | services 配置一行搞定自动扩缩容 |
| 硬件锁定 | 换云要重写全部配置 | 厂商无关,换后端只需改一个字段 |
| AI Agent 调 GPU | 手动 SSH | npx skills add dstackai/dstack 让 Agent 自己申请 |
快速安装
环境要求
- Linux / macOS / Windows (WSL 2)
- Git
- OpenSSH
- Python 3.10+
Step 1:安装 dstack CLI(含 all 插件)
uv tool install "dstack[all]" -U
⚠️ 建议使用
uv(Astral 的 Python 包管理器),速度比 pip 快且隔离更好。如未安装:curl -LsSf https://astral.sh/uv/install.sh | sh
Step 2:启动 dstack Server
dstack server
输出示例:
Applying ~/.dstack/server/config.yml...
The admin token is "bbae0f28-d3dd-4820-bf61-8f4bb40815da"
The server is running at http://127.0.0.1:3000/
首次运行会自动创建 ~/.dstack/server/config.yml 配置文件。
访问 http://127.0.0.1:3000/ 打开 Web UI。
Step 3:配置后端(连接 GPU 云或 K8s)
编辑 ~/.dstack/server/config.yml,以 AWS 为例:
projects:
- name: main
backends:
- type: aws
creds:
type: default
默认使用 default credentials(
~/.aws/credentials),也可以用 access_key 方式指定。
支持的完整后端列表: - VM-based:AWS、Azure、GCP、Crusoe、Lambda Labs、Hyperstack、Vast.ai、On-prem (SSH) - Container-based:上述云 + Kubernetes
Step 4:让 CLI 指向 Server(如 CLI 未随 Server 一起装)
dstack project add \
--name main \
--url http://127.0.0.1:3000 \
--token bbae0f28-d3dd-4820-bf61-8f4bb40815da
配置写入 ~/.dstack/config.yml。
核心用法
核心概念
| 概念 | 作用 |
|---|---|
| Fleet(集群) | 一组 GPU 实例(来自任意后端),统一管理 |
| Dev Environments | 交互式开发环境(对应桌面 IDE) |
| Tasks | 批处理任务(支持单机/多机/分布式) |
| Services | 模型/API 服务(带自动扩缩容) |
| Volumes | 持久化存储卷 |
| Gateways | HTTP 路由与负载均衡 |
用 YAML 声明工作负载
所有配置均在仓库内以 YAML 文件管理,示例:
fleet 定义(创建 GPU 集群):
# fleet.yml
fleet:
- name: my-gpu-fleet
backends: [aws]
config:
instance_type: g5.2xlarge # NVIDIA A10G
region: us-east-1
count: 2
dstack apply -f fleet.yml
Task 定义(分布式训练):
# task.yml
tasks:
- name: train-job
fleet: my-gpu-fleet
image: pytorch/pytorch:2.4.0-cuda12.1
script:
- torchrun --nproc_per_node=2 train.py
dstack apply -f task.yml
dstack tasks submit -f task.yml
Service 定义(模型部署):
# service.yml
services:
- name: my-llm
fleet: my-gpu-fleet
image: vllm/vllm-openai:latest
port: 8000
scaling:
min: 1
max: 4
env:
MODEL_NAME: Qwen/Qwen3-8B
dstack apply -f service.yml
让 AI Agent 直接调 dstack
安装 dstack 官方 Agent Skill:
npx skills add dstackai/dstack
之后 AI Agent(Claude、Codex、Cursor 等)可以直接创建 Fleet、提交 Task、管理 Service,无需人工介入。
管理命令速查
dstack fleets list # 查看集群
dstack tasks list # 查看任务
dstack services list # 查看服务
dstack apply -f config.yml # 应用 YAML 配置
dstack volumes list # 查看存储卷
典型适用场景
| 场景 | 为什么用 dstack |
|---|---|
| 多云 GPU 训练 | 一套配置在 AWS/GCP/Lambda Labs 之间无缝切换 |
| 分布式训练(多机多卡) | 声明式配置替代 SLURM 脚本,支持 PyTorch Distributed、TorchRun |
| 模型服务化部署 | services 配置自动处理扩缩容、负载均衡、健康检查 |
| GPU 资源共享团队 | 多用户/多项目共享 Fleet,按需排队,避免 GPU 闲置 |
| Kubernetes 集成 | 无需学 K8s,直接用 dstack YAML 管理 |
| AI Agent 自动化 | Agent 通过 Skill 自己申请 GPU,人工零介入 |
坑与注意
- Server 是必须的:CLI 本身只是客户端,必须先有 dstack server 运行才能工作。单 CLI 无法直接调度 GPU。
- SSH 公钥配置:裸机和部分云后端需要配置 SSH 公钥才能连接节点,确保
~/.ssh/id_rsa.pub已配置。 - 后端凭证安全:
~/.dstack/server/config.yml包含云凭证,不要提交到 Git——已配置~/.gitignore规则,但仍需注意。 - Windows 必须用 WSL 2:dstack server 不支持原生 Windows,必须在 WSL 2 环境下运行。
- Kubernetes 后端需要 kubeconfig:确保
~/.kube/config正确配置,且 dstack server 有权限访问。 - AWS 权限要完整:policy 需要能操作 EC2、ELB、IAM 等,具体最小权限集见官方文档(比 AWSBedrock 复杂)。
- TPU 支持有限:dstack 支持 Google TPU,但社区和文档以 NVIDIA GPU 为主力,TPU 场景建议先查 Issue。
- Server 本地运行 vs 远程:上面的
dstack server启动的是本地 server,生产环境建议通过 Docker 或后台进程运行,文档见Server Deployment。
与同类对比
| 项目 | 定位 | dstack 优势 |
|---|---|---|
| Ray (Anyscale) | 分布式 Python 计算框架 | dstack 更偏 GPU 基础设施层,Ray 更偏计算框架,可叠加 |
| Kubernetes + Kubeflow | K8s 原生 ML 平台 | dstack YAML 比 K8s YAML 简洁 10 倍,无需 K8s 背景 |
| AWS SageMaker | 云厂商托管 ML | dstack 厂商无关,不锁定 AWS;SageMaker 功能更全但更重 |
| Fluid | 分布式缓存编排 | Fluid 专注数据,dstack 专注 GPU 计算,互补 |
| SkyPilot | 多云任务调度 | SkyPilot 偏任务(Task),dstack 有完整的 Fleet/Service/Volume 全家桶 |
| RunPod / Modal | Serverless GPU | dstack 支持私有集群和自托管,比纯 Serverless 更可控 |
| KubeFlow | K8s ML 工具链 | dstack 定位更轻量,KubeFlow 功能更全但学习曲线陡 |
一句话推荐结论
如果你受够了在多个云控制台之间来回点、训练脚本写死某一朵云、或者每次换硬件都要重写一整套 Terraform——dstack 是目前门槛最低、厂商绑定最少的统一 GPU 编排方案;特别是需要让 AI Agent 自主调度 GPU 资源的团队,它和 Claude 等 Agent 的 Skill 集成是独家亮点,值得试用。