idou老师教你学Istio 09: 如何用Istio实现K8S Ingress流量管理

1,283 阅读2分钟

前言

在 Istio 的世界里,如果想把外部的请求流量引入网格,你需要认识并会学会配置 Istio Ingress Gateway

什么是 Ingress Gateway

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