日韩成人免费在线_国产成人一二_精品国产免费人成电影在线观..._日本一区二区三区久久久久久久久不

當(dāng)前位置:首頁 > 科技  > 軟件

Istio流量管理之請求路由分析

來源: 責(zé)編: 時(shí)間:2023-11-09 09:15:13 301觀看
導(dǎo)讀前面我們了解了 Gateway 和 VirtualService 資源對象的作用,以及它們是如何影響 Envoy 的配置的,那么這些資源對象又是如何影響流量的呢?通過 Istio 如何實(shí)現(xiàn)流量管理的呢?流量管理概述Istio 的流量路由規(guī)則可以很容易的

U8428資訊網(wǎng)——每日最新資訊28at.com

前面我們了解了 Gateway 和 VirtualService 資源對象的作用,以及它們是如何影響 Envoy 的配置的,那么這些資源對象又是如何影響流量的呢?通過 Istio 如何實(shí)現(xiàn)流量管理的呢?U8428資訊網(wǎng)——每日最新資訊28at.com

U8428資訊網(wǎng)——每日最新資訊28at.com

U8428資訊網(wǎng)——每日最新資訊28at.com

流量管理概述

Istio 的流量路由規(guī)則可以很容易的控制服務(wù)之間的流量和 API 調(diào)用。Istio 簡化了服務(wù)級(jí)別屬性的配置,比如熔斷器、超時(shí)和重試,并且能輕松的設(shè)置重要的任務(wù),如 A/B 測試、金絲雀發(fā)布、基于流量百分比切分的分階段發(fā)布等。它還提供了開箱即用的故障恢復(fù)特性, 有助于增強(qiáng)應(yīng)用的健壯性,從而更好地應(yīng)對被依賴的服務(wù)或網(wǎng)絡(luò)發(fā)生故障的情況。U8428資訊網(wǎng)——每日最新資訊28at.com

為了在網(wǎng)格中路由,Istio 需要知道所有的 endpoint 在哪以及它們屬于哪些服務(wù)。為了定位到 service registry(服務(wù)注冊中心),Istio 會(huì)連接到一個(gè)服務(wù)發(fā)現(xiàn)系統(tǒng)。如果在 Kubernetes 集群上安裝了 Istio,那么它將自動(dòng)檢測該集群中的服務(wù)和 endpoint。U8428資訊網(wǎng)——每日最新資訊28at.com

請求路由

首先我們來實(shí)現(xiàn)下最基本的流量請求路由的功能,這里我們將學(xué)習(xí)如何將請求動(dòng)態(tài)路由到微服務(wù)的多個(gè)版本。U8428資訊網(wǎng)——每日最新資訊28at.com

我們知道 Bookinfo 示例包含四個(gè)獨(dú)立的微服務(wù),每個(gè)微服務(wù)都有多個(gè)版本。其中 reviews 服務(wù)的三個(gè)不同版本已經(jīng)部署并同時(shí)運(yùn)行。我們可以在瀏覽器中訪問 Bookinfo 應(yīng)用程序并刷新幾次。正常會(huì)看到三種不同的 reviews 服務(wù)版本的輸出,有時(shí)書評(píng)的輸出包含星級(jí)評(píng)分,有時(shí)則不包含。這是因?yàn)闆]有明確的默認(rèn)服務(wù)版本可路由,Istio 將以循環(huán)方式將請求路由到所有可用版本。U8428資訊網(wǎng)——每日最新資訊28at.com

我們首先來將所有流量路由到微服務(wù)的 v1 版本,稍后,您將應(yīng)用規(guī)則根據(jù) HTTP 請求 header 的值路由流量。U8428資訊網(wǎng)——每日最新資訊28at.com

路由到指定版本

要只路由到一個(gè)版本,則需要為微服務(wù)設(shè)置默認(rèn)版本的 VirtualService。U8428資訊網(wǎng)——每日最新資訊28at.com

應(yīng)用規(guī)則

Istio 使用 VirtualService 來定義路由規(guī)則,只需要應(yīng)用下面的資源對象即可:U8428資訊網(wǎng)——每日最新資訊28at.com

$ kubectl apply -f samples/bookinfo/networking/virtual-service-all-v1.yamlvirtualserviceworking.istio.io/productpage createdvirtualserviceworking.istio.io/reviews createdvirtualserviceworking.istio.io/ratings createdvirtualserviceworking.istio.io/details created

