This topic describes how to create an Agent Sandbox in an ACS cluster, including installing components, creating a warm pool, integrating an SDK, and configuring a domain name and certificate for a production environment.
Before you begin
-
If you have an existing cluster, you must upgrade the following components after activating the service.
-
Upgrade the
acs-virtual-nodecomponent to v2.17.0 or later. -
Upgrade the
Kube Schedulercomponent version.Cluster version
Kube Schedulercomponent versionv1.28
v1.28.12-aliyun-1.4.6 or later
v1.30
v1.30.3-aliyun-1.6.2 or later
v1.31
v1.31.0-aliyun-1.5.2 or later
v1.32
v1.32.0-apsara.6.11.11.3187ac8f or later
-
Install components
If the components are installed, upgrade the ack-agent-sandbox-controller component to v0.5.14-release.1 or later and the ack-sandbox-manager component to v0.6.0 or later.
-
Log on to the ACS console. In the left navigation pane, click Clusters.
-
On the Clusters page, click the name of the target cluster. In the left navigation pane, choose Add-ons.
-
Install the Ingress controller and sandbox-related components.
-
Install an Ingress controller.
Install an Ingress controller component supported by ACS to access the
sandbox-managerservice from outside the cluster. The following steps use Install the ALB Ingress Controller as an example to create a new public-facing ALB instance. -
Install the ack-agent-sandbox-controller component.
When you install this component for the first time, you must grant the AliyunCSManagedAgentSandboxRole role permissions to access your cloud resources. You can use the default parameter settings. If you require higher concurrency, you can increase the resource specifications.
For more information about the features of this component, see ack-agent-sandbox-controller.
-
Install the ack-sandbox-manager component.
-
Prepare an E2B domain name.
For more information about preparing a domain name, configuring DNS resolution, and applying for a certificate, see Use in a production environment.
-
Configure the component parameters.
Change
classNametoalb(the IngressClass that is automatically created when you install the Ingress controller component), changedomainto your actual domain name, and changeadminApiKeyto a custom API Key. Keep the other configurations at their default values. After the component is installed, an Ingress namedsandbox-manageris created in thesandbox-systemnamespace. -
If you use the ALB Ingress controller, you must also add an HTTPS:443 listener for both the ALB instance and the Ingress.
-
-
Create an Agent Sandbox
Step 1: Create a warm pool
A warm pool is a set of pre-created sandbox replicas managed by a SandboxSet custom resource. Applications can retrieve ready-to-use sandboxes directly from the warm pool, significantly improving delivery efficiency.
-
In the navigation pane on the left, choose Custom Resources. On the CRDs tab, click Create from YAML.
-
Create a SandboxSet resource by using the following YAML. The
ack-sandbox-managerautomatically detects this resource and initializes a sandbox template configuration named code-interpreter.To enable dynamic storage mounting, you must allow privileged containers and access to the hostPath (
/var/run/csi). You can submit a ticket to have the security restrictions lifted. However, you must assume responsibility for the associated security risks. For more information about this mechanism, see the shared responsibility model.apiVersion: agents.kruise.io/v1alpha1 kind: SandboxSet metadata: name: code-interpreter namespace: default spec: runtimes: - name: csi # Supports CSI mounting. The corresponding sidecar is injected into new sandboxes. - name: agent-runtime # Injects environment management tools such as envd. replicas: 4 template: metadata: labels: alibabacloud.com/acs: "true" alibabacloud.com/compute-class: agent-sandbox # Agent Sandbox instance type. alibabacloud.com/compute-qos: default # Compute QoS: default or best-effort. spec: automountServiceAccountToken: false containers: - image: registry-cn-zhangjiakou-vpc.ack.aliyuncs.com/acs/code-interpreter:v1.6 # Replace the region with the one where your cluster is deployed. imagePullPolicy: IfNotPresent name: sandbox resources: limits: cpu: "1" memory: 1Gi requests: cpu: "1" memory: 1Gi ephemeral-storage: 30Gi terminationGracePeriodSeconds: 30 -
Click Pods in the left navigation bar, select the corresponding namespace, and view the created
code-interpreterSandbox.You can view SandboxSet resources by running the
kubectl get sbscommand, whereAVAILABLEindicates the number of ready Sandboxes.
When a sandbox is allocated, the SandboxSet automatically triggers a replenishment process to maintain the number of instances in the warm pool.
Step 2: Obtain an Agent Sandbox
E2B SDK
-
Install Python in your local environment.
-
Install the E2B Python SDK (only versions earlier than v2.25.0 are supported).
pip install "e2b-code-interpreter==2.7.0" "e2b==2.24.0" -
Configure environment variables.
# Use the default domain name configured when installing the ack-sandbox-manager component. Do not include the asterisk (*). Modify this based on your actual configuration. export E2B_DOMAIN=your.domain.com # Use the default API key configured when installing the ack-sandbox-manager component. Modify this based on your actual configuration. export E2B_API_KEY=admin-987654321 -
Save the following code as the
main.pyfile.# Import the E2B SDK from e2b_code_interpreter import Sandbox sbx: Sandbox = Sandbox.create(template="code-interpreter") print(f"sandbox id: {sbx.sandbox_id}") result = sbx.run_code("print('hello, world')") print(f"run code result: {result}") text = input("enter some text to be saved to file 'text.txt' inside sandbox: ") sbx.files.write("text.txt", text) print(f"read file from sandbox via files api: [{sbx.files.read('text.txt')}]") print(f"read file from sandbox via commands api: [{sbx.commands.run('cat text.txt')}]") input("press ENTER to kill the sandbox") print(sbx.kill()) -
Run the
main.pyfile to create and verify the Sandbox.After the prompt appears for the first time, enter text such as
acs agent sandboxand press the ENTER key. This action writesacs agent sandboxto the/home/user/text.txtfile in the Pod namedcode-interpreter-29***. If you press the ENTER key again, the current Sandbox is deleted.python main.pyExpected output:
sandbox id: default--code-interpreter-29*** run code result: Execution(Results: [], Logs: Logs(stdout: ['hello, world\n'], stderr: []), Error: None) enter some text to be saved to file 'text.txt' inside sandbox: acs agent sandbox read file from sandbox via files api: [acs agent sandbox] read file from sandbox via commands api: [CommandResult(stderr='', stdout='acs agent sandbox', exit_code=0, error='')] press ENTER to kill the sandbox True
SandboxClaim
After a sandbox is allocated from the warm pool, the auto-replenishment process starts immediately to quickly backfill the pool.
-
In the navigation pane on the left, choose Custom Resources. On the CRDs tab, click Create from YAML.
-
Use the following YAML to create a SandboxClaim resource and obtain a sandbox.
apiVersion: agents.kruise.io/v1alpha1 kind: SandboxClaim metadata: name: code-interpreter # The name of the SandboxSet. namespace: default # The Namespace of the SandboxSet. spec: templateName: code-interpreter # The name of the SandboxSet. replicas: 1 # The number of sandboxes you want to obtain from the SandboxSet. claimTimeout: 5m # The timeout duration for the SandboxClaim. ttlAfterCompleted: 15m # The Time-to-Live (TTL) period after the SandboxClaim is completed. After the TTL period expires, the SandboxClaim resource is deleted. The obtained sandbox is not deleted. -
In the left navigation bar, click Pods, select the
defaultnamespace, and view the new Sandbox namedcode-interpreter-xxxxx.You can get the Sandbox status with the
kubectl get sbx -l agents.kruise.io/claim-name=code-interpretercommand.
You can also use the Kubernetes SDK to manage sandbox resources.
Replace the Agent Sandbox container image
E2B SDK
Refer to the following Python code. Replace <YOUR_IMAGE> with the actual image address. After you retrieve a Sandbox from the warm pool, use the metadata parameter to replace the container image with the specified image.
from e2b_code_interpreter import Sandbox
# The default timeout period is 300 seconds.
sbx = Sandbox.create(template="some-template", timeout=300, metadata={
# Perform an in-place update to replace the sandbox container's image with the specified image.
"e2b.agents.kruise.io/image": "<YOUR_IMAGE>"
})
SandboxClaim
Replace <YOUR_IMAGE> with the actual image address.
apiVersion: agents.kruise.io/v1alpha1
kind: SandboxClaim
metadata:
name: my-sandbox-claim
namespace: default
spec:
templateName: code-interpreter # The name of the SandboxSet warm pool.
replicas: 1
claimTimeout: 5m
ttlAfterCompleted: 15m
inplaceUpdate:
# The target image for the update.
image: <YOUR_IMAGE> # Replace cn-zhangjiakou with your actual region ID.
Agent Sandbox resource specifications
When you allocate a sandbox from a warm pool, in-place vertical scaling (VPA) is supported. This allows you to scale up low-specification sandbox instances to meet your business requirements without re-creating Pods. This approach combines the rapid provisioning of a warm pool with the flexibility to meet diverse resource demands.
-
Currently, you can only adjust the CPU. Memory and other resource settings are ignored, and only the main container is adjusted. For detailed limitations and usage instructions, see In-place vertical scaling for containers based on CPU metrics.
-
For clusters with a version earlier than 1.32, you must configure the
InPlacePodVerticalScaling=truefeature gate for the Kube API Server in Add-ons to enable in-place scaling.
Step 1: Enable in-place scaling
In the SandboxSet's spec.template.metadata.annotations, add scaling.alibabacloud.com/enable-inplace-resource-resize: "true" to allow sandboxes in the warm pool to perform in-place VPA when they are allocated.
The following example creates a warm pool with smaller resource specifications. You can dynamically scale up the resources to your desired specifications during allocation.
apiVersion: agents.kruise.io/v1alpha1
kind: SandboxSet
metadata:
name: code-interpreter
spec:
template:
metadata:
annotations:
# Allows in-place VPA when a sandbox is allocated from the warm pool.
scaling.alibabacloud.com/enable-inplace-resource-resize: "true"
spec:
containers:
- name: interpreter
image: xxx
resources:
requests:
cpu: 500m
memory: 1Gi
limits:
cpu: 500m
memory: 1Gi
replicas: 5 # The number of warm instances.
Step 2: Declare target resource specifications
When allocating an Agent Sandbox from a warm pool, you can use one of the following methods to dynamically scale up the CPU to the specified size:
E2B SDK
In the metadata of Sandbox.create, specify the target CPU specification by using a predefined key.
from e2b_code_interpreter import Sandbox
sbx = Sandbox.create(template="code-interpreter", metadata={
"e2b.agents.kruise.io/cpu-request": "1000m",
"e2b.agents.kruise.io/cpu-limit": "1000m"
})
SandboxClaim
Declare the target resource specifications in the spec.inplaceUpdate.resources field of the SandboxClaim.
apiVersion: agents.kruise.io/v1alpha1
kind: SandboxClaim
metadata:
name: code-interpreter-vpa-claim
namespace: default
spec:
templateName: code-interpreter
replicas: 1
claimTimeout: 5m
ttlAfterCompleted: 15m
inplaceUpdate: # Declare the target resource specifications.
resources:
requests:
cpu: 1000m
limits:
cpu: 1000m
Delete an Agent Sandbox
To permanently remove an instance, delete the sandbox by using one of the following methods.
E2B SDK
Delete a sandbox instance by using the E2B SDK: Replace <YOUR_SANDBOX_ID> with the actual sandbox ID.
from e2b_code_interpreter import Sandbox
sandbox = Sandbox.connect("<YOUR_SANDBOX_ID>")
sandbox.kill()
Sandbox CR
# Before you run the command, replace <NAMESPACE> with the Namespace of the resource and <RESOURCE_NAME> with the name of the CR.
kubectl -n <NAMESPACE> delete sandbox <RESOURCE_NAME>
Production environment
Prepare a domain name
You can refer to Add or delete an internal authoritative domain (Zone) to configure the domain names your.domain.com and *.your.domain.com and resolve them to the Ingress address. If access is entirely within the ACS cluster, you can also directly use the in-cluster Headless Service address: sandbox-manager.sandbox-system.svc.cluster.local.
Obtain a certificate
The E2B client can send requests to the backend over HTTPS. In a production environment, use one of the following methods to obtain a certificate.
Method 1: cert-manager
Method 2: Self-signed certificate
Method 3: Public certificate
DNS resolution
Run the following command to view the endpoint information:
kubectl get ingress sandbox-manager -o jsonpath='{range .status.loadBalancer.ingress[*]}{.hostname}{.ip}{"\n"}{end}' -n sandbox-system
Based on the provided endpoint information, configure DNS resolution for the domain your.domain.com or *.your.domain.com. For more information about DNS resolution, see Quick links.
-
If the output is an IP address (such as
47.114.***.***), map the host record*.your.domain.comto that IP address as an A record. -
If the output is a domain name, such as
alb-*****62roo70i*****.cn-hangzhou.alb.aliyuncsslb.com, resolve the host record*.your.domain.comto the domain name as a CNAME record. -
If multiple endpoints are returned, you can resolve the record to any one of them or configure round-robin DNS for all of them.
FAQ
ack-agent-sandbox-controller installation error: "[RAM Role AliyunCSManagedAgentSandboxRole is not granted]"
-
Go to the Resource Access Management (RAM) console and choose Identities > Roles. Search for AliyunCSManagedAgentSandboxRole and click the role name to open the details page.
-
On the Permissions tab, select theAliyunCSManagedAgentSandboxRolePolicy policy and click Remove Permission.
-
Click the authorization link to grant the permissions again.