對於接收的請求,HTTP規則根據請求URI選擇處理。虛擬服務支援URI路徑的精確匹配,首碼匹配與正則匹配規則。本文介紹如何配置URI路徑匹配。
前提條件
已完成準備工作,並部署helloworld服務和sleep服務。具體操作,請參見準備工作。
操作步驟
驗證服務配置。
在ACK叢集對應的kubeconfig環境下,執行以下命令,進入sleep環境開啟bash。
kubectl exec -it deploy/sleep -- sh執行以下命令, 向helloworld服務發送請求。
curl helloworld:5000/hello預期輸出為以下兩者之一,helloworld-v1和helloworld-v2隨機響應,表明當前請求被發送給了helloworld服務。
Hello version: v2, instance: helloworld-v2-7d48f6b995-6**** Hello version: v1, instance: helloworld-v1-6986f64596-s****
使用以下內容,建立虛擬服務,添加URI Path Matching配置。具體操作,請參見管理虛擬服務。
apiVersion: networking.istio.io/v1beta1 kind: VirtualService metadata: name: helloworld-vs namespace: default spec: hosts: - helloworld http: - match: - uri: exact: /hello route: - destination: host: helloworld-v1驗證配置是否生效。
在ACK叢集對應的kubeconfig環境下,執行以下命令,進入sleep環境開啟bash。
kubectl exec -it deploy/sleep -- sh執行以下命令, 向helloworld服務的
/hello路徑發送請求。curl helloworld:5000/hello預期輸出始終為以下結果,由helloworld-v1響應。
Hello version: v1, instance: helloworld-v1-6986f64596-s****