假设有一个外部服务,外部服务ip为:10.10.102.90,其中32033为v1版本,32034为v2版本。
现在需要把这个服务引入到istio中,使用egressgateway转发访问该服务的流量,并且需要实现多版本路由,使得header中x-version的值为v1的路由到v1版本,x-version的值为v2的路由到v2版本。
使用serviceentry引入该服务
apiVersion: networking.istio.io/v1beta1
kind: ServiceEntry
metadata:name: gindemo-service-entry
spec:endpoints:- address: 10.10.102.90labels:version: v1ports:http: 32033- address: 10.10.102.90labels:version: v2ports:http: 32034hosts:- gindemo.test.chlocation: MESH_EXTERNALports:- name: httpnumber: 80protocol: HTTPresolution: STATIC
定义一个egress gateway:
apiVersion: networking.istio.io/v1beta1
kind: Gateway
metadata:name: gindemo-egressgateway
spec:selector:istio: egressgatewayservers:- port:number: 80name: httpprotocol: HTTPhosts:- gindemo.test.ch
创建一个dr,作为流量入口,接收网格内请求外部服务的流量:
apiVersion: networking.istio.io/v1beta1
kind: DestinationRule
metadata:name: egressgateway-for-gindemo
spec:host: istio-egressgateway-1-19-6.istio-system.svc.cluster.localsubsets:- name: gindemotrafficPolicy:loadBalancer:simple: RANDOM
创建服务的dr,声明服务的多个版本:
apiVersion: networking.istio.io/v1beta1
kind: DestinationRule
metadata:name: gindemo-destination-rule
spec:host: gindemo.test.chsubsets:- labels:version: v1name: v1- labels:version: v2name: v2trafficPolicy:loadBalancer:simple: RANDOM
创建一个vs,定义服务路由规则:
apiVersion: networking.istio.io/v1beta1
kind: VirtualService
metadata:name: direct-gindemo-through-egress-gateway
spec:gateways:- gindemo-egressgateway # 接收来自网关的流量- mesh # 接收来自网格内的流量hosts:- gindemo.test.chhttp:- match: # 该match实现将来自网格内的流量,转发到egressgateway- gateways:- mesh port: 80route:- destination:host: istio-egressgateway-1-19-6.istio-system.svc.cluster.localport:number: 80subset: gindemoweight: 100- match: # 该match实现将来自egressgateway的流量转发到serviceentry- gateways:- gindemo-egressgatewayheaders:x-version:exact: v1route:- destination:host: gindemo.test.chsubset: v1- match: # 该match实现将来自egressgateway的流量转发到serviceentry- gateways:- gindemo-egressgatewayheaders:x-version:exact: v2port: 80route:- destination:host: gindemo.test.chsubset: v2