Appearance

NetworkPolicy and isolation

Restrict which clients can connect to an API and prove the policy using allowed and blocked requests.

What you will understand

  • Identify which Pods a NetworkPolicy isolates and in which directions.
  • Read namespace and Pod selectors within each peer entry.
  • Verify a working baseline and both outcomes after applying a rule.

Builds on: Pod networks and service routing

When a NetworkPolicy blocks a connection

An API should accept requests from the shop frontend, but another application in the cluster can currently call it. A NetworkPolicy lets you describe allowed connections to or from selected Pods. A compatible network implementation has to enforce the policy. Creating the object alone does not prove traffic changed.

Policies select Pods in their own namespace. A Pod becomes isolated for ingress when an ingress policy selects it, and for egress when an egress policy selects it. A new connection between two Pods must satisfy the source’s egress rules if it is egress-isolated, and the destination’s ingress rules if it is ingress-isolated. Reply traffic for an allowed connection is implicitly allowed. Toggle each direction in the scene and check both ends before predicting the outcome.

Open Inspect the policy decision to compare three fresh probes from the lab’s original client. Follow the working baseline, the blocked request, and the narrow allowance that restores HTTP.

Both policies can affect the same connection

For a Pod-to-Pod connection, evaluate source egress and destination ingress for whichever directions are isolated.

  1. Select the destination Pods

    An ingress policy selects the API. An empty allow set blocks new inbound connections in that direction.

  2. Allow the intended ingress

    A second policy can allow frontend TCP 8080. It adds permission even while default-deny remains present.

  3. Check client egress

    If frontend egress is isolated without a matching allow, the connection still fails before reaching the permitted destination.

  4. Both directions permit traffic

    Once the relevant source egress and destination ingress allow this edge, the connection and its return traffic can pass.

An enforcing implementation and ordinary cross-Pod TCP connection; reply traffic is implicit; self and own-node exceptions are outside this path.

Inspect the policy decision

Follow the networking lab’s HTTP probe from m08-client to the m08 backend on TCP port 80. The client and backend are in kv-lab.

1 / 5

From m08-clientTo Policy enforcement

Confirm HTTP works first

The client reaches the ready backend through its Service. The successful probe gives you a working baseline before any policy change.

HTTP probe from m08-client
Before
No probe yet
After
HTTP_OK
Confirm HTTP works first

The client reaches the ready backend through its Service. The successful probe gives you a working baseline before any policy change.

This cluster has NetworkPolicy enforcement. The client’s egress is unrestricted, the backend stays ready, and each probe opens a fresh connection. The diagram shows the effective policy decision. Packet handling varies by network plugin.

If the client becomes egress-isolated and no egress rule allows this traffic, which direction now blocks the connection?
Reveal prediction answer

Both directions have to permit the connection. An ingress allowance leaves source egress rules in effect.

Compare the client labels, the two NetworkPolicy objects, and the output of fresh probes from m08-client.

Reproduce all three HTTP results in the NetworkPolicy lab
Inspect YAML or commands

Isolate API ingress

policyTypes: [Ingress]
ingress: []
Explore another case

Allow the shop frontend to call the API

The example selects Pods labeled app: api in the policy’s namespace. Its one peer entry lists both a namespaceSelector and a podSelector, so a client must satisfy both. It needs role: frontend and its namespace needs team: shop. The rule applies to TCP port 8080 on the backend. A separate Service might expose port 80 to the caller and forward it to 8080.

apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: api-from-shop-frontend
spec:
  podSelector:
    matchLabels: {app: api}
  policyTypes: [Ingress]
  ingress:
    - from:
        - namespaceSelector:
            matchLabels: {team: shop}
          podSelector:
            matchLabels: {role: frontend}
      ports:
        - protocol: TCP
          port: 8080

Indentation matters. Putting the two selectors in separate list entries would allow either group. A Pod selector by itself refers to Pods in the policy’s namespace. NetworkPolicy permissions are additive across the policies that select a Pod in a direction. A second policy granting broader access can permit a connection excluded by the first policy’s peer list. Inspect the selector scene and predict which clients pass.

Read peer selectors the way Kubernetes does

Two selectors in one peer entry must both match. Separate peer entries add alternatives.

  1. Choose which server is isolated

    Top-level podSelector selects the API Pods in the policy namespace; it does not select the clients.

  2. Namespace and Pod in one peer

    Both namespace team=shop and Pod role=frontend must match, allowing only the shop frontend among these sources.

  3. Separate peers broaden access

    If namespaceSelector and podSelector become separate list entries, either peer can match. Shop batch would then match the namespace peer.

  4. Test from a real client Pod

    Run baseline, denied and allowed requests on the actual cluster. Persisted policy YAML alone proves no data-plane behavior.

Labels shown on the two source Pods and their namespaces are the only peer attributes used.

Test before and after the policy

Start with a successful request from each test client. Record its namespace labels, Pod labels, and the Service port. Apply the policy, then open a fresh connection from the permitted frontend and another from a client outside the allowed set. A successful baseline helps rule out a bad hostname or a missing endpoint. Enforcement can take time after the API accepts the policy.

kubectl get networkpolicies
kubectl get namespaces --show-labels
kubectl get pods --show-labels -o wide
# Establish a working baseline from the example client first.
# This assumes a Service exposing port 80 and forwarding to backend 8080.
kubectl exec client -- wget -T 3 -qO- http://api:80
kubectl apply -f api-policy.yaml
kubectl describe networkpolicy api-from-shop-frontend
# Repeat from one permitted and one deliberately unpermitted client.

Use the networking lab to inspect the environment. Follow the policy experiment only after confirming your cluster has a policy-capable network implementation. A basic kind setup may require an additional network setup for enforcement.

Check for other rules and DNS failures

A policy that isolates egress can block DNS, so separate name lookup from the application request. Existing connections and traffic involving nodes can have implementation-specific behavior or exceptions. Start fresh connections from known clients and make one change at a time. That gives you a reproducible result to explain.

Check your understanding

A frontend Pod has the right role label, but is in a namespace with team: payments. Does the example allow it?

The namespace does not match team: shop, so the peer entry does not allow that client. Inspect other policies that select the API as well, since permissions from an additional policy are added to this one.

Primary documentation

Go to the source

Reviewed 2026-10-27 · Kubernetes 1.37 · Related local exercise

Search the book

Search loads when you open it.

Keyboard shortcuts

Ctrl + K / ⌘ + K
Search the book
?
Open this guide
Tab / Shift + Tab
Move between controls
Enter / Space
Activate a focused button or disclosure
← / →
Focused visual timeline · previous or next step
Home / End
Focused visual timeline · first or last point
Esc
Close the open dialog or menu