該資源清單中定義了四個(gè) VirtualService 對象,分別是 productpage、reviews、ratings、details,它們分別對應(yīng)著 Bookinfo 應(yīng)用中的四個(gè)微服務(wù),完整的清單如下所示:U8428資訊網(wǎng)——每日最新資訊28at.com

# virtual-service-all-v1.yamlapiVersion: networking.istio.io/v1alpha3kind: VirtualServicemetadata:  name: productpagespec:  hosts:    - productpage  http:    - route:        - destination:            host: productpage            subset: v1---apiVersion: networking.istio.io/v1alpha3kind: VirtualServicemetadata:  name: reviewsspec:  hosts:    - reviews  http:    - route:        - destination:            host: reviews            subset: v1---apiVersion: networking.istio.io/v1alpha3kind: VirtualServicemetadata:  name: ratingsspec:  hosts:    - ratings  http:    - route:        - destination:            host: ratings            subset: v1---apiVersion: networking.istio.io/v1alpha3kind: VirtualServicemetadata:  name: detailsspec:  hosts:    - details  http:    - route:        - destination:            host: details            subset: v1---

我們可以看到這里的 VirtualService 對象中都定義了 subset 字段,這個(gè)字段就是用來指定微服務(wù)的版本的,這里我們將所有的微服務(wù)都指定為 v1 版本,這樣所有的流量都會(huì)被路由到 v1 版本的微服務(wù)中,包括 reviews 服務(wù),這樣我們就不會(huì)再看到星級(jí)評(píng)分了。U8428資訊網(wǎng)——每日最新資訊28at.com

但是如果我們現(xiàn)在直接去訪問 Bookinfo 應(yīng)用的話,是不能正常訪問的,因?yàn)槲覀儔焊瓦€沒指定這些 v1 版本的微服務(wù)到底在哪里。U8428資訊網(wǎng)——每日最新資訊28at.com

U8428資訊網(wǎng)——每日最新資訊28at.com

bookinfo errorU8428資訊網(wǎng)——每日最新資訊28at.com

這個(gè)時(shí)候就需要用到另外一個(gè)資源對象 DestinationRule 了,我們需要為每個(gè)微服務(wù)創(chuàng)建一個(gè) DestinationRule 對象,用來指定這些微服務(wù)的實(shí)際地址,這樣 VirtualService 對象才能將流量路由到這些微服務(wù)中。Istio 在 DestinationRule 目標(biāo)規(guī)則中使用 subsets 定義服務(wù)的版本,運(yùn)行以下命令為 Bookinfo 服務(wù)創(chuàng)建默認(rèn)的目標(biāo)規(guī)則即可:U8428資訊網(wǎng)——每日最新資訊28at.com

$ kubectl apply -f samples/bookinfo/networking/destination-rule-all.yamldestinationruleworking.istio.io/productpage createddestinationruleworking.istio.io/reviews createddestinationruleworking.istio.io/ratings createddestinationruleworking.istio.io/details created

該資源清單中定義了四個(gè) DestinationRule 對象,分別是 productpage、reviews、ratings、details 幾個(gè)服務(wù)的目標(biāo)規(guī)則,它們分別對應(yīng)著 Bookinfo 應(yīng)用中的四個(gè)微服務(wù),完整的清單如下所示:U8428資訊網(wǎng)——每日最新資訊28at.com

# destination-rule-all.yamlapiVersion: networking.istio.io/v1alpha3kind: DestinationRulemetadata:  name: productpagespec:  host: productpage  subsets:    - name: v1      labels:        version: v1---apiVersion: networking.istio.io/v1alpha3kind: DestinationRulemetadata:  name: reviewsspec:  host: reviews  subsets:    - name: v1      labels:        version: v1    - name: v2      labels:        version: v2    - name: v3      labels:        version: v3---apiVersion: networking.istio.io/v1alpha3kind: DestinationRulemetadata:  name: ratingsspec:  host: ratings  subsets:    - name: v1      labels:        version: v1    - name: v2      labels:        version: v2    - name: v2-mysql      labels:        version: v2-mysql    - name: v2-mysql-vm      labels:        version: v2-mysql-vm---apiVersion: networking.istio.io/v1alpha3kind: DestinationRulemetadata:  name: detailsspec:  host: details  subsets:    - name: v1      labels:        version: v1    - name: v2      labels:        version: v2---

