Skip to content

Linux Foundation Black Friday Sale: Up to 65% off Save now

Back to the blog
Glossary6 min read

Kubernetes network policy example: deny all, then allow

Sajeeva Nambadawa Vithanage22 Nov 2026 · Senior Software Site Reliability Engineer

A Kubernetes network policy example is the quickest way to understand a feature that trips people up in real clusters and in hands-on exams alike. By default every pod can talk to every other pod. A NetworkPolicy is how you change that, one label at a time.

This explainer walks through how NetworkPolicy actually decides what's allowed, then builds a small, realistic set of policies you can apply to a test namespace today.

Key points

  • Pods accept all traffic until a NetworkPolicy selects them. Then only what a policy allows gets through.
  • Policies are additive: there are no "deny" rules, only allow lists that add up.
  • One from item with two selectors means AND. Two items mean OR. This is the most common mistake.
  • A default deny on egress also blocks DNS, so you'll need to allow it back.
  • Nothing happens unless your network plugin enforces NetworkPolicy.

How NetworkPolicy decides what's allowed

A NetworkPolicy selects pods with a podSelector and says which traffic those pods may receive (Ingress), send (Egress), or both, via the policyTypes list.

The rules that matter:

  1. Unselected pods are open. The Kubernetes docs are clear that, by default, a pod is non-isolated for both ingress and egress: all connections are allowed.
  2. Selection isolates. Once any policy selects a pod and lists Ingress in its policyTypes, that pod only accepts connections the policy allows (plus traffic from its own node). The same goes for Egress.
  3. Policies add up. If several policies select the same pod, the allowed traffic is the union of all of them. Order doesn't matter, and you can't write a rule that removes something another policy allows.
  4. Both ends must agree. For a connection between two pods, the egress policy on the source and the ingress policy on the destination both need to allow it.

One thing people forget: NetworkPolicy is implemented by the network plugin. The docs put it plainly: creating a NetworkPolicy "without a controller that implements it will have no effect". If your test cluster's plugin doesn't enforce policies, your YAML is accepted and silently ignored.

A Kubernetes network policy example, step by step

Picture a namespace called shop with three kinds of pods: app: web, app: api and app: db. The goal is simple: web can reach the API, the API can reach the database, and nothing else gets in.

1. Deny everything coming in

Start by isolating every pod in the namespace for ingress. An empty podSelector selects all pods, and having no ingress rules means nothing is allowed. This is the docs' own default-deny example:

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

2. Allow web to reach the API

Now add a policy that selects the API pods and lets in traffic from web pods on the API's port:

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

A podSelector inside from with no namespaceSelector only matches pods in the policy's own namespace.

3. Allow the API to reach the database

Same pattern, one tier down:

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

The port numbers here are only examples; use whatever your containers listen on.

4. Check what you've got

kubectl get networkpolicy -n shop
kubectl describe networkpolicy api-from-web -n shop

describe prints the selectors and ports in plain words, which is the fastest way to spot a typo in a label.

The AND versus OR trap

This is the part that catches people. Look closely at the dashes.

One item in from that has both a namespaceSelector and a podSelector means both must match: pods labelled role: client in namespaces labelled user: alice.

ingress:
- from:
  - namespaceSelector:
      matchLabels:
        user: alice
    podSelector:
      matchLabels:
        role: client

Two items (note the second dash) mean either matches: pods labelled role: client in the local namespace, or any pod in a namespace labelled user: alice.

ingress:
- from:
  - namespaceSelector:
      matchLabels:
        user: alice
  - podSelector:
      matchLabels:
        role: client

Both are valid YAML and both apply without complaint. Only one does what you meant.

Pro tip: to pick a namespace by name, use the label the control plane sets on every namespace, kubernetes.io/metadata.name. Its value is the namespace's name, and it can't be changed.

Egress and the DNS surprise

Locking down outbound traffic uses the same idea with Egress. The docs' default-deny-all example isolates both directions:

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

Apply that and your pods can no longer resolve service names. As the Kubernetes docs warn, a default deny-all egress policy also blocks DNS traffic, so you need a separate policy that allows egress to your cluster's DNS service. The egress rules also accept ipBlock (a CIDR, with optional except ranges) for traffic leaving the cluster, and endPort (stable since Kubernetes v1.25) for a range of ports.

What NetworkPolicy can't do

Reality check: NetworkPolicy is deliberately simple. The Kubernetes docs list several things it doesn't cover, including:

  • explicit deny rules (the model is "deny by default, allow by rule");
  • anything TLS related;
  • targeting Services by name (use pod and namespace labels instead);
  • default policies applied to every namespace;
  • logging blocked or accepted connections.

If you need those, that's where your network plugin's own policy features or a service mesh come in. Know which you're using before you rely on it.

Why it matters for your exams

Network policies are a regular topic in Kubernetes administration and security study, so expect to read and write them under time pressure. Practise until you can spot the AND/OR difference at a glance and write a default deny from memory.

If you're preparing for the CKA, the CKS or the new CKNE, it's worth building the shop example above in a practice cluster with a plugin that enforces policies, then breaking it on purpose. And if you'd like someone to check your thinking, you can find a mentor on CertDirectory who has passed the exam you're working towards.

FAQ

What does a Kubernetes network policy do? It controls which traffic can reach selected pods (ingress) and which traffic they can send (egress), using pod labels, namespace labels and IP ranges. Pods that no policy selects stay fully open.

Does a NetworkPolicy work without Calico or Cilium? It works with any network plugin that enforces NetworkPolicy. If your plugin doesn't, Kubernetes accepts the object but nothing changes.

How do I deny all traffic in a namespace? Apply a policy with podSelector: {} and both Ingress and Egress in policyTypes, with no rules. Remember to allow DNS egress back if your pods need name resolution.

Can a NetworkPolicy deny a specific pod? Not directly. There are no deny rules: you isolate pods and then allow only the traffic you want.

How do I allow traffic from another namespace? Use a namespaceSelector in the from list, typically matching kubernetes.io/metadata.name: <namespace>. Add a podSelector in the same item if only some pods in that namespace should get in.

Sources

Build the example, break it, fix it, and you'll never mix up those two dashes again.