ASM支援通過出口網關訪問外部LLM服務,主要適用於請求發起方位於叢集內的情境。本文介紹當請求方位於叢集內且注入了sidecar時,如何通過出口網關訪問叢集外服務。
前提條件
-
已添加叢集到ASM執行個體,且ASM執行個體版本為1.22及以上。
-
已建立入口網關。
-
已部署sleep樣本應用。具體操作,請參見建立測試應用sleep。
-
已經開通阿里雲百鍊,並且擷取了可用的API_KEY。具體操作,請參見擷取API Key
功能概述
未引入出口網關時,用戶端請求被Sidecar攔截,由Sidecar代理向LLM供應商發起請求。Sidecar和用戶端部署在同一個Pod中,這種使用方式存在諸多安全風險,可能出現API_KEY泄漏、未授權訪問等問題,如果您比較對這方面風險比較敏感的話,請務必使用出口網關路由模型。
在請求鏈路中引入ASM出口網關之後,由於出口網關的獨立於用戶端部署,您可以通過ACK叢集提供的RBAC機制方便的限制可以操作出口網關的使用者,進而提升出口網關的安全水位。此時,API_KEY動態添加、認證鑒權策略全部在出口網關上執行,您可以使用全量的ASM網關安全能力。
本文示範的請求鏈路如下所示:
步驟一:建立出口網關和網關規則
-
建立出口網關,配置開放80連接埠並開啟支援雙向TLS認證。具體操作,請參見建立出口網關。
-
使用以下內容,建立egress-gw.yaml。
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 -
使用ASM執行個體的kubeconfig,執行以下命令,建立網關規則。
kubectl apply -f egress-gw.yaml
步驟二:建立僅在出口網關生效的LLMProvider
僅在出口網關生效的LLMProvider可以確保API KEY只會存在於出口網關的記憶體中,不會輕易被用戶端擷取。
-
使用以下內容,建立dashscope-qwen.yaml。
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 # 千問開源系列大模型 stream: false apiKey: ${API_KEY} -
執行以下命令,建立LLMProvider。
kubectl apply -f dashscope-qwen.yaml
步驟三:建立LLMRoute
-
使用以下內容建立dashscope-route.yaml,將流量指向出口網關。
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 # sidecar收到dashscope.aliyuncs.com的請求之後,轉寄給出口網關 matches: - gateways: - mesh backendRefs: - providerHost: istio-egressgateway.istio-system.svc.cluster.local - name: egress-gw-route # 出口網關收到dashscope.aliyuncs.com的請求之後,轉寄給真正的provider matches: - gateways: - istio-system/egress-gw backendRefs: - providerHost: dashscope.aliyuncs.com -
執行以下命令,建立LLMRoute。
kubectl apply -f dashscope-route.yaml
步驟四:執行測試
使用ACK叢集kubeconfig,執行以下命令發起測試。
kubectl exec deployment/sleep -it -- curl --location 'http://dashscope.aliyuncs.com' \
--header 'Content-Type: application/json' \
--data '{
"messages": [
{"role": "user", "content": "請介紹你自己"}
]
}'
預期輸出:
{"choices":[{"message":{"role":"assistant","content":"我是來自阿里雲的大規模語言模型,我叫千問。我的主要功能是回答使用者的問題、提供資訊和進行對話交流。我可以理解使用者的提問,並基於自然語言產生相應的答案或建議。我也可以學習新的知識,並將其應用於各種情境中。如果您有任何問題或需要協助,請隨時告訴我,我會儘力為您提供支援。"},"finish_reason":"stop","index":0,"logprobs":null}],"object":"chat.completion","usage":{"prompt_tokens":3,"completion_tokens":72,"total_tokens":75},"created":1720680044,"system_fingerprint":null,"model":"qwen1.5-72b-chat","id":"chatcmpl-1c33b950-3220-9bfe-9066-xxxxxxxxxxxx"}
{"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"}%
步驟五:配置授權策略
-
使用以下內容,建立authpolicy.yaml。
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 -
使用ASM kubeconfig,執行以下命令配置授權策略。
kubectl apply -f authpolicy.yaml -
再次執行步驟四中的命令發起測試。
預期輸出:
RBAC: access denied可以看到請求被拒絕。
ASM網關針對普通HTTP請求所具備的安全能力同樣適用於LLM請求,包括完整的授權策略、JWT認證和自訂授權服務等。通過在出口網關上應用這些策略,可以更有效地保障您的應用安全。