apiVersion: networking.istio.io/v1alpha3
kind: Gateway
metadata:
  name: bookinfo-gateway
spec:
  selector:
    istio: ingressgateway # use istio default controller
  servers:
  - port:
      number: 80
      name: http
      protocol: HTTP
    hosts:
    - "*"
Istio Service Mesh一定要設定一個Service Mesh入口,之前已經有討論過,詳情可以看[Day17] 如何為Cluster選擇一個好的Gateway
,Istio Istio Gateway的設定可以針對Namespace,不同的Namespace有不同的Gateway設定,具有高度的彈性,設定了Gateway要如何設定Gateway To Service。就必須添加下面的Istio VirtualService。
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: bookinfo
spec:
  hosts:
  - "*"
  gateways:
  - bookinfo-gateway
  http:
  - match:
    - uri:
        exact: /productpage
    - uri:
        prefix: /static
    - uri:
        exact: /login
    - uri:
        exact: /logout
    - uri:
        prefix: /api/v1/products
    route:
    - destination:
        host: productpage
        port:
          number: 9080
直接將Kubernetes Service以Host的方式設定在VirtualService。上面Destination host都是Kubernetes Cluster CoreDNS可以解析的Host-Nam,而Destination port則是Kubernetes Service targetPort。
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: bookinfo
spec:
  hosts:
  - "*"
  gateways:
  - bookinfo-gateway
  http:
  - match:
    - authority:
         exact: "bookinfo.com:31380"
    route:
    - destination:
        port:
          number: 9080
        host: productpage
可以利用http match authority
curl http://bookinfo.com:31380
可以直接訪問到productpage這個頁面,這樣子我針對不同的Namespace設定了多個Gateway,同時設定不同的Domain在設定VirtualService就可以達到不同環境不同Domain,可以明確找到該Namespace的資源。
Istio提供了非常多簡單設定就可以達到的功能,但Troubleshooting跟Performance tuning會有很高的難度,下一篇會在介紹Istio如果想加入更多的Network設定還可以有什麼選項。如果只是簡單的應用這邊就可以囉。