使用k8s网络策略或calico时,我只能将这些工具用于pod到pod集群网络策略。我已经有了外部群集策略的网络规则。
例如,如果我应用这个印花布规则:
apiVersion: projectcalico.org/v3
kind: NetworkPolicy
metadata:
name: allow-ingress-from-b
namespace: app
spec:
selector: app == 'a'
ingress:
- action: Allow
protocol: TCP
source:
selector: app == 'b'
destination:
ports:
- 80在本例中,我允许从应用程序B到应用程序A的流量,但这将禁止所有其他进入流量到应用程序A。是否可以仅在pod到pod之间应用此规则?
发布于 2020-03-04 06:36:55
您应该阅读The NetworkPolicy resource,它提供了一个带有Ingress和Egress的示例NetworkPolicy。
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: test-network-policy
namespace: default
spec:
podSelector:
matchLabels:
role: db
policyTypes:
- Ingress
- Egress
ingress:
- from:
- ipBlock:
cidr: 172.17.0.0/16
except:
- 172.17.1.0/24
- namespaceSelector:
matchLabels:
project: myproject
- podSelector:
matchLabels:
role: frontend
ports:
- protocol: TCP
port: 6379
egress:
- to:
- ipBlock:
cidr: 10.0.0.0/24
ports:
- protocol: TCP
port: 5978解释如下:
对于入口和出口流量(如果它们还不是isolated)
- any pod in the “default” namespace with the label “role=frontend”
- any pod in a namespace with the label “project=myproject”
- IP addresses in the ranges 172.17.0.0–172.17.0.255 and 172.17.2.0–172.17.255.255 (ie, all of 172.17.0.0/16 except 172.17.1.0/24)
上的CIDR 10.0.0.0/24
有关更多示例,请参见Declare Network Policy演练。
因此,如果您使用podSelector,则可以选择要应用此网络策略的pods。
https://stackoverflow.com/questions/60515445
复制相似问题