2-配置动态路由之虚拟服务与目标规则
程序员文章站
2022-06-17 13:06:54
...
任务说明
将请求路由到服务的不同版本
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
---
上一篇: 用Gateway管理进入网格的流量
推荐阅读