Use Cloud Native API Gateway to access ACS Agent Sandbox

更新时间:
复制 MD 格式

Use API gateway ingress as traffic entrance could manage ACS Ah=gent Sandbox in ACS clusters.

Agent Sandbox is an isolated runtime environment provided by ACS for AI Agents. You can create warm pools by using SandboxSet, obtain Sandbox instances by using SandboxClaim or E2B SDK, and access runtime services inside Sandbox instances through entry points exposed by ack-sandbox-manager.

Solution overview

ACS Agent Sandbox uses Ingress resources automatically created by ack-sandbox-manager to expose two types of entry points:

  • Sandbox management entry point — Exposes E2B API and management API through Ingress. This entry point supports lifecycle management operations such as creating, connecting, claiming, and releasing Sandbox instances.

  • Sandbox access entry point — Exposes Sandbox data plane API through Ingress. This entry point routes user requests to specific Sandbox instances for operations such as code execution and runtime service access.

This tutorial configures the className of the Ingress to apig, and Cloud-native API Gateway Ingress handles the Sandbox management and access traffic.

For complete instructions on Agent Sandbox component installation, SandboxSet warm pools, E2B SDK integration, and production domain certificate configuration, see Create an Agent Sandbox in an ACS cluster.

Installation

  1. Install APIG Controller.

    Log on to the ACS console, select the target cluster, click Component Management, search for apig controller, and then click Install. After the installation is complete, the system automatically creates a Cloud-native API Gateway to listen for Ingress resources in the cluster.

    For detailed instructions, see Manage the APIG Controller component.

  2. Install the ack-agent-sandbox-controller component.

    In the ACS console, go to the Component Management page of the target cluster, search for ack-agent-sandbox-controller, and then click Install.

You can keep the default parameter settings. If you require additional concurrency, increase the resource specifications accordingly.

For detailed information about this component, see ack-agent-sandbox-controller.

  1. Install the ack-sandbox-manager component.

    In the ACS console, go to the Component Management page of the target cluster, search for ack-sandbox-manager, and then click Install. This component creates a sandbox-manager Ingress in the sandbox-system namespace to expose the Sandbox management and access entry points.

Select HTTP mode or HTTPS mode based on your access requirements during installation. Keep the default values for other parameters such as resource specifications, concurrency, and logging.

HTTP mode is suitable for quick integration testing. No certificate preparation is required.

Parameter

Recommended value

Description

e2b.domain

The domain name used to access Cloud-native API Gateway

The actual domain name for accessing ACS Sandbox, such as your.domain.com. You can use a temporary domain name for demo purposes. Replace it with a real domain name in production environments.

ingress.className

apig

Specifies that Cloud-native API Gateway Ingress handles the Ingress resources automatically created by the component.

ingress.tls

false

Disables Ingress TLS.

Enable E2B_API_KEY authentication

Off for demo, enable as needed for production

When disabled, any API key is accepted. Use this setting only in temporary demo environments.

e2b.adminApiKey

Custom API key

The initial API key used when authentication is enabled, such as 1234567890987654321.

HTTPS mode is suitable for verifying TLS entry point capabilities. After you install the component, you must create a TLS Secret.

Parameter

Recommended value

Description

e2b.domain

The domain name used to access Cloud-native API Gateway

The actual domain name for accessing ACS Sandbox, such as your.domain.com. You can use a temporary domain name for demo purposes. Replace it with a real domain name in production environments.

ingress.className

apig

Specifies that Cloud-native API Gateway Ingress handles the Ingress resources automatically created by the component.

ingress.tls

true

Enables Ingress TLS.

TLS Secret

sandbox-system/sandbox-manager-tls

You typically do not need to set this parameter on the installation page. After TLS is enabled, create a certificate Secret with this name.

Enable E2B_API_KEY authentication

Off for demo, enable as needed for production

When disabled, any API key is accepted. Use this setting only in temporary demo environments.

e2b.adminApiKey

Custom API key

The initial API key used when authentication is enabled, such as 1234567890987654321.

Verify connectivity

After you complete the component installation and configuration, verify that the Sandbox management entry point is available through Cloud-native API Gateway.

Go to the APIG console, and obtain the APIG access endpoint from Overview > Access Point > Access Domain Names and IPs of the gateway instance:

APIG_ENDPOINT=<APIG access endpoint>

In test environments, you can access the endpoint by using the APIG access endpoint and the Host header:

curl -sS -i \
  -H "Host: your.domain.com" \
  "http://${APIG_ENDPOINT}/kruise/api/templates"

A 200 OK response indicates that you can access the ACS Sandbox management entry point through Cloud-native API Gateway. The examples in this tutorial use your.domain.com as the e2b.domain value specified during ack-sandbox-manager installation. Replace it with your actual domain name. If you have configured domain name resolution, you can also access the entry point directly by using the corresponding domain name.

Manage sandbox instances

Prepare a Sandbox template

