Misconfigured Pod NetworkPolicy

NetworkPolicy restrictions may not apply as intended when a Pod is outside the policy’s namespace or selector.

Description

A NetworkPolicy selects Pods in its own namespace using podSelector. The network plugin must support policy enforcement.

Potential impact

Matching labels alone leave a Pod outside the policy if the namespaces differ.

Remediation

Check the namespace and labels on the running Pods. Review every policy that selects them because their allowances add up. Test that DNS and required application traffic work and that a connection you intend to block fails.

Examples

The policy in app-another misses the Pod in app-one. The second policy denies ingress and egress by default for all Pods in app-safe. Create the namespaces and supply a production image before using the manifests.

Before

yaml
apiVersion: v1
kind: Pod
metadata:
  name: sample-pod
  namespace: app-one
  labels:
    app: shouldmatch
spec:
  containers:
    - name: nginx
      image: nginx:1.14.2
---
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: sample-policy
  namespace: app-another
spec:
  podSelector:
    matchLabels:
      app: shouldmatch

After

yaml
apiVersion: v1
kind: Pod
metadata:
  name: sample-pod
  namespace: app-safe
spec:
  containers:
    - name: app
      image: images.my-company.example/app:v4
---
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: sample-policy
  namespace: app-safe
spec:
  podSelector: {}
  policyTypes:
    - Ingress
    - Egress

To protect the existing Pod in app-one, create a matching policy there. If you migrate to app-safe, verify the new Pod and its policy before retiring the old Pod.

References