使用 WebAssembly 外掛程式擴展航點
Istio 提供使用 WebAssembly (Wasm) 擴展其功能的能力。Wasm 擴展性的主要優勢之一是可以在執行階段動態載入擴充功能。本文概述如何在 Istio 內使用 Wasm 功能擴展 Ambient 模式。在 Ambient 模式中,Wasm 設定必須套用至部署在每個命名空間中的航點代理。
開始之前
在閘道
透過 Kubernetes Gateway API,Istio 提供了一個集中式的入口點,用於管理進出服務網格的流量。我們將在閘道層級設定一個 WasmPlugin,確保所有通過閘道的流量都受到擴展的驗證規則約束。
為閘道設定 WebAssembly 外掛程式
在這個範例中,您將新增一個 HTTP 基本身份驗證模組到您的網格中。您將配置 Istio 從遠端映像註冊表拉取基本身份驗證模組並載入它。它將被配置為在呼叫 /productpage
時執行。這些步驟與分發 WebAssembly 模組中的步驟類似,不同之處在於使用 targetRefs
欄位而不是標籤選擇器。
若要使用遠端 Wasm 模組設定 WebAssembly 篩選器,請建立一個目標為 bookinfo-gateway
的 WasmPlugin
資源。
$ kubectl get gateway
NAME CLASS ADDRESS PROGRAMMED AGE
bookinfo-gateway istio bookinfo-gateway-istio.default.svc.cluster.local True 42m
$ kubectl apply -f - <<EOF
apiVersion: extensions.istio.io/v1alpha1
kind: WasmPlugin
metadata:
name: basic-auth-at-gateway
spec:
targetRefs:
- kind: Gateway
group: gateway.networking.k8s.io
name: bookinfo-gateway # gateway name retrieved from previous step
url: oci://ghcr.io/istio-ecosystem/wasm-extensions/basic_auth:1.12.0
phase: AUTHN
pluginConfig:
basic_auth_rules:
- prefix: "/productpage"
request_methods:
- "GET"
- "POST"
credentials:
- "ok:test"
- "YWRtaW4zOmFkbWluMw=="
EOF
HTTP 篩選器將作為身份驗證篩選器注入到閘道中。Istio 代理將解釋 WasmPlugin 配置,從 OCI 映像註冊表將遠端 Wasm 模組下載到本地檔案,並透過參考該檔案將 HTTP 篩選器注入到閘道中。
透過閘道驗證流量
測試沒有憑證的
/productpage
。$ kubectl exec deploy/curl -- curl -s -w "%{http_code}" -o /dev/null "http://bookinfo-gateway-istio.default.svc.cluster.local/productpage" 401
測試具有 WasmPlugin 資源中設定的憑證的
/productpage
。$ kubectl exec deploy/curl -- curl -s -o /dev/null -H "Authorization: Basic YWRtaW4zOmFkbWluMw==" -w "%{http_code}" "http://bookinfo-gateway-istio.default.svc.cluster.local/productpage" 200
在航點,適用於命名空間中的所有服務
航點代理在 Istio 的環境模式中扮演至關重要的角色,促進服務網格內的安全高效通訊。下面,我們將探討如何將 Wasm 配置應用於航點,動態增強代理功能。
部署航點代理
依照航點部署說明在 bookinfo 命名空間中部署一個航點代理。
$ istioctl waypoint apply --enroll-namespace --wait
驗證流量是否到達服務。
$ kubectl exec deploy/curl -- curl -s -w "%{http_code}" -o /dev/null http://productpage:9080/productpage
200
為航點設定 WebAssembly 外掛程式
若要使用遠端 Wasm 模組設定 WebAssembly 篩選器,請建立一個目標為 waypoint
閘道的 WasmPlugin
資源。
$ kubectl get gateway
NAME CLASS ADDRESS PROGRAMMED AGE
bookinfo-gateway istio bookinfo-gateway-istio.default.svc.cluster.local True 23h
waypoint istio-waypoint 10.96.202.82 True 21h
$ kubectl apply -f - <<EOF
apiVersion: extensions.istio.io/v1alpha1
kind: WasmPlugin
metadata:
name: basic-auth-at-waypoint
spec:
targetRefs:
- kind: Gateway
group: gateway.networking.k8s.io
name: waypoint # gateway name retrieved from previous step
url: oci://ghcr.io/istio-ecosystem/wasm-extensions/basic_auth:1.12.0
phase: AUTHN
pluginConfig:
basic_auth_rules:
- prefix: "/productpage"
request_methods:
- "GET"
- "POST"
credentials:
- "ok:test"
- "YWRtaW4zOmFkbWluMw=="
EOF
檢視已設定的外掛程式
$ kubectl get wasmplugin
NAME AGE
basic-auth-at-gateway 28m
basic-auth-at-waypoint 14m
透過航點代理驗證流量
測試沒有憑證的內部
/productpage
。$ kubectl exec deploy/curl -- curl -s -w "%{http_code}" -o /dev/null http://productpage:9080/productpage 401
測試具有憑證的內部
/productpage
。$ kubectl exec deploy/curl -- curl -s -w "%{http_code}" -o /dev/null -H "Authorization: Basic YWRtaW4zOmFkbWluMw==" http://productpage:9080/productpage 200
在航點,適用於特定服務
若要針對特定服務使用遠端 Wasm 模組設定 WebAssembly 篩選器,請建立一個直接針對特定服務的 WasmPlugin 資源。
建立一個目標為 reviews
服務的 WasmPlugin
,以便該擴展僅適用於 reviews
服務。在此配置中,身份驗證令牌和前綴是專門為 reviews 服務量身定制的,確保只有指向它的請求才受此身份驗證機制約束。
$ kubectl apply -f - <<EOF
apiVersion: extensions.istio.io/v1alpha1
kind: WasmPlugin
metadata:
name: basic-auth-for-service
spec:
targetRefs:
- kind: Service
group: ""
name: reviews
url: oci://ghcr.io/istio-ecosystem/wasm-extensions/basic_auth:1.12.0
phase: AUTHN
pluginConfig:
basic_auth_rules:
- prefix: "/reviews"
request_methods:
- "GET"
- "POST"
credentials:
- "ok:test"
- "MXQtaW4zOmFkbWluMw=="
EOF
驗證針對服務的流量
使用在一般
waypoint
代理設定的憑證測試內部/productpage
。$ kubectl exec deploy/curl -- curl -s -w "%{http_code}" -o /dev/null -H "Authorization: Basic YWRtaW4zOmFkbWluMw==" http://productpage:9080/productpage 200
使用在特定
reviews-svc-waypoint
代理設定的憑證測試內部/reviews
。$ kubectl exec deploy/curl -- curl -s -w "%{http_code}" -o /dev/null -H "Authorization: Basic MXQtaW4zOmFkbWluMw==" http://reviews:9080/reviews/1 200
測試沒有憑證的內部
/reviews
。$ kubectl exec deploy/curl -- curl -s -w "%{http_code}" -o /dev/null http://reviews:9080/reviews/1 401
當執行提供的命令且不帶憑證時,它會驗證訪問內部 /productpage
是否會導致 401 未經授權的回應,這展示了在沒有正確身份驗證憑證的情況下無法訪問資源的預期行為。
清除
移除 WasmPlugin 設定。
$ kubectl delete wasmplugin basic-auth-at-gateway basic-auth-at-waypoint basic-auth-for-service
依照環境模式解除安裝指南移除 Istio 和範例測試應用程式。