現(xiàn)在我們就可以正常訪問 Bookinfo 應(yīng)用了,并且無論刷新多少次,頁面的評(píng)論部分都不會(huì)顯示評(píng)級(jí)星標(biāo),這是因?yàn)槲覀儗?Istio 配置為將 reviews 服務(wù)的所有流量路由到版本 reviews:v1,而此版本的服務(wù)不訪問星級(jí)評(píng)分服務(wù)。U8428資訊網(wǎng)——每日最新資訊28at.com

U8428資訊網(wǎng)——每日最新資訊28at.com

v1版本reviewU8428資訊網(wǎng)——每日最新資訊28at.com

這樣我們就成功將流量路由到服務(wù)的某一個(gè)版本上了。U8428資訊網(wǎng)——每日最新資訊28at.com

原理分析

前面章節(jié)中我們只定義了一個(gè)名為 bookinfo 的 VirtualService 資源對象就可以正常訪問了:U8428資訊網(wǎng)——每日最新資訊28at.com

apiVersion: networking.istio.io/v1beta1kind: VirtualServicemetadata:  name: bookinfo  namespace: defaultspec:  gateways:    - bookinfo-gateway  hosts:    - "*"  http:    - match:        - uri:            exact: /productpage        - uri:            prefix: /static        - uri:            exact: /login        - uri:            exact: /logout        - uri:            prefix: /api/v1/products      route:        - destination:            host: productpage            port:              number: 9080

很明顯上面這個(gè)虛擬服務(wù)對象是我們訪問 Bookinfo 應(yīng)用的入口路由規(guī)則,所以這個(gè)虛擬服務(wù)對象實(shí)際上是為 istio-ingressgateway 入口網(wǎng)關(guān)服務(wù)定義的。 它將所有的流量都路由到了 productpage 這個(gè)服務(wù)上,而 productpage 這個(gè)服務(wù)又會(huì)去調(diào)用其他的服務(wù)來獲取數(shù)據(jù),在 productpage 服務(wù)中調(diào)用其他微服務(wù) 其實(shí)就是直接通過服務(wù)名稱來調(diào)用的,比如調(diào)用 reviews 服務(wù)就是直接通過 reviews:9080 這個(gè)服務(wù)來調(diào)用的,我們可以查看 productpage 的代碼來驗(yàn)證這一點(diǎn):U8428資訊網(wǎng)——每日最新資訊28at.com

U8428資訊網(wǎng)——每日最新資訊28at.com

productpageU8428資訊網(wǎng)——每日最新資訊28at.com

我們可以再次查看 Bookinfo 在網(wǎng)格內(nèi)的請求架構(gòu)圖:U8428資訊網(wǎng)——每日最新資訊28at.com

U8428資訊網(wǎng)——每日最新資訊28at.com

BookInfo 架構(gòu)U8428資訊網(wǎng)——每日最新資訊28at.com

當(dāng)我們在瀏覽器中訪問 http://<gateway url>/productpage 時(shí),請求將進(jìn)入網(wǎng)格中的 istio-ingressgateway 服務(wù),然后將請求轉(zhuǎn)發(fā)到 productpage 服務(wù)。productpage 服務(wù)將調(diào)用 reviews 和 details 服務(wù)來填充頁面的內(nèi)容,然后將其返回給用戶。(reviews 服務(wù)包括 3 個(gè)不同版本的應(yīng)用,可以通過 version 標(biāo)簽區(qū)分)U8428資訊網(wǎng)——每日最新資訊28at.com

現(xiàn)在我們只想將流量路由到 reviews:v1 版本去,按照傳統(tǒng)的方法只需要將 reviews 的 Service 對象去強(qiáng)制關(guān)聯(lián) version: v1 這個(gè)標(biāo)簽即可,現(xiàn)在我們所有的服務(wù)都被注入了一個(gè) Envoy 的 Sidecar 代理,通過 Envoy 很容易就可以實(shí)現(xiàn)這個(gè)路由功能,而相應(yīng)的在 Istio 中我們只需要通過 VirtualService 和 DestinationRule 這兩個(gè)資源對象就可以來實(shí)現(xiàn)了。上面我們創(chuàng)建的關(guān)于 reviews 服務(wù)的這兩個(gè)對象如下所示:U8428資訊網(wǎng)——每日最新資訊28at.com

