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.
Select the destination Pods
- Ingress gateAPIno ingress allow
An ingress policy selects the API. An empty allow set blocks new inbound connections in that direction.
Allow the intended ingress
- Extra allow policyIngress gateadd allowed edge
- Ingress gateAPITCP 8080
A second policy can allow frontend TCP 8080. It adds permission even while default-deny remains present.
Check client egress
- FrontendEgress gateegress denied
If frontend egress is isolated without a matching allow, the connection still fails before reaching the permitted destination.
Both directions permit traffic
- FrontendEgress gateallow
- Egress gateIngress gateTCP 8080
- Ingress gateAPIallow
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.
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.
- Before
- No probe yet
- After
- HTTP_OK
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.
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 labInspect 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: 8080Indentation 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.
Choose which server is isolated
- API policyAPI Podapp=api
Top-level podSelector selects the API Pods in the policy namespace; it does not select the clients.
Namespace and Pod in one peer
- Shop frontendAPI policyboth match
- Other frontendAPI policynamespace mismatch
- Shop batchAPI policyPod label mismatch
Both namespace team=shop and Pod role=frontend must match, allowing only the shop frontend among these sources.
Separate peers broaden access
- Shop batchAPI policynamespace peer alone
If namespaceSelector and podSelector become separate list entries, either peer can match. Shop batch would then match the namespace peer.
Test from a real client Pod
- Shop frontendAPI Podpositive probe
- Other frontendAPI Podnegative probe
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.
Explore another case
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