面向用户服务的 Istio 请求路由不适用于入口网关

Istio Request Routing for user-facing service doesn't work with ingress-gateway

我在 Istio Ingress Gateway 后面的 Istio 请求路由有问题:

我有两个版本(v1、v2)的简单 node.js 应用程序(web-api),前面有一个 Istio Ingress Gateway 和一个应该执行 80/ 20 版本 1 和 2 之间的分发,但事实并非如此。 Kiali 显示 50/50 分布。

当我添加一个简单的前端服务来传递请求时,一切都按预期进行。 根据 Istio 文档,使用 Istio ingress 允许在面向用户的服务中使用请求路由规则。但对我来说不是,我不明白为什么?

deployment.yaml:

apiVersion: apps/v1beta2 # for versions before 1.9.0 use apps/v1beta2
kind: Deployment
metadata:
  name: web-api-v1
spec:
  selector:
    matchLabels:
      app: web-api
      project: istio-test
      version: v1
  replicas: 1
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        app: web-api
        project: istio-test
        version: v1
    spec:
      containers:
      - image: web-api:1
        name: web-api-v1
        env:
        - name: VERS
          value: "=> Version 1"
        ports:
        - containerPort: 3000
          name: http
      restartPolicy: Always    
---
apiVersion: apps/v1beta2 # for versions before 1.9.0 use apps/v1beta2
kind: Deployment
metadata:
  name: web-api-v2
spec:
  selector:
    matchLabels:
      app: web-api
      project: istio-test
      version: v2
  replicas: 1
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        app: web-api
        project: istio-test
        version: v2
    spec:
      containers:
      - image: web-api:1
        name: web-api-v1
        env:
        - name: VERS
          value: "=> Version 2"
        ports:
        - containerPort: 3000
          name: http
      restartPolicy: Always    
---

service.yaml

apiVersion: v1
kind: Service
metadata:
  name: web-api
  labels:
    app: web-api
    project: istio-test
spec:
  type: NodePort
  ports:
    - port: 3000  
      name: http
      protocol: TCP
  selector:
    app: web-api
---

istio-ingress.yaml:

apiVersion: networking.istio.io/v1alpha3
kind: Gateway
metadata:
  name: default-gateway-ingress
spec:
  selector:
    istio: ingressgateway # use Istio default gateway implementation
  servers:
  - port:
      number: 80
      name: http
      protocol: HTTP
    hosts:
    - "*"
--- 
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: virtualservice-ingress
spec:
  hosts:
  - "*"
  gateways:
  - default-gateway-ingress
  http:
  - match:
    - uri:
        exact: /test
    route:
    - destination:
        host: web-api
        port:
          number: 3000
--- 

istio-虚拟service.yaml:

apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: web-api
spec:
  hosts:
  - web-api
  http:
  - route:
    - destination:
        host: web-api
        subset: v1
      weight: 80  
    - destination:
        host: web-api
        subset: v2 
      weight: 20   
---

我把这个例子放在https://github.com/Harald-U/istio-test

您必须将 web-api 虚拟服务附加到网关并删除 virtualservice-ingress 对象。

Web-api 虚拟服务应如下所示:

apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: web-api
spec:
  hosts:
  - "*"
  gateways:
  - default-gateway-ingress
  http:
  - route:
    - destination:
        host: web-api
        subset: v1
      weight: 80  
    - destination:
        host: web-api
        subset: v2 
      weight: 20   

在 Stefans 的帮助下,我能够以这种方式修复它:

apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: virtualservice-ingress
spec:
  hosts:
  - "*"
  gateways:
  - default-gateway-ingress
  http:
  - match:
    - uri:
        exact: /test
    route:
      - destination:
          host: web-api
          subset: v1
        weight: 80  
      - destination:
          host: web-api
          subset: v2 
        weight: 20       
---

现在我有了入口规则(匹配/测试)并且请求路由也正常工作。