apiVersion: networking.istio.io/v1alpha3kind: VirtualServicemetadata:  name: reviewsspec:  hosts:    - reviews  http:    - route:        - destination:            host: reviews            subset: v1---apiVersion: networking.istio.io/v1alpha3kind: DestinationRulemetadata:  name: reviewsspec:  host: reviews  subsets:    - name: v1      labels:        version: v1    - name: v2      labels:        version: v2    - name: v3      labels:        version: v3

那么這兩個(gè)對象是如何來影響 Envoy Sidecar 的呢?前面我們已經(jīng)分析了流量從 istio-ingressgateway 進(jìn)來后被路由到了 productpage 服務(wù),那么 productpage 又該如何去訪問其他微服務(wù)呢?同樣我們可以使用 istioctl proxy-config 來查看 productpage 服務(wù)的 Envoy 配置。U8428資訊網(wǎng)——每日最新資訊28at.com

每個(gè) Envoy Sidecar 都有一個(gè)綁定到 0.0.0.0:15001 的監(jiān)聽器,然后利用 IP tables 將 pod 的所有入站和出站流量路由到這里,此監(jiān)聽器會(huì)配置一個(gè) useOriginalDst: true,這意味著它將請求交給最符合請求原始目標(biāo)的監(jiān)聽器。如果找不到任何匹配的虛擬監(jiān)聽器,它會(huì)將請求發(fā)送給返回 404 的 BlackHoleCluster,我們可以查看下 15001 端口的監(jiān)聽器配置:U8428資訊網(wǎng)——每日最新資訊28at.com

$ istioctl proxy-config listeners productpage-v1-564d4686f-wwqqf --port 15001 -oyaml- address:    socketAddress:      address: 0.0.0.0      portValue: 15001  filterChains:  - filterChainMatch:      destinationPort: 15001    filters:    - name: istio.stats      typedConfig:        '@type': type.googleapis.com/stats.PluginConfig    - name: envoy.filterswork.tcp_proxy      typedConfig:        '@type': type.googleapis.com/envoy.extensions.filterswork.tcp_proxy.v3.TcpProxy        cluster: BlackHoleCluster        statPrefix: BlackHoleCluster    name: virtualOutbound-blackhole  - filters:    - name: istio.stats      typedConfig:        '@type': type.googleapis.com/stats.PluginConfig    - name: envoy.filterswork.tcp_proxy      typedConfig:        '@type': type.googleapis.com/envoy.extensions.filterswork.tcp_proxy.v3.TcpProxy        # ......        cluster: PassthroughCluster        statPrefix: PassthroughCluster    name: virtualOutbound-catchall-tcp  name: virtualOutbound  trafficDirection: OUTBOUND  useOriginalDst: true

實(shí)際上我們的請求是到 9080 端口(productpage 服務(wù)綁定 9080 端口)的 HTTP 出站請求,這意味著它被切換到 0.0.0.0:9080 虛擬監(jiān)聽器。所以我們查看下 9080 端口的監(jiān)聽器配置:U8428資訊網(wǎng)——每日最新資訊28at.com

# productpage 默認(rèn)訪問其他服務(wù)的 9080 端口$ istioctl proxy-config listeners productpage-v1-564d4686f-wwqqf --port 9080 -oyaml- address:    socketAddress:      address: 0.0.0.0      portValue: 9080  # ......        rds:          configSource:            ads: {}            initialFetchTimeout: 0s            resourceApiVersion: V3          routeConfigName: "9080"  # RDS的路由配置名稱  # ......  name: 0.0.0.0_9080  trafficDirection: OUTBOUND  # 出流量

可以看到此監(jiān)聽器在其配置的 RDS 中查找名為 9080 的路由配置,我們可以使用 istioctl proxy-config routes 命令來查看這個(gè)路由配置的詳細(xì)信息:U8428資訊網(wǎng)——每日最新資訊28at.com

