正如我在评论中所说:
你好。您可以将 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