【问题标题】:K8s network policy restrict egress for one endpoint onlyK8s 网络策略仅限制一个端点的出口
【发布时间】:2022-02-27 17:20:32
【问题描述】:

我需要为我的 pod 创建一个NetwrokPolicy,从网络方面需要 访问集群外仅一个特定端点,仅此端点。

端点如下所示。 https://140.224.232.236:8088

在应用以下网络策略之前,我已对 pod(图像基于 alpine)执行并运行 ping www.google.com,它按预期工作,

但是,当我应用网络策略时,我尝试ping google.com 我得到了 ping: bad address 'www.google.com',但是当我 ping 到 ip 时就像

  1. ping 140.224.232.236
  2. ping 140.224.232.236:8080

它卡住了,在我看到这样的东西之前

64 bytes from 140.224.232.236: seq=518 ttl=245 time=137.603 ms
64 bytes from 140.224.232.236: seq=519 ttl=245 time=137.411 ms
64 bytes from 140.224.232.236: seq=520 ttl=245 time=137.279 ms
64 bytes from 140.224.232.236: seq=521 ttl=245 time=137.138 ms
....

现在它只是停留在这个问题上,有什么想法吗?

ping 140.224.232.236
PING 140.224.232.236 (140.224.232.236): 56 data bytes

仅此而已,这是什么意思?

我做的是以下

apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
    name: test-network-policy
    namespace: dev
spec:
    podSelector:
      matchLabels:
        app.kubernetes.io/name: foo
  policyTypes:
  - Egress
  egress:
  - to:
    - ipBlock:
        cidr: 140.224.232.236/32
    ports:
    - protocol: TCP
      port: 8088 
  1. 我已将它应用到 pod 的同一个 ns 上。
  2. pod 选择器标签的名称取自我要应用网络策略的 pod,并具有以下内容。 app.kubernetes.io/name: foo

【问题讨论】:

  • 您遇到了什么问题?顺便说一句,你看到 thisthis 了吗?
  • 我在端口 8088 上的实际端点不起作用? Ping 使用 ICMP 而不是 TCP,并且不允许使用此网络策略。

标签: amazon-web-services azure security kubernetes


【解决方案1】:

正如您使用端口定义的出口:

ports:
- protocol: TCP
  port: 8080

这意味着允许在 TCP 端口 8080 上连接到 ip 140.224.232.236。

但是,ping(ICMP) 不知道端口。每次 Ping 都会失败。

  1. ping 是一个网络实用程序,用于测试远程主机通过 Internet 协议 (IP) 的可达性。 ping 实用程序通过向远程主机发送一系列 Internet 控制消息协议 (ICMP) 回显请求数据包来实现此目的。

  2. 您无法使用 ping 命令探测特定端口,因为 ICMP 属于 第 3 层 IP 层,而不是第 4 层传输层(例如,TCP/UDP)。

  3. 为了 ping 远程主机的特定端口,您需要使用具有端口号概念的 4 层传输协议,以及许多命令行工具,例如:

【讨论】:

    【解决方案2】:

    您允许出口流量通过 TCP 传输到端口 8088,而不是 ICMP。 Ping 不适用于应用此功能。

    如果您想允许非 TPC 或 UDP 协议使用 vanilla 的 Kubernetes API 进行网络策略,那么您需要省略端口定义

    您也可以使用Calico 的网络策略 API 来解决此问题。

    这为在集群中配置网络策略提供了更大的灵活性。

    【讨论】:

      【解决方案3】:

      您的网络政策是完美的。您不能使用 ping 测试您的 TCP 端口,因为 ping 与 ICMP 协议一起使用。

      有一些方法可以在不更改网络策略的情况下测试您的配置。

      例如:-

      1. 执行到 pod 并运行 one liner。您应该得到“端口已打开”
      $  </dev/tcp/140.224.232.236/8088 && echo Port is open || echo Port is closed
      Port is open
      
      
      /dev/tcp/host/port
          If host is a valid hostname or Internet address, and port is an integer port number
          or service name, bash attempts to open a TCP connection to the corresponding socket.
      /dev/udp/host/port
          If host is a valid hostname or Internet address, and port is an integer port number
          or service name, bash attempts to open a UDP connection to the corresponding socket.
      
      1. 如果安装了curl/wget,则通过对 pod 执行 exec 来运行 cur/wget
      curl -k https://140.224.232.236:8088
      wget --no-check-certificate https://140.224.232.236:8088
      
      1. 如果存在网络工具nc,则执行到 pod 并运行此命令,如果返回 '0',则它可以工作。
      nc 140.224.232.236 8088 &> /dev/null; echo $?
      0
      

      或以下命令应返回“成功”

      nc -zv 140.224.232.236 8088 
      Connection to 140.224.232.236 8088 port [tcp/*] succeeded!
      

      【讨论】:

        猜你喜欢
        • 2020-01-01
        • 1970-01-01
        • 2021-09-14
        • 2021-12-02
        • 2021-04-10
        • 2021-12-16
        • 2019-01-21
        • 2019-02-04
        • 1970-01-01
        相关资源
        最近更新 更多