ASM allows you to access external LLM services through an egress gateway. This approach is primarily for scenarios where the client application is located inside a cluster. This topic shows how to use an egress gateway to access an external service from an application that is running inside a cluster with an injected sidecar.
Prerequisites
-
An ACK cluster is added to an ASM instance of version 1.22 or later.
-
A sidecar injection policy is configured.
-
An ingress gateway is created.
-
The sleep sample application is deployed. For more information, see Create the sleep test Application.
-
You have activated Alibaba Cloud Model Studio and obtained a valid API key. For more information, see Get an API Key.
Overview
Without an egress gateway, client requests are intercepted by the sidecar proxy, which then sends the requests to the LLM provider. Because the sidecar and the client are deployed in the same Pod, this method poses several security risks, such as API key leaks and unauthorized access. If you are concerned about these risks, we strongly recommend using an egress gateway to route requests.
Using an ASM egress gateway improves security. Because the egress gateway is deployed independently of the client, you can use the Role-Based Access Control (RBAC) mechanism of your ACK cluster to restrict who can manage it, improving the gateway's overall security. The egress gateway handles all security operations, such as dynamically adding an API key and enforcing authentication and authorization policies. This design lets you use the full range of ASM gateway security features.
The following diagram shows the request path used in this topic:
Step 1: Create an egress gateway and Gateway resource
-
Create an egress gateway, expose port 80, and enable Support two-way TLS authentication. For details, see Create an Egress Gateway.
-
Create a file named
egress-gw.yamlwith the following content.apiVersion: networking.istio.io/v1beta1 kind: Gateway metadata: name: egress-gw namespace: istio-system spec: selector: istio: egressgateway servers: - hosts: - '*' port: name: http number: 80 protocol: HTTPS tls: mode: ISTIO_MUTUAL -
Use the kubeconfig file for your ASM instance to run the following command and create the Gateway resource.
kubectl apply -f egress-gw.yaml
Step 2: Create an LLMProvider for the egress gateway
Applying the LLMProvider only to the egress gateway ensures the API key is stored solely in the gateway's memory, making it inaccessible to the client.
-
Create a file named
dashscope-qwen.yamlwith the following content.apiVersion: istio.alibabacloud.com/v1beta1 kind: LLMProvider metadata: name: dashscope-qwen namespace: istio-system spec: workloadSelector: labels: istio: egressgateway host: dashscope.aliyuncs.com path: /compatible-mode/v1/chat/completions configs: defaultConfig: openAIConfig: model: qwen1.5-72b-chat # The Qwen series of open-source large models stream: false apiKey: ${API_KEY} -
Run the following command to create the LLMProvider.
kubectl apply -f dashscope-qwen.yaml
Step 3: Create an LLMRoute
-
Create a file named
dashscope-route.yamlwith the following content to route traffic to the egress gateway.apiVersion: istio.alibabacloud.com/v1beta1 kind: LLMRoute metadata: name: dashscope-route spec: host: dashscope.aliyuncs.com gateways: - mesh - istio-system/egress-gw rules: - name: mesh-route # When the sidecar receives a request for dashscope.aliyuncs.com, it forwards the request to the egress gateway. matches: - gateways: - mesh backendRefs: - providerHost: istio-egressgateway.istio-system.svc.cluster.local - name: egress-gw-route # When the egress gateway receives a request for dashscope.aliyuncs.com, it forwards the request to the actual provider. matches: - gateways: - istio-system/egress-gw backendRefs: - providerHost: dashscope.aliyuncs.com -
Run the following command to create the LLMRoute.
kubectl apply -f dashscope-route.yaml
Step 4: Test the configuration
Use the kubeconfig file for your ACK cluster to run the following command to test the setup.
kubectl exec deployment/sleep -it -- curl --location 'http://dashscope.aliyuncs.com' \
--header 'Content-Type: application/json' \
--data '{
"messages": [
{"role": "user", "content": "Introduce yourself"}
]
}'
Expected output:
{"choices":[{"message":{"role":"assistant","content":"Hello! I am Qwen, a pre-trained language model developed by Alibaba Cloud. My purpose is to assist users in generating various types of text, such as articles, stories, poems, and answering questions by leveraging my extensive knowledge and understanding of context. Although I'm an AI, I don't have a physical body or personal experiences like human beings do, but I've been trained on a vast corpus of text data, which allows me to engage in conversations, provide information, or help with various tasks to the best of my abilities. So, feel free to ask me anything, and I'll do my best to provide helpful and informative responses!"},"finish_reason":"stop","index":0,"logprobs":null}],"object":"chat.completion","usage":{"prompt_tokens":12,"completion_tokens":130,"total_tokens":142},"created":1720680044,"system_fingerprint":null,"model":"qwen1.5-72b-chat","id":"chatcmpl-3608dcd5-e3ad-9ade-bc70-xxxxxxxxxxxxxx"}
Step 5: Configure an authorization policy
-
Create a file named
authpolicy.yamlwith the following content.apiVersion: security.istio.io/v1beta1 kind: AuthorizationPolicy metadata: name: test namespace: istio-system spec: action: DENY rules: - from: - source: principals: - cluster.local/ns/default/sa/sleep to: - operation: hosts: - dashscope.aliyuncs.com selector: matchLabels: istio: egressgateway -
Use the kubeconfig file for your ASM instance to run the following command to apply the authorization policy.
kubectl apply -f authpolicy.yaml -
Run the command from Step 4 again to test the policy.
Expected output:
RBAC: access deniedThe output shows that the request is denied.
The security capabilities that an ASM gateway provides for standard HTTP requests also apply to LLM requests. These capabilities include a full range of authorization policies, JWT authentication, and custom authorization services. By applying these policies at the egress gateway, you can more effectively secure your applications.