2024-08-02 istio-ingress-gateway

image-20240801100451585

Istio 服务网格中的网关

  • 使用网关为网格来管理入站和出站流量,可以让用户指定要进入或离开网格的流量。
  • 使用网关为网格来管理入站和出站流量,可以让用户指定要进入或离开网格的流量。
  • 网关配置被用于运行在网格内独立 Envoy 代理中,而不是服务工作负载的应用 Sidecar 代理。

Gateway 用于为 HTTP / TCP 流量配置负载均衡器,并不管该负载均衡器将在哪里运行。网格中可以存在任意数量的 Gateway,并且多个不同的 Gateway 实现可以共存。实际上,通过在配置中指定一组工作负载(Pod)标签,可以将 Gateway 配置绑定到特定的工作负载,从而允许用户通过编写简单的 Gateway Controller 来重用现成的网络设备。

Gateway 只用于配置 L4-L6 功能(例如,对外公开的端口,TLS 配置),所有主流的 L7 代理均以统一的方式实现了这些功能。然后,通过在 Gateway 上绑定 VirtualService 的方式,可以使用标准的 Istio 规则来控制进入 Gateway 的 HTTP 和 TCP 流量。

例如,下面这个简单的 Gateway 配置了一个 Load Balancer,以允许访问 host bookinfo.com 的 https 外部流量进入网格中:

apiVersion: networking.istio.io/v1alpha3
kind: Gateway
metadata:
  name: bookinfo-gateway
spec:
  selector:
    app: my-ingress-gateway
  servers:
  - port:
      number: 443
      name: https
      protocol: HTTPS
    hosts:
    - bookinfo.com
    tls:
      mode: SIMPLE
      serverCertificate: /tmp/tls.crt
      privateKey: /tmp/tls.key

要为进入上面的 Gateway 的流量配置相应的路由,必须为同一个 host 定义一个 VirtualService(在下一节中描述),并使用配置中的 gateways 字段绑定到前面定义的 Gateway 上:

apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: bookinfo
spec:
  hosts:
    - bookinfo.com
  gateways:
  - bookinfo-gateway # <---- bind to gateway
    http:
  - match:
    - uri:
        prefix: /reviews
    route:
    ...

然后就可以为出口流量配置带有路由规则的虚拟服务。

Gateway 配置信息 #

image-20240801100547565

Server 配置信息 #

image-20240801100559826

Port 配置信息 #

image-20240801100614509

Server.TLSOptions 配置信息 #

image-20240801100629542

image-20240801100639164

Server.TLSOptions.TLSmode 配置信息 #

image-20240801100653028

Server.TLSOptions.TLSProtocol 配置信息 #

image-20240801100704319

Gateway配置要点 #

  • Gateway定义运行在网格边缘的负载均衡器,负责接收入站或出站的HTTP/TCP连接
    • 主要定义应该暴露到网格外部的端口、要使用的协议类型、以及SNI配置等
  • Gateway的定义主要通过如下两个关键字段
    • selector:Pod标签选择器,用于指定当前Gateway配置要附加到的Ingress Gateway Pod实例
      • Pod标签选择器,负责在为Istio部署的一到多个Ingress Gateway实例中完成Pod筛选
      • 仅符合选择器条件的Ingress Gateway实例才会添加该Gateway资源中定义的配置
    • server:开放的服务列表,即服务的访问入口,可通过port、hosts、defaultEndpoints和tls来定义;
      • port:服务对外发布的端口,即用于接收请求的端口;
      • hosts:Gateway发布的服务地址,通常是一个FQDN格式的域名,支持使用*通配符;
      • defaultEndpoint:默认后端;
      • tls:发布为HTTPS协议服务时与TLS相关的配置
  • 提示:Gateway资源仅定义了要暴露的访问入口,但流量接入到网格内部之后的路由机制,仍然需要由VirtualService资源进行定义;

image-20240801100735922

Gateway #

1、hosts字段不接受非FQDN格式的字符串,但可以使 用“*”通配符

2、gateway资源应该定义在目标ingressgateway Pod运行在名称空间

image-20240801100755681

Gateway配置示例 #

示例一 #

apiVersion: networking.istio.io/v1beta1
kind: Gateway
metadata:
  name: my-gateway
  namespace: some-config-namespace
