使用 istio 在应用程序版本之间进行负载平衡时如何使用子集?

How to use subsets when load balancing between app versions with istio?

我有以下服务:

apiVersion: v1
kind: Service
metadata:
  name: downstream-service
spec:
  type: ClusterIP
  selector:
    app: downstream
  ports:
    - protocol: TCP
      port: 80
      targetPort: 80

我想根据我在部署中定义如下的应用程序版本进行负载平衡:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: downstream-deployment-v1
  labels:
    app: downstream
    version: v1
spec:
  replicas: 1
  selector:
    matchLabels:
      app: downstream
      version: v1
  template:
    metadata:
      labels:
        app: downstream
        version: v1
    spec:
      containers:
      - name: downstream
        image: downstream:0.1
        ports:
        - containerPort: 80
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: downstream-deployment-v2
  labels:
    app: downstream
    version: v2
spec:
  replicas: 1
  selector:
    matchLabels:
      app: downstream
      version: v2
  template:
    metadata:
      labels:
        app: downstream
        version: v2
    spec:
      containers:
      - name: downstream
        image: downstream:0.2
        ports:
        - containerPort: 80

现在这两个部署都按预期路由流量 50/50,但我想根据 https://istio.io/latest/docs/reference/config/networking/virtual-service/#HTTPRouteDestination 调整权重,所以我定义了 DestinationRuleVirtualService:

apiVersion: networking.istio.io/v1beta1
kind: DestinationRule
metadata:
  name: downstream-destination
spec:
  host: downstream-service.svc.cluster.local
  subsets:
  - name: v1
    labels:
      version: v1
  - name: v2
    labels:
      version: v2
---
apiVersion: networking.istio.io/v1beta1
kind: VirtualService
metadata:
  name: downstream-virtualservice
spec:
  hosts:
  - downstream-service.svc.cluster.local
  http:
  - name: "downstream-service-v1-routes"
    route:
    - destination:
        host: downstream-service.svc.cluster.local
        subset: v1
      weight: 5
  - name: "downstream-service-v2-routes"
    route:
    - destination:
        host: downstream-service.svc.cluster.local
        subset: v2
      weight: 95

但是有了这个我仍然得到 50/50 的分配。

我尝试用 downstream-service 替换 downstream-service.svc.cluster.local 但结果是没有在 yaml 中定义 weights 并且删除了 subsets 我会得到 50/50 的分割,但是当我添加子集(没有权重)时,我会得到 v1 实例上的所有流量。

我做错了什么?


编辑

这可能是原因,但我不确定该怎么做:

$ istioctl x describe service downstream-service
Service: downstream-service
   Port:  80/auto-detect targets pod port 80
DestinationRule: downstream-service for "downstream-service"
   Matching subsets: v1,v2
   No Traffic Policy
VirtualService: downstream-route
   2 HTTP route(s)
$ istioctl x describe pod downstream-deployment-v2-69bdfc8fbf-bm22f
Pod: downstream-deployment-v2-69bdfc8fbf-bm22f
   Pod Ports: 80 (downstream), 15090 (istio-proxy)
--------------------
Service: downstream-service
   Port:  80/auto-detect targets pod port 80
DestinationRule: downstream-service for "downstream-service"
   Matching subsets: v2
      (Non-matching subsets v1)
   No Traffic Policy
VirtualService: downstream-route
   1 additional destination(s) that will not reach this pod
      Route to non-matching subset v1 for (everything)
$ istioctl x describe pod downstream-deployment-v1-65bd866c47-66p9k
Pod: downstream-deployment-v1-65bd866c47-66p9k
   Pod Ports: 80 (downstream), 15090 (istio-proxy)
--------------------
Service: downstream-service
   Port:  80/auto-detect targets pod port 80
DestinationRule: downstream-service for "downstream-service"
   Matching subsets: v1
      (Non-matching subsets v2)
   No Traffic Policy
VirtualService: downstream-route
   1 additional destination(s) that will not reach this pod
      Route to non-matching subset v2 for (everything)

EDIT2

所以我启动 kiali 只是为了看看:

The weight is assumed to be 100 because there is only one route destination

https://kiali.io/documentation/v1.13/validations/#_the_weight_is_assumed_to_be_100_because_there_is_only_one_route_destination

虽然不确定如何解决这个问题。

好吧,看来我错过了一个很大的 "typo",这是因为 routes 有很多 destination 加权而不是http 有许多加权 routes。

所以我的VirtualService的正确版本如下:

apiVersion: networking.istio.io/v1beta1
kind: VirtualService
metadata:
  name: downstream-service
spec:
  hosts:
  - downstream-service
  http:
  - name: "downstream-service-routes"
    route:
    - destination:
        host: downstream-service
        subset: v1
      weight: 10
    - destination:
        host: downstream-service
        subset: v2
      weight: 90