Skip to content

Commit 9c18906

Browse files
authored
Add Kruise Sandbox deployer feature (#442)
1 parent db8c714 commit 9c18906

File tree

16 files changed

+3176
-6
lines changed

16 files changed

+3176
-6
lines changed

cookbook/en/advanced_deployment.md

Lines changed: 100 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ kernelspec:
1414

1515
# Advanced Deployment Guide
1616

17-
This guide covers the multiple advanced deployment methods available in AgentScope Runtime, providing production-ready solutions for different scenarios: **Local Daemon**, **Detached Process**, **Kubernetes Deployment**, **ModelStudio Deployment**, **AgentRun Deployment**, **PAI Deployment**, **Knative Deployment** and **Function Compute (FC) Deployment**.
17+
This guide covers the multiple advanced deployment methods available in AgentScope Runtime, providing production-ready solutions for different scenarios: **Local Daemon**, **Detached Process**, **Kubernetes Deployment**, **ModelStudio Deployment**, **AgentRun Deployment**, **PAI Deployment**, **Knative Deployment**, **Kruise Deployment** and **Function Compute (FC) Deployment**.
1818

1919
## Overview of Deployment Methods
2020

@@ -29,6 +29,7 @@ AgentScope Runtime offers multiple distinct deployment approaches, each tailored
2929
| **AgentRun** | AgentRun Platform | Cloud-managed | Platform-managed | Container-level |
3030
| **PAI** | Alibaba Cloud PAI Platform | Cloud-managed | Platform-managed | Container-level |
3131
| **Knative** | Enterprise & Cloud | Single-node (multi-node support coming) | Orchestrated | Container-level |
32+
| **Kruise** | Enterprise & Cloud | Single-node | Orchestrated | Container-level / MicroVM-level |
3233
| **Function Compute (FC)** | Alibaba Cloud Serverless | Cloud-managed | Platform-managed | MicroVM-level |
3334

3435
### Deployment Modes (DeploymentMode)
@@ -1222,7 +1223,104 @@ if __name__ == "__main__":
12221223
- Provides automatic scaling from zero to thousands of instances, intelligent traffic routing
12231224
- Resource limits and health checks configured
12241225
1225-
## Method 8: Serverless Deployment: Function Compute (FC)
1226+
## Method 8: Kruise Deployment
1227+
1228+
**Best For**: Scenarios requiring instance-level isolation, pause/resume capabilities, and secure multi-tenant runtime environments.
1229+
1230+
### Features
1231+
- Custom resource deployment based on Kruise Sandbox CRD (`agents.kruise.io/v1alpha1`)
1232+
- Instance-level isolation, ensuring secure runtime environments across different agents
1233+
- Supports pausing and resuming, effectively saving resource consumption
1234+
- Automatically creates LoadBalancer Service for external access
1235+
- Deployment state persistence management
1236+
1237+
### Kruise Deployment Prerequisites
1238+
1239+
```bash
1240+
# Ensure Docker is running
1241+
docker --version
1242+
1243+
# Verify Kubernetes access
1244+
kubectl cluster-info
1245+
1246+
# Check registry access (e.g., Alibaba Cloud)
1247+
docker login your-registry
1248+
1249+
# Check Kruise Sandbox is installed
1250+
# Installation guide: https://github.com/openkruise/agents
1251+
kubectl get crd sandboxes.agents.kruise.io
1252+
```
1253+
1254+
### Implementation
1255+
1256+
Using the agent and endpoints defined in the {ref}`Common Agent Setup <en-common-agent-setup>` section:
1257+
1258+
```{code-cell}
1259+
# kruise_deploy.py
1260+
import asyncio
1261+
import os
1262+
from agentscope_runtime.engine.deployers.kruise_deployer import (
1263+
KruiseDeployManager,
1264+
K8sConfig,
1265+
)
1266+
from agentscope_runtime.engine.deployers.utils.docker_image_utils import (
1267+
RegistryConfig,
1268+
)
1269+
from agent_app import app # Import the configured app
1270+
1271+
async def deploy_to_kruise():
1272+
"""Deploy AgentApp to Kruise Sandbox"""
1273+
1274+
# Configure registry and K8s connection
1275+
deployer = KruiseDeployManager(
1276+
kube_config=K8sConfig(
1277+
k8s_namespace="agentscope-runtime",
1278+
kubeconfig_path=None,
1279+
),
1280+
registry_config=RegistryConfig(
1281+
registry_url="your-registry-url",
1282+
namespace="agentscope-runtime",
1283+
),
1284+
)
1285+
1286+
# Execute deployment
1287+
result = await app.deploy(
1288+
deployer,
1289+
port="8090",
1290+
image_name="agent_app",
1291+
image_tag="v1.0",
1292+
requirements=["agentscope", "fastapi", "uvicorn"],
1293+
base_image="python:3.10-slim-bookworm",
1294+
environment={
1295+
"PYTHONPATH": "/app",
1296+
"DASHSCOPE_API_KEY": os.environ.get("DASHSCOPE_API_KEY"),
1297+
},
1298+
labels={
1299+
"app": "agent-kruise",
1300+
},
1301+
runtime_config={
1302+
"resources": {
1303+
"requests": {"cpu": "200m", "memory": "512Mi"},
1304+
"limits": {"cpu": "1000m", "memory": "2Gi"},
1305+
},
1306+
},
1307+
platform="linux/amd64",
1308+
push_to_registry=True,
1309+
)
1310+
1311+
print(f"Deployment successful: {result['url']}")
1312+
return result, deployer
1313+
1314+
if __name__ == "__main__":
1315+
asyncio.run(deploy_to_kruise())
1316+
```
1317+
1318+
**Key Points**:
1319+
- Isolated deployment based on Kruise Sandbox CRD, each agent runs in an independent environment
1320+
- Automatically creates LoadBalancer Service, supports automatic switching between local and cloud environments
1321+
- Deployment state is automatically persisted, supports lifecycle management via CLI
1322+
1323+
## Method 9: Serverless Deployment: Function Compute (FC)
12261324
12271325
**Best For**: Alibaba Cloud users who need to deploy agents to Function Compute (FC) service with automated build, upload, and deployment workflows. FC provides a true serverless experience with pay-per-use pricing and automatic scaling.
12281326

cookbook/en/cli.md

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,8 @@ agentscope deploy PLATFORM SOURCE [OPTIONS]
402402
- `modelstudio`: Alibaba Cloud ModelStudio
403403
- `agentrun`: Alibaba Cloud AgentRun
404404
- `k8s`: Kubernetes/ACK
405+
- `knative`: Knative/ACK Knative
406+
- `kruise`: Kruise Sandbox
405407

406408
#### Common Options (All Platforms)
407409

@@ -645,6 +647,73 @@ agentscope deploy knative app_agent.py \
645647

646648
**Note:** `USE_LOCAL_RUNTIME=True` uses local agentscope runtime instead of PyPI version.
647649

650+
#### 4.5. Kruise Deployment
651+
652+
Deploy to Kruise Sandbox custom resource (`agents.kruise.io/v1alpha1`) on a Kubernetes cluster.
653+
654+
Key differences from standard K8s Deployment and Knative:
655+
- **Instance-level isolation**: Ensures secure environment isolation across different agents
656+
- **Pause/Resume**: Supports pausing and resuming, effectively saving resource consumption
657+
- **Auto-created LoadBalancer Service**: Automatically creates load balancer service for external access
658+
659+
##### Command Syntax
660+
661+
```bash
662+
agentscope deploy kruise SOURCE [OPTIONS]
663+
```
664+
665+
##### Platform-Specific Options
666+
667+
| Option | Type | Default | Description |
668+
|--------|------|---------|-------------|
669+
| `--namespace` | string | `"agentscope-runtime"` | Kubernetes namespace |
670+
| `--kube-config-path` | path | `None` | Path to kubeconfig file |
671+
| `--port` | integer | `8080` | Container port |
672+
| `--image-name` | string | `"agent_app"` | Docker image name |
673+
| `--image-tag` | string | `"linux-amd64"` | Docker image tag |
674+
| `--registry-url` | string | `"localhost"` | Remote registry URL |
675+
| `--registry-namespace` | string | `"agentscope-runtime"` | Remote registry namespace |
676+
| `--push` | flag | `False` | Push image to registry |
677+
| `--base-image` | string | `"python:3.10-slim-bookworm"` | Base Docker image |
678+
| `--requirements` | string | `None` | Python requirements (comma-separated or file path) |
679+
| `--cpu-request` | string | `"200m"` | CPU resource request (e.g., '200m', '1') |
680+
| `--cpu-limit` | string | `"1000m"` | CPU resource limit (e.g., '1000m', '2') |
681+
| `--memory-request` | string | `"512Mi"` | Memory resource request (e.g., '512Mi', '1Gi') |
682+
| `--memory-limit` | string | `"2Gi"` | Memory resource limit (e.g., '2Gi', '4Gi') |
683+
| `--image-pull-policy` | choice | `"IfNotPresent"` | Image pull policy: `Always`, `IfNotPresent`, `Never` |
684+
| `--deploy-timeout` | integer | `300` | Deployment timeout in seconds |
685+
| `--platform` | string | `"linux/amd64"` | Target platform (e.g., 'linux/amd64', 'linux/arm64') |
686+
| `--pypi-mirror` | string | `None` | PyPI mirror URL for pip package installation (e.g., 'https://pypi.tuna.tsinghua.edu.cn/simple'). If not specified, uses pip default |
687+
688+
##### Prerequisites
689+
690+
- Kubernetes cluster access
691+
- Kruise Sandbox CRD (`agents.kruise.io/v1alpha1`) installed, see [Kruise Agents](https://github.com/openkruise/agents)
692+
- Docker installed (for building images)
693+
- `kubectl` configured
694+
695+
##### Examples
696+
697+
```bash
698+
# Basic deployment
699+
export USE_LOCAL_RUNTIME=True
700+
agentscope deploy kruise app_agent.py \
701+
--image-name agent_app \
702+
--env DASHSCOPE_API_KEY=sk-xxx \
703+
--image-tag linux-amd64-1 \
704+
--registry-url your-registry.com \
705+
--push
706+
707+
# Custom namespace and resources
708+
agentscope deploy kruise app_agent.py \
709+
--namespace production \
710+
--cpu-limit 2 \
711+
--memory-limit 4Gi \
712+
--env DASHSCOPE_API_KEY=sk-xxx
713+
```
714+
715+
**Note:** `USE_LOCAL_RUNTIME=True` uses local agentscope runtime instead of PyPI version.
716+
648717
### 5. Deployment Management
649718

650719
#### 5.1. List Deployments

cookbook/zh/advanced_deployment.md

Lines changed: 101 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ kernelspec:
1414

1515
# 高级部署
1616

17-
章节演示了AgentScope Runtime中可用的八种高级部署方法,为不同场景提供生产就绪的解决方案:**本地守护进程****独立进程****Kubernetes部署****ModelStudio部署****AgentRun部署****PAI部署****Knative****函数计算(Function Compute, FC)部署**
17+
章节演示了AgentScope Runtime中可用的九种高级部署方法,为不同场景提供生产就绪的解决方案:**本地守护进程****独立进程****Kubernetes部署****ModelStudio部署****AgentRun部署****PAI部署****Knative****Kruise部署****函数计算(Function Compute, FC)部署**
1818

1919
## 部署方法概述
2020

@@ -28,7 +28,8 @@ AgentScope Runtime提供多种不同的部署方式,每种都针对特定的
2828
| **ModelStudio** | 百炼应用开发平台 | 云端管理 | 平台管理 | 容器级 |
2929
| **AgentRun** | AgentRun平台 | 云端管理 | 平台管理 | 容器级 |
3030
| **PAI** | 阿里云PAI平台 | 云端管理 | 平台管理 | 容器级 |
31-
| **Knative** | 企业与云端 | 单节点(将支持多节点) | 编排 | 容器级 |
31+
| **Knative** | 企业与云端 | 单节点(未来支持多节点) | 编排 | 容器级 |
32+
| **Kruise** | 企业与云端 | 单节点 | 编排 | 容器级/微虚拟机级 |
3233
| **函数计算(FC)** | 阿里云 Serverless | 云端管理 | 平台管理 | 微虚拟机级 |
3334

3435

@@ -1219,7 +1220,104 @@ if __name__ == "__main__":
12191220
- 支持基于请求自动弹性、缩容至 0
12201221
- 配置资源限制和健康检查
12211222

1222-
## 方法8:Serverless部署:函数计算(Function Compute, FC)
1223+
## 方法8:Kruise部署
1224+
1225+
**最适合**:需要实例级隔离、暂停恢复能力和安全多租户运行环境的场景。
1226+
1227+
### 特性
1228+
- 基于 Kruise Sandbox CRD(`agents.kruise.io/v1alpha1`)的自定义资源部署
1229+
- 实例级隔离,确保不同 agent 运行环境安全
1230+
- 支持暂停和恢复,有效节省资源消耗
1231+
- 自动创建 LoadBalancer Service 提供外部访问
1232+
- 部署状态持久化管理
1233+
1234+
### Kruise 部署前置条件
1235+
1236+
```bash
1237+
# 确保Docker正在运行
1238+
docker --version
1239+
1240+
# 验证Kubernetes访问
1241+
kubectl cluster-info
1242+
1243+
# 检查镜像仓库访问(以阿里云为例)
1244+
docker login your-registry
1245+
1246+
# 检查 Kruise Sandbox CRD 已安装
1247+
# 安装指南:https://github.com/openkruise/agents
1248+
kubectl get crd sandboxes.agents.kruise.io
1249+
```
1250+
1251+
### 实现
1252+
1253+
使用 {ref}`通用智能体配置<zh-common-agent-setup>` 部分定义的智能体和端点:
1254+
1255+
```{code-cell}
1256+
# kruise_deploy.py
1257+
import asyncio
1258+
import os
1259+
from agentscope_runtime.engine.deployers.kruise_deployer import (
1260+
KruiseDeployManager,
1261+
K8sConfig,
1262+
)
1263+
from agentscope_runtime.engine.deployers.utils.docker_image_utils import (
1264+
RegistryConfig,
1265+
)
1266+
from agent_app import app # 导入已配置的 app
1267+
1268+
async def deploy_to_kruise():
1269+
"""将 AgentApp 部署到 Kruise Sandbox"""
1270+
1271+
# 配置镜像仓库和 K8s 连接
1272+
deployer = KruiseDeployManager(
1273+
kube_config=K8sConfig(
1274+
k8s_namespace="agentscope-runtime",
1275+
kubeconfig_path=None,
1276+
),
1277+
registry_config=RegistryConfig(
1278+
registry_url="your-registry-url",
1279+
namespace="agentscope-runtime",
1280+
),
1281+
)
1282+
1283+
# 执行部署
1284+
result = await app.deploy(
1285+
deployer,
1286+
port="8090",
1287+
image_name="agent_app",
1288+
image_tag="v1.0",
1289+
requirements=["agentscope", "fastapi", "uvicorn"],
1290+
base_image="python:3.10-slim-bookworm",
1291+
environment={
1292+
"PYTHONPATH": "/app",
1293+
"DASHSCOPE_API_KEY": os.environ.get("DASHSCOPE_API_KEY"),
1294+
},
1295+
labels={
1296+
"app": "agent-kruise",
1297+
},
1298+
runtime_config={
1299+
"resources": {
1300+
"requests": {"cpu": "200m", "memory": "512Mi"},
1301+
"limits": {"cpu": "1000m", "memory": "2Gi"},
1302+
},
1303+
},
1304+
platform="linux/amd64",
1305+
push_to_registry=True,
1306+
)
1307+
1308+
print(f"✅ 部署成功:{result['url']}")
1309+
return result, deployer
1310+
1311+
if __name__ == "__main__":
1312+
asyncio.run(deploy_to_kruise())
1313+
```
1314+
1315+
**关键点**:
1316+
- 基于 Kruise Sandbox CRD 的隔离部署,每个 agent 独立运行环境
1317+
- 自动创建 LoadBalancer Service,支持本地和云端环境自动切换
1318+
- 部署状态自动持久化,支持通过 CLI 管理生命周期
1319+
1320+
## 方法9:Serverless部署:函数计算(Function Compute, FC)
12231321

12241322
**最适合**:阿里云用户,需要将智能体部署到函数计算(FC)服务,实现自动化的构建、上传和部署流程。FC 提供真正的 Serverless 体验,按量付费并自动扩缩容。
12251323

0 commit comments

Comments
 (0)