spec:
  selector:
    app: my-gateway-controller
  servers:
  - port:
      number: 80
      name: http
      protocol: HTTP
    hosts:
    - uk.bookinfo.com
    - eu.bookinfo.com
    tls:
      httpsRedirect: true # sends 301 redirect for http requests
  - port:
      number: 443
      name: https-443
      protocol: HTTPS
    hosts:
    - uk.bookinfo.com
    - eu.bookinfo.com
    tls:
      mode: SIMPLE # enables HTTPS on this port
      serverCertificate: /etc/certs/servercert.pem
      privateKey: /etc/certs/privatekey.pem
  - port:
      number: 9443
      name: https-9443
      protocol: HTTPS
    hosts:
    - "bookinfo-namespace/*.bookinfo.com"
    tls:
      mode: SIMPLE # enables HTTPS on this port
      credentialName: bookinfo-secret # fetches certs from Kubernetes secret
  - port:
      number: 9080
      name: http-wildcard
      protocol: HTTP
    hosts:
    - "*"
  - port:
      number: 2379 # to expose internal service via external port 2379
      name: mongo
      protocol: MONGO
    hosts:
    - "*"
apiVersion: networking.istio.io/v1beta1
kind: VirtualService
metadata:
  name: bookinfo-rule
  namespace: bookinfo-namespace
spec:
  hosts:
  - reviews.prod.svc.cluster.local
  - uk.bookinfo.com
  - eu.bookinfo.com
  gateways:
  - some-config-namespace/my-gateway
  - mesh # applies to all the sidecars in the mesh
  http:
  - match:
    - headers:
        cookie:
          exact: "user=dev-123"
    route:
    - destination:
        port:
          number: 7777
        host: reviews.qa.svc.cluster.local
  - match:
    - uri:
        prefix: /reviews/
    route:
    - destination:
        port:
          number: 9080 # can be omitted if it's the only port for reviews
        host: reviews.prod.svc.cluster.local
      weight: 80
    - destination:
        host: reviews.qa.svc.cluster.local
      weight: 20
apiVersion: networking.istio.io/v1beta1
kind: VirtualService
metadata:
  name: bookinfo-mongo
  namespace: bookinfo-namespace
spec:
  hosts:
  - mongosvr.prod.svc.cluster.local # name of internal Mongo service
  gateways:
  - some-config-namespace/my-gateway # can omit the namespace if gateway is in same namespace as virtual service.
  tcp:
  - match:
    - port: 27017
    route:
    - destination:
        host: mongo.prod.svc.cluster.local
        port:
          number: 5555

示例二 #

apiVersion: networking.istio.io/v1beta1
kind: Gateway
metadata:
  name: my-gateway
  namespace: some-config-namespace
spec:
  selector:
    app: my-gateway-controller
  servers:
  - port:
      number: 80
      name: http
      protocol: HTTP
    hosts:
    - "ns1/*"
    - "ns2/foo.bar.com"

参考文档 #

https://istio.io/latest/zh/docs/reference/config/networking/gateway/

通过例子来理解 #

image-20240801100855454

apiVersion: networking.istio.io/v1alpha3
kind: Gateway
metadata: 
  name: nginx-gw
spec:
  selector:
    app: istio-ingressgateway
  servers:
  - port:
      number: 80
      name: http
      protocol: HTTP
    hosts:
    - nginx.test.com
---
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: nginx-vs
spec:
  hosts:
  - nginx.test.com
  gateways:
  - nginx-gw
  http:
  - route:
    - destination:
        host: nginx-svc
---
apiVersion: v1
kind: Service
metadata:
  name: nginx-svc
spec:
  ports:
  - port: 80
    protocol: TCP
    targetPort: 80
  selector:
    app: nginx
  type: ClusterIP
---
apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: nginx
  name: nginx-deployment
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nginx
  strategy:
    rollingUpdate:
      maxSurge: 25%
      maxUnavailable: 25%
    type: RollingUpdate
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
        - image: 'nginx:latest'
          name: nginx-deployment

通过命令访问 curl -H “Host: nginx.gateway.comhttp://ingressgateway:nodeport/

  • istio-ingressgateway 就是小区的大门(唯一的大门),所有进入的流量都需要经过,
  • ingressgateway 相当于路标引导去到A B C D的一栋建筑里面,分开域名去导流,
  • virtualservice 就像到建筑里的电梯一样,按照不同的楼层进行管理路由的作用,
  • destinationrule 到达具体的楼层后按照不同的门房号 1 2 3 4 进入到真正的屋里去。

Istio 调整sidecar镜像仓库 #

# Configmap istio-system.istio-sidecar-injector
# 无需重启任务服务, 直接生效

    "hub": "docker.io/istio",
改为
    "hub": "私有镜像仓库/istio",

下载镜像会从:
docker.io/istio/proxyv2:1.15.2
变为
私有镜像仓库/istio/proxyv2:1.15.2