Kubernetes — 核心资源对象 — Network Policy

举报
云物互联 发表于 2022/08/21 00:08:13 2022/08/21
【摘要】 目录 文章目录 目录Network PolicyNetwork Policy RulesIngress RuleEgress Rule Network Policy Rule Selecto...

目录

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

文章来源: is-cloud.blog.csdn.net,作者:范桂飓,版权归原作者所有,如需转载,请联系作者。

原文链接:is-cloud.blog.csdn.net/article/details/126437294

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。