使用外部 HTTPS 代理

配置 Egress Gateway 示例展示如何通过名为 Egress Gateway 的 Istio 组件将流量从网格引导到外部服务。但是,有些情况下需要一个外部的传统(非 Istio)HTTPS 代理来访问外部服务。例如,您的公司可能已经有了这样的代理,并且可能需要所有应用程序通过代理来引导其流量。

此示例演示如何启用对外部 HTTPS 代理的访问。由于应用程序使用 HTTP CONNECT 方法与 HTTPS 代理建立连接,因此配置流量到外部 HTTPS 代理不同于将流量配置为外部 HTTP 和 HTTPS 服务。

Before you begin

Zip

  1. $ kubectl apply -f @samples/sleep/sleep.yaml@

Otherwise, manually inject the sidecar before deploying the sleep application with the following command:

Zip

  1. $ kubectl apply -f <(istioctl kube-inject -f @samples/sleep/sleep.yaml@)

You can use any pod with curl installed as a test source.

  • Set the SOURCE_POD environment variable to the name of your source pod:
  1. $ export SOURCE_POD=$(kubectl get pod -l app=sleep -o jsonpath={.items..metadata.name})

部署 HTTPS 代理

本例中为了模拟传统代理,在集群内部署了一个 HTTPS 代理。此外,为了模拟在集群外运行的更真实的代理,通过代理的 IP 地址而不是 Kubernetes 服务的域名来寻址代理的 pod。本例使用的是 squid,但是您可以使用任何支持 HTTP CONNECT 连接的 HTTPS 代理。

  • 为 HTTPS 代理创建一个命名空间,而不标记为用于 SideCar 注入。如果没有标签,则在新命名空间中 SideCar 注入是不可用的,因此 Istio 将无法控制那里的流量。您需要在集群之外通过这种行为来模拟代理。
  1. $ kubectl create namespace external
  • 为 Squid 代理创建配置文件。
  1. $ cat <<EOF > ./proxy.conf
  2. http_port 3128
  3. acl SSL_ports port 443
  4. acl CONNECT method CONNECT
  5. http_access deny CONNECT !SSL_ports
  6. http_access allow localhost manager
  7. http_access deny manager
  8. http_access allow all
  9. coredump_dir /var/spool/squid
  10. EOF
  • 创建 Kubernetes ConfigMap 以保存代理的配置:
  1. $ kubectl create configmap proxy-configmap -n external --from-file=squid.conf=./proxy.conf
  • 使用 Squid 部署容器:
  1. $ kubectl apply -f - <<EOF
  2. apiVersion: apps/v1
  3. kind: Deployment
  4. metadata:
  5. name: squid
  6. namespace: external
  7. spec:
  8. replicas: 1
  9. template:
  10. metadata:
  11. labels:
  12. app: squid
  13. spec:
  14. volumes:
  15. - name: proxy-config
  16. configMap:
  17. name: proxy-configmap
  18. containers:
  19. - name: squid
  20. image: sameersbn/squid:3.5.27
  21. imagePullPolicy: IfNotPresent
  22. volumeMounts:
  23. - name: proxy-config
  24. mountPath: /etc/squid
  25. readOnly: true
  26. EOF
  • external 命名空间中部署 sleep 示例,以测试到代理的通信量,而不进行 Istio 流量控制。

Zip

  1. $ kubectl apply -n external -f @samples/sleep/sleep.yaml@
  • 获取代理 pod 的 IP 地址并定义 PROXY_IP 环境变量来存储它:
  1. $ export PROXY_IP=$(kubectl get pod -n external -l app=squid -o jsonpath={.items..podIP})
  • 定义 PROXY_PORT 环境变量以存储代理的端口。本例子中 Squid 使用 3128 端口。
  1. $ export PROXY_PORT=3128
  • external 命名空间中的 sleep pod 通过代理向外部服务发送请求:
  1. $ kubectl exec -it $(kubectl get pod -n external -l app=sleep -o jsonpath={.items..metadata.name}) -n external -- sh -c "HTTPS_PROXY=$PROXY_IP:$PROXY_PORT curl https://en.wikipedia.org/wiki/Main_Page" | grep -o "<title>.*</title>"
  2. <title>Wikipedia, the free encyclopedia</title>
  • 检查您请求的代理的访问日志:
  1. $ kubectl exec -it $(kubectl get pod -n external -l app=squid -o jsonpath={.items..metadata.name}) -n external -- tail -f /var/log/squid/access.log
  2. 1544160065.248 228 172.30.109.89 TCP_TUNNEL/200 87633 CONNECT en.wikipedia.org:443 - HIER_DIRECT/91.198.174.192 -

