【问题标题】:Kubernetes NetworkPolicies Blocking DNSKubernetes 网络策略阻止 DNS
【发布时间】:2021-04-02 02:18:59
【问题描述】:

我有一个 AKS 群集 (Azure CNI),我正在尝试在其上实施 NetworkPolicies。我已经创建了网络策略

apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: myserver
spec:
  podSelector:
    matchLabels:
      service: my-server
  policyTypes:
  - Ingress
  - Egress
  ingress:
  - from:
    - podSelector:
        matchLabels:
          service: myotherserver
    - podSelector:
        matchLabels:
          service: gateway
    - podSelector:
        matchLabels:
          service: yetanotherserver
    ports:
     - port: 8080
       protocol: TCP
  egress:
    - to:
      ports:
       - port: 53
         protocol: UDP
       - port: 53
         protocol: TCP
       - port: 5432
         protocol: TCP
       - port: 8080
         protocol: TCP

但是当我应用该策略时,我会看到无法解析主机名的重复消息。我已经在 myserver pod 上安装了 dnsutils;并且可以看到 DNS 请求正在超时;我也尝试在同一个 pod 上安装 tcpdump;我可以看到从 myserver 到 kube-dns 的请求。我没有看到任何回复。

如果我删除网络策略 DNS 会直接返回;所以我确定我的网络策略存在问题,但找不到允许 DNS 流量的方法。如果有人能阐明我哪里出错了,将不胜感激!

【问题讨论】:

    标签: kubernetes dns azure-aks kubernetes-networkpolicy


    【解决方案1】:

    为避免重复创建单独的网络策略以打开 DNS 流量。首先我们标记kube-system 命名空间。然后允许来自所有 pod 的 DNS 流量到 kube-system 命名空间。

    kubectl label namespace kube-system name=kube-system
    
    kubectl create -f - <<EOF
    apiVersion: networking.k8s.io/v1
    kind: NetworkPolicy
    metadata:
      name: allow-dns-access
      namespace: <your-namespacename>
    spec:
      podSelector:
        matchLabels: {}
      policyTypes:
      - Egress
      egress:
      - to:
        - namespaceSelector:
            matchLabels:
              name: kube-system
        ports:
        - protocol: UDP
          port: 53
    
    EOF
    

    【讨论】:

      【解决方案2】:

      不需要name 标签到目标命名空间的解决方案。我不知道为什么,但to 部分中的podSelector 无效,除非还定义了namespaceSelector

      apiVersion: networking.k8s.io/v1
      kind: NetworkPolicy
      metadata:
        name: allow-dns-access
        namespace: <your-namespacename>
      spec:
        podSelector:
          matchLabels: {}
        policyTypes:
        - Egress
        egress:
        - to:
          - namespaceSelector: {}
            podSelector: {}
              matchLabels:
                k8s-app: kube-dns
          ports:
          - protocol: UDP
            port: 53
      

      【讨论】:

        猜你喜欢
        • 2021-12-02
        • 2019-02-04
        • 2021-03-09
        • 1970-01-01
        • 2021-08-18
        • 2021-04-28
        • 2022-01-23
        • 2020-08-24
        • 2020-08-10
        相关资源
        最近更新 更多