# 查看 9080 這個(gè)路由配置$ istioctl proxy-config routes productpage-v1-564d4686f-wwqqf --name 9080 -oyaml- name: "9080"  virtualHosts:  - domains:    - details.default.svc.cluster.local    - details    - details.default.svc    - details.default    - 10.111.83.224    name: details.default.svc.cluster.local:9080    routes:    - decorator:        operation: details.default.svc.cluster.local:9080/*      match:        prefix: /      metadata:        filterMetadata:          istio:            config: /apis/networking.istio.io/v1alpha3/namespaces/default/virtual-service/details      route:        cluster: outbound|9080|v1|details.default.svc.cluster.local        # ......  - domains:    - productpage.default.svc.cluster.local    - productpage    - productpage.default.svc    - productpage.default    - 10.97.120.23    name: productpage.default.svc.cluster.local:9080    routes:    - decorator:        operation: productpage.default.svc.cluster.local:9080/*      match:        prefix: /      name: default      route:        cluster: outbound|9080||productpage.default.svc.cluster.local        # ......  - domains:    - ratings.default.svc.cluster.local    - ratings    - ratings.default.svc    - ratings.default    - 10.101.184.235    name: ratings.default.svc.cluster.local:9080    routes:    - decorator:        operation: ratings.default.svc.cluster.local:9080/*      match:        prefix: /      metadata:        filterMetadata:          istio:            config: /apis/networking.istio.io/v1alpha3/namespaces/default/virtual-service/ratings      route:        cluster: outbound|9080|v1|ratings.default.svc.cluster.local        # ......  - domains:    - reviews.default.svc.cluster.local    - reviews    - reviews.default.svc    - reviews.default    - 10.97.120.56    name: reviews.default.svc.cluster.local:9080    routes:    - decorator:        operation: reviews.default.svc.cluster.local:9080/*      match:        prefix: /      metadata:        filterMetadata:          istio:            config: /apis/networking.istio.io/v1alpha3/namespaces/default/virtual-service/reviews      route:        cluster: outbound|9080|v1|reviews.default.svc.cluster.local        # ......  - domains:    - '*'    name: allow_any    routes:    - match:        prefix: /      name: allow_any      route:        cluster: PassthroughCluster        # ......

這個(gè)路由配置中其實(shí)包含了 K8s Service 對象中監(jiān)聽 9080 端口的所有服務(wù),如果沒有創(chuàng)建對應(yīng)的 VirtualService 對象,對應(yīng)的路由配置就沒有 metadata.filterMetadata.istio.config 這個(gè)屬性。比如現(xiàn)在我們正在通過 productpage 請求前往 reviews 服務(wù),因此 Envoy 將選擇我們的請求與域匹配的虛擬主機(jī)。一旦在域上匹配,Envoy 會(huì)查找與請求匹配的第一條路徑,我們這里沒有任何高級(jí)路由,因此只有一條路由匹配所有內(nèi)容。這條路由告訴 Envoy 將請求發(fā)送到 outbound|9080|v1|reviews.default.svc.cluster.local 集群,因?yàn)榍懊嫖覀儎?chuàng)建的 reviews 這個(gè) VirtualService 對象配置了的 destination.subset: v1,所以這里的集群命名上多了一個(gè) subset。U8428資訊網(wǎng)——每日最新資訊28at.com

需要注意的是我們在 VirtualService 對象里面配置了 destination.subset: v1,那么必須要有對應(yīng)的 subset 存在才行,否則不會(huì)生成對應(yīng)的 Envoy 集群配置,那么就不能正常訪問該服務(wù)了,而該 subset 就是通過前面的 DestinationRule 對象來定義的,現(xiàn)在我們就可以來查看這個(gè)集群配置了:U8428資訊網(wǎng)——每日最新資訊28at.com

