Skip to content
Hands-on IT IT Drills

Day 1: NetworkPolicy Basics

Goal: Lock down the namespace shop with default deny and then open it selectively, with podSelector, namespaceSelector, ports and egress. · Duration: approx. 75 min · Prerequisite: Day 0

Briefing

  • Without a policy, everything is allowed. As soon as a policy selects a Pod for a direction (ingress or egress), everything that is not explicitly allowed is forbidden for that Pod in that direction.
  • Policies are additive: There is no “deny” field. Multiple policies result in the union of their permissions.
  • spec.podSelector selects the Pods that are protected. from/to describe the other side.
  • A podSelector in from only means Pods in the policy’s own namespace.
               policy selects api (podSelector app=api)
  frontend ──────────── from: podSelector app=frontend ───► api :8080  ✔
  client   ─────────────────────────────────────────────X  api         ✘

Drill 1: Measure the starting point

cd ~/guard-lab/tag01
alias cc=~/guard-lab/code/demo/conncheck.sh
kubectl get networkpolicy -A
~/guard-lab/code/matrix.sh | head -3

Expected:

No resources found
✔ erlaubt   default/client -> http://frontend.shop (HTTP 200)
✔ erlaubt   default/client -> http://api.shop (HTTP 200)
✔ erlaubt   default/client -> tcp://db.shop:5432

Everyone may do everything, including the client from default talking directly to the database. That is the normal state of a fresh cluster.

Tip: No matrix.sh? Then you skipped the challenge from Day 0. The solution is there.

Save your progress? Sign up for free

Drill 2: Default deny for ingress

cat > default-deny.yaml <<'EOF'
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: default-deny-ingress
  namespace: shop
spec:
  podSelector: {}
  policyTypes:
  - Ingress
EOF
kubectl apply -f default-deny.yaml
cc default/client http://frontend.shop
cc shop/frontend http://api.shop
cc shop/api tcp://db.shop:5432
cc shop/frontend http://api.tenant-a

Expected:

networkpolicy.networking.k8s.io/default-deny-ingress created
✘ blockiert default/client -> http://frontend.shop (Timeout)
✘ blockiert shop/frontend -> http://api.shop (Timeout)
✘ blockiert shop/api -> tcp://db.shop:5432 (keine Verbindung)
✔ erlaubt   shop/frontend -> http://api.tenant-a (HTTP 200)

podSelector: {} selects all Pods in the namespace. policyTypes: [Ingress] without ingress rules means: nothing may come in. Not even Pods in the same namespace. Going out (tenant-a) still works, because egress is not affected.

kubectl -n shop get pods
kubectl -n monitoring logs deploy/scraper --tail=3

Expected:

NAME                        READY   STATUS    RESTARTS   AGE
api-5ccd4587ff-gwhbn        1/1     Running   0          4m4s
db-5d6755ff6b-z5vv9         1/1     Running   0          4m4s
frontend-5f9bb6b549-8hpqx   1/1     Running   0          4m4s
FAIL shop
ok   tenant-a
ok   tenant-b

The Pods stay Ready: In kindnet, the kubelet’s readiness probes still get through (more on that on Day 2). The scraper, on the other hand, is locked out of shop.

Save your progress? Sign up for free

Drill 3: podSelector – frontend may reach the api

cat > api-from-frontend.yaml <<'EOF'
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: api-from-frontend
  namespace: shop
spec:
  podSelector:
    matchLabels:
      app: api
  policyTypes:
  - Ingress
  ingress:
  - from:
    - podSelector:
        matchLabels:
          app: frontend
    ports:
    - protocol: TCP
      port: 8080
EOF
kubectl apply -f api-from-frontend.yaml
cc shop/frontend http://api.shop
cc shop/db http://api.shop
cc default/client http://api.shop

Expected:

networkpolicy.networking.k8s.io/api-from-frontend created
✔ erlaubt   shop/frontend -> http://api.shop (HTTP 200)
✘ blockiert shop/db -> http://api.shop (Timeout)
✘ blockiert default/client -> http://api.shop (Timeout)

Why port 8080 and not 80? The Service translates 80 to the targetPort 8080. The policy only sees the packet after that, at the Pod. The port in a policy is always the container port.

kubectl -n shop describe networkpolicy api-from-frontend | sed -n '/Spec/,$p'

