Why this matters: flat networks let attackers move sideways
By default, Kubernetes allows every pod to reach every other pod in every namespace. Suppose an attacker gets a shell in a web pod through an app bug. From there they can reach your database, your internal admin APIs and pods in other teams' namespaces, with nothing at the network layer to stop them. Moving from one compromised workload to the next like this is called lateral movement.
The curriculum asks you to "Use Network security policies to restrict cluster level access." The method is always the same: block everything first, then allow only the traffic you name.
A reminder from Level 1: the CKS is a 2-hour, hands-on exam with 15 to 20 tasks and a pass mark of 67%. You must hold a valid CKA to sit it. Only timed practice at the terminal builds the speed you need.
How NetworkPolicy behaves (the rules to remember)
- Something must enforce it. A NetworkPolicy does nothing unless the network plugin supports it. Calico and Cilium do. So does kindnet in kind v0.24.0 and later.
- Pods start non-isolated. A pod accepts all traffic until a policy with
IngressinpolicyTypesselects it. The same applies to outbound traffic andEgress. - Policies only allow. You cannot write a "deny" rule. Policies add together, and the traffic allowed is the union of all policies that select the pod. The order they were applied in does not matter.
- Both ends must agree. A connection works only if the sender's egress rules and the receiver's ingress rules both allow it.
podSelector: {}selects every pod in the namespace where the policy lives.
Task 1: Apply default-deny policies
DO: Create these in the namespace you want to lock down (-n shop).
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: default-deny-ingress
namespace: shop
spec:
podSelector: {}
policyTypes:
- Ingress
---
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: default-deny-egress
namespace: shop
spec:
podSelector: {}
policyTypes:
- Egress
There is no ingress: or egress: list, so nothing is allowed. You can also put both types in one policy (policyTypes: [Ingress, Egress]).
Warning: a default-deny egress policy also blocks DNS lookups. Until you allow DNS (Level 3), test with pod IPs, not Service names.
Task 2: Allow only named flows
The allow rules you will write most often:
- Pod to pod (same namespace):
from: - podSelector: {matchLabels: {app: api}} - Namespace to namespace:
from: - namespaceSelector: {matchLabels: {kubernetes.io/metadata.name: monitoring}}. Kubernetes adds this label to every namespace automatically, so you can select a namespace by its name without labelling it first. - Port:
ports: - port: http(a namedcontainerPort) orport: 80, withprotocol: TCP.
The AND/OR trap. Look closely at the dashes:
# AND: probe pods that are IN the monitoring namespace
- from:
- namespaceSelector: {matchLabels: {kubernetes.io/metadata.name: monitoring}}
podSelector: {matchLabels: {app: probe}}
# OR: ANY pod in monitoring, OR app=probe pods in the policy's own namespace
- from:
- namespaceSelector: {matchLabels: {kubernetes.io/metadata.name: monitoring}}
- podSelector: {matchLabels: {app: probe}}
One list item (one dash) holding both selectors means AND. Two list items means OR. Getting this wrong opens access far wider than the task asked for.
Task 3: Prove it with test pods
DO, in order:
- Get the target IP:
DB_IP=$(kubectl -n shop get pod db -o jsonpath='{.status.podIP}') - Test from an allowed identity:
kubectl -n shop run t1 --rm -it --restart=Never --image=busybox:1.36 --labels=app=api -- wget -qO- -T 2 http://$DB_IP - Test from a denied identity: the same command with
--labels=app=rogue. It should time out. - Test from another namespace: run it with
-n monitoringand the right labels. - If a result surprises you, run
kubectl -n shop describe netpoland read the selectors line by line.
The --labels flag lets one test pod take on any identity, which is quicker than editing existing pods.
Timed practice task (8 minutes)
Setup:
kubectl create ns shop; kubectl create ns monitoring
cat <<EOF | kubectl apply -f -
apiVersion: v1
kind: Pod
metadata: {name: db, namespace: shop, labels: {app: db}}
spec:
containers:
- name: web
image: nginx
ports: [{name: http, containerPort: 80}]
EOF
Task: In shop, deny all ingress by default. Then allow traffic to app=db pods on the named port http only from (a) app=api pods in shop and (b) app=probe pods in the monitoring namespace. Prove that app=api works, app=rogue fails, and app=probe in monitoring works.
Solution:
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata: {name: default-deny-ingress, namespace: shop}
spec:
podSelector: {}
policyTypes: [Ingress]
---
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata: {name: db-allow, namespace: shop}
spec:
podSelector: {matchLabels: {app: db}}
policyTypes: [Ingress]
ingress:
- from:
- podSelector: {matchLabels: {app: api}}
- namespaceSelector: {matchLabels: {kubernetes.io/metadata.name: monitoring}}
podSelector: {matchLabels: {app: probe}}
ports:
- {protocol: TCP, port: http}
Check it with the three kubectl run ... --labels=... tests from Task 3. Run the probe test with -n monitoring.
What the exam asks about this
- Write a default-deny policy for a namespace, often with a set name you must use exactly.
- Allow traffic from one namespace, one pod label or a combination, on a given port. Expect the AND/OR distinction to matter.
- Fix an existing policy that lets too much through. Look for a stray dash or an empty selector.
- Use the NetworkPolicy page on kubernetes.io as your template source. Copy the YAML from it, then edit.
Key takeaways
- Without policies, pod traffic is flat, so one compromised pod can reach everything else.
podSelector: {}plus apolicyTypewith no rules gives you default deny.- Policies only allow and add together. Both the sender's egress and the receiver's ingress must allow a connection.
- Both selectors in one
fromitem means AND. Separate items means OR. kubernetes.io/metadata.nameselects a namespace by its name.- Default-deny egress also blocks DNS, so test with pod IPs and labelled busybox pods.