Kubernetes · Chapter 20 of 28

NetworkPolicy

By the end of this chapter you can say which pods may talk to which, with NetworkPolicies: shut a namespace to incoming traffic, let customers in through the ingress controller, let one Service reach another on one port only, limit where a pod may connect without breaking its DNS, and test all of it from inside a pod. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: More workloads and traffic
  • 5 tasks
  • Kubernetes
  • In the all labs plan

The first 2 chapters of Kubernetes are free. This one comes with the all labs plan.

Why you need this

Jhatpat's cart keeps about 18,000 open baskets in Redis on a festival evening, with customers' phone numbers and addresses in them. Before the payment gateway signs Jhatpat up for card payments this Diwali, its security review asks one question: which programs can open a connection to that Redis? The honest answer today is "every pod in the cluster": the catalog, the storefront, a debug pod Tushar started last week, and any pod a bad image might run.

Nothing you have learned so far closes a door. Services (chapter 7) and the Ingress (chapter 19) only add ways in. Namespaces (chapter 15) keep names apart, not traffic: a pod in ch15-kanpur reaches redis.ch20 just by asking for it. Kubernetes's network has no walls until you write them, and Ammar's rule for Diwali is the opposite: nothing talks to anything unless someone wrote down that it may.

What you build: 5 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1

    default-deny.yaml: nothing gets in

    Stop all incoming traffic to every pod in ch20.

    1. Open default-deny.yaml. Make a NetworkPolicy named default-deny.

    2. Make it select every pod in the namespace.

    3. Give it the policy type Ingress, and no ingress rules. Do not add the type Egress.

    4. Apply it. Send a request from the toolbox pod to storefront with lab curl --from toolbox. It should time out.

    To pass: default-deny selects every pod, is of type Ingress only, and has no ingress rules. A request from toolbox to the storefront Service times out. No other policy in ch20 may let toolbox in.

  2. Task 2

    storefront-from-ingress.yaml: let customers in

    Let customers reach storefront again, but only through the ingress controller.

    1. Keep default-deny in place.

    2. Open storefront-from-ingress.yaml. Make a NetworkPolicy that selects the storefront pods (app=storefront).

    3. Allow in only the pods with label app.kubernetes.io/name=ingress-nginx in the namespace ingress-nginx, on port 8080.

    4. Apply it. Run lab curl http://lucknow.jhatpat.in/healthz.

    To pass: default-deny still exists. lab curl http://lucknow.jhatpat.in/healthz is answered 200 by storefront. A request from the toolbox pod to storefront still times out.

  3. Task 3

    catalog-from-storefront.yaml: only storefront reaches catalog

    Only storefront may reach the catalog, and only on port 8080.

    1. Open catalog-from-storefront.yaml. Make a NetworkPolicy that selects the catalog pods (app=catalog).

    2. Allow in only the storefront pods (app=storefront) of ch20.

    3. Name the port in the rule: 8080 (or its name, http).

    4. Apply it. Load the home page with lab curl http://lucknow.jhatpat.in/. Then try the catalog from the toolbox pod.

    To pass: default-deny still exists. storefront reaches the catalog, and the home page answers 200 with the products. Requests to the catalog from toolbox and from the redis pod time out. Every rule that lets traffic in to catalog has a from and names port 8080 only, with no other namespaces or addresses.

  4. Task 4

    cart-egress.yaml: fix the policy that broke DNS

    Tushar's NetworkPolicy cart-egress limits where the cart pods may connect. Since then, cart crashes. Find why and fix it.

    1. Read cart's logs. Find what it fails to do before it talks to Redis.

    2. In cart-egress.yaml, keep the name, the type Egress, the cart pods and the Redis rule.

    3. Add a rule that allows DNS: port 53, UDP and TCP, to the pods with label k8s-app=kube-dns in the namespace kube-system.

    4. Apply it. Restart the Deployment cart, so a fresh pod starts.

    To pass: a cart pod is ready, and its log says it connected to Redis. cart still cannot reach anything else, such as the ingress controller's Service. A rule with no to and no ports does not count: it allows everything.

  5. Task 5 · interview question

    Interview: a policy that allows nothing

    Interview question: default-deny has no rules at all. Why does it block anything?

    1. Open answer.txt. Write below the lines that start with #.

    2. In a sentence or two of your own, say what happens to a pod once a policy selects it.

    3. Say what policies can do to traffic, and what a policy with no rules allows.

    To pass: your answer says what being selected does to a pod, says what policies do to traffic, and says what happens when no rule allows anything. Lines that start with # do not count. Ammar will ask: "default-deny has no rules at all. Why does it block anything?"

What you will learn

  • The default: every pod can reach every pod, in every namespace.
  • NetworkPolicy: selects pods and lists what may reach them; it only ever allows.
  • Default deny: a policy that selects every pod and allows nothing.
  • Peers: pods, namespaces and the AND that a missing dash turns into an OR.
  • Egress: where a pod may connect, and the DNS trap.
  • Testing from inside: lab curl --from and kubectl exec ... curl, and why a blocked request times out.

More in More workloads and traffic

Build "NetworkPolicy" yourself

Jhatpat delivers groceries in ten minutes from 14 dark stores across Lucknow, and its hand-run servers fall over on festival evenings. You move it onto Kubernetes. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.

The first 2 chapters of Kubernetes are free. This one comes with the all labs plan.