Expected:

Spec:
  PodSelector:     app=api
  Allowing ingress traffic:
    To Port: 8080/TCP
    From:
      PodSelector: app=frontend
  Not affecting egress traffic
  Policy Types: Ingress

describe is your fastest reading test: It translates the YAML into sentences.

Save your progress? Sign up for free

Drill 4: namespaceSelector – frontend for everyone

The frontend should be reachable from every namespace (later only from the ingress controller, Day 3).

cat > frontend-from-all.yaml <<'EOF'
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: frontend-from-all
  namespace: shop
spec:
  podSelector:
    matchLabels:
      app: frontend
  policyTypes:
  - Ingress
  ingress:
  - from:
    - namespaceSelector: {}
    ports:
    - port: http
EOF
kubectl apply -f frontend-from-all.yaml
cc default/client http://frontend.shop
kubectl exec client -- curl -s http://frontend.shop/api/orders
cc default/client http://api.shop

Expected:

networkpolicy.networking.k8s.io/frontend-from-all created
✔ erlaubt   default/client -> http://frontend.shop (HTTP 200)
{"svc":"api","ns":"shop","path":"/api/orders"}
✘ blockiert default/client -> http://api.shop (Timeout)

Two lessons: namespaceSelector: {} means “all Pods in all namespaces”. And port: http is a named port: It points to the port that the container calls http. The client now reaches the api only via the frontend, exactly as it should be.

Save your progress? Sign up for free

Drill 5: AND or OR – the indentation trap

The scraper from monitoring should read the api’s /metrics (port 9113). First, a troublemaker in the monitoring namespace:

kubectl -n monitoring run intruder --image=curlimages/curl:8.22.0 \
  --labels=app=intruder -- sleep 3600
cat > api-metrics-or.yaml <<'EOF'
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: api-metrics
  namespace: shop
spec:
  podSelector:
    matchLabels:
      app: api
  policyTypes:
  - Ingress
  ingress:
  - from:
    - namespaceSelector:
        matchLabels:
          purpose: monitoring
    - podSelector:
        matchLabels:
          app: scraper
    ports:
    - port: metrics
EOF
kubectl apply -f api-metrics-or.yaml
cc monitoring/scraper http://api.shop:9113/metrics
cc monitoring/intruder http://api.shop:9113/metrics

Expected:

pod/intruder created
networkpolicy.networking.k8s.io/api-metrics created
✔ erlaubt   monitoring/scraper -> http://api.shop:9113/metrics (HTTP 200)
✔ erlaubt   monitoring/intruder -> http://api.shop:9113/metrics (HTTP 200)

Two dashes under from are two sources, so OR: “any Pod in a monitoring namespace” or “Pods with app=scraper in the namespace shop”. The intruder gets through. Now a single dash, both selectors in one element, so AND:

sed -e 's/    - podSelector:/      podSelector:/' api-metrics-or.yaml \
  > api-metrics-and.yaml
diff api-metrics-or.yaml api-metrics-and.yaml
kubectl apply -f api-metrics-and.yaml
cc monitoring/scraper http://api.shop:9113/metrics
cc monitoring/intruder http://api.shop:9113/metrics

Expected:

17c17
<     - podSelector:
---
>       podSelector:
networkpolicy.networking.k8s.io/api-metrics configured
✔ erlaubt   monitoring/scraper -> http://api.shop:9113/metrics (HTTP 200)
✘ blockiert monitoring/intruder -> http://api.shop:9113/metrics (Timeout)

Two spaces and a hyphen decide your security. Read every policy back with describe: With AND, NamespaceSelector and PodSelector sit directly below each other in the same From block.

Warning: purpose=monitoring is a self-assigned label. Anyone who may label namespaces gets in. kubernetes.io/metadata.name is safer (Day 3).

Save your progress? Sign up for free

Drill 6: Additive – policies add up

There are now two policies on the api. Which one applies?

kubectl -n shop get networkpolicy
cc shop/frontend http://api.shop
cc shop/frontend http://api.shop:9113/metrics
cc monitoring/scraper http://api.shop

Expected:

NAME                   POD-SELECTOR   AGE
api-from-frontend      app=api        52s
api-metrics            app=api        23s
default-deny-ingress   <none>         78s
frontend-from-all      app=frontend   34s
✔ erlaubt   shop/frontend -> http://api.shop (HTTP 200)
✘ blockiert shop/frontend -> http://api.shop:9113/metrics (Timeout)
✘ blockiert monitoring/scraper -> http://api.shop (Timeout)