现在,您在没有 Istio 的情况下完成了以下任务:

  • 您部署了 HTTPS 代理。
  • 您使用 curl 通过代理访问 wikipedia.org 外部服务。下一步,您必须配置 Istio 启用的 pod 的流量到 HTTPS 代理。

配置流量到外部 HTTPS 代理

  • 为 HTTPS 代理定义 TCP(不是 HTTP!)服务入口。尽管应用程序使用 HTTP CONNECT 方法与 HTTPS 代理建立连接,但必须为 TCP 通信而不是 HTTP 通信配置代理。一旦建立了连接,代理就简单地充当 TCP 隧道。
  1. $ kubectl apply -f - <<EOF
  2. apiVersion: networking.istio.io/v1alpha3
  3. kind: ServiceEntry
  4. metadata:
  5. name: proxy
  6. spec:
  7. hosts:
  8. - my-company-proxy.com # ignored
  9. addresses:
  10. - $PROXY_IP/32
  11. ports:
  12. - number: $PROXY_PORT
  13. name: tcp
  14. protocol: TCP
  15. location: MESH_EXTERNAL
  16. EOF
  • external 命名空间中的 sleep pod 发送请求。因为 sleep pod 有 Sidecar,可以让 Istio 控制其流量。
  1. $ kubectl exec -it $SOURCE_POD -c sleep -- sh -c "HTTPS_PROXY=$PROXY_IP:$PROXY_PORT curl https://en.wikipedia.org/wiki/Main_Page" | grep -o "<title>.*</title>"
  2. <title>Wikipedia, the free encyclopedia</title>
  • 查看您的请求的 Istio SideCar 代理的日志:
  1. $ kubectl logs $SOURCE_POD -c istio-proxy
  2. [2018-12-07T10:38:02.841Z] "- - -" 0 - 702 87599 92 - "-" "-" "-" "-" "172.30.109.95:3128" outbound|3128||my-company-proxy.com 172.30.230.52:44478 172.30.109.95:3128 172.30.230.52:44476 -
  • 查看您请求的代理的访问日志:
  1. $ kubectl exec -it $(kubectl get pod -n external -l app=squid -o jsonpath={.items..metadata.name}) -n external -- tail -f /var/log/squid/access.log
  2. 1544160065.248 228 172.30.109.89 TCP_TUNNEL/200 87633 CONNECT en.wikipedia.org:443 - HIER_DIRECT/91.198.174.192 -

理解原理

在本例中,您采取了以下步骤:

  • 部署了一个 HTTPS 代理来模拟外部代理。
  • 创建了一个 TCP 服务入口,用作引导 Istio 控制的流量 到外部代理。请注意,您不能为通过外部代理访问的外部服务创建服务入口,例如 wikipedia.org。这是因为从 Istio 的角度来看,请求只发送到外部代理;Istio 并不知道外部代理会进一步转发请求。

清理

Zip

  1. $ kubectl delete -f @samples/sleep/sleep.yaml@
  • 关闭 external 命名空间中的 sleep 服务:

Zip

  1. $ kubectl delete -f @samples/sleep/sleep.yaml@ -n external
  • 关闭 Squid 代理,删除 ConfigMap 和配置文件:
  1. $ kubectl delete -n external deployment squid
  2. $ kubectl delete -n external configmap proxy-configmap
  3. $ rm ./proxy.conf
  • 删除 external 命名空间:
  1. $ kubectl delete namespace external
  • 删除 Service Entry:
  1. $ kubectl delete serviceentry proxy

相关内容

Secure Control of Egress Traffic in Istio, part 3

Comparison of alternative solutions to control egress traffic including performance considerations.

Secure Control of Egress Traffic in Istio, part 2

Use Istio Egress Traffic Control to prevent attacks involving egress traffic.

Secure Control of Egress Traffic in Istio, part 1

Attacks involving egress traffic and requirements for egress traffic control.

Egress Gateway Performance Investigation

Verifies the performance impact of adding an egress gateway.

Consuming External MongoDB Services

Describes a simple scenario based on Istio's Bookinfo example.

Monitoring and Access Policies for HTTP Egress Traffic

Describes how to configure Istio for monitoring and access policies of HTTP egress traffic.