<p>I am using Azure Kubernetes Service (AKS) and want to make sure pods inside a specific namespace can only receive ingress traffic from other pods in the same namespace.</p>
<p>I found this network policy to achieve this namespace isolation (from <a href="https://github.com/ahmetb/kubernetes-network-policy-recipes/blob/master/04-deny-traffic-from-other-namespaces.md" rel="nofollow noreferrer">here</a>):</p>
<pre class="lang-yaml prettyprint-override"><code>kind: NetworkPolicy
apiVersion: networking.k8s.io/v1
metadata:
  namespace: my-namespace
  name: deny-from-other-namespaces
spec:
  podSelector:
    matchLabels:
  ingress:
  - from:
    - podSelector: {}
</code></pre>
<p>After I create this network policy, it successfully blocks traffic between pods on &quot;my-namespace&quot; and another namespace while communication between the pods in &quot;my-namespace&quot; is still possible. However, this is only true if both pods are scheduled on the same node. If both pods are in &quot;my-namespace&quot; but run on different nodes, then the connection between them no longer works. As soon as I delete above network policy, the connection works again.
I would think that this is not the intended behavior, as the pods are in the same namespace and ingress traffic should therefore be allowed.
Does anybody know what could cause this issue?</p>
<p>I am running Kubernetes version 1.19.6 with kubenet and calico network policies.</p>

<p>Looks like you hit a known problem in AKS clusters v1.19+ around &quot;Pod IP SNAT/Masquerade behavior&quot;.</p>
<p>How it affects clusters using Calico's plugin for Network Policies was explained there by other users:</p>
<blockquote>
<p>Just for information of other users, this issue causes problem for a NetworkPolicy with podSelector configs. Since the policy will be set based on the ipset of the pods in the IPtables by Calico, but the source IP of the packet is set to the node IP and even the packets that are supposed to be allowed will be dropped.</p>
</blockquote>
<p>Please read more about this problem in github issue <a href="https://github.com/Azure/AKS/issues/2031" rel="nofollow noreferrer">#2031</a>, along with the hard fix (node image upgrade) or workaround (run Daemonset creating SNAT exemption in iptables).</p>
