idou老師教你學Istio 09: 如何用Istio實現K8S Ingress流量管理

前言

在 Istio 的世界裏,若是想把外部的請求流量引入網格,你須要認識並會學會配置 Istio Ingress Gatewayapi

什麼是 Ingress Gateway

因爲 Kubernetes Ingress API 只能支持最基本的 HTTP 路由,使用 Kubernetes Ingress資源來配置外部流量的方式不能知足需求。所以 Istio v1alpha3 routing API 引入新的 Istio Ingress Gateway 取代 Kubernetes Ingress。bash

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 服務的目的。spa

如何用 HTTPS 加密 Gateway?

咱們也能夠爲服務啓用 TLS 保護,以 HTTPS 的形式對網格外提供服務。code

首先須要使用工具生成客戶端和服務器端的證書和密鑰。而後使用密鑰和證書做爲輸入,建立一個 Secret。orm

$ kubectl create -n istio-system secret tls istio-ingressgateway-certs --key key.pem --cert cert.pem
複製代碼

接下來修改 Gateway 對象,爲 Ingress gateway 開放一個 443 端口,用於提供 HTTPS 服務:cdn

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 了。

相關文章
相關標籤/搜索