在kubernetes网络策略中,我们可以将输入值设置为空白数组,即[],也可以将值设置为- {}。
使用这两个值有什么区别?
我试过的第一个YAML -它不起作用,
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: internal-policy
spec:
podSelector:
matchLabels:
name: internal
policyTypes: ["Ingress","Egress"]
ingress: []
egress:
- to:
- podSelector:
matchLabels:
name: mysql
ports:
- protocol: TCP
port: 3306第二个YAML,这是katacoda场景中的答案
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: internal-policy
namespace: default
spec:
podSelector:
matchLabels:
name: internal
policyTypes:
- Egress
- Ingress
ingress:
- {}
egress:
- to:
- podSelector:
matchLabels:
name: mysql
ports:
- protocol: TCP
port: 3306发布于 2019-07-11 16:08:59
在这两种情况下,您都指定了策略类型:
ingress: []此规则(为空)和拒绝所有入口流量,(如果规范中不存在入口规则,则结果相同)。
您可以通过运行以下命令来验证这一点:
kubectl describe networkpolicy internal-policy
Allowing ingress traffic:
<none> (Selected pods are isolated for ingress connectivity) ingress:
- {}此规则允许所有入口流量
kubectl describe networkpolicy internal-policy
Allowing ingress traffic:
To Port: <any> (traffic allowed to all ports)
From: <any> (traffic not restricted by source)根据文档:网络策略
入口规则:每个NetworkPolicy都可以包含一个白名单入口规则的列表。每个规则都允许与from和ports部分匹配的通信量。
希望能帮上忙。
https://stackoverflow.com/questions/56973152
复制相似问题