All Products
Search
Document Center

Container Compute Service:Create an Agent Sandbox in an ACS cluster

Last Updated:Jul 10, 2026

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

  1. Create an ACS cluster and activate Agent Sandbox.

  2. If you have an existing cluster, you must upgrade the following components after activating the service.

    1. Upgrade the acs-virtual-node component to v2.17.0 or later.

    2. Upgrade the Kube Scheduler component version.

      Cluster version

      Kube Scheduler component version

      v1.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

Note

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.

  1. Log on to the ACS console. In the left navigation pane, click Clusters.

  2. On the Clusters page, click the name of the target cluster. In the left navigation pane, choose Add-ons.

  3. Install the Ingress controller and sandbox-related components.

    1. Install an Ingress controller.

      Install an Ingress controller component supported by ACS to access the sandbox-manager service from outside the cluster. The following steps use Install the ALB Ingress Controller as an example to create a new public-facing ALB instance.

    2. 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.

    3. Install the ack-sandbox-manager component.

      1. 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.

      2. Configure the component parameters.

        Change className to alb (the IngressClass that is automatically created when you install the Ingress controller component), change domain to your actual domain name, and change adminApiKey to a custom API Key. Keep the other configurations at their default values. After the component is installed, an Ingress named sandbox-manager is created in the sandbox-system namespace.

        Parameter details

        Parameter

        Parameter

        Description

        sandboxManager

        replicaCount

        Number ofsandbox-manager instances. Default: 3.

        E2B

        domain

        The E2B domain name. For more information, see Prepare a domain name.

        Enable E2B_API_KEY verification

        Specifies whether to enable API_KEY authentication. This feature is enabled by default.

        adminApiKey

        If authentication is enabled, this parameter sets the initial key for the first installation. Replace the default value with your custom API key.

        Controller

        logLevel

        The log level of the controller. Default: 1.

        resources.requests.cpu

        The CPU resource request for the controller. Default: 2.

        resources.requests.memory

        The memory resource request for the controller. Default: 4 Gi.

        Proxy

        resources.requests.cpu

        The CPU resource request for the proxy. Default: 2.

        resources.requests.memory

        The memory resource request for the proxy. Default: 4 Gi.

        Ingress

        className

        The IngressClass name configured in the cluster, such as alb, mse.

      3. If you use the ALB Ingress controller, you must also add an HTTPS:443 listener for both the ALB instance and the Ingress.

        Update the AlbConfig to add anHTTPS:443 listener for the ALB instance.

        1. In the navigation pane on the left, choose Workloads > Custom Resources. On the Resource Objects tab, search for and click AlbConfig.

        2. In the list of AlbConfig resource objects, find the target resource alb, and click Edit YAML in the Actions column to its right.

        3. Add the spec.listeners.port: 443 and spec.listeners.protocol: HTTPS fields, then click OK.

          spec:
            config:
              addressAllocatedMode: Fixed
              addressType: Internet
              zoneMappings:
                - vSwitchId: vsw-xxx
                - vSwitchId: vsw-xxx
            listeners:
              - port: 80
                protocol: HTTP
              - port: 443
                protocol: HTTPS

        Update the Ingress to associate the HTTPS:443 listener.

        1. In the left navigation bar, select Network > Ingresses. In the Actions column for the sandbox-manager ingress, click Update.

        2. Add the following configuration, and click OK.

          • Annotations: alb.ingress.kubernetes.io/listen-ports: [{"HTTP": 80}, {"HTTPS": 443}]

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.
  1. In the navigation pane on the left, choose Custom Resources. On the CRDs tab, click Create from YAML.

  2. Create a SandboxSet resource by using the following YAML. The ack-sandbox-manager automatically 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

    Using the code-interpreter image

    • Image pull: The registry-cn-zhangjiakou-vpc.ack.aliyuncs.com/acs/code-interpreter image is a sample image that ACS created based on the e2b-code-interpreter project. It is compatible with the run_code interface of the E2B client. To improve the image pull speed, you can change the region in the image address to your actual region or remove -vpc to pull the image over the public network.

      Important

      This image is for demonstration and testing purposes only and is not recommended for production environments. If you need to deploy it in a production environment, perform the necessary security hardening, performance optimization, and feature enhancements to meet your security and stability requirements.

    • Use a custom image: If you want to use the E2B code-interpreter image, either directly or as a base image, use a versioned image provided by ACS. ACS does not guarantee runtime compatibility with the officiallatest image from E2B. If you need to use a custom image, ensure it meets the following conditions:

      1. Contains basic commands such ascp,mv, andmkdir.

      2. Includes bash, and the executable file is located at /bin/bash.

      When you use custom images, the run_code method of E2B is temporarily unavailable.

  3. Click Pods in the left navigation bar, select the corresponding namespace, and view the created code-interpreter Sandbox.

    You can view SandboxSet resources by running the kubectl get sbs command, where AVAILABLE indicates 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

  1. Install Python in your local environment.

  2. 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"
  3. 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
  4. Save the following code as the main.py file.

    # 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())
                                

    create API: metadata parameter details

    For information about how to use the metadata parameter of the create API, see Usage Example.

    Key

    Description

    Value

    e2b.agents.kruise.io/never-timeout

    Specifies whether to automatically clean up the Sandbox. If you set this parameter to true, the Sandbox is not automatically cleaned up based on the timeout period.

    • false

    • true

    e2b.agents.kruise.io/image

    Specifies a runtime image for the requested sandbox. If the specified image is different from the one declared in the warm pool, an in-place image update is automatically performed.

    A specific image name, such as ghcr.io/openclaw/openclaw:****

    e2b.agents.kruise.io/wait-ready-timeout-seconds

    Specifies the maximum time to wait for an in-place image update to complete.

    An integer. Default: 60.

    e2b.agents.kruise.io/csi-volume-config

    Dynamically mounts NAS or OSS volumes when you request a sandbox. Specify the configuration as a JSON string that supports multiple mount points. Each mount point supports the following parameters:

    1. pvName: The name of the PersistentVolume for the OSS or NAS volume.

    2. mountPath: The mount directory inside the sandbox.

    3. subPath: The bucket path in OSS or NAS, specified as an absolute path.

    volume_config = [

    {"pvName": "oss-pv-sandbox-system", "mountPath": "/home/data-oss1", "subPath": "data-subPath1","readOnly": true}]

    e2b.agents.kruise.io/claim-timeout-seconds

    The maximum time to wait for an available sandbox. If the time limit is exceeded, the request fails.

    An integer. Default: 60.

    e2b.agents.kruise.io/create-on-no-stock

    By default, sandboxes are always retrieved from the warm pool. If no sandboxes are available, the system waits for them to be replenished and become ready. If you set this parameter to true, the system creates a sandbox directly from the warm pool template to reduce waiting time.

    • false

    • true

    Default: false

    e2b.agents.kruise.io/skip-init-runtime

    Specifies whether to initialize the Agent-runtime in the Sandbox instance when you request the instance. You can set this to false if no runtime process exists or if a high degree of customization is required.

    • false

    • true

    Default: true

    e2b.agents.kruise.io/reserve-failed-sandbox

    If an error occurs when obtaining a sandbox, the management component immediately cleans up the abnormal sandbox. Set this to true to retain the failed sandbox instance for troubleshooting.

    • false

    • true

    Default: false

    Custom metadata

    When requesting a sandbox, you can add annotations to the instance with custom key-value pairs.

    Specify a custom value, such as {"userId": "alice"}.

  5. Run the main.py file to create and verify the Sandbox.

    After the prompt appears for the first time, enter text such as acs agent sandbox and press the ENTER key. This action writes acs agent sandbox to the /home/user/text.txt file in the Pod named code-interpreter-29***. If you press the ENTER key again, the current Sandbox is deleted.
    python main.py

    Expected 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.

  1. In the navigation pane on the left, choose Custom Resources. On the CRDs tab, click Create from YAML.

  2. 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.
  3. In the left navigation bar, click Pods, select the default namespace, and view the new Sandbox named code-interpreter-xxxxx.

    You can get the Sandbox status with the kubectl get sbx -l agents.kruise.io/claim-name=code-interpreter command.
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.

