02 Virtual Service
官方文档:https://istio.io/latest/zh/docs/reference/config/networking/
配置如何影响流量路由到哪一个Service。
| Field | Type | Description | Required |
|---|---|---|---|
hosts | string[] | 1、流量的访问入口标识。可以是IP,也可以是DNS名字(可使用通配符)。 2、如果是短名称,会自动补全(根据istio所在平台特性)。如果是k8s平台,会自动填充上VirtualService所在的名称空间,而不是根据目标Service所在的名称空间。 | Yes |
gateways | string[] | 该VirtualService路由规则适应于哪些gateways和sidecars。 | No |
http | HTTPRoute[] | http流量的路由规则 | No |
tls | TLSRoute[] | 未终止的tls流量(TLS/HTTPS)的路由规则 | No |
tcp | TCPRoute[] | tcp流量路由规则 | No |
exportTo | string[] | 导出虚拟服务允许其被哪些其他名称空间中定义的边车和网关使用。 | No |
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
name: reviews-route-two-domains
spec:
hosts:
- reviews.com
http:
- route:
- destination:
host: dev.reviews.com
weight: 25
- destination:
host: reviews.com
weight: 75
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
name: bookinfo-sni
spec:
hosts:
- "*.bookinfo.com"
gateways:
- mygateway
tls:
- match:
- port: 443
sniHosts:
- login.bookinfo.com
route:
- destination:
host: login.prod.svc.cluster.local
- match:
- port: 443
sniHosts:
- reviews.bookinfo.com
route:
- destination:
host: reviews.prod.svc.cluster.local
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
name: bookinfo-Mongo
spec:
hosts:
- mongo.prod.svc.cluster.local
tcp:
- match:
- port: 27017
route:
- destination:
host: mongo.backup.svc.cluster.local
port:
number: 5555
在VirtualService中,http字段的每一个列表元素都是一条路由规则,用来描述匹配条件和路由行为。
多条路由规则按从上到下的顺序选择,虚拟服务中定义的第一条规则有最高优先级。本示例中,不满足第一个路由规则的流量均流向一个默认的目标,该目标在第二条规则中指定。因此,第二条规则没有 match 条件,直接将流量导向 v3 子集。
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
name: reviews
spec:
hosts:
- reviews
http:
- match:
- headers:
end-user:
exact: jason
route:
- destination:
host: reviews
subset: v2
- route:
- destination:
host: reviews
subset: v3
- host:必须是istio服务注册中心中的服务。例如k8s的servce,例如ServiceEntry定义的服务。