LevelUpWith

Explore / Exam Prep for the Certified Kubernetes Security Specialist (CKS) / Lesson 2

Lesson 2: Network Policies: Default Deny and Allow Rules

[Cluster Setup] Shows how unrestricted pod-to-pod traffic lets an attacker move sideways, then uses Network security policies to restrict cluster level access by applying a default-deny policy and allowing only named ingress flows by podSelector, namespaceSelector and port.

Free to read, no account needed. Part of Exam Prep for the Certified Kubernetes Security Specialist (CKS).

By the end of this level you'll be able to

  • Write default-deny ingress and egress NetworkPolicies for a namespace
  • Allow only specific pod-to-pod and namespace-to-namespace ingress on named ports
  • Prove a policy works with test pods and kubectl exec within a timed practice task

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)

  1. 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.
  2. Pods start non-isolated. A pod accepts all traffic until a policy with Ingress in policyTypes selects it. The same applies to outbound traffic and Egress.
  3. 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.
  4. Both ends must agree. A connection works only if the sender's egress rules and the receiver's ingress rules both allow it.
  5. 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 named containerPort) or port: 80, with protocol: 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:

  1. Get the target IP: DB_IP=$(kubectl -n shop get pod db -o jsonpath='{.status.podIP}')
  2. 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
  3. Test from a denied identity: the same command with --labels=app=rogue. It should time out.
  4. Test from another namespace: run it with -n monitoring and the right labels.
  5. If a result surprises you, run kubectl -n shop describe netpol and 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 a policyType with 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 from item means AND. Separate items means OR.
  • kubernetes.io/metadata.name selects a namespace by its name.
  • Default-deny egress also blocks DNS, so test with pod IPs and labelled busybox pods.

A 6-question knowledge check follows this lesson in the app, free and without an account.

Keep going: levels 3 and 4 are free

Create a free account to open the next two levels, try the AI tutor, save your progress and pick up where you left off. No card needed.

Next: Level 3, Egress Policies and Protecting Node Metadata.

Create a free account →See the whole course

More like this