Note
  • 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=true feature 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

The following steps show how to use cert-manager to manage and deploy a self-signed certificate for sandbox-manager. Ensure you have kubectl installed and the permissions to use the cert-manager API.

Step 1: Install cert-manager

Refer to the official documentation to install cert-manager v1.14.

We recommend using v1.14 because newer versions use the community image registry (quay.io).
Step 2: Manage certificates with cert-manager
  1. Replace *.your.domain.com and your.domain.com in cert-manager.yaml with your domain name.

    The example YAML uses a self-signed CA certificate. You can also use your own CA certificate.
  2. Run the kubectl apply -f cert-manager.yaml command to add the configuration to the ACS cluster.

Step 3: Verify the certificate status
  1. Verify that the certificate was created and issued correctly.

    kubectl get certificates -n sandbox-system
    kubectl describe certificate sandbox-manager-ingress-cert -n sandbox-system
    kubectl describe secret sandbox-manager-tls -n sandbox-system
  2. Check the Ingress status.

    kubectl get ingress sandbox-manager -n sandbox-system
    kubectl describe ingress sandbox-manager -n sandbox-system
Step 4: Configure client trust

If you are using a self-signed certificate, the client needs to trust the root CA certificate.

  1. Obtain the CA certificate.

    kubectl get secret sandbox-ca-key-pair -n sandbox-system -o jsonpath='{.data.tls\.crt}' | base64 -d > ca.crt
  2. Configure the client. Set the environment variable SSL_CERT_FILE to the path of the CA certificate or add the CA certificate to the system's trust store.

    export SSL_CERT_FILE=/path/to/ca.crt

