我有在K3s中运行的应用程序,并且希望仅基于命名空间实现网络策略。
让我们假设目前我有三个命名空间A、B和C。我想允许namespace-A
的出口(从pod到互联网的外部调用(,而剩余的namespace[B & C]
出口调用应该被阻止/拒绝
这在Kubernetes网络策略(而不是calico或cilium(中可能吗?
您可以像文档中描述的那样定义deny all egress
策略:
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: default-deny-egress
namespce: your-namespace
spec:
podSelector: {}
policyTypes:
- Egress
此策略将应用于命名空间中的所有pod,因为pod选择器为空,这意味着(引用文档(:
一个空的podSelector选择名称空间中的所有pods。
策略将阻止所有出口流量,因为它将Egress
作为策略类型,但没有任何egress
部分。
如果您想允许集群内出口,您可能需要在策略中添加egress
部分,例如:
egress:
- to:
- namespaceSelector:
matchLabels:
networking/namespace: kube-system
podSelector:
matchLabels:
k8s-app: kube-dns
ports:
- protocol: TCP
port: 53
- protocol: UDP
port: 53
这允许从创建网络策略的命名空间到端口53(TCP和UDP(上命名空间kube-system
中标记为k8s-app: kube-dns
的pod的所有流量。