Both apply. The allowed set is the union: frontend on 8080 OR scraper on 9113. Port and source stay coupled per rule. The frontend does not suddenly get 9113 just because another rule opens 9113. Order and names do not matter, and there is no policy that overrides another.

Save your progress? Sign up for free

Drill 7: Egress – the database talks to nobody

cat > db-no-egress.yaml <<'EOF'
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: db-no-egress
  namespace: shop
spec:
  podSelector:
    matchLabels:
      app: db
  policyTypes:
  - Egress
EOF
kubectl apply -f db-no-egress.yaml
cc shop/db http://api.tenant-a
cc shop/db http://frontend.shop
cc shop/frontend http://api.tenant-a

Expected:

networkpolicy.networking.k8s.io/db-no-egress created
✘ blockiert shop/db -> http://api.tenant-a (Timeout)
✘ blockiert shop/db -> http://frontend.shop (Timeout)
✔ erlaubt   shop/frontend -> http://api.tenant-a (HTTP 200)

A compromised database can now neither send data out nor scan its neighbors. It cannot even ask DNS anymore. That is why conncheck.sh reports “Timeout” instead of “DNS-Fehler” (DNS error): The DNS query starves. Why that becomes a problem for Pods that are supposed to talk, you will see on Day 2.

Save your progress? Sign up for free

Checkpoint

cc=~/guard-lab/code/demo/conncheck.sh
while read -r src url want; do
  "$cc" "$src" "$url" "$want" >/dev/null \
    && echo "✔ $src -> $url ($want)" || echo "✘ $src -> $url (erwartet: $want)"
done <<'EOF'
shop/frontend http://api.shop allow
default/client http://frontend.shop allow
default/client http://api.shop deny
monitoring/scraper http://api.shop:9113/metrics allow
monitoring/intruder http://api.shop:9113/metrics deny
shop/db http://frontend.shop deny
EOF
kubectl -n shop get netpol default-deny-ingress >/dev/null 2>&1 \
  && echo "✔ default-deny-ingress" || echo "✘ default-deny-ingress fehlt"

Challenge

Right now the database is reachable for nobody, not even for the api. Write a policy db-from-api: Only Pods with tier=api may access the named port postgres. Also check whether tenant-a/api (which also has tier=api) gets in.

Hint: A podSelector in from only applies in the policy’s namespace.

Solution

cat > db-from-api.yaml <<'EOF'
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: db-from-api
  namespace: shop
spec:
  podSelector:
    matchLabels:
      tier: data
  policyTypes:
  - Ingress
  ingress:
  - from:
    - podSelector:
        matchLabels:
          tier: api
    ports:
    - port: postgres
EOF
kubectl apply -f db-from-api.yaml
cc shop/api tcp://db.shop:5432 allow
cc shop/frontend tcp://db.shop:5432 deny
cc tenant-a/api tcp://db.shop:5432 deny

Expected:

networkpolicy.networking.k8s.io/db-from-api created
✔ erlaubt   shop/api -> tcp://db.shop:5432
✘ blockiert shop/frontend -> tcp://db.shop:5432 (keine Verbindung)
✘ blockiert tenant-a/api -> tcp://db.shop:5432 (keine Verbindung)

tenant-a/api does carry tier=api, but it lives in a different namespace. Without a namespaceSelector, only shop counts.

Clean up for Day 2:

kubectl -n shop delete networkpolicy --all
kubectl -n monitoring delete pod intruder

Save your progress? Sign up for free

Enterprise Note

  • Start every namespace with default deny, and do it automatically: Kyverno creates the policy when the namespace is created (Day 9).
  • As sources, use labels that the app team owner cannot set at will, or secure them via admission (Day 8, Day 13).
  • Check every policy with describe and with a connection test. An indentation error turns AND into OR without kubectl apply complaining.
  • Standard NetworkPolicies only know L3/L4 and only allow. For L7, FQDNs and explicit denies you need CNI extensions (Day 4) or AdminNetworkPolicies (Day 5).
  • Plan rollouts audit-style: first with tests in staging, then in production while monitoring the dropped packets (Hubble, Day 4).