上下文
我有一个Google引擎(GKE)集群,启用了工作负载标识。作为工作负载标识的一部分,K8s-元数据代理 DaemonSet在集群上运行。我有一个名称空间my-namespace
,并且想拒绝名称空间中所有豆荚的出口流量,除了出口到K8s-元数据-代理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-元数据代理DaemonSet,但是我似乎找不到一种方法来指定.spec.egress[0].to
来实现我想要的粒度。
我尝试了以下to
:
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元数据代理的通信被阻塞。
问题
如何在DaemonSet中的出口规则的to
部分中选择k8s元数据代理networking.k8s.io/v1/NetworkPolicy
。
发布于 2020-10-05 05:55:43
正如我在评论中所说:
你好。您可以添加到您的均衡器定义podSelector.matchLabels,以允许您的荚只连接到特定标签的口袋妖怪。你可以在这里读到更多关于它的信息:cloud.google.com/kubernetes-engine/docs/tutorials/…
这句话可能会误导人,因为官方文档:gke-metadata-server
描述了与的通信:。
侧重于上述文件的一部分:
了解GKE元数据服务器 GKE元数据服务器是为与Kubernetes一起使用而设计的新元数据服务器。它以雏形的形式运行,每个集群节点上都有一个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
假设:
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
标签的豆荚的所有通信量,而不是指定给元数据服务器!
您可以通过以下方法创建并exec
到带有标签app=nginx
的荚中:
kubectl run -it --rm nginx \
--image=nginx \
--labels="app=nginx" \
--namespace=restricted-namespace \
-- /bin/bash
Tip! 默认情况下,使用图像
nginx
是因为它安装了curl
!通过这个例子,您将无法与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
追加资源:
若要允许特定的吊舱只向特定端口上的特定吊舱发送通信量,可以使用以下策略:
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
https://stackoverflow.com/questions/64159047
复制相似问题