【问题标题】:What is the difference between ingress value as blank array and value - {}?入口值作为空白数组和值 - {} 有什么区别?
【发布时间】:2019-07-10 14:41:05
【问题描述】:

在 kubernetes 网络策略中,我们可以将 Ingress 值设置为空白数组,即 [] 或者我们也可以将值设置为 - {}

使用这两个值有什么区别?

我尝试的第一个 YAML - 没有用

apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: internal-policy
spec:
  podSelector:
    matchLabels:
      name: internal
  policyTypes: ["Ingress","Egress"]
  ingress: []
  egress:
  - to:
    - podSelector:
        matchLabels:
          name: mysql
    ports:
    - protocol: TCP
      port: 3306

在 katacoda 场景中回答的第二个 YAML

apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: internal-policy
  namespace: default
spec:
  podSelector:
    matchLabels:
      name: internal
  policyTypes:
  - Egress
  - Ingress
  ingress:
    - {}
  egress:
  - to:
    - podSelector:
        matchLabels:
          name: mysql
    ports:
    - protocol: TCP
      port: 3306

【问题讨论】:

    标签: kubernetes kubernetes-ingress nginx-ingress traefik-ingress katacoda


    【解决方案1】:

    在这两种情况下,您都指定了策略类型:入口和出口

    1. 在第一个示例中:
         ingress: []
    

    此规则(为空)并拒绝所有入口流量,(如果规范中不存在入口规则,则结果相同)。

    您可以通过运行来验证这一点:

    kubectl describe networkpolicy internal-policy 
    
    Allowing ingress traffic:
        <none> (Selected pods are isolated for ingress connectivity)
    
    1. 在第二个例子中:
         ingress:
            - {}
    

    此规则允许所有入口流量

    kubectl describe networkpolicy internal-policy 
    
     Allowing ingress traffic:
        To Port: <any> (traffic allowed to all ports)
        From: <any> (traffic not restricted by source)
    

    根据文档:Network Policies

    入口规则: 每个 NetworkPolicy 可能包含一个白名单入口规则列表。每个规则都允许与 from 和 ports 部分匹配的流量。

    希望对您有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-08-12
      • 1970-01-01
      • 2015-02-13
      • 1970-01-01
      • 2021-11-14
      • 1970-01-01
      • 1970-01-01
      • 2010-12-25
      相关资源
      最近更新 更多