Kubernetes network policy: one Pod's traffic passes a wall, another's is blocked
DevOps Engineering

Kubernetes Network Policy: Default Deny, Examples and Traps

Kubernetes network policy explained: default deny, YAML examples, DNS, the AND/OR trap and how to test it.

Michael Zion

24 min read

A Kubernetes network policy (the NetworkPolicy object) decides which Pods may talk to which, by IP address and port. Without one, every Pod in the cluster can reach every other Pod. 5 things to know before you write your first policy:

Bottom line

  1. No policy = everything is open. A Pod accepts and opens any connection until a policy selects it
  2. Rules only allow. There is no deny rule. A policy isolates the Pods it selects, and its rules list the exceptions
  3. Your network plugin does the work. On a plugin that does not enforce NetworkPolicy, the object is accepted and does nothing
  4. Default deny egress also blocks DNS. Allow port 53 in the same change, or every name lookup times out
  5. One dash in the YAML changes who gets in. A namespaceSelector and a podSelector in 1 list entry mean AND. In 2 entries they mean OR

The policy to start a namespace with, on Kubernetes v1.37. It selects every Pod in the namespace and allows no incoming connection:

apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: default-deny-ingress
  namespace: shop
spec:
  podSelector: {}
  policyTypes:
  - Ingress

And the first exception, which lets the web Pods call the api Pods on port 8080:

apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: api-allow-web
  namespace: shop
spec:
  podSelector:
    matchLabels:
      app: api
  policyTypes:
  - Ingress
  ingress:
  - from:
    - podSelector:
        matchLabels:
          app: web
    ports:
    - protocol: TCP
      port: 8080

(That is the ingress half. Once you deny egress as well, the web Pods need their own rule to get out, and every Pod needs a rule for DNS.)

The 4 commands you will use most:

kubectl get networkpolicy -A
kubectl get netpol -n shop
kubectl describe networkpolicy api-allow-web -n shop
kubectl run test --rm -ti --image=busybox -n shop -- /bin/sh

Every Pod in your cluster can reach the database? Get a free DevOps assessment: a senior engineer reviews your CI/CD, infra, Kubernetes, cloud cost and on-call setup.

Find your situation:

What a Kubernetes network policy is

A NetworkPolicy is a namespaced object in the networking.k8s.io/v1 API. It controls traffic at layer 3 and layer 4: IP addresses, ports and protocols (TCP, UDP and SCTP). It knows nothing about HTTP paths, hostnames or TLS.

A policy names 3 kinds of peers: other Pods (by label), namespaces (by label) and IP ranges. It only matters for a connection that has a Pod on at least one end.

The part that is easy to miss: Kubernetes stores the object, and your network plugin enforces it. The API server has no firewall of its own. If the plugin does not enforce NetworkPolicy, kubectl apply succeeds, kubectl get netpol lists the policy, and traffic flows exactly as before. No error, no warning.

So the first question on any cluster is which plugin runs there.

(If you came from Kubernetes RBAC: RBAC decides who may call the Kubernetes API. NetworkPolicy decides which Pods may open connections to each other. They do not overlap. A locked-down ServiceAccount can still reach every database port in the cluster.)

How a Kubernetes network policy isolates a Pod

A Pod has 2 independent kinds of isolation: one for ingress (connections coming in) and one for egress (connections going out). By default it has neither.

  • A Pod becomes isolated for ingress when any policy selects it and has Ingress in its policyTypes
  • A Pod becomes isolated for egress when any policy selects it and has Egress in its policyTypes

Once a Pod is isolated in a direction, only the connections that some policy's rules allow get through in that direction. Everything else is dropped.

Rules only allow. What no rule allows is denied.

Kubernetes network policy isolation: with no policy the api Pod accepts traffic from the web and batch Pods, after a policy selects api it is isolated for ingress and only the sources a rule lists get in
No policy: every source is allowed. A policy selects the Pod: only what a rule lists.

3 consequences:

  • Policies add up. If 5 policies select a Pod, the allowed traffic is the union of all their rules. There is no order and no conflict
  • You cannot subtract. A second policy can never take away what the first one allowed. To close something, you edit or delete the rule that opens it
  • A policy with no rules is still a policy. It selects Pods, isolates them and allows nothing. That is all a default deny is

