流量標籤(TrafficLabel )CRD用於配置自訂的流量標籤。為支援基於標籤的路由能力,您還需要建立相應的目標規則(DestinationRule)和虛擬服務(VirtualService),才能將流量根據標籤路由到對應的工作負載。本文介紹如何基於流量標籤定義路由規則。
前提條件
已建立TrafficLabel。具體操作,請參見流量標籤TrafficLabel說明。
操作步驟
使用以下內容,建立dr-productpage.yaml檔案。
以下檔案表示將
productpage分為test1、test2、test3等多個子集。apiVersion: networking.istio.io/v1alpha3 kind: DestinationRule metadata: name: dr-productpage spec: host: productpage subsets: - name: test1 labels: version: test1 - name: test2 labels: version: test2 - name: test3 labels: version: test3 ... - name: testn labels: version: testn - name: base labels: version: base執行以下命令,建立目標規則DestinationRule。
kubectl apply -f dr-productpage.yaml使用以下內容,建立vs-productpage.yaml檔案。
match.headers和route.destination.subset表示將流量根據標籤值路由到目標工作負載。例如,將帶有名稱為asm-labels-test且值為test1的標籤的請求路由到test1子集下的工作負載。apiVersion: networking.istio.io/v1alpha3 kind: VirtualService metadata: name: vs-productpage spec: hosts: - productpage http: - match: - headers: asm-labels-test: exact: test1 route: - destination: host: productpage subset: test1 - match: - headers: asm-labels-test: exact: test2 route: - destination: host: productpage subset: test2 - match: - headers: asm-labels-test: exact: test3 route: - destination: host: productpage subset: test3 - route: - destination: host: productpage subset: base參數
說明
match.headers.asm-labels-test
流量標籤名稱。
match.headers.exact
流量標籤值。
route.destination.subset
路由目標對應的目標工作負載所在的子集(Subset)。
執行以下命令,建立虛擬服務VirtualService。
kubectl apply -f vs-productpage.yaml
相關操作
簡化虛擬服務的定義
若工作負載存在多個版本時,虛擬服務VirtualService的配置將變得較為複雜。您可以使用以下內容簡化虛擬服務的定義,並對流量進行降級。
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
name: vs-productpage
spec:
hosts:
- productpage
http:
- route:
- destination:
host: productpage
subset: $asm-labels-test
擴充虛擬服務的定義,支援流量降級
當目標服務不可用時,ASM企業版提供流量降級功能。fallback參數定義在route參數下,用於定義標籤路由的目標服務不可用(包括目標服務未定義或對應的Pod不存在等情況)時回退到備用服務。YAML樣本如下。
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
name: vs-productpage
spec:
hosts:
- productpage
http:
- route:
- destination:
host: productpage
subset: $asm-labels-test
fallback:
target:
host: productpage
subset: base
參數 | 說明 |
target.host | 路由的目標服務名稱。 |
target.subset | 路由的目標服務的子集。 |