Gateway / VirtualService实践 - I

本节我们将基于bookinfo这个app来实验Gateway和VirtualService

Gateway

在上一章介绍Kiali时,为了能访问到app页面,我们已经部署了一个Gateway:

❯ kubectl apply -f istio-1.14.3/samples/bookinfo/networking/bookinfo-gateway.yaml
gateway.networking.istio.io/bookinfo-gateway created
virtualservice.networking.istio.io/bookinfo created

查看它的定义:

❯ cat istio-1.14.3/samples/bookinfo/networking/bookinfo-gateway.yaml

apiVersion: networking.istio.io/v1alpha3
kind: Gateway
metadata:
  name: bookinfo-gateway
spec:
  selector:
    istio: ingressgateway # use istio default controller
  servers:
  - port:
      number: 80
      name: http
      protocol: HTTP
    hosts:
    - "*"
---
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: bookinfo
spec:
  hosts:
  - "*"
  gateways:
  - bookinfo-gateway
  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

它匹配hosts为*,当然我们也可以使用更精确的域名。关于Gateway本节就不继续展开过多测试了

创建DestinationRule

目前为止我们bookinfo的架构如下图所示,我们在bookinfo-gatewayProductPage之间创建了 一个VirtualService。接下来我们将在productPageReviews之间继续创建一个VirtualService,来控制它们之间的流量:

image-20220813203223396

首先我们先为productpage, reviews, ratings, details这四个服务创建好DestinationRule,以便我们后面在VirtualService定义中引用:

❯ cat istio-1.14.3/samples/bookinfo/networking/destination-rule-all.yaml
apiVersion: networking.istio.io/v1alpha3
kind: DestinationRule
metadata:
  name: productpage
spec:
  host: productpage
  subsets:
  - name: v1
    labels:
      version: v1
---
apiVersion: networking.istio.io/v1alpha3
kind: DestinationRule
metadata:
  name: reviews
spec:
  host: reviews
  subsets:
  - name: v1
    labels:
      version: v1
  - name: v2
    labels:
      version: v2
  - name: v3
    labels:
      version: v3
---
apiVersion: networking.istio.io/v1alpha3
kind: DestinationRule
metadata:
  name: ratings
spec:
  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/v1alpha3
kind: DestinationRule
metadata:
  name: details
spec:
  host: details
  subsets:
  - name: v1
    labels:
      version: v1
  - name: v2
    labels:
      version: v2
---

创建:

❯ kubectl apply -f destination-rule-all.yaml
destinationrule.networking.istio.io/productpage created
destinationrule.networking.istio.io/reviews created
destinationrule.networking.istio.io/ratings created
destinationrule.networking.istio.io/details created

Reviews三个版本的介绍

上图我们看到Reviews服务是有三个版本的,它们分别长这样:

V1没有星星

image-20220813201614843

V2是黑星星:

image-20220813201625623

V3是红星星:

image-20220813201642361

记住这三个版本的UI,后面我们将用它来测试VirtualService的效果


创建Reviews VirtualService

我们的目标如下图,在ProductPage和Reviews服务之间创建一个VirtualService,来控制流量的分配:

image-20220813201408883

关于reviews VirtualService的定义如下:

❯ cat istio-1.14.3/samples/bookinfo/networking/virtual-service-reviews-80-20.yaml
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: reviews
spec:
  hosts:
    - reviews
  http:
  - route:
    - destination:
        host: reviews
        subset: v1
      weight: 80
    - destination:
        host: reviews
        subset: v2
      weight: 20

创建这个VirtualService:

❯ kubectl apply -f virtual-service-reviews-80-20.yaml
virtualservice.networking.istio.io/reviews created

从上面的yaml定义中,我们能看出:

  • 80%的流量被发送到v1
  • 20%的流量被发送到v2

在浏览器中不断刷新页面,我们能看到大部分时间返回v1版本,偶尔返回v2版本:

image-20220813204857755

Kiali页面查看config

上面我们创建了一些Gateway, VirtualService, DestinationRule,这些列表可以在Kiali的页面中查看到:

image-20220813202251182

点击后进入,查看具体的定义:

image-20220813202505139

当然我们也可以在里面编辑,并保存生效:

image-20220813202311388


使用curl生成持续的流量访问:

while sleep 0.1; do curl -sS 'http://a8a1deecac1ad4ba7993b01e393d363d-322922569.us-west-2.elb.amazonaws.com/productpage' &>/dev/null; done

回到Kiali的Graph页面,看到此时已经没有了v3版本的访问:

image-20220813202743393