$ istioctl proxy-config cluster productpage-v1-564d4686f-wwqqf --fqdn reviews.default.svc.cluster.local -o yaml- edsClusterConfig:    edsConfig:      ads: {}      initialFetchTimeout: 0s      resourceApiVersion: V3    serviceName: outbound|9080||reviews.default.svc.cluster.local  lbPolicy: LEAST_REQUEST  metadata:    filterMetadata:      istio:        config: /apis/networking.istio.io/v1alpha3/namespaces/default/destination-rule/reviews        services:        - host: reviews.default.svc.cluster.local          name: reviews          namespace: default  # ......  name: outbound|9080||reviews.default.svc.cluster.local  type: EDS- edsClusterConfig:    edsConfig:      ads: {}      initialFetchTimeout: 0s      resourceApiVersion: V3    serviceName: outbound|9080|v1|reviews.default.svc.cluster.local  lbPolicy: LEAST_REQUEST  metadata:    filterMetadata:      istio:        config: /apis/networking.istio.io/v1alpha3/namespaces/default/destination-rule/reviews        services:        - host: reviews.default.svc.cluster.local          name: reviews          namespace: default        subset: v1  name: outbound|9080|v1|reviews.default.svc.cluster.local  # ......  type: EDS- edsClusterConfig:    edsConfig:      ads: {}      initialFetchTimeout: 0s      resourceApiVersion: V3    serviceName: outbound|9080|v2|reviews.default.svc.cluster.local  filters:  - name: istio.metadata_exchange    typedConfig:      '@type': type.googleapis.com/envoy.tcp.metadataexchange.config.MetadataExchange      protocol: istio-peer-exchange  lbPolicy: LEAST_REQUEST  metadata:    filterMetadata:      istio:        config: /apis/networking.istio.io/v1alpha3/namespaces/default/destination-rule/reviews        services:        - host: reviews.default.svc.cluster.local          name: reviews          namespace: default        subset: v2  name: outbound|9080|v2|reviews.default.svc.cluster.local  # ......  type: EDS- edsClusterConfig:    edsConfig:      ads: {}      initialFetchTimeout: 0s      resourceApiVersion: V3    serviceName: outbound|9080|v3|reviews.default.svc.cluster.local  filters:  - name: istio.metadata_exchange    typedConfig:      '@type': type.googleapis.com/envoy.tcp.metadataexchange.config.MetadataExchange      protocol: istio-peer-exchange  lbPolicy: LEAST_REQUEST  metadata:    filterMetadata:      istio:        config: /apis/networking.istio.io/v1alpha3/namespaces/default/destination-rule/reviews        services:        - host: reviews.default.svc.cluster.local          name: reviews          namespace: default        subset: v3  name: outbound|9080|v3|reviews.default.svc.cluster.local  # ......  type: EDS

從上面配置可以看到里面一共包含了 4 個(gè) reviews 相關(guān)的集群,一個(gè)是原始的不包含 subset 的,而另外三個(gè)就是前面我們在 DestinationRule 對象中配置的 3 個(gè) subset,所以其實(shí) DestinationRule 映射到 Envoy 的配置文件中就是 Cluster。U8428資訊網(wǎng)——每日最新資訊28at.com

最后我們同樣還可以查看每個(gè)集群下面包含的 endpoint 有哪些:U8428資訊網(wǎng)——每日最新資訊28at.com

$ istioctl proxy-config endpoint productpage-v1-564d4686f-wwqqf --cluster "outbound|9080||reviews.default.svc.cluster.local" -o yaml- edsServiceName: outbound|9080||reviews.default.svc.cluster.local  - address:      socketAddress:        address: 10.244.2.84        portValue: 9080    # ......    weight: 1  - address:      socketAddress:        address: 10.244.2.83        portValue: 9080    # ......    weight: 1  - address:      socketAddress:        address: 10.244.2.88        portValue: 9080    # ......    weight: 1  name: outbound|9080||reviews.default.svc.cluster.local  observabilityName: outbound|9080||reviews.default.svc.cluster.local$ istioctl proxy-config endpoint productpage-v1-564d4686f-wwqqf --cluster "outbound|9080|v1|reviews.default.svc.cluster.local" -o yaml- edsServiceName: outbound|9080|v1|reviews.default.svc.cluster.local  hostStatuses:  - address:      socketAddress:        address: 10.244.2.84        portValue: 9080    weight: 1  name: outbound|9080|v1|reviews.default.svc.cluster.local  observabilityName: outbound|9080|v1|reviews.default.svc.cluster.local# 過濾 versinotallow=v1 的 reviews pod$ kubectl get pod -l app=reviews,versinotallow=v1 -o wideNAME                          READY   STATUS    RESTARTS        AGE     IP            NODE    NOMINATED NODE   READINESS GATESreviews-v1-86896b7648-zjh2n   2/2     Running   4 (5h18m ago)   6d17h   10.244.2.84   node2   <none>           <none>