Policies are also strictly per namespace. A policy only selects Pods in its own namespace, and a namespace with no policies is fully open.

2 exceptions are built in. Reply traffic on an allowed connection is always allowed, so you never write a rule for the response. And connections from the node a Pod runs on are always allowed in, whatever the policies say. (Calico's docs give kubelet health checks as the reason.) The Kubernetes docs say the same for traffic from a Pod to its node, but plugins differ there.

Both ends must allow the connection

For a connection from Pod A to Pod B, 2 checks happen: the egress side of A and the ingress side of B. Both must allow it.

A side that no policy has isolated allows everything. So in a namespace with only ingress policies, you think about the destination only. The moment you add a default deny for egress, every connection needs 2 rules: one that lets the source out and one that lets the destination accept.

This is a common reason for "I allowed it and it still times out". The ingress rule on the api Pod is correct, and the web Pod is egress isolated with no rule that lets it reach api.

Replies on an allowed connection are allowed automatically.

A connection from the web Pod to the api Pod on TCP 8080 needs the egress rules on web and the ingress rules on api to allow it, and a side no policy isolates allows everything
The source's egress and the destination's ingress both have a say.

NetworkPolicy YAML, field by field

A policy that uses every part of the spec:

apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: db-policy
  namespace: shop
spec:
  podSelector:
    matchLabels:
      app: db
  policyTypes:
  - Ingress
  - Egress
  ingress:
  - from:
    - podSelector:
        matchLabels:
          app: api
    ports:
    - protocol: TCP
      port: 5432
  egress:
  - to:
    - ipBlock:
        cidr: 10.20.0.0/24
    ports:
    - protocol: TCP
      port: 443
FieldWhat it doesIf you leave it out or empty
spec.podSelectorThe Pods this policy applies to, in the policy's namespace{} selects every Pod in the namespace
spec.policyTypesThe directions the policy isolates: Ingress, Egress or bothIngress is always set. Egress is set only if the policy has egress rules
spec.ingressA list of rules. Each rule allows traffic that matches its from and its portsNo ingress is allowed (when Ingress is in policyTypes, which it is unless you list only Egress)
spec.egressA list of rules. Each rule allows traffic that matches its to and its portsNo egress is allowed (when Egress is in policyTypes)
from / toA list of peers, joined with ORMatches every source or destination
portsA list of ports, joined with ORMatches every port
ports[].protocolTCP, UDP or SCTPTCP
ports[].portA number, or the name of a container portEvery port of that protocol (GKE Dataplane V2 does not accept a protocol without a port)
ports[].endPortTurns port into a range up to this numberA single port

Inside 1 rule, from and ports are joined with AND: the traffic must match a peer and a port. Between rules it is OR.

The policyTypes trap

The defaulting of policyTypes catches people in both directions:

  • A policy with only egress rules and no policyTypes gets [Ingress, Egress]. It has no ingress rules, so it also blocks every incoming connection to the Pods it selects. An egress-only policy must say policyTypes: [Egress]
  • A "deny all egress" policy with no policyTypes gets [Ingress] only, because it has no egress section. It denies ingress and leaves egress open

I write policyTypes in every policy. It costs 2 lines and removes the guessing.

Ports, ranges and protocols

  • endPort is stable since Kubernetes v1.25. It needs a numeric port, and it must be equal to or greater than it. If the plugin does not support it, the rule applies to the single port only
  • A named port (port: http) follows the name in each Pod's container spec, so the rule keeps working when the number changes
  • ICMP is undefined. A deny-all is only guaranteed to stop TCP, UDP and SCTP. Whether ping gets through depends on the plugin, so a ping test tells you nothing about your policy

podSelector and namespaceSelector: AND vs OR

A peer in from or to can be written 4 ways:

PeerSelects
podSelector aloneMatching Pods in the policy's own namespace
namespaceSelector aloneEvery Pod in the matching namespaces
Both in 1 entryMatching Pods in the matching namespaces
ipBlockAn IP range. Cannot be combined with the other 2 in one entry

The third row is where policies go wrong. Compare these 2 rules:

# 1 entry: AND
ingress:
- from:
  - namespaceSelector:
      matchLabels:
        team: payments
    podSelector:
      matchLabels:
        app: web
# 2 entries: OR
ingress:
- from:
  - namespaceSelector:
      matchLabels:
        team: payments
  - podSelector:
      matchLabels:
        app: web

The first allows Pods labeled app=web in namespaces labeled team=payments. The second allows every Pod in every team=payments namespace, plus the app=web Pods of the policy's own namespace. The only difference is the dash in front of podSelector.

One dash changes who gets in.

Kubernetes NetworkPolicy namespaceSelector and podSelector in 1 entry mean AND (app=web Pods in team=payments namespaces), in 2 entries they mean OR (every Pod in team=payments namespaces plus app=web Pods in this namespace)
The same 2 selectors, with and without a dash in front of podSelector.

Both versions are valid YAML and both apply without a warning. The way to see which one you wrote is kubectl describe networkpolicy: an AND peer prints its NamespaceSelector and PodSelector under a single From: line, and OR peers print as separate From: entries. (An empty selector prints as <none>. Read it as "all".)

Selecting a namespace by name

A policy cannot name a namespace directly. It can only match labels. Since Kubernetes v1.22 every namespace carries the label kubernetes.io/metadata.name with its own name as the value, set by the control plane and impossible to change. That makes it the safe way to say "this namespace":

- namespaceSelector:
    matchLabels:
      kubernetes.io/metadata.name: monitoring

For several namespaces, use matchExpressions with the In operator, or put your own label on them. With your own labels, remember that whoever can label a namespace can walk through your policy. The same goes for Pods: anyone who can create a Pod in an allowed namespace can give it the label app=web. NetworkPolicy trusts labels, so who may create Pods and label namespaces is part of your network security.

(An empty namespaceSelector: {} selects every namespace. In a rule meant for 1 team, that is the whole cluster.)

ipBlock: for addresses outside the cluster

ipBlock takes a cidr and an optional except list of smaller ranges inside it. It is meant for IPs outside the cluster: a managed database, a partner API, an office range.

3 things to know before you rely on it:

  • Do not use it for Pods. Pod IPs change whenever a Pod is replaced, and plugins disagree on whether an ipBlock matches a Pod IP at all. With Calico they match (GKE documents this for its Calico mode). Cilium does not match them by default. On GKE Dataplane V2 and on AKS with Cilium, even 0.0.0.0/0 does not cover Pod traffic (on AKS with Cilium, node IPs are left out too). The same policy can open traffic on one cluster and block it on another
  • The source IP may have been rewritten. Traffic that enters through a load balancer or a NodePort can arrive with the IP of the load balancer or of a node. Whether the policy sees the original client IP depends on the plugin, the cloud and the Service configuration
  • Egress to a Service IP is not guaranteed to match. The docs say connections to Service IPs that get rewritten to external IPs "may or may not" be subject to ipBlock rules

For anything inside the cluster, use selectors.

Default deny: the policies to start a namespace with

The Kubernetes security checklist lists default policies in each namespace that select all Pods and deny everything. The reason is simple: with an allow list, a new workload that nobody wrote a policy for starts closed.

The full version, for both directions:

apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: default-deny-all
  namespace: shop
spec:
  podSelector: {}
  policyTypes:
  - Ingress
  - Egress

The 4 building blocks, all with podSelector: {}:

PolicypolicyTypesRules
Deny all ingressIngressnone
Deny all egressEgressnone
Deny all ingress and egressIngress, Egressnone
Allow all ingressIngressingress: [{}] (1 empty rule)

The difference between "no rules" and "1 empty rule" is the whole difference between deny all and allow all. An empty rule has no from and no ports, so it matches everything.

A default deny covers 1 namespace. There is no built-in way to apply a policy to every namespace, so each new namespace needs its own copy. Put it in whatever creates your namespaces: the Helm chart, the Terraform module, the GitOps template.

The order I roll this out in an existing cluster, 1 namespace at a time:

  1. Deny all ingress, plus the ingress allow rules the workloads need. This is the easy half: you only think about who calls each app
  2. Let it run until nobody complains
  3. Deny all egress together with the DNS rule below, plus the egress rules each workload needs: its database, its queue, the external APIs it calls

Egress is where the surprises are. Apps call things nobody remembers: a license server, a metrics endpoint, the cloud metadata API.

Start with application namespaces. Leave kube-system and the namespaces of controllers, operators and admission webhooks for last, and treat them separately: they talk to the Kubernetes API server in both directions, and those rules are harder to write. (Calico's docs carry the same warning for a cluster-wide default deny.)

Allow DNS, or nothing resolves

A default deny for egress also blocks DNS. The Pod is allowed to reach api on port 8080, and it never gets there, because the lookup of the name api is a UDP packet to port 53 that no rule allows. The symptom looks like a network outage: every call by name times out, and calls by IP work.

The usual rule, for every Pod in the namespace:

apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: allow-dns
  namespace: shop
spec:
  podSelector: {}
  policyTypes:
  - Egress
  egress:
  - to:
    - namespaceSelector:
        matchLabels:
          kubernetes.io/metadata.name: kube-system
      podSelector:
        matchLabels:
          k8s-app: kube-dns
    ports:
    - protocol: UDP
      port: 53
    - protocol: TCP
      port: 53

Allow port 53 to the cluster DNS in the same change as the deny.

Default deny egress blocks DNS: the web Pod is allowed to reach api on TCP 8080 but its lookup to CoreDNS in kube-system on port 53 is blocked, so the name never resolves
The allow rule is there. The lookup never gets out.

The cluster DNS Pods run in kube-system and carry the label k8s-app: kube-dns, also when the server is CoreDNS (the name is historical). The Kubernetes docs give the warning and no YAML. Calico's and Cilium's docs select the same label in their own policy formats. Open TCP 53 as well as UDP: large answers fall back to TCP.

53 here is the port the DNS Pods listen on. That is the case on a stock CoreDNS. If your distribution runs the DNS Pods on another port behind the Service, the rule has to name that port. kubectl get endpointslices -n kube-system -l kubernetes.io/service-name=kube-dns shows it.

NodeLocal DNSCache changes the rule

If the cluster runs NodeLocal DNSCache, lookups are answered by a cache Pod on the node. It runs with hostNetwork and listens on a link-local address (169.254.20.10 by default). With kube-proxy in iptables mode it also listens on the cluster DNS Service IP, so /etc/resolv.conf in a Pod can still show the usual address.

On most plugins a selector cannot match a hostNetwork Pod. The add-on's README warns that a selector rule "might not be enough" and suggests an ipBlock rule for TCP and UDP 53 to the cluster DNS Service IP. Check with kubectl get daemonset -n kube-system node-local-dns, and test a lookup right after you apply the deny.

Kubernetes network policy examples

5 policies a namespace usually needs after the default deny and the DNS rule.

Let web call api when egress is denied too

apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: web-allow-egress-to-api
  namespace: shop
spec:
  podSelector:
    matchLabels:
      app: web
  policyTypes:
  - Egress
  egress:
  - to:
    - podSelector:
        matchLabels:
          app: api
    ports:
    - protocol: TCP
      port: 8080

This is the other half of api-allow-web from the top of the article. With a default deny in both directions, the call works only when both exist.

Let the ingress controller reach an app

apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: web-allow-ingress-controller
  namespace: shop
spec:
  podSelector:
    matchLabels:
      app: web
  policyTypes:
  - Ingress
  ingress:
  - from:
    - namespaceSelector:
        matchLabels:
          kubernetes.io/metadata.name: ingress-nginx
    ports:
    - protocol: TCP
      port: 8080

The port is the container port of the web Pods. If your ingress controller runs with hostNetwork, a namespace selector will usually not match it (see the traps).

Let Pods in a namespace talk to each other

apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: allow-same-namespace
  namespace: shop
spec:
  podSelector: {}
  policyTypes:
  - Ingress
  ingress:
  - from:
    - podSelector: {}

An empty podSelector as a peer means every Pod in this namespace. This is a reasonable first step for a team namespace: closed to the rest of the cluster, open inside.

Let an app call an external database

apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: api-allow-db-egress
  namespace: shop
spec:
  podSelector:
    matchLabels:
      app: api
  policyTypes:
  - Egress
  egress:
  - to:
    - ipBlock:
        cidr: 10.20.0.0/24
    ports:
    - protocol: TCP
      port: 5432

NetworkPolicy cannot allow a hostname. If the database IP changes, the policy has to change with it. Allowing by DNS name needs a plugin feature such as Cilium's toFQDNs.

Allow outbound HTTPS, except the cloud metadata API

apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: allow-internet-no-metadata
  namespace: shop
spec:
  podSelector:
    matchLabels:
      app: fetcher
  policyTypes:
  - Egress
  egress:
  - to:
    - ipBlock:
        cidr: 0.0.0.0/0
        except:
        - 169.254.169.254/32
    ports:
    - protocol: TCP
      port: 443

The metadata API at 169.254.169.254 can hand out cloud credentials of the node, which is why the Kubernetes security checklist says to restrict it when Pods do not need it. Check before you block it: on GKE with Workload Identity Federation, Pods must be able to reach the metadata server, and on EKS a Pod that uses the node's instance role needs it too.

3 things this example does not do:

  • 0.0.0.0/0 is every IPv4 address. That includes your VPC's private ranges and, depending on the plugin, nodes, Pods and the API server (see ipBlock). Add your private ranges to except if the Pod should only reach public addresses. On a dual-stack cluster, IPv6 needs its own entry
  • except only narrows this rule. Policies add up, so another policy that selects the same Pods can still allow the address
  • The port already does half the job. The metadata API answers on port 80, and this rule allows 443 only. The except starts to matter when you add port 80 or drop the ports section

How to check network policies in Kubernetes

List them. The short name is netpol:

kubectl get netpol -A
kubectl get netpol -n shop -o yaml

Read one the way Kubernetes read it:

kubectl describe networkpolicy default-deny-ingress -n shop

The output spells out the effect in words. For the default deny at the top of this article, it looks roughly like this (the phrases come from the kubectl source, the layout is an example):

Spec:
  PodSelector:     <none> (Allowing the specific traffic to all pods in this namespace)
  Allowing ingress traffic:
    <none> (Selected pods are isolated for ingress connectivity)
  Not affecting egress traffic
  Policy Types: Ingress

A direction the policy does not touch reads Not affecting ingress traffic or Not affecting egress traffic. If you expected a policy to isolate egress and you see that line, policyTypes is the reason. A policy that does isolate egress and has no rules prints <none> (Selected pods are isolated for egress connectivity).

There is no kubectl command that answers "which policies select this Pod" or "can A reach B". You compare the Pod's labels with each policy's podSelector in the namespace:

kubectl get pod api-7d9f8 -n shop --show-labels
kubectl get netpol -n shop

Then test with real traffic. The "yes" from a real client Pod, the "no" from a throwaway Pod without the labels:

kubectl exec -n shop deploy/web -- wget --spider --timeout=1 api:8080
kubectl run test --rm -ti --image=busybox -n shop -- wget --spider --timeout=1 api:8080

(Do not start a test Pod with --labels="app=web" in a live namespace. If a Service selects app=web, it will send real requests to your busybox. And if the client image has no wget or curl, run the test in a staging namespace.)

4 notes on reading the result:

  • A blocked connection usually times out. You get wget: download timed out, where a closed port answers at once with "connection refused". Always set a timeout
  • Test by name and by IP. If the IP works and the name does not, the allow rule is fine and DNS is what is blocked
  • Test the "no" as well as the "yes". A policy that allows everyone passes the positive test
  • api:8080 is the Service's port. The policy names the container port. They can differ

For bigger setups, the SIG Network project has a CLI called policy-assistant that simulates which policies allow or deny a given connection, from a cluster or from YAML files.

Which network plugins enforce NetworkPolicy

PluginEnforces NetworkPolicy?Notes
CalicoYesAdds its own policy kinds with deny rules, ordering and logging
CiliumYeseBPF based. Adds layer 7 rules, DNS name rules and deny policies
AntreaYesAdds its own tiered policies for cluster admins
kube-routerYesPolicy controller enabled with --run-firewall
FlannelNoThe flanneld binary does not enforce policies. Its Helm chart has a netpol.enabled option that deploys a separate controller
kind (its default CNI)Yes, since v0.24.0Through the kube-network-policies controller
Amazon VPC CNIYes, when enabledOff until you turn it on

"Enforces NetworkPolicy" does not mean identical behavior. The API leaves several things to the plugin: ipBlock against Pod IPs, hostNetwork Pods, ICMP, what happens to open connections. A policy set that was tested on Calico needs a new test on Cilium, and the other way around.

Network policy on EKS, GKE and AKS

None of the 3 enforces policies on every cluster out of the box.

Amazon EKS

  • The VPC CNI supports NetworkPolicy since v1.14.0 (August 2023), and it is off until you set "enableNetworkPolicy": "true" in the add-on configuration. Today's EKS docs ask for version 1.21 if you also want admin policies
  • It applies to EC2 Linux nodes only: Fargate and Windows nodes are not covered
  • In the default standard mode, a new Pod starts with a default allow until its policies are configured, so there is a short window where everything is open. Strict mode starts Pods with a default deny, and then CoreDNS itself needs policies
  • The Service port must equal the container port, and a named port needs the same name in the Service
  • There is a limit of 24 port and protocol combinations per IP range across all policies
  • If a policy does nothing, check that a policyendpoint object exists in the Pod's namespace

Google GKE

  • With Dataplane V2 (Cilium based), enforcement is always on. Dataplane V2 can only be chosen when the cluster is created. To check: kubectl -n kube-system get pods -l k8s-app=cilium shows Pods named anetd-
  • On a Standard cluster without Dataplane V2, enabling the Calico based enforcement takes 2 gcloud commands and re-creates the nodes. Until then, policies are not enforced
  • Policies must allow egress to the metadata server when workloads use Workload Identity Federation

Azure AKS

  • 3 engines: Cilium (with Azure CNI Powered by Cilium), Azure Network Policy Manager and Calico. Microsoft recommends Cilium
  • Azure Network Policy Manager is on its way out: support on Windows nodes ended on September 30, 2026, and on Linux nodes it ends on September 30, 2028
  • Installing Azure Network Policy Manager or Calico on an existing cluster with az aks update --network-policy reimages every node pool at the same time
  • AKS-managed Calico only supports the standard networking.k8s.io policies

What NetworkPolicy cannot do

Most of these are on the Kubernetes docs' own list of what NetworkPolicy cannot do, as of v1.37:

You want toNetworkPolicyWhere it lives
Write an explicit deny ruleNo. Allow rules onlyClusterNetworkPolicy, or Calico and Cilium policies
Apply 1 policy to every namespaceNoClusterNetworkPolicy, or Calico and Cilium cluster-wide policies
Filter by HTTP path, method or hostnameNo. Layer 3 and 4 onlyA service mesh, or Cilium layer 7 rules
Encrypt traffic or require TLSNoA service mesh, or the plugin's encryption
Target a Service by nameNo. Pods and namespaces by labelCalico service rules, Cilium
Log blocked connectionsNoThe plugin's own tooling (see below)
Write a policy for nodesNo. Only CIDRsCalico host endpoints, Cilium host policies
Block traffic from the Pod's own nodeNo. It is always allowed
Force traffic through a common gatewayNoA service mesh or an egress gateway

NetworkPolicy answers 1 question: may this Pod open a connection to that address on that port. It does not know who is calling at the application level. For identity you still need mTLS or tokens.

The traps: Services, hostNetwork and new Pods

Rules name the Pod port

A Service maps its port to a targetPort on the Pods. With Calico and Cilium, the policy is checked after that mapping. So for a Service that listens on 80 and forwards to containers on 8080, the rule says 8080. A rule for port 80 allows nothing. (On EKS with the VPC CNI, AWS requires the Service port to equal the container port, so a Service that maps 80 to 8080 has to change.)

hostNetwork Pods are treated as the node

A Pod with hostNetwork: true shares the node's IP. The docs call the behavior undefined and describe the most common one: the plugin ignores these Pods when matching podSelector and namespaceSelector, and treats their traffic as node traffic.

2 results. A policy that selects a hostNetwork Pod usually does not restrict it. And a rule that allows "Pods in the ingress-nginx namespace" does not match a hostNetwork ingress controller: its traffic arrives from node IPs.

Combine that with the rule that a Pod's own node is always allowed in: traffic that reaches a Pod with its local node's IP as the source gets through a default deny on the common plugins.

The API server is outside your selectors

2 kinds of traffic that a default deny breaks and that no Pod selector can allow:

  • Pods that call the Kubernetes API. Controllers, operators and anything with a client library need egress to the API server. The rule is plugin specific. Cilium has a kube-apiserver entity, and Calico can allow the kubernetes Service by name. With plain NetworkPolicy it is usually an ipBlock for the API server's addresses (kubectl get endpointslices -n default shows them), and whether it matches depends on the plugin. Test it
  • The API server calling Pods. Admission webhooks and aggregated APIs such as metrics-server receive connections from the control plane, which is not a Pod. Under a default deny for ingress they need an ipBlock rule for the control plane's addresses. A blocked webhook that is set to fail closed stops every create it covers

Traffic through a load balancer may change its source

With externalTrafficPolicy: Cluster, a connection reaches the Pod with the IP of the node that received it as the source. An ipBlock rule for the real client range will not match it. On GKE, the Application Load Balancer's health check ranges need an allow rule too. Check the same for your cloud.

A new Pod can start before its policy is in place

Policies are applied by an agent on each node, and that takes a moment. The docs describe both outcomes:

  • A Pod created right after a new policy may start unprotected for a short time
  • A Pod may start with the isolation applied and the allow rules still on their way, which means no connectivity at all at first

The API gives no way to ask "is this policy active yet". The advice in the docs is to write apps that tolerate a missing connection at startup, and to use an init container that waits for a required destination when it matters. Retry on startup is the cheap fix.

Open connections may survive a policy change

Whether a new policy cuts connections that are already open is up to the plugin. With Calico, changes apply to new connections at once, and to open ones only after they reconnect. A long-lived database connection can keep working after the rule that allowed it is gone. If you need the cut now, restart the client Pods.

ClusterNetworkPolicy: rules for the whole cluster

Everything above is per namespace, and written by whoever owns the namespace. Platform teams want the opposite: 1 rule for the whole cluster that application teams cannot override, such as "nothing talks to the metadata API" or "monitoring may scrape everything".

The Kubernetes project's answer is ClusterNetworkPolicy, from the SIG Network policy API group. It replaced the earlier AdminNetworkPolicy and BaselineAdminNetworkPolicy, and reached v1beta1 on November 6, 2026 (with the same schema as v1alpha2, which some plugin docs still show). It is a CRD in the policy.networking.k8s.io group that you install, and your plugin has to support it.

What it adds to NetworkPolicy:

  • Cluster scope. 1 object covers Pods in many namespaces
  • Actions. Each rule says Accept, Deny or Pass. Deny exists here
  • Tiers. Evaluation goes Admin tier, then NetworkPolicy, then Baseline tier. Admin rules win over the namespace policies. Baseline rules are defaults that a namespace policy can override
  • Priority. A number from 0 to 1000 inside a tier. Lower goes first

(A detail for the Baseline tier: a NetworkPolicy always makes the final decision for the Pods it selects. Baseline rules only apply to Pods that no NetworkPolicy selects.)

Listed implementations as of December 2026: kube-network-policies, Calico (from v3.32), Cilium (from v1.20, behind a flag) and Kube-OVN (experimental). EKS has its own ClusterNetworkPolicy under a separate AWS API group.

Calico and Cilium also have their own older policy kinds (Calico's GlobalNetworkPolicy, Cilium's CiliumClusterwideNetworkPolicy) with deny rules and more matching options. They work, and they tie the policies to that plugin. Cilium's docs warn that running several policy kinds at once makes the result hard to reason about. My default: plain NetworkPolicy for what application teams own, 1 cluster-wide kind for the platform rules, and nothing else.

Where to see dropped traffic

NetworkPolicy has no logging. A blocked packet disappears, and the app sees a timeout. To see what was dropped you ask the plugin:

PlatformHow
Ciliumhubble observe --verdict DROPPED. A policy drop reads "Policy denied DROPPED"
Cilium, before enforcingPolicy Audit Mode (--policy-audit-mode=true) allows everything and logs what would have been dropped
CalicoA Log action in a Calico policy writes to the node's kernel log (iptables data plane). Calico Whisker (tech preview) shows flow logs with denies
EKS (VPC CNI)Network policy logs on the node, off by default, at /var/log/aws-routed-eni/network-policy-agent.log
GKENetwork policy logging, on Dataplane V2 only

Set this up before you roll out a default deny for egress. Finding the 1 blocked call that broke checkout is quick with flow logs and slow without them.

Kubernetes network policy best practices: a checklist

  1. Confirm the plugin enforces NetworkPolicy, with a test that proves a connection is blocked
  2. Put a default deny for ingress and egress in every application namespace, created together with the namespace. Handle kube-system and controller namespaces separately
  3. Ship the DNS allow rule in the same change as the egress deny
  4. Write policyTypes in every policy
  5. Select namespaces with kubernetes.io/metadata.name, or with labels that only admins can set
  6. Run kubectl describe on every policy that has both a namespaceSelector and a podSelector
  7. Use selectors for anything inside the cluster and ipBlock for what is outside
  8. Write the container port in rules, where a Service maps to another port
  9. Restrict the cloud metadata API for Pods that do not need it
  10. Keep each policy next to the app it protects, in the same chart or repo, and name it after what it allows (api-allow-web)
  11. Test the "no" as well as the "yes", with a timeout, by name and by IP
  12. Turn on the plugin's drop logs before the egress rollout
  13. Make apps retry at startup
  14. Re-test the policy set when you change the plugin or move to another cloud

I do number 1 on every new cluster, including the ones where "of course it is enabled". A policy that is stored and ignored looks exactly like a policy that works, until you test it.

FAQ

What is a network policy in Kubernetes?

A NetworkPolicy is a namespaced object that controls which connections Pods may accept and open, by Pod label, namespace label, IP range and port. It works at layer 3 and 4 and is enforced by the cluster's network plugin.

Is there a default network policy in Kubernetes?

No. Kubernetes itself creates none, and without policies every Pod can reach every other Pod. A "default deny" is a policy you create yourself, per namespace, with an empty podSelector and no rules.

How do I deny all traffic in a namespace?

Create a NetworkPolicy with podSelector: {}, both Ingress and Egress in policyTypes, and no rules. Add a rule that allows DNS on port 53, or name resolution stops for every Pod in the namespace.

Why is my network policy not working?

The usual causes, in order: the network plugin does not enforce NetworkPolicy (or the feature is switched off, as on EKS by default), the podSelector does not match the Pod's labels, the policy is in another namespace than the Pod, policyTypes does not include the direction you meant, or the rule names the Service port where it should name the container port.

Do network policies apply to Services?

Policies select Pods. They cannot select a Service. Traffic that goes through a Service is checked against the Pods behind it. With Calico and Cilium the check happens after the Service's port mapping, so rules use the Pod's port.

What is the difference between ingress and egress in a network policy?

Ingress rules control connections coming into the selected Pods. Egress rules control connections those Pods open to others. They are isolated separately, and for a connection between 2 Pods both the source's egress and the destination's ingress must allow it.

Can a network policy block traffic between namespaces?

Yes. A default deny for ingress in a namespace blocks traffic from every other namespace, and from Pods in the same namespace too. You then allow the sources you want with a podSelector or a namespaceSelector.

Does a network policy affect Pods that are already running?

Yes, new connections to and from running Pods follow the policy once the plugin has applied it. Connections that are already open may or may not be cut: that depends on the plugin.

Network policies are cheap to write and easy to postpone, and a flat network is what turns 1 compromised Pod into a compromised cluster. If you want a second pair of eyes on how your clusters are segmented, book a free DevOps assessment. For the kubectl side of all this, the kubectl cheat sheet has the commands in one place.