<p>How to define a network policy to prevent communication across pods in different namespaces within the same K8s cluster?</p>

<p>Checkout the git repo at <a href="https://github.com/ahmetb/kubernetes-network-policy-recipes/blob/master/04-deny-traffic-from-other-namespaces.md" rel="noreferrer">here</a> for many netpolicy usecases, following is one of it.</p>
<pre><code>kind: NetworkPolicy
apiVersion: networking.k8s.io/v1
metadata:
  namespace: default
  name: deny-from-other-namespaces
spec:
  podSelector:
    matchLabels:
  ingress:
  - from:
    - podSelector: {}
</code></pre>
<blockquote>
<p>*<strong>Note a few things about this manifest:</strong></p>
</blockquote>
<blockquote>
<p>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.*</p>
</blockquote>
