mit*_*man 7 kubernetes project-calico kubernetes-networkpolicy
我有一个多租户集群,其中通过命名空间实现多租户。每个租户都有自己的命名空间。来自租户的 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
Run Code Online (Sandbox Code Playgroud)
为每个命名空间 ( tenant1, tenant2, ... )部署,这限制了其命名空间内 pod 之间的通信。但是,这会阻止kube-system命名空间中的 Pod 与此命名空间中的 Pod 通信。
但是,kube-system默认情况下命名空间没有任何标签,因此我不能专门将此命名空间列入白名单。
我通过手动给它一个标签找到了解决这个问题的(肮脏的)解决方法:
kubectl label namespace/kube-system permission=talk-to-all
Run Code Online (Sandbox Code Playgroud)
并将白名单规则添加到网络策略中:
...
- from:
- namespaceSelector:
matchLabels:
permission: talk-to-all # allow namespaces that have the "talk-to-all privilege"
Run Code Online (Sandbox Code Playgroud)
有没有更好的解决方案,不用手动给kube-system标签?
编辑:我尝试另外添加一个“OR”规则,以专门允许来自标签为“app=nginx-ingress”的 pod 进行通信,但没有运气:
- from
...
- podSelector:
matchLabels:
app: nginx-ingress # Allow pods that have the app=nginx-ingress label
Run Code Online (Sandbox Code Playgroud)
命名空间选择器旨在仅通过标签匹配命名空间。无法按名称选择命名空间。
\n\npodSelector 只能选择与 NetworkPolicy 对象位于同一命名空间中的 Pod。对于位于不同命名空间中的对象,只能选择整个命名空间。
\n\n以下是 Kubernetes 网络策略实施的示例:
\n\napiVersion: networking.k8s.io/v1\nkind: NetworkPolicy\nmetadata:\n name: test-network-policy\n namespace: default\nspec:\n podSelector:\n matchLabels:\n role: db\n policyTypes:\n - Ingress\n - Egress\n ingress:\n - from:\n - ipBlock:\n cidr: 172.17.0.0/16\n except:\n - 172.17.1.0/24\n - namespaceSelector:\n matchLabels:\n project: myproject\n - podSelector:\n matchLabels:\n role: frontend\n ports:\n - protocol: TCP\n port: 6379\n egress:\n - to:\n - ipBlock:\n cidr: 10.0.0.0/24\n ports:\n - protocol: TCP\n port: 5978\nRun Code Online (Sandbox Code Playgroud)\n\n点击此链接阅读对网络政策整个概念的详细解释,或点击此链接观看讲座。
\n\n\n\nCalico API 为您提供了更多编写 NetworkPolicy 规则的选项,因此,在某些时候,您可以以更少的努力和精力来实现您的目标。
\n\n例如,使用网络策略的 Calico 实现,您可以:
\n\n但是,您仍然只能通过标签来匹配命名空间。
\n\n请考虑阅读 Calico文档以获取详细信息。
\n\n以下是 Calico 网络策略实施的示例:
\n\napiVersion: projectcalico.org/v3\nkind: NetworkPolicy\nmetadata:\n name: allow-tcp-6379\n namespace: production\nspec:\n selector: role == \'database\'\n types:\n - Ingress\n - Egress\n ingress:\n - action: Allow\n protocol: TCP\n source:\n selector: role == \'frontend\'\n destination:\n ports:\n - 6379\n egress:\n - action: Allow\nRun Code Online (Sandbox Code Playgroud)\n
| 归档时间: |
|
| 查看次数: |
3419 次 |
| 最近记录: |