【发布时间】:2018-11-21 06:51:01
【问题描述】:
我有一个多租户集群,其中多租户是通过命名空间实现的。每个租户都有自己的命名空间。一个租户的 Pod 不能与其他租户的 Pod 通信。但是,每个租户中的一些 pod 必须使用 Ingress 向 Internet 公开服务。
这我走了多远(我正在使用 Calico):
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: tenant1-isolate-namespace
namespace: tenant1
spec:
policyTypes:
- Ingress
podSelector: {} # Select all pods in this namespace
ingress:
- from:
- namespaceSelector:
matchLabels:
name: tenant1 # white list current namespace
为每个命名空间(tenant1、tenant2、...)部署,这限制了其命名空间内 pod 之间的通信。但是,这会阻止 kube-system 命名空间中的 pod 与此命名空间中的 pod 通信。
但是,kube-system 命名空间默认没有任何标签,所以我不能专门将该命名空间列入白名单。
我通过手动给它一个标签找到了一个(肮脏的)解决方法:
kubectl label namespace/kube-system permission=talk-to-all
并将白名单规则添加到networkpolicy:
...
- from:
- namespaceSelector:
matchLabels:
permission: talk-to-all # allow namespaces that have the "talk-to-all privilege"
有没有更好的解决方案,不用手动给kube-system打标签?
编辑:我尝试另外添加一个“OR”规则,以专门允许来自标签为“app=nginx-ingress”的 pod 进行通信,但没有运气:
- from
...
- podSelector:
matchLabels:
app: nginx-ingress # Allow pods that have the app=nginx-ingress label
【问题讨论】:
-
您是否设法将每个 ns 中的专用 pod 暴露给互联网,但仍然阻止命名空间之间的任何集群间通信?我尝试了你肮脏的解决方法,但我的专用 pod 无法从互联网(AWS ELB)访问......在我的入口网络策略中,我有 - podSelector:{} - namespaceSelector:matchLabels:permission:kube-system 我可以访问我的来自 kube-system 命名空间中 pod 的受保护命名空间不允许我访问受保护命名空间中的 ELB 公开 pod - 似乎来自 ELB 的外部流量不通过 kube-system 命名空间
标签: kubernetes project-calico kubernetes-networkpolicy