VirtualService

定义对特定目标服务的一组流量规则,它将满足条件的流量转发到对应的服务后端,这个服务后端可以是一个或多个服务,也可以是DestinationRule定义的服务子集。

示例:

apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: forecast-route
spec:
  hosts:  ##定义流量发送的目标地址,可以是service名字也可以是具体的IP地址,通常是service的名字
  - forecast
  http:  ##这个字段可以是httptcp或者tlshttps
  - match: ##定义匹配规则
    - headers:  ##匹配header中location取值为north的请求
        location:
          exact: north
    route: ##定义具体的路由规则,这里指的是当请求符合匹配规则时,走该规则
    - destination: ##定义流量被转发到哪里去
        host: forecast  ##这个host跟上面的host类似,通常也是用service名字定义
        subset: v2   ##具体规则需要在DestinationRule中定义,这里只是指定一个名字v2
  - route:
    - destination:
        host: forecast
        subset: v1

DestinationRule

定义流量路由之后的去处,它和VirtualService配合使用,在VirtualService中定义了subsets的名字,那么在DestinationRule中定义subsets具体的内容,而且在DestinationRule里还提供了具体的流量策略。

示例:

apiVersion: networking.istio.io/v1alpha3
kind: DestinationRule
metadata:
  name: forecast-dr ##定义DestinationRule的名字
spec:
  host: forecast ##定义流量发送的目标地址,和VirtualService中的该字段含义一样
  subsets:  ##定义subsets规则
  - name: v2  ##名字是v2,和VirtualService里定义的要保持一致
    labels:
      version: v2
    trafficPolicy:  ##定义流量策略,可选字段trafficPolicy、connectionPool(连接池设置,可以实现限速或限流的效果)、outlierDetection(异常点检查,可以做熔断效果)、tls
      loadBalancer:  ##定义负载均衡算法,可选字段simple、consistentHash(一致性hash)、localityLbSetting(位置负载均衡)
        simple: ROUND_ROBIN  ##simple指的是标准负载均衡算法,可以是ROUND_ROBIN、LEAST_CONN(最少连接)、RANDOM(随机)、PASSTHROUGH(直接转发到客户端连接的目标地址,即没有做负载均衡)
  - name: v1
    labels:
      version: v1

Gateway

定义外部访问的端口、协议以及与服务网格内部服务的映射关系。

示例:

apiVersion: networking.istio.io/v1alpha3
kind: Gateway
metadata:
  name: weather-gateway
  namespace: istio-system
spec:
  selector: ##负载选择器,通过它来找到执行Gateway规则的Envoy
    istio: ingressgateway
  servers: ##定义要开放的端口、协议等信息
  - port:
      number: 80
      name: http
      protocol: HTTP
    hosts:  ##定义该Gateway发布的服务地址,支持通配*,这里的hosts定义要和VirtualService里面的hosts匹配才可以关联起来
    - "*"

定义了gateway后,然后在VirtualService里面引用

示例:

apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: frontend-route
  namespace: weather
spec:
  hosts:
  - "*"
  gateways:
  - istio-system/weather-gateway
  http:
  - match:
    - port: 80
    route:
    - destination:
        host: frontend
        port:
          number: 3000
        subset: v1