Method 2: Self-signed certificate

Step 1: Create the certificate
  1. Create a self-signed certificate by using the generate-certificates.sh script. You can run the following command to view how to use the script.

    bash generate-certificates.sh --help

    Expected output:

    Usage: generate-certificates.sh [OPTIONS]
    Options:
      -d, --domain DOMAIN     Specify certificate domain (default: example.com)
      -o, --output DIR        Specify output directory (default: .)
      -D, --days DAYS         Specify certificate validity days (default: 365)
      -h, --help              Show this help message
    Examples:
      generate-certificates.sh -d myapp.example.com
      generate-certificates.sh --domain api.example.com --days 730
  2. Run the generate-certificates.sh -d your.domain.com command to generate certificates. After the certificates are generated, the following files are created:

    • fullchain.pem: Server certificate public key

    • privkey.pem: Server certificate private key

    • ca-fullchain.pem: CA certificate public key

    • ca-privkey.pem: CA certificate private key

    This script generates single-domain (your.domain.com) and wildcard-domain (*.your.domain.com) certificates that are compatible with the native E2B protocol and the OpenKruise-customized E2B protocol.

Step 2: Install the certificate

Run the following command to create a TLS Secret for the Ingress from the server certificate:

kubectl create secret tls sandbox-manager-tls \
        --cert=fullchain.pem \
        --key=privkey.pem -n sandbox-system
There may be a delay before the certificate takes effect. The exact timing depends on the Ingress controller you are using.
Step 3: Configure client trust

The client needs to set the SSL_CERT_FILE environment variable to the file path of the CA public key (ca-fullchain.pem) that was generated in Step 1.

export SSL_CERT_FILE=/path/to/ca-fullchain.pem
python main.py # Call the E2B SDK by using Python.

Method 3: Public certificate

Step 1: Prepare the certificate

The E2B client must use HTTPS to send requests to the backend. In a production environment, we recommend that you obtain a public domain certificate.

The following example uses Let's Encrypt to obtain a free test certificate (Let's Encrypt certificates require a public domain name).

  1. Install certbot by using your system's package manager (such as brew or snap). For more installation information, see the official documentation.

  2. Modify the -d and --email parameters to apply for a certificate for the wildcard domain your.domain.com. Follow the prompts from the command to complete the verification process.

Because the issued certificate includes both a wildcard domain and a single domain, you must perform the TXT record authentication process twice in a row.
sudo certbot certonly \
  --manual \
  --preferred-challenges=dns \
  --email your-email@example.com \
  --server https://acme-v02.api.letsencrypt.org/directory \
  --agree-tos \
  -d "your.domain.com" \
  -d "*.your.domain.com"
Step 2: Export the certificate
sudo cp /etc/letsencrypt/live/your.domain.com/fullchain.pem ./fullchain.pem
sudo cp /etc/letsencrypt/live/your.domain.com/privkey.pem ./privkey.pem
Step 3: Install the certificate
kubectl create secret tls sandbox-manager-tls \
  --cert=fullchain.pem \
  --key=privkey.pem \
  --namespace=sandbox-system

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.com to 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.com to 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]"

  1. 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.

  2. On the Permissions tab, select theAliyunCSManagedAgentSandboxRolePolicy policy and click Remove Permission.

  3. Click the authorization link to grant the permissions again.