Wildcard 主机的 egress

    假定您想要为 Istio 中所有语种的 wikipedia.org 站点开启 egress 流量。每个语种的 wikipedia.org 站点均有自己的主机名,譬如:英语和德语对应的主机分别为 en.wikipedia.orgde.rikipedia.org。您希望通过通用配置项开启所有 Wikipedia 站点的 egress 流量,无需单独配置每个语种的站点。

    开始之前

    • 按照中的说明安装 Istio。

    • 启动 sleep 示例,以获取发送请求的测试源。如果您启用了,运行以下命令部署示例应用程序:

    Zip

    否则,在使用以下命令部署 sleep 应用程序之前,手动注入 sidecar:

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

    您可以使用任何安装了 curl 的 pod 作为测试源。

    • 为了发送请求,您需要创建 SOURCE_POD 环境变量来存储源 pod 的名称:
    1. $ export SOURCE_POD=$(kubectl get pod -l app=sleep -o jsonpath={.items..metadata.name})

    访问通用域中一组主机的第一个也是最简单的方法,是使用一个 wildcard 主机配置一个简单的 ServiceEntry,直接从 sidecar 调用服务。当直接调用服务时(譬如:不是通过一个 egress 网关),一个 wildcard 主机的配置与任何其他主机(如:全域名主机)没有什么不同,只是当通用域中有许多台主机时,这样比较方便。

    • *.wikipedia.org 定义一个 ServiceEntry 以及相应的 VirtualSevice
    1. $ kubectl apply -f - <<EOF
    2. apiVersion: networking.istio.io/v1alpha3
    3. kind: ServiceEntry
    4. metadata:
    5. name: wikipedia
    6. spec:
    7. hosts:
    8. - "*.wikipedia.org"
    9. ports:
    10. - number: 443
    11. name: tls
    12. protocol: TLS
    13. ---
    14. apiVersion: networking.istio.io/v1alpha3
    15. kind: VirtualService
    16. metadata:
    17. name: wikipedia
    18. spec:
    19. hosts:
    20. - "*.wikipedia.org"
    21. tls:
    22. - match:
    23. - port: 443
    24. sni_hosts:
    25. - "*.wikipedia.org"
    26. route:
    27. - destination:
    28. host: "*.wikipedia.org"
    29. port:
    30. number: 443
    31. EOF
    1. $ kubectl exec -it $SOURCE_POD -c sleep -- sh -c 'curl -s https://en.wikipedia.org/wiki/Main_Page | grep -o "<title>.*</title>"; curl -s https://de.wikipedia.org/wiki/Wikipedia:Hauptseite | grep -o "<title>.*</title>"'
    2. <title>Wikipedia, the free encyclopedia</title>
    3. <title>Wikipedia Die freie Enzyklopädie</title>
    1. $ kubectl delete serviceentry wikipedia
    2. $ kubectl delete virtualservice wikipedia

    配置访问 wildcard 主机的 egress 网关

    能否配置通过 egress 网关访问 wildcard 主机取决于这组 wildcard 域名有唯一一个通用主机。以 *.wikipedia.org 为例。每个语种特殊的站点都有自己的 wikipedia.org 服务器。您可以向任意一个 *.wikipedia.org 站点的 IP 发送请求,包括 www.wikipedia.org,该站点所有特定主机。

    通常情况下,通用域中的所有域名并不由一个唯一的 hosting server 提供服务。此时,需要一个更加复杂的配置。

    • *.wikipedia.org 创建一个 egress Gateway、一个目标规则以及一个虚拟服务,来引导请求通过 egress 网关并从 egress 网关访问外部服务。
    1. $ kubectl apply -f - <<EOF
    2. apiVersion: networking.istio.io/v1alpha3
    3. kind: Gateway
    4. metadata:
    5. name: istio-egressgateway
    6. spec:
    7. selector:
    8. istio: egressgateway
    9. servers:
    10. - port:
    11. number: 443
    12. name: tls
    13. protocol: TLS
    14. hosts:
    15. - "*.wikipedia.org"
    16. tls:
    17. mode: PASSTHROUGH
    18. ---
    19. apiVersion: networking.istio.io/v1alpha3
    20. kind: DestinationRule
    21. metadata:
    22. name: egressgateway-for-wikipedia
    23. spec:
    24. host: istio-egressgateway.istio-system.svc.cluster.local
    25. subsets:
    26. - name: wikipedia
    27. ---
    28. apiVersion: networking.istio.io/v1alpha3
    29. kind: VirtualService
    30. metadata:
    31. name: direct-wikipedia-through-egress-gateway
    32. spec:
    33. hosts:
    34. - "*.wikipedia.org"
    35. gateways:
    36. - mesh
    37. - istio-egressgateway
    38. tls:
    39. - match:
    40. - gateways:
    41. - mesh
    42. port: 443
    43. sni_hosts:
    44. - "*.wikipedia.org"
    45. route:
    46. - destination:
    47. host: istio-egressgateway.istio-system.svc.cluster.local
    48. subset: wikipedia
    49. port:
    50. number: 443
    51. weight: 100
    52. - match:
    53. - gateways:
    54. - istio-egressgateway
    55. port: 443
    56. sni_hosts:
    57. - "*.wikipedia.org"
    58. route:
    59. - destination:
    60. host: www.wikipedia.org
    61. number: 443
    62. weight: 100
    63. EOF
    • 为目标服务器 www.wikipedia.org 创建一个 ServiceEntry
    1. $ kubectl apply -f - <<EOF
    2. apiVersion: networking.istio.io/v1alpha3
    3. kind: ServiceEntry
    4. metadata:
    5. spec:
    6. hosts:
    7. - www.wikipedia.org
    8. ports:
    9. - number: 443
    10. name: tls
    11. protocol: TLS
    12. resolution: DNS
    13. EOF
    1. $ kubectl exec -it $SOURCE_POD -c sleep -- sh -c 'curl -s https://en.wikipedia.org/wiki/Main_Page | grep -o "<title>.*</title>"; curl -s https://de.wikipedia.org/wiki/Wikipedia:Hauptseite | grep -o "<title>.*</title>"'
    2. <title>Wikipedia, the free encyclopedia</title>
    3. <title>Wikipedia Die freie Enzyklopädie</title>
    • 检查 egress 网关代理访问 *.wikipedia.org 的计数器统计值。如果 Istio 部署在 istio-system 命名空间中,打印输出计数器的命令为:

    清除单点服务器的 wildcard 配置

    1. $ kubectl delete serviceentry www-wikipedia
    2. $ kubectl delete gateway istio-egressgateway
    3. $ kubectl delete virtualservice direct-wikipedia-through-egress-gateway
    4. $ kubectl delete destinationrule egressgateway-for-wikipedia

    前面章节的配置生效,是因为 *.wikipedia.org 站点可以由任意一个 wikipedia.org 服务器提供服务。然而,情况并不总是如此。譬如,你可能想要配置 egress 控制到更为通用的 wildcard 域,如:.com.org

    配置流量流向任意 wildcard 域,为 Istio 网关引入一个挑战。在前面的章节中,您在配置中,将 www.wikipedia.org 配置为网关的路由目标主机,将流量导向该地址。然而,网关并不知道它接收到的请求中任意 arbitrary 主机的 IP 地址。这是 Istio 默认的 egress 网关代理 的限制。Envoy 或者将流量路由至提前定义好的主机,或者路由至提前定义好的 IP 地址,或者是请求的最初 IP 地址。在网关案例中,请求的最初目标 IP 被丢弃,因为请求首先路由至 egress 网关,其目标 IP 为网关的 IP 地址。

    因此,基于 Envoy 的 Istio 网关无法将流量路由至没有预先配置的 arbitrary 主机,从而,无法对任意 wildcard 域实施流量控制。为了对 HTTPS 和任意 TLS 连接开启流量控制,您需要在 Envoy 的基础上再部署一个 SNI 转发代理。Envoy 将访问 wildcard 域的请求路由至 SNI 转发代理,代理反过来将请求转发给 SNI 值中约定的目标地址。

    带 SNI 代理的 egress 网关,以及相关的 Istio 架构部分如下图所示:

    Egress Gateway with SNI proxy

    如下章节向您展示如何重新部署带 SNI 代理的 egress 网关,并配置 Istio 通过网关将 HTTPS 请求导向任意 wildcard 域。

    安装带 SNI 代理的 egress 网关

    本章节,除了标准的 Istio Envoy 代理,您将再部署一个带 SNI 代理的 egress 网关。本示例使用 Nginx 作为 SNI 代理。任何一个能够根据任意的、非预先配置的 SNI 值路由流量的 SNI 代理均可。SNI 代理将监听在端口 8443 上,您可以绑定任意其它端口,egress GatewayVirtualServices 中配置的端口除外。SNI 代理将流量转发至端口 443

    • 创建一个 Nginx SNI 代理的配置文件。您可以按需编辑文件指定附加的 Nginx 设置。注意 serverlisten 原语指定端口为 8443,其 proxy_pass 原语使用 ssl_preread_server_name 端口为 443ssl_prereadon 以开启 SNI 读。
    1. $ cat <<EOF > ./sni-proxy.conf
    2. user www-data;
    3. events {
    4. }
    5. stream {
    6. log_format log_stream '\$remote_addr [\$time_local] \$protocol [\$ssl_preread_server_name]'
    7. '\$status \$bytes_sent \$bytes_received \$session_time';
    8. access_log /var/log/nginx/access.log log_stream;
    9. error_log /var/log/nginx/error.log;
    10. # tcp forward proxy by SNI
    11. server {
    12. resolver 8.8.8.8 ipv6=off;
    13. listen 127.0.0.1:8443;
    14. proxy_pass \$ssl_preread_server_name:443;
    15. ssl_preread on;
    16. }
    17. }
    18. EOF
    • 创建一个 Kubernetes 来保存 Nginx SNI 代理的配置文件:
    1. $ kubectl create configmap egress-sni-proxy-configmap -n istio-system --from-file=nginx.conf=./sni-proxy.conf
    • 下面的命令将生成 istio-egressgateway-with-sni-proxy.yaml,您可以选择性编辑该配置文件然后部署。
    1. $ cat <<EOF | istioctl manifest generate --set values.global.istioNamespace=istio-system -f - > ./istio-egressgateway-with-sni-proxy.yaml
    2. gateways:
    3. enabled: true
    4. istio-ingressgateway:
    5. enabled: false
    6. istio-egressgateway:
    7. enabled: false
    8. istio-egressgateway-with-sni-proxy:
    9. enabled: true
    10. labels:
    11. app: istio-egressgateway-with-sni-proxy
    12. istio: egressgateway-with-sni-proxy
    13. replicaCount: 1
    14. autoscaleMin: 1
    15. autoscaleMax: 5
    16. cpu:
    17. targetAverageUtilization: 80
    18. serviceAnnotations: {}
    19. type: ClusterIP
    20. ports:
    21. - port: 443
    22. name: https
    23. secretVolumes:
    24. - name: egressgateway-certs
    25. secretName: istio-egressgateway-certs
    26. mountPath: /etc/istio/egressgateway-certs
    27. - name: egressgateway-ca-certs
    28. secretName: istio-egressgateway-ca-certs
    29. mountPath: /etc/istio/egressgateway-ca-certs
    30. configVolumes:
    31. - name: sni-proxy-config
    32. configMapName: egress-sni-proxy-configmap
    33. additionalContainers:
    34. - name: sni-proxy
    35. image: nginx
    36. volumeMounts:
    37. - name: sni-proxy-config
    38. mountPath: /etc/nginx
    39. readOnly: true
    40. EOF
    • 部署新的 egress 网关:
    1. $ kubectl apply -f ./istio-egressgateway-with-sni-proxy.yaml
    2. serviceaccount "istio-egressgateway-with-sni-proxy-service-account" created
    3. role "istio-egressgateway-with-sni-proxy-istio-system" created
    4. rolebinding "istio-egressgateway-with-sni-proxy-istio-system" created
    5. service "istio-egressgateway-with-sni-proxy" created
    6. deployment "istio-egressgateway-with-sni-proxy" created
    7. horizontalpodautoscaler "istio-egressgateway-with-sni-proxy" created
    • 验证新的 egress 网关正在运行。注意 pod 有两个容器(一个是 Envoy 代理,另一个是 SNI 代理)。
    1. $ kubectl get pod -l istio=egressgateway-with-sni-proxy -n istio-system
    2. NAME READY STATUS RESTARTS AGE
    3. istio-egressgateway-with-sni-proxy-79f6744569-pf9t2 2/2 Running 0 17s
    • 创建一个 service entry,静态地址为 127.0.0.1 (localhost),关闭发送至新 service entry 的双向 TLS 请求。
    1. $ kubectl apply -f - <<EOF
    2. apiVersion: networking.istio.io/v1alpha3
    3. kind: ServiceEntry
    4. metadata:
    5. name: sni-proxy
    6. spec:
    7. hosts:
    8. - sni-proxy.local
    9. location: MESH_EXTERNAL
    10. ports:
    11. - number: 8443
    12. name: tcp
    13. endpoints:
    14. - address: 127.0.0.1
    15. ---
    16. apiVersion: networking.istio.io/v1alpha3
    17. kind: DestinationRule
    18. metadata:
    19. name: disable-mtls-for-sni-proxy
    20. spec:
    21. host: sni-proxy.local
    22. trafficPolicy:
    23. tls:
    24. mode: DISABLE
    25. EOF

    配置流量流经包含 SNI 代理的 egress 网关

    • *.wikipedia.org 定义一个 ServiceEntry
    1. $ cat <<EOF | kubectl create -f -
    2. apiVersion: networking.istio.io/v1alpha3
    3. kind: ServiceEntry
    4. metadata:
    5. name: wikipedia
    6. spec:
    7. hosts:
    8. - "*.wikipedia.org"
    9. ports:
    10. - number: 443
    11. name: tls
    12. protocol: TLS
    13. EOF
    • *.wikipedia.org 创建一个 egress Gateway,端口 443,协议 TLS,以及一个虚拟服务负责引导目标为 *.wikipedia.org 的流量流经网关。

    根据您是否希望在源 pod 与 egress 网关之间开启,选择指令。

    您可能希望开启双向 TLS 以使得 egress 网关得以监控源 pods 的身份标识并基于身份标识信息启用 Mixer 的强制策略。

    1. $ kubectl apply -f - <<EOF
    2. apiVersion: networking.istio.io/v1alpha3
    3. kind: Gateway
    4. metadata:
    5. name: istio-egressgateway-with-sni-proxy
    6. spec:
    7. selector:
    8. istio: egressgateway-with-sni-proxy
    9. servers:
    10. - port:
    11. number: 443
    12. name: tls
    13. protocol: TLS
    14. hosts:
    15. - "*.wikipedia.org"
    16. tls:
    17. mode: PASSTHROUGH
    18. ---
    19. apiVersion: networking.istio.io/v1alpha3
    20. kind: DestinationRule
    21. metadata:
    22. name: egressgateway-for-wikipedia
    23. spec:
    24. host: istio-egressgateway-with-sni-proxy.istio-system.svc.cluster.local
    25. subsets:
    26. - name: wikipedia
    27. ---
    28. apiVersion: networking.istio.io/v1alpha3
    29. kind: VirtualService
    30. metadata:
    31. name: direct-wikipedia-through-egress-gateway
    32. spec:
    33. hosts:
    34. - "*.wikipedia.org"
    35. gateways:
    36. - mesh
    37. - istio-egressgateway-with-sni-proxy
    38. tls:
    39. - match:
    40. - gateways:
    41. - mesh
    42. port: 443
    43. sni_hosts:
    44. - "*.wikipedia.org"
    45. route:
    46. - destination:
    47. host: istio-egressgateway-with-sni-proxy.istio-system.svc.cluster.local
    48. subset: wikipedia
    49. port:
    50. number: 443
    51. weight: 100
    52. - match:
    53. - gateways:
    54. - istio-egressgateway-with-sni-proxy
    55. port: 443
    56. sni_hosts:
    57. - "*.wikipedia.org"
    58. route:
    59. - destination:
    60. host: sni-proxy.local
    61. port:
    62. number: 8443
    63. weight: 100
    64. EOF
    1. $ kubectl exec -it $SOURCE_POD -c sleep -- sh -c 'curl -s https://en.wikipedia.org/wiki/Main_Page | grep -o "<title>.*</title>"; curl -s https://de.wikipedia.org/wiki/Wikipedia:Hauptseite | grep -o "<title>.*</title>"'
    2. <title>Wikipedia, the free encyclopedia</title>
    3. <title>Wikipedia Die freie Enzyklopädie</title>
    • 检查 Egress 网关 Envoy 代理的日志。如果 Istio 被部署在 istio-system 命名空间中,打印日志的命令为:
    1. $ kubectl logs -l istio=egressgateway-with-sni-proxy -c istio-proxy -n istio-system

    您将看到类似如下的内容:

    1. [2019-01-02T16:34:23.312Z] "- - -" 0 - 578 79141 624 - "-" "-" "-" "-" "127.0.0.1:8443" outbound|8443||sni-proxy.local 127.0.0.1:55018 172.30.109.84:443 172.30.109.112:45346 en.wikipedia.org
    2. [2019-01-02T16:34:24.079Z] "- - -" 0 - 586 65770 638 - "-" "-" "-" "-" "127.0.0.1:8443" outbound|8443||sni-proxy.local 127.0.0.1:55034 172.30.109.84:443 172.30.109.112:45362 de.wikipedia.org
    • 检查 SNI 代理的日志。如果 Istio 被部署在 istio-system 命名空间中,打印日志的命令为:
    1. $ kubectl logs -l istio=egressgateway-with-sni-proxy -n istio-system -c sni-proxy
    2. 127.0.0.1 [01/Aug/2018:15:32:02 +0000] TCP [en.wikipedia.org]200 81513 280 0.600
    3. 127.0.0.1 [01/Aug/2018:15:32:03 +0000] TCP [de.wikipedia.org]200 67745 291 0.659
    • 检查 mixer 日志。如果 Istio 部署在 istio-system 命名空间中,打印日志的命令为:
    1. $ kubectl -n istio-system logs -l istio-mixer-type=telemetry -c mixer | grep '"connectionEvent":"open"' | grep '"sourceName":"istio-egressgateway' | grep 'wikipedia.org'
    2. {"level":"info","time":"2018-08-26T16:16:34.784571Z","instance":"tcpaccesslog.logentry.istio-system","connectionDuration":"0s","connectionEvent":"open","connection_security_policy":"unknown","destinationApp":"","destinationIp":"127.0.0.1","destinationName":"unknown","destinationNamespace":"default","destinationOwner":"unknown","destinationPrincipal":"cluster.local/ns/istio-system/sa/istio-egressgateway-with-sni-proxy-service-account","destinationServiceHost":"","destinationWorkload":"unknown","protocol":"tcp","receivedBytes":298,"reporter":"source","requestedServerName":"en.wikipedia.org","sentBytes":0,"sourceApp":"istio-egressgateway-with-sni-proxy","sourceIp":"172.30.146.88","sourceName":"istio-egressgateway-with-sni-proxy-7c4f7868fb-rc8pr","sourceNamespace":"istio-system","sourceOwner":"kubernetes://apis/extensions/v1beta1/namespaces/istio-system/deployments/istio-egressgateway-with-sni-proxy","sourcePrincipal":"cluster.local/ns/sleep/sa/default","sourceWorkload":"istio-egressgateway-with-sni-proxy","totalReceivedBytes":298,"totalSentBytes":0}

    注意属性 requestedServerName

    清除任意域的 wildcard 配置

    • 删除 *.wikipedia.org 的配置项:
    1. $ kubectl delete serviceentry wikipedia
    2. $ kubectl delete gateway istio-egressgateway-with-sni-proxy
    3. $ kubectl delete virtualservice direct-wikipedia-through-egress-gateway
    4. $ kubectl delete destinationrule egressgateway-for-wikipedia
    5. $ kubectl delete --ignore-not-found=true envoyfilter forward-downstream-sni egress-gateway-sni-verifier
    • 删除部署 egressgateway-with-sni-proxy 的配置项:
    1. $ kubectl delete serviceentry sni-proxy
    2. $ kubectl delete destinationrule disable-mtls-for-sni-proxy
    3. $ kubectl delete -f ./istio-egressgateway-with-sni-proxy.yaml
    4. $ kubectl delete configmap egress-sni-proxy-configmap -n istio-system
    • 删除您创建的配置文件:

      相关内容

      管控出口流量的备选方案比较,包括性能因素。

      Istio 中的安全管控出口流量,第二部分

      使用 Istio 的出口流量管控来阻止相关出口流量攻击。

      涉及出口流量攻击和出口流量管控要求。

      Egress gateway 性能测试

      评估加入 Egress gateway 对性能造成的影响。

      描述了一个基于 Istio 的 Bookinfo 示例的简单场景。

      HTTP Egress 流量监控和访问策略