可以看到不包含 subset 的集群下面的 endpoint 其實(shí)就是 reviews 這個(gè) Service 對象的 endpoint 集合,包含 subset 就只有和該子集匹配的后端實(shí)例了。到了這一步,一切皆明了,后面的事情就跟之前的套路一樣了,具體的 Endpoint 對應(yīng)打了標(biāo)簽 versinotallow=v1 的 Pod。U8428資訊網(wǎng)——每日最新資訊28at.com

到這里我們是不是就實(shí)現(xiàn)了通過 VirtualService 和 DestinationRule 對象將流量路由到了指定的版本上面了,上面的整個(gè)過程就是請求從 productpage 到 reviews 的過程,從 reviews 到網(wǎng)格內(nèi)其他應(yīng)用的流量與上面類似,就不展開討論了。U8428資訊網(wǎng)——每日最新資訊28at.com

基于用戶身份的路由

接下來我們繼續(xù)更改路由配置,將來自特定用戶的所有流量路由到特定服務(wù)版本。我們這里將配置來自名為 Jason 的用戶的所有流量被路由到服務(wù) reviews:v2。U8428資訊網(wǎng)——每日最新資訊28at.com

注意 Istio 對用戶身份沒有任何特殊的內(nèi)置機(jī)制,productpage 服務(wù)在所有到 reviews 服務(wù)的 HTTP 請求中都增加了一個(gè)自定義的 end-user 請求頭來實(shí)現(xiàn)該效果:headers['end-user'] = session['user']。U8428資訊網(wǎng)——每日最新資訊28at.com

要實(shí)現(xiàn)該功能,只需要?jiǎng)?chuàng)建下面的資源對象即可:U8428資訊網(wǎng)——每日最新資訊28at.com

$ kubectl apply -f samples/bookinfo/networking/virtual-service-reviews-test-v2.yamlvirtualserviceworking.istio.io/reviews configured

該資源清單文件創(chuàng)建了一個(gè)如下所示的 VirtualService 資源對象:U8428資訊網(wǎng)——每日最新資訊28at.com

# virtual-service-reviews-test-v2.yamlapiVersion: networking.istio.io/v1alpha3kind: VirtualServicemetadata:  name: reviewsspec:  hosts:    - reviews  http:    - match:        - headers:            end-user:              exact: jason      route:        - destination:            host: reviews            subset: v2    - route:        - destination:            host: reviews            subset: v1

該對象設(shè)置了一條路由規(guī)則,它會(huì)根據(jù) productpage 服務(wù)發(fā)起的請求的 end-user 自定義請求頭內(nèi)容進(jìn)行匹配,如果有該內(nèi)容且為 jason 則會(huì)將流量路由到 reviews 服務(wù)的 v2 版本,其余的還是被路由到 v1 版本去。U8428資訊網(wǎng)——每日最新資訊28at.com

現(xiàn)在我們可以前往瀏覽器訪問 Bookinfo 應(yīng)用,多刷新幾次可以看到評(píng)論始終訪問到的是 v1 版本的服務(wù),即沒有星標(biāo)的:U8428資訊網(wǎng)——每日最新資訊28at.com

U8428資訊網(wǎng)——每日最新資訊28at.com

BookinfoU8428資訊網(wǎng)——每日最新資訊28at.com

然后我們點(diǎn)擊頁面右上角的 Sign in 按鈕,使用 jason 進(jìn)行登錄,登錄后頁面就會(huì)出現(xiàn)帶有黑色星標(biāo)的 v2 版本的評(píng)論服務(wù),即使多刷新幾次依然如此:U8428資訊網(wǎng)——每日最新資訊28at.com

U8428資訊網(wǎng)——每日最新資訊28at.com

Bookinfo jasonU8428資訊網(wǎng)——每日最新資訊28at.com

如果我們選擇使用其他用戶進(jìn)行登錄或者注銷則星標(biāo)就會(huì)消失,這是因?yàn)槌?nbsp;Jason 之外,所有用戶的流量都被路由到 reviews:v1。U8428資訊網(wǎng)——每日最新資訊28at.com

