Reputation: 10405
How to define a network policy to prevent communication across pods in different namespaces within the same K8s cluster?
Upvotes: 1
Views: 1195
Reputation: 18371
Checkout the git repo at here for many netpolicy usecases, following is one of it.
kind: NetworkPolicy
apiVersion: networking.k8s.io/v1
metadata:
namespace: default
name: deny-from-other-namespaces
spec:
podSelector:
matchLabels:
ingress:
- from:
- podSelector: {}
*Note a few things about this manifest:
namespace: default deploys it to the default namespace. it applies the policy to ALL pods in default namespace as the spec.podSelector.matchLabels is empty and therefore selects all pods. it allows traffic from ALL pods in the default namespace, as spec.ingress.from.podSelector is empty and therefore selects all pods.*
Upvotes: 5