Advanced
Searching inLabs
How to search
  • Search a task or a tool (lvm, selinux, @podman); filter by level and by book or course.
  • With an empty search box, a filter alone lists the labs — for example all hard labs of one course.
  • "two words" finds the exact phrase.
  • Plain words find names, texts and examples.
  • ↑ ↓ to move · Enter to open · Esc to close · / to search
Try:

Lab 10.1CKA, Chapter 10

Services, DNS and a default-deny NetworkPolicy for a 3-tier app

Level
Medium
45 min
Machines
a Kubernetes cluster with a CNI that enforces NetworkPolicy (Calico or Cilium)
Status
Not started

Goal

Run a frontend, an API and a database in one namespace, reach them through Services and DNS, then lock the namespace down: deny everything first, then allow exactly the three flows the app needs (frontend → API, API → database, everyone → DNS). You test every allowed and every blocked path.

Before you start

Tasks

  1. Create three deployments in shop: frontend (nginx), api (nginx) and db (postgres:16-alpine with POSTGRES_PASSWORD=lab). Label them tier=frontend|api|db.
  2. Expose api (port 80) and db (port 5432) with ClusterIP Services. Expose frontend with a NodePort.
  3. From a test pod, reach api by short name, by FQDN (api.shop.svc.cluster.local) and check the Service endpoints.
  4. Apply a default-deny policy for ingress and egress in shop. Show that the test now fails, also DNS.
  5. Allow egress to DNS (kube-dns in kube-system, UDP and TCP 53) for all pods in shop.
  6. Allow frontend → api on TCP 80, and api → db on TCP 5432 (ingress and egress rules).
  7. Prove: frontend reaches api; frontend does not reach db; api reaches db.

Check your work

kubectl -n shop get netpol
kubectl -n shop exec deploy/frontend -- curl -s -m 3 -o /dev/null -w "%{http_code}\n" http://api
kubectl -n shop exec deploy/frontend -- timeout 3 bash -c "echo > /dev/tcp/db/5432" && echo open || echo blocked
kubectl -n shop exec deploy/api -- timeout 3 bash -c "echo > /dev/tcp/db/5432" && echo open || echo blocked
  • 4 policies: default-deny, allow-dns, allow-frontend-to-api, allow-api-to-db.
  • frontend → api gives 200; frontend → db prints blocked; api → db prints open.

Hints

Show the hints
  • podSelector: {} selects every pod in the namespace; policyTypes: [Ingress, Egress] with no rules = deny all.
  • Egress needs both sides: the source must be allowed out (egress) and the target must allow in (ingress).
  • kube-dns pods have the label k8s-app=kube-dns; namespaces have the automatic label kubernetes.io/metadata.name.
  • The nginx image has bash and curl, so you can test from it.

Solution

Show the solution
  1. Workloads and Services.
    kubectl -n shop create deploy frontend --image=nginx
    kubectl -n shop create deploy api --image=nginx
    kubectl -n shop create deploy db --image=postgres:16-alpine
    kubectl -n shop set env deploy/db POSTGRES_PASSWORD=lab
    for t in frontend api db; do kubectl -n shop label deploy $t tier=$t; kubectl -n shop patch deploy $t -p "{\"spec\":{\"template\":{\"metadata\":{\"labels\":{\"tier\":\"$t\"}}}}}"; done
    kubectl -n shop expose deploy api --port 80
    kubectl -n shop expose deploy db --port 5432
    kubectl -n shop expose deploy frontend --port 80 --type NodePort
    kubectl -n shop get endpoints
  2. Test DNS and Services.
    kubectl -n shop run t --rm -it --image=busybox:1.36 --restart=Never -- sh -c "nslookup api; wget -qO- -T 3 http://api.shop.svc.cluster.local | head -1"
  3. The policies.
    shop-netpol.yaml
    apiVersion: networking.k8s.io/v1
    kind: NetworkPolicy
    metadata: {name: default-deny, namespace: shop}
    spec:
      podSelector: {}
      policyTypes: [Ingress, Egress]
    ---
    apiVersion: networking.k8s.io/v1
    kind: NetworkPolicy
    metadata: {name: allow-dns, namespace: shop}
    spec:
      podSelector: {}
      policyTypes: [Egress]
      egress:
        - to:
            - namespaceSelector: {matchLabels: {kubernetes.io/metadata.name: kube-system}}
              podSelector: {matchLabels: {k8s-app: kube-dns}}
          ports: [{protocol: UDP, port: 53}, {protocol: TCP, port: 53}]
    ---
    apiVersion: networking.k8s.io/v1
    kind: NetworkPolicy
    metadata: {name: allow-frontend-to-api, namespace: shop}
    spec:
      podSelector: {matchLabels: {tier: api}}
      policyTypes: [Ingress]
      ingress:
        - from: [{podSelector: {matchLabels: {tier: frontend}}}]
          ports: [{protocol: TCP, port: 80}]
    ---
    apiVersion: networking.k8s.io/v1
    kind: NetworkPolicy
    metadata: {name: allow-api-to-db, namespace: shop}
    spec:
      podSelector: {matchLabels: {tier: db}}
      policyTypes: [Ingress]
      ingress:
        - from: [{podSelector: {matchLabels: {tier: api}}}]
          ports: [{protocol: TCP, port: 5432}]
    ---
    apiVersion: networking.k8s.io/v1
    kind: NetworkPolicy
    metadata: {name: allow-egress-app, namespace: shop}
    spec:
      podSelector: {matchExpressions: [{key: tier, operator: In, values: [frontend, api]}]}
      policyTypes: [Egress]
      egress:
        - to: [{podSelector: {matchLabels: {tier: api}}}]
          ports: [{protocol: TCP, port: 80}]
        - to: [{podSelector: {matchLabels: {tier: db}}}]
          ports: [{protocol: TCP, port: 5432}]

    The last policy opens the egress side. It lets frontend try port 5432, but the ingress policy of db still blocks it, because only tier=api may come in.

    kubectl apply -f shop-netpol.yaml
    kubectl -n shop exec deploy/frontend -- curl -s -m 3 -o /dev/null -w "%{http_code}\n" http://api
    200
  4. Run the three checks from Check your work: 200, blocked, open. Clean up with kubectl delete ns shop.

My notes

No notes yet.

Lessons for this lab

Commands in this lab