作为空白数组的入口值和值-{}有什么区别?

时间:2019-07-10 14:41:05

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

在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

1 个答案:

答案 0 :(得分:0)

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

  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都可以包括白名单进入规则的列表。每个规则都允许匹配“发件人”和“端口”部分的流量。

希望获得帮助。