Egress Gateway

定义 Egress gateway 并引导 HTTP 流量

首先创建一个 ServiceEntry,允许流量直接访问一个外部服务。

1、为 edition.cnn.com 定义一个 ServiceEntry

kubectl apply -f - <<EOF
apiVersion: networking.istio.io/v1alpha3
kind: ServiceEntry
metadata:
  name: cnn
spec:
  hosts:
  - edition.cnn.com
  ports:
  - number: 80
    name: http-port
    protocol: HTTP
  - number: 443
    name: https
    protocol: HTTPS
  resolution: DNS
EOF

2、发送 HTTPS 请求到 https://edition.cnn.com/politics,验证 ServiceEntry 是否已正确应用。

kubectl exec "$SOURCE_POD" -c sleep -- curl -sSL -o /dev/null -D - http://edition.cnn.com/politics
...
HTTP/1.1 301 Moved Permanently
...
location: https://edition.cnn.com/politics
...

HTTP/2 200
Content-Type: text/html; charset=utf-8
...

输出结果应该与 发起 TLS 的 Egress 流量 中的 配置对外部服务的访问 示例相同,都还没有发起 TLS。

3、为 edition.cnn.com 端口 80 创建 egress Gateway。并为指向 egress gateway 的流量创建一个 destination rule。

要通过 egress gateway 引导多个主机,您可以在 Gateway 中包含主机列表,或使用 * 匹配所有主机。 应该将 DestinationRule 中的subset 字段用于其他主机。
kubectl apply -f - <<EOF
apiVersion: networking.istio.io/v1alpha3
kind: Gateway
metadata:
  name: istio-egressgateway
spec:
  selector:
    istio: egressgateway
  servers:
  - port:
      number: 80
      name: http
      protocol: HTTP
    hosts:
    - edition.cnn.com
---
apiVersion: networking.istio.io/v1alpha3
kind: DestinationRule
metadata:
  name: egressgateway-for-cnn
spec:
  host: istio-egressgateway.istio-system.svc.cluster.local
  subsets:
  - name: cnn
EOF

4、定义一个 VirtualService,将流量从 sidecar 引导至 Egress Gateway,再从 Egress Gateway 引导至外部服务:

kubectl apply -f - <<EOF
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: direct-cnn-through-egress-gateway
spec:
  hosts:
  - edition.cnn.com
  gateways:
  - istio-egressgateway
  - mesh
  http:
  - match:
    - gateways:
      - mesh
      port: 80
    route:
    - destination:
        host: istio-egressgateway.istio-system.svc.cluster.local
        subset: cnn
        port:
          number: 80
      weight: 100
  - match:
    - gateways:
      - istio-egressgateway
      port: 80
    route:
    - destination:
        host: edition.cnn.com
        port:
          number: 80
      weight: 100
EOF

5、再次发送 HTTP 请求到 https://edition.cnn.com/politics

kubectl exec "$SOURCE_POD" -c sleep -- curl -sSL -o /dev/null -D - http://edition.cnn.com/politics
...
HTTP/1.1 301 Moved Permanently
...
location: https://edition.cnn.com/politics
...

HTTP/2 200
Content-Type: text/html; charset=utf-8
...

6、检查 istio-egressgateway pod 的日志,并查看与我们的请求对应的行。如果 Istio 部署在 istio-system 命名空间中,则打印日志的命令是:

$ kubectl logs -l istio=egressgateway -c istio-proxy -n istio-system | tail

参考:

https://preliminary.istio.io/latest/zh/docs/tasks/traffic-management/egress/egress-gateway/

原文地址:https://www.cnblogs.com/fat-girl-spring/p/15235001.html