idou老師教你學Istio 09: 如何用Istio實現K8S Ingress流量管理
由於 Kubernetes Ingress API 只能支援最基本的 HTTP 路由,使用 Kubernetes Ingress資源來配置外部流量的方式不能滿足需求。因此 Istio v1alpha3 routing API 引入新的 Istio Ingress Gateway 取代 Kubernetes Ingress。
Gateway 為 HTTP/TCP 流量配置了一個負載均衡,用於承載網格邊緣的進入和發出連線。在同一個網格中可以有多個不同的 gateway 存在。這一規範中描述了一系列開放埠,以及這些埠所使用的協議、負載均衡的 SNI 配置等內容。使用者可以利用標準的 Istio 路由規則控制 HTTP 和 TCP 請求進入網格。
從下圖可以看到 Istio gateway 在整個網格中的使用情況:

如何配置 Gateway 控制 Ingress 流量
如果你已經安裝好了 bookinfo 的應用,為了能在外部訪問 bookinfo 中的 productpage 服務,只需要配置 Gateway 和相關的 VirtualService。
apiVersion: networking.istio.io/v1alpha3 kind: Gateway metadata: name: bookinfo-gateway spec: selector: istio: ingressgateway servers: - hosts: - bookinfo.com port: number: 80 name: http protocol: HTTP 複製程式碼
為了配置相應的路由,需要為相同的 host 定義一 個VirtualService 並且用配置中 gateways 的欄位繫結到剛才建立的 Gateway:
apiVersion: networking.istio.io/v1alpha3 kind: VirtualService metadata: name: bookinfo spec: hosts: - bookinfo.com gateways: - bookinfo-gateway # <---- 繫結gateway - mesh # <----對內部通訊進行流量控制 http: - match: - uri: exact: /productpage route: - destination: host: productpage port: number: 9080 複製程式碼
這樣就達到了在外網開放 productpage 服務的目的。
如何用 HTTPS 加密 Gateway?
我們也可以為服務啟用 TLS 保護,以 HTTPS 的形式對網格外提供服務。
首先需要使用工具生成客戶端和伺服器端的證書和金鑰。然後使用金鑰和證書作為輸入,建立一個 Secret。
$ kubectl create -n istio-system secret tls istio-ingressgateway-certs --key key.pem --cert cert.pem 複製程式碼
接下來修改 Gateway 物件,為 Ingress gateway 開放一個 443 埠,用於提供 HTTPS 服務:
apiVersion: networking.istio.io/v1alpha3 kind: Gateway metadata: name: bookinfo-gateway spec: selector: istio: ingressgateway servers: - hosts: - bookinfo.com port: number: 80 name: http protocol: HTTP - hosts: - "*" port: number: 443 name: https protocol: HTTPS tls: mode: SIMPLE serverCertificate: /etc/istio/ingressgateway-certs/tls.crt privateKey: /etc/istio/ingressgateway-certs/tls.key 複製程式碼
這樣簡單的配置就可以通過 HTTPS 協議訪問bookinfo.com 了。