1

语境

我有一个启用了Workload Identity的 Google Kubernetes Engine (GKE) 集群。作为 Workload Identity 的一部分,k8s-metadata-proxy DaemonSet 在集群上运行。我有一个命名空间my-namespace,并且想要拒绝命名空间中 pod 的所有出口流量,但到 k8s-metadata-proxy DaemonSet 的出口除外。因此,我有以下 NetworkPolicy:

apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: test-network-policy
  namespace: my-namespace
spec:
  # Apply to all pods.
  podSelector: {}
  policyTypes:
  - Egress
  egress:
  - ports:
    # This is needed to whitelist k8s-metadata-proxy. See https://github.com/GoogleCloudPlatform/k8s-metadata-proxy
    - protocol: TCP
      port: 988

问题

NetworkPolicy 过于宽泛,因为它允许将 TCP 流量出口到端口 988 上的任何主机,而不仅仅是出口到 k8s-metadata-proxy DaemonSet,但我似乎无法找到一种方法来指定.spec.egress[0].to实现我想要的粒度。

我已经尝试了以下tos:

  egress:
  - to:
    - namespaceSelector:
        matchLabels:
          namespace: kube-system
    ports:
    - protocol: TCP
      port: 988
  - to:
    - ipBlock:
        cidr: <cidr of pod IP range>
    - ipBlock:
        cidr: <cidr of services IP range>
    ports:
    - protocol: TCP
      port: 988

但是这些规则会导致到 k8s-metadata-proxy 的流量被阻止。

问题

如何to在 a 的出口规则部分选择 k8s-metadata-proxy DaemonSet networking.k8s.io/v1/NetworkPolicy

4

1 回答 1

2

正如我在评论中所说:

你好。您可以将 podSelector.matchLabels 添加到 Egress 定义中,以允许您的 pod 仅连接到具有特定标签的 Pod。您可以在此处阅读更多相关信息:cloud.google.com/kubernetes-engine/docs/tutorials/...</a>

此评论可能会产生误导,因为gke-metadata-server官方文档中描述了与的通信:

重点关注上述文档的部分:

了解 GKE 元数据服务器

GKE 元数据服务器是一种新的元数据服务器,专为与 Kubernetes 一起使用而设计。它作为daemonset运行,每个集群节点上都有一个 Pod。元数据服务器拦截对http://metadata.google.internal ( 169.254.169.254:80) 的 HTTP 请求,包括GET /computeMetadata/v1/instance/service-accounts/default/token为 Pod 配置为充当的 Google 服务帐户检索令牌之类的请求。到元数据服务器的流量永远不会离开托管 Pod 的 VM 实例。

注意:如果您有严格的集群网络策略,则必须允许端口 988 上的 127.0.0.1/32 出口,以便您的 Pod 可以与 GKE 元数据服务器通信。

仅允许交通的规则GKE Metadata server在上述引文的最后一段中进行了描述。YAML定义应如下所示:

kind: NetworkPolicy
apiVersion: networking.k8s.io/v1
metadata:
  name: egress-rule
  namespace: restricted-namespace # <- namespace your pod is in 
spec:
  policyTypes:
  - Egress
  podSelector:
    matchLabels:
      app: nginx # <- label used by pods trying to communicate with metadata server
  egress:
  - to:
    - ipBlock:
        cidr: 127.0.0.1/32 # <- allow communication with metadata server #1 
  - ports:
    - protocol: TCP
      port: 988 # <- allow communication with metadata server #2 

假如说:

  • 你有一个 Kubernetes 集群:
    • Network Policy启用
    • Workload Identity启用
  • Pods正在尝试从restricted-namespace命名空间进行通信

用于描述所需的输出NetworkPolicy

  • $ kubectl describe networkpolicy -n restricted-namespace egress-rule
Name:         egress-rule
Namespace:    restricted-namespace
Created on:   2020-10-04 18:31:10 +0200 CEST
Labels:       <none>
Annotations:  kubectl.kubernetes.io/last-applied-configuration:
                {"apiVersion":"networking.k8s.io/v1","kind":"NetworkPolicy","metadata":{"annotations":{},"name":"egress-rule","namespace":"restricted-name...
Spec:
  PodSelector:     app=nginx
  Allowing ingress traffic:
    <none> (Selected pods are isolated for ingress connectivity)
  Allowing egress traffic:
    To Port: <any> (traffic allowed to all ports)
    To:
      IPBlock:
        CIDR: 127.0.0.1/32
        Except: 
    ----------
    To Port: 988/TCP
    To: <any> (traffic not restricted by source)
  Policy Types: Egress

免责声明!

app=nginx应用这些规则将拒绝来自标签不是发往元数据服务器的Pod 的所有流量!

您可以通过以下方式创建并exec进入带有标签的 pod app=nginx

kubectl run -it --rm nginx \
--image=nginx \
--labels="app=nginx" \
--namespace=restricted-namespace \
-- /bin/bash

小费!

使用图像nginx,因为它curl已默认安装!

通过此示例,您将无法与 DNS 服务器进行通信。您可以:

  • 允许您的 pod 与 DNS 服务器通信
  • 设置env元数据服务器的变量 (169.254.169.254)

沟通的例子GKE Metadata Server

  • $ curl 169.254.169.254/computeMetadata/v1/instance/ -H 'Metadata-Flavor: Google'
attributes/
hostname
id
service-accounts/
zone

其他资源:



要允许特定 pod 仅将流量发送到特定端口上的特定 pod,您可以使用以下策略:

kind: NetworkPolicy
apiVersion: networking.k8s.io/v1
metadata:
  name: egress-rule
  namespace: restricted-namespace # <- namespace of "source" pod
spec:
  policyTypes:
  - Egress
  podSelector:
    matchLabels:
      app: ubuntu # <- label for "source" pod
  egress:
  - to:
    - podSelector:
        matchLabels:
          app: nginx # <- label for "destination" pod
  - ports:
    - protocol: TCP
      port: 80 # <- allow only port 80
于 2020-10-05T05:55:43.673 回答