<p>I'm setting up a namespace in my kubernetes cluster to deny any outgoing network calls like <a href="http://company.com" rel="nofollow noreferrer">http://company.com</a> but to allow inter pod communication within my namespace like <a href="http://my-nginx" rel="nofollow noreferrer">http://my-nginx</a> where my-nginx is a kubernetes service pointing to my nginx pod.</p>

<p>How to achieve this using network policy. Below network policy helps in blocking all outgoing network calls </p>

<pre><code>kind: NetworkPolicy
apiVersion: networking.k8s.io/v1
metadata:
  name: deny-all-egress
  namespace: sample
spec:
  policyTypes:
  - Egress
  podSelector: {}
</code></pre>

<p>How to white list only the inter pod calls?</p>

Pradeep
<p>This can be done using the following combination of network policies:</p>

<pre><code># The same as yours
kind: NetworkPolicy
apiVersion: networking.k8s.io/v1
metadata:
  name: deny-all-egress
  namespace: sample
spec:
  policyTypes:
  - Egress
  podSelector: {}
---
# allows connections to all pods in your namespace from all pods in your namespace
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: allow-namespace-egress
  namespace: sample
spec:
  podSelector: {}
  policyTypes:
  - Egress
  egress:
  - to:
    - podSelector:
        matchLabels: {}
---
# allows connections from all pods in your namespace to all pods in your namespace
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: allow-namespace-internal
  namespace: sample
spec:
  podSelector: {}
  policyTypes:
  - Ingress
  ingress:
  - from:
    - podSelector:
        matchLabels: {}
</code></pre>

<p>assuming your network policy implementation implements the full spec.</p>
