定义仅允许使用网络策略导出到 kube-apiserver 的规则的最佳方法是什么?
虽然有 Service
kube-apiserver 的资源,没有 Pods
,据我所知,这不能用标签来完成。使用 IP 白名单,不能保证跨集群工作。这里有什么推荐的做法吗?
最佳答案
您必须使用 apiserver 的 IP 地址。您不能使用标签。
要查找 apiserver 的 IP 地址,请运行:kubectl cluster-info
在输出中寻找这样的一行:Kubernetes master is running at https://<ip>
这是您的 apiserver IP 的 IP 地址。
网络策略应如下所示(假设 apiserver IP 为 34.76.197.27):
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: egress-apiserver
spec:
podSelector: {}
policyTypes:
- Egress
egress:
- to:
- ipBlock:
cidr: 34.76.197.27/32
ports:
- protocol: TCP
port: 443
The policy above applies to all pods in the namespaces it is applied to .
要选择特定的 pod,请使用需要 apiserver 访问的 pod 的标签编辑 podSelector 部分:
podSelector:
matchLabels:
app: apiserver-allowed
请记住,默认的导出策略是 ALLOW ALL,这意味着其他 pod 仍然可以访问 apiserver。
You can change this behavior by adding a "BLOCK ALL" egress policy per namespace但请记住不要阻止对 DNS 服务器和其他基本服务的访问。
For more info see "Egress and DNS (Pitfall!)" in this post .
请注意,在某些情况下,可能有多个 apiserver(用于可扩展性),在这种情况下,您需要添加所有 IP 地址。
关于kubernetes - 使用网络策略将 kube-apiserver 列入白名单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58790124/