• Kubernetes — 核心资源对象 — Network Policy


    目录

    Network Policy

    通过设置 Network Policy,可以允许被 Label Selector 选定的 Pods 被哪些 IP 地址访问(Ingress 入站策略),以及允许选定的 Pods 去访问哪些 IP 地址(Egress 出站)。本质上是以 App/Pod 为中心的 L3-4 ACL(访问控制策略)设计。

    Network Policy 通常与某个 Namespace 绑定,默认情况下,如果 Namespace 中不存在 Network Policy,则所有 Ingress/Egress 该 Namespace 中的 Pods 的流量都被允许。并且针对某个 App/Pod 的多条 Policies 的效果是叠加的,所以不存在顺序和冲突的情况。

    需要注意的是,Network Policy 由 CNI 支撑,所以 Network Policy 支持的能力取决于 CNI 的能力。

    一个 Network Policy 的示例:

    • 必需字段:apiVersion、 kind 和 metadata。
    • spec:NetworkPolicy Spec,包含了在一个 Namespace 中定义一个 Network Policy 所需要的所有信息。
    • podSelector:每个 NetworkPolicy 都必须包含一个 podSelector。NULL 表示选择该 Namespace 下的所有 Pods。
    • policyTypes:每个 NetworkPolicy 都必须包含一个 policyTypes 列表,可选元素为 Ingress 和 Egress。NULL 表示 Ingress。
    • ingress:每个 NetworkPolicy 可包含一个 Ingress Rule 的白名单列表。Ingress Rule 允许同时匹配 from 和 ports 字段的流量。示例中包含了一条简单的 Ingress Rule,它匹配某个特定的端口,和 3 个来源,第一个通过 ipBlock 指定,第二个通过 namespaceSelector 指定,第三个通过 podSelector 指定。
    • egress:每个 NetworkPolicy 可包含一个 Egress Rule 的白名单列表。 Egress Rule 允许同时匹配 to 和 ports 字段的流量。示例中包含一条规则,它匹配某个特定的端口上,和一个目的,通过 ipBlock 指定。
    apiVersion: networking.k8s.io/v1
    kind: NetworkPolicy
    metadata:
      name: test-network-policy
      namespace: default
    spec:
      podSelector:
        matchLabels:
          role: db
      policyTypes:
        - Ingress
        - Egress
      ingress:
        - from:
            - ipBlock:
                cidr: 172.17.0.0/16
                except:
                  - 172.17.1.0/24
            - namespaceSelector:
                matchLabels:
                  project: myproject
            - podSelector:
                matchLabels:
                  role: frontend
          ports:
            - protocol: TCP
              port: 6379
      egress:
        - to:
            - ipBlock:
                cidr: 10.0.0.0/24
          ports:
            - protocol: TCP
              port: 5978
    
    • 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
    • 33
    • 34

    Network Policy Rules

    Ingress Rule

    示例 1:为 Namespace test-ns2 创建 Network Policy default-deny Ingress,表示默认拒绝 Namespace 内所有 Pods 的 Ingress 流量。

    • podSekectir 为空,表示选择所有 Pods,即控制整个 Namespace。
    • policyTypes 只定义了 Ingress,又把 podSelector 设置为空,表示拒绝 other Namespace 的所有 Ingress 流量。
    • 没有定义 Egress,表示(默认)允许 Namespace 所有的 Pods 的 Egress 流量。
      在这里插入图片描述

    示例 2:允许 Subnet 20.10.10.0/24 里的 Pods 访问 Namespace test-ns1 中有 Label nginx-ns1 的 Pods 的 Port 80,并且将 IP 20.10.10.3/32 的 Pod 除外。
    在这里插入图片描述

    Egress Rule

    示例 1:禁止 Namespace test-ns1 中的 Label nginx-ns1 的 Pods 去(egress 出站)请求 Namespace test-ns2 的 ClusterIP Service 10.96.0.12。
    在这里插入图片描述

    Network Policy Rule Selectors

    Network Policy 支持在 Ingress Rule 的 from 或 Egress Rule 的 to 部分中指定 4 种 Selectors(选择器):

    1. namespaceSelector:表示选定的 Namespace 中的所有 Pods 作为 Ingress from 或 Egress to 的来源或目的 Pods。
    2. podSelector:表示 Namespace 中选定的 Pods 作为 Ingress from 或 Egress to 的来源或目的 Pods。
    3. ipBlock:表示指定的 IP block(IP 地址块)作为 Ingress from 或 Egress to 的来源或目的 IP 地址。该 IP 地址应该是 Service IP 或 External IP,因为 Pod IP 总是短暂且随机产生的。

    namespaceSelector:此选择器将选择特定的名字空间,应将所有 Pod 用作其 入站流量来源或出站流量目的地。

    namespaceSelector 和 podSelector:一个指定 namespaceSelector 和 podSelector 的 to/from 条目选择特定名字空间中的特定 Pod。 注意使用正确的 YAML 语法;下面的策略:

    namespaceSelector

    在这里插入图片描述

    apiVersion: networking.k8s.io/v1
    kind: NetworkPolicy
    metadata:
      name: test-network-policy
    spec:
      podSelector:                  # 规则对具有 role=db 标签的 Pod 生效
        matchLabels:
          role: db
      ingress:                      # 表示入规则
      - from:
        - namespaceSelector:        # 只允许具有 project=myproject 标签的命名空间中的 Pod 访问
            matchLabels:
              project: myproject
        ports:                      # 只能使用 TCP 协议访问 6379 端口
        - protocol: TCP
          port: 6379
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    podSelector

    在这里插入图片描述

    apiVersion: networking.k8s.io/v1
    kind: NetworkPolicy
    metadata:
      name: test-network-policy
      namespace: default
    spec:
      policyTypes:
        - Ingress
        - Egress
      podSelector:                  # 规则对具有 role=db 标签的 Pod 生效
        matchLabels:
          role: db
      ingress:                      # 表示入规则
      - from:
        - podSelector:              # 只允许具有 role=frontend 标签的 Pod 访问
            matchLabels:
              role: frontend
        ports:                      # 只能使用 TCP 协议访问 6379 端口
        - protocol: TCP
          port: 6379
      egress:
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
  • 相关阅读:
    攻防演练中怎样做目标网情搜集
    python面试题——什么是GIL ;什么时候释放GIL锁;互斥锁(同步锁)和GIL的区别
    django中导出csv文件
    Python实现BS架构Flask的Web学生管理信息系统
    在windows下持续ping ip,将返回结果及时间记录到文件中
    FAST-LIO2代码解析(三)
    什么时候不宜使用氟橡胶密封件?
    在 Ubuntu Server 上配置静态 IP 地址
    性能测试监控指标及分析调优 | 京东云技术团队一、哪些因素会成为系统的瓶颈?
    信息化,数字化,智能化是三种不同的概念吗?
  • 原文地址:https://blog.csdn.net/Jmilk/article/details/126437294