• 第四章 Istio出口流量管理


    访问外部服务

    为了更好的做好网络访问控制,k8s结合Istio出口网络升级示意图
    在这里插入图片描述

    来自 Istio 的 pod 的所有出站流量都会重定向到其 sidecar 代理,因此集群外部 URL 的可访问性取决于代理的配置。默认情况下,Istio 配置 Envoy 代理来传递未知服务的请求。
    一般来说服务访问网格外部服务默认放行,但是如果想要严格管控网格流量,需要对出方向进行管控。

    这里展示三种不同的方式访问外部服务:

    • 允许 Envoy 代理将请求传递给网格外服务。
    • 配置服务条目以提供对外部服务的受控访问。
    • 直接访问外部服务。

    Envoy 代理将请求传递给网格外服务

    Istio 有一个配置: meshConfig.outboundTrafficPolicy.mode用于配置外部服务的 sidecar 处理,如果此选项设置为ALLOW_ANY,Istio 代理将允许对未知服务的调用通过。如果该选项设置为REGISTRY_ONLY,则 Istio 代理会阻止任何没有 HTTP 服务或网格内定义的服务条目的主机。 ALLOW_ANY是默认值,目的是允许快速开始使用 Istio,而无需控制对外部服务的访问。

    配置服务条目以提供对外部服务的受控访问

    Service Entry(服务条目)允许在Istio的内部服务注册表中添加额外的服务记录,以便网格中自动发现的服务可以访问或路由到这些手动指定的服务。如果我们想要控制出口网络,必须先要设置ServiceEntry
    正常情况我们对外访问先设置为ALLOW_ANY。这样,就可以开始在某些外部服务上使用 Istio 功能,而不会阻止任何其他服务。配置完所有服务条目后,可以将模式切换为REGISTRY_ONLY阻止任何其他无意/恶意的访问。

    访问外部 HTTP 服务

    1. 创建一个ServiceEntry以允许访问外部 HTTP 服务(httpbin-ext.yml)
    apiVersion: networking.istio.io/v1alpha3
    kind: ServiceEntry
    metadata:
      name: httpbin-ext
    spec:
      hosts:
      - httpbin.org
      ports:
      - number: 80
        name: http
        protocol: HTTP
      resolution: DNS
      location: MESH_EXTERNAL
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    网格内服务访问httpbin.org时,Sidecar 代理将忽略原始目标 IP 地址并将流量定向到httpbin.org,通过DNS解析,执行 DNS 查询以获取httpbin.org 真实IP 地址。
    2. 从k8s内部向外部 HTTP 服务发出请求:

    $ kubectl exec "$SOURCE_POD" -c sleep -- curl -sS http://httpbin.org/headers
    {
      "headers": {
        "Accept": "*/*",
        "Host": "httpbin.org",
        ...
        "X-Envoy-Decorator-Operation": "httpbin.org:80/*",
        ...
      }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    请注意 Istio sidecar 代理添加的标头:X-Envoy-Decorator-Operation。
    3. 检查 sidecar 代理的日志:

    $ kubectl logs "$SOURCE_POD" -c istio-proxy | tail
    [2019-01-24T12:17:11.640Z] "GET /headers HTTP/1.1" 200 - 0 599 214 214 "-" "curl/7.60.0" "17fde8f7-fa62-9b39-8999-302324e6def2" "httpbin.org" "35.173.6.94:80" outbound|80||httpbin.org - 35.173.6.94:80 172.30.109.82:55314 -
    
    • 1
    • 2

    请注意 HTTP 请求相关的条目httpbin.org/headers。

    直接访问外部服务

    与Envoy 直通外部服务(使用ALLOW_ANY流量策略指示 Istio sidecar 代理直通未知服务的调用)不同,这种方法完全绕过了 sidecar,实质上禁用了指定 IP 的所有 Istio 功能。无法像使用该ALLOW_ANY方法那样逐步添加特定目的地的服务条目。因此,仅当由于性能或其他原因无法使用 sidecar 配置外部访问时,才建议将此配置方法作为最后的手段。

    简单方法是将global.proxy.includeIPRanges配置选项设置为用于内部集群服务的一个或多个 IP 范围。这些 IP 范围值取决于集群运行的平台。允许这些IP直接访问外部服务
    --set values.global.proxy.includeIPRanges="172.16.0.0/16"

    要以更安全的方式实施出口流量控制,必须通过出口网关引导出口流量,并查看其他注意事项

    出口网关

    Istio 使用入口和出口网关 来配置在服务网格边缘执行的负载均衡器。入口网关允许定义所有传入流量流经的网格的入口点。出口网关是一个对称的概念;它定义了网格的出口点。出口网关允许 Istio 功能(例如监控和路由规则)应用于网格出口流量。

    1. 检查Istio出口网关是否部署
    $ kubectl get pod -l istio=egressgateway -n istio-system
    
    • 1

    如果没有返回 Pod,请部署 Istio 出口网关。(由于各家云厂商有自己的构建方法,此处不做介绍)
    2. 创建ServiceEntry
    假设我们还是使用上文的httpbin-ext配置
    3. Gateway为httpbin.org创建出口、端口 80,并为定向到出口网关的流量创建目标规则。

    apiVersion: networking.istio.io/v1alpha3
    kind: Gateway
    metadata:
      name: istio-egressgateway
    spec:
      selector:
        istio: egressgateway
      servers:
      - port:
          number: 80
          name: http
          protocol: HTTP
        hosts:
        - httpbin.org
    ---
    apiVersion: networking.istio.io/v1alpha3
    kind: DestinationRule
    metadata:
      name: egressgateway-for-httpbin
    spec:
      host: istio-egressgateway.istio-system.svc.cluster.local
      subsets:
      - name: httpbin
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    1. 定义 VirtualService将流量从 sidecar 引导至出口网关,以及从出口网关引导至外部服务:
    apiVersion: networking.istio.io/v1alpha3
    kind: VirtualService
    metadata:
      name: direct-httpbin-through-egress-gateway
    spec:
      hosts:
      - httpbin.org
      gateways:
      - istio-egressgateway
      - mesh
      http:
      - match:
        - gateways:
          - mesh
          port: 80
        route:
        - destination:
            host: istio-egressgateway.istio-system.svc.cluster.local
            subset: httpbin
            port:
              number: 80
          weight: 100
      - match:
        - gateways:
          - istio-egressgateway
          port: 80
        route:
        - destination:
            host: httpbin.org
            port:
              number: 80
          weight: 100
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32

    清理 HTTP 网关

    $ kubectl delete gateway istio-egressgateway
    $ kubectl delete serviceentry httpbin
    $ kubectl delete virtualservice direct-httpbin-through-egress-gateway
    $ kubectl delete destinationrule egressgateway-for-httpbin
    
    • 1
    • 2
    • 3
    • 4

    其他

    请注意,在 Istio 中定义出口Gateway本身并不为运行出口网关服务的节点提供任何特殊处理。集群管理员或云提供商应在专用节点上部署出口网关,并引入额外的安全措施以使这些节点比网格的其他节点更安全。

    Istio无法安全地强制所有出口流量实际上都流经出口网关。Istio 仅通过其 sidecar 代理启用此类流程。如果攻击者绕过sidecar代理,就可以直接访问外部服务,而无需遍历出口网关。这样,攻击者就逃脱了 Istio 的控制和监视。集群管理员或云提供商必须确保没有流量绕过出口网关离开网格。Istio 外部的机制必须强制执行此要求。例如,集群管理员可以配置防火墙来拒绝所有非来自出口网关的流量。Kubernetes网络策略还可以禁止所有非源自出口网关的出口流量(请参阅 下一节的示例)。此外,集群管理员或云提供商可以配置网络,以确保应用程序节点只能通过网关访问互联网。为此,集群管理员或云提供商可以阻止将公共 IP 分配给网关以外的 Pod,并可以配置 NAT 设备以丢弃并非源自出口网关的数据包。

  • 相关阅读:
    GDP-L-岩藻糖,鸟苷二磷酰岩藻糖,Guanosine 5′-diphospho-β-L-fucose sodium salt
    dhtmlx甘特图marker不随小时移动
    Java之SpringCloud Alibaba【八】【Spring Cloud微服务Gateway整合sentinel限流】
    Rocky Linux 工作流编排和混合云解决方案一站式资源
    甘特图怎么做项目进度计划
    38. UE5 RPG 修改火球术的攻击方向以及按住Shift攻击
    【LeetCode】208.实现Trie(前缀树)
    【JavaSe笔记】——类和对象、this引用、构造方法、封装、static、代码块
    机器学习模型太慢?来看看英特尔(R) 扩展加速 ⛵
    【算法】折半查找
  • 原文地址:https://blog.csdn.net/qq_36464836/article/details/133889129