2-配置动态路由之虚拟服务与目标规则

任务说明

将请求路由到服务的不同版本

VirtualService

虚拟服务定义了一系列的流量路由规则,将流量路由到指定的目标服务

apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: reviews-route
spec:
  hosts: 设置具体的目标地址
  - reviews.prod.svc.cluster.local
  http: 会对应到具体的路由匹配规则
  - match: 满足什么样的请求是可以被我们接收的
    - uri: 根据uri匹配
        prefix: "/wpcatalog"
    - uri:
        prefix: "/consumercatalog"
    rewrite:
      uri: "/newcatalog"
    route:
    - destination:
        host: reviews.prod.svc.cluster.local
        subset: v2
  - route:
    - destination:
        host: reviews.prod.svc.cluster.local
        subset: v1

DestinationRule

DestinationRule定义了流量路由规则匹配后流量的访问策略。在这些策略中可以定义负载均衡、连接池大小,以及负载均衡池中不健康实例的探测和实例的摘除规则等

apiVersion: networking.istio.io/v1alpha3
kind: DestinationRule
metadata:
  name: reviews-destination
spec:
  host: reviews.prod.svc.cluster.local具体的最终路由到的目标地址
  subsets: 子集,一般是给服务限定版本
  - name: v1
    labels:
      version: v1
  - name: v2
    labels:
      version: v2

应用场景

  • 按服务版本路由
  • 按比例切分流量(灰度发布)
  • 根据匹配规则路由(match匹配)

在这里插入图片描述

配置

可以看到,为应用创建了4个不同的虚拟服务
kubectl apply -f samples/bookinfo/networking/virtual-service-all-v1.yaml 
virtualservice.networking.istio.io/productpage created
virtualservice.networking.istio.io/reviews created
virtualservice.networking.istio.io/ratings created
virtualservice.networking.istio.io/details created

为每一个服务都定义了一个目标规则
kubectl apply -f samples/bookinfo/networking/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

配置分析

apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: productpage
spec:
  hosts:
  - productpage
  http:
  - route:
    - destination:
        host: productpage
        subset: v1
---
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: reviews
spec:
  hosts:
  - reviews
  http:
  - route: 解释了请求为什么被打向v1版本
    - destination:
        host: reviews
        subset: v1
---

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: reviews有三个版本,因此子集有三个,刚才的虚拟服务就是根据这里的子集来的
  - name: v1
    labels:
      version: v1
  - name: v2
    labels:
      version: v2
  - name: v3
    labels:
      version: v3
---

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值