同樣的我們可以去查看下對應(yīng)的 Envoy Sidecar 配置的變化,因?yàn)檫@里我們只更新了一個(gè) VirtualService 對象,所以只會(huì)對 Envoy 的路由表產(chǎn)生影響,查看對應(yīng)的路由配置即可:U8428資訊網(wǎng)——每日最新資訊28at.com

$ istioctl proxy-config routes productpage-v1-564d4686f-wwqqf --name 9080 -oyaml- name: "9080"  validateClusters: false  virtualHosts:  # ......  - domains:    - reviews.default.svc.cluster.local    - reviews    - reviews.default.svc    - reviews.default    - 10.97.120.56    includeRequestAttemptCount: true    name: reviews.default.svc.cluster.local:9080    routes:    - decorator:        operation: reviews.default.svc.cluster.local:9080/*      match:        caseSensitive: true        headers:        - name: end-user          stringMatch:            exact: jason        prefix: /      metadata:        filterMetadata:          istio:            config: /apis/networking.istio.io/v1alpha3/namespaces/default/virtual-service/reviews      route:        cluster: outbound|9080|v2|reviews.default.svc.cluster.local        maxGrpcTimeout: 0s        # ......    - decorator:        operation: reviews.default.svc.cluster.local:9080/*      match:        prefix: /      metadata:        filterMetadata:          istio:            config: /apis/networking.istio.io/v1alpha3/namespaces/default/virtual-service/reviews      route:        cluster: outbound|9080|v1|reviews.default.svc.cluster.local        maxGrpcTimeout: 0s        # ......

從配置上我們可以看到現(xiàn)在的 Envoy 配置中新增了一條路由規(guī)則,如下所示:U8428資訊網(wǎng)——每日最新資訊28at.com

match:  caseSensitive: true  headers:    - name: end-user      stringMatch:        exact: jason  prefix: /route:  cluster: outbound|9080|v2|reviews.default.svc.cluster.local

當(dāng)請求頭中包含 end-user:jason 的時(shí)候請求會(huì)被路由到 outbound|9080|v2|reviews.default.svc.cluster.local 這個(gè) Envoy Cluster 集群,這個(gè)集群就是前面我們通過 DestinationRule 創(chuàng)建的 v2 這個(gè)子集,所以最后請求會(huì)被路由到帶有黑色星標(biāo)的評(píng)論服務(wù)去。U8428資訊網(wǎng)——每日最新資訊28at.com

U8428資訊網(wǎng)——每日最新資訊28at.com

Kiali DashboardU8428資訊網(wǎng)——每日最新資訊28at.com

到這里我們就明白了要通過 Istio 實(shí)現(xiàn)服務(wù)的流量管理,需要用到 Gateway、VirtualService、DestinationRule 三個(gè) CRD 對象,這些對象其實(shí)最終都是去拼湊 Envoy 的配置,每個(gè)對象管理 Envoy 配置的一部分,把這個(gè)關(guān)系搞清楚我們就能更好的掌握 Istio 的使用了。U8428資訊網(wǎng)——每日最新資訊28at.com

本文鏈接:http://www.www897cc.com/showinfo-26-17904-0.htmlIstio流量管理之請求路由分析

聲明:本網(wǎng)頁內(nèi)容旨在傳播知識(shí),若有侵權(quán)等問題請及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。郵件:2376512515@qq.com

上一篇: Spring Cache 緩存注解這樣用,實(shí)在是太香了!

下一篇: 使用 CSS Grid 的響應(yīng)式網(wǎng)頁設(shè)計(jì):消除媒體查詢過載

標(biāo)簽:
  • 熱門焦點(diǎn)
Top 主站蜘蛛池模板: 乌兰察布市| 平山县| 祁东县| 渝北区| 哈巴河县| 青冈县| 望都县| 溆浦县| 察隅县| 宁安市| 舞阳县| 丰镇市| 潮州市| 章丘市| 钟山县| 浮山县| 岐山县| 瑞安市| 交口县| 木兰县| 沈丘县| 米脂县| 小金县| 嘉定区| 安康市| 德格县| 洛阳市| 新巴尔虎右旗| 青铜峡市| 汝阳县| 玛纳斯县| 东宁县| 余干县| 黎城县| 宜兰市| 平山县| 吉木萨尔县| 衡南县| 子洲县| 闽清县| 仲巴县|