Sandbox templates are provided by SandboxSet resources in the cluster. After you create a SandboxSet, ack-sandbox-manager watches this resource and synchronizes it as a template that can be queried and used through the management API. The following example creates a Sandbox template and warm pool named code-interpreter. You can deploy this template by using kubectl, the console YAML editor, or other methods.

apiVersion: agents.kruise.io/v1alpha1
kind: SandboxSet
metadata:
  name: code-interpreter
  namespace: default
spec:
  runtimes:
    - name: csi
    - name: agent-runtime
  replicas: 1
  template:
    metadata:
      labels:
        alibabacloud.com/acs: "true"
        alibabacloud.com/compute-class: agent-sandbox
        alibabacloud.com/compute-qos: default
    spec:
      automountServiceAccountToken: false
      containers:
        - name: sandbox
          image: registry-cn-hangzhou-vpc.ack.aliyuncs.com/acs/code-interpreter:v1.6
          imagePullPolicy: IfNotPresent
          resources:
            limits:
              cpu: "1"
              memory: 1Gi
            requests:
              cpu: "1"
              memory: 1Gi
              ephemeral-storage: 30Gi
      terminationGracePeriodSeconds: 30

Create a Sandbox

Call the ack-sandbox-manager management API through Cloud-native API Gateway to create a Sandbox. The following example uses the code-interpreter template created in the previous step and synchronized to ack-sandbox-manager.

Run the following command to view the available Sandbox templates:

curl -sS \
  -H "Host: your.domain.com" \
  "http://${APIG_ENDPOINT}/kruise/api/templates"

After the response contains code-interpreter, run the following command to create a Sandbox:

curl -sS \
  -H "Host: your.domain.com" \
  -H "content-type: application/json" \
  -d '{"templateID":"code-interpreter"}' \
  "http://${APIG_ENDPOINT}/kruise/api/sandboxes"

Record the sandboxID from the response. You will use this ID to access and delete the Sandbox later.

Access a Sandbox

Use the sandboxID returned when creating the Sandbox to access the Sandbox.

SANDBOX_ID=<sandboxID returned when creating the Sandbox>

Run the following command to access the health check endpoint in the Sandbox:

curl -sS -i \
  -H "Host: your.domain.com" \
  -H "e2b-sandbox-id: ${SANDBOX_ID}" \
  -H "e2b-sandbox-port: 49999" \
  "http://${APIG_ENDPOINT}/health"

Run the following command to execute code in the Sandbox:

curl -sS -i \
  -H "Host: your.domain.com" \
  -H "e2b-sandbox-id: ${SANDBOX_ID}" \
  -H "e2b-sandbox-port: 49999" \
  -H "content-type: application/json" \
  -d '{"code":"print(\"hello from apig\")"}' \
  "http://${APIG_ENDPOINT}/execute"

A 200 OK response that contains hello from apig indicates that the code was executed successfully.

Delete a Sandbox

If you no longer need a Sandbox, call the management API through Cloud-native API Gateway to delete it.

SANDBOX_ID=<sandboxID returned when creating the Sandbox>

curl -sS -i -X DELETE \
  -H "Host: your.domain.com" \
  "http://${APIG_ENDPOINT}/kruise/api/sandboxes/${SANDBOX_ID}"

A 204 No Content response indicates that the delete request has been accepted by the management API. After deletion, accessing the Sandbox typically returns 502 with API Error: healthy sandbox ... not found, indicating that the gateway can no longer route traffic to the Sandbox.

Deletion involves asynchronous cleanup, and Sandbox resources may still be releasing in the background for a short period. This does not affect the fact that the gateway has already stopped forwarding traffic to the Sandbox.

Bind with Certificate

If you choose HTTPS when installing ack-sandbox-manager (ingress.tls=true), a certificate to access ACS Sandbox domain is necessary. Please create TLS Secret in sandbox-system.

For more information about detailed configurations about domain and certificates, see Create an Agent Sandbox.

To create a self-signed certificate, run:

DEMO_DOMAIN=your.domain.com
TMP_DIR=$(mktemp -d)

openssl req -x509 -newkey rsa:2048 -sha256 -days 365 -nodes \
  -subj "/CN=${DEMO_DOMAIN}" \
  -addext "subjectAltName=DNS:${DEMO_DOMAIN},DNS:*.${DEMO_DOMAIN}" \
  -keyout "${TMP_DIR}/privkey.pem" \
  -out "${TMP_DIR}/fullchain.pem"

kubectl -n sandbox-system create secret tls sandbox-manager-tls \
  --cert="${TMP_DIR}/fullchain.pem" \
  --key="${TMP_DIR}/privkey.pem" \
  --dry-run=client -o yaml | kubectl apply -f -

Please use real certificates and sparse domain and wildcard subdomains to gateway entrance in productions.

In test environment, use APIG to verify HTTPS entrance:

curl -k -sS -i \
  --connect-to "your.domain.com:443:${APIG_ENDPOINT}:443" \
  "https://your.domain.com/kruise/api/templates"

Directly access the domain after configuration:

curl -k -sS -i "https://your.domain.com/kruise/api/templates"