Skip to content
EgyKode
06 · KubernetesLab 40 / 59
Guided labkubernetes

Kubernetes Security Hardening (NetworkPolicies) & HPA

Deny traffic between Pods by default, then allow only what the application needs — and scale it under load.

Time
47 min
Level
Intermediate
Objectives
4 objectives
Cost
Low cost

Where this fits in the platform

Before you start

CostLow cost

Depends on an existing cluster. NetworkPolicies and HPA objects are free; the cluster and any nodes the HPA scales up are not.

Nothing to pay in the browser. Open the terminal runs this against a simulated cloud — the same API calls and the same commands, with no account and no bill. The figure above applies only if you build it in your own.

How to clean up

The scenario#

Every Pod can reach every other Pod, in every namespace. A compromise of the public-facing service is a port scan away from the database.

The application is also fixed at two replicas, so traffic either wastes money or drops requests.

Hands-on environment

Run this lab in a real terminal, free and in your browser. The environment is temporary and yours alone — break it as much as you like.

Open the terminal

Opens in Killercoda, in a new tab — keep this page open for the steps.

Run it on your own machine

Run this lab on your own machine. One command starts the environment, with everything the lab needs already installed:

Create the cluster with `./egykode cluster calico` for this one. kind's default CNI accepts NetworkPolicy objects and enforces none of them, so your policies would appear to apply while blocking nothing. HPA also needs metrics-server, which the same command installs.

You will need:

  • docker
  • kubectl
  • kind
git clone https://github.com/EgyKode/EgyKode-lab.git
cd EgyKode-lab
./egykode start k8s
./egykode shell

You need Docker and Git installed. Everything else runs inside the environment. The first start downloads it and takes a few minutes; later starts are seconds.

Not sure what you already have? Run: npm run doctor — it checks and changes nothing.

Run it on AWS

This lab builds real cloud infrastructure, so it needs your own AWS account. Follow the cost and cleanup notes above — the resources are yours, and so is the bill.

Anything you tick here is your own record. EgyKode cannot see inside that terminal, so the success criteria stay self-assessed even when the environment checks your work for you.

Default deny — start by breaking it

Step 1 of 3

What you are building#

Two independent controls that are often taught together and solve different problems: NetworkPolicy decides what a Pod may talk to; HPA decides how many of it there are.

text
   ingress controller ---> api  ---> postgres
                            |
                            +------> kube-dns :53   (easy to forget)
 
   everything else --X--> api        default deny

Build it#

Verify it worked#

Terminal
# The negative test — this is the actual security claim
kubectl run intruder --rm -it --image=curlimages/curl --restart=Never -n platform -- \
  curl -s --max-time 5 http://api:80/          # must time out
 
# The positive test, from a Pod the policy allows
kubectl run probe --rm -it --image=curlimages/curl --restart=Never \
  -n ingress-nginx -- curl -s -o /dev/null -w '%{http_code}\n' http://api.platform/
 
# DNS still resolves from the app
kubectl exec -n platform deploy/api -- nslookup postgres.platform.svc.cluster.local
 
# HPA has a reading, not <unknown>
kubectl get hpa -n platform
kubectl top pods -n platform
 
# Generate load and watch it scale
kubectl run load --rm -it --image=busybox:1.36 --restart=Never -n platform -- \
  sh -c 'while true; do wget -q -O- http://api:80/ >/dev/null; done'
kubectl get hpa api -n platform -w
kubectl get deploy api -n platform -w

Both the timeout and the success matter. Proving traffic flows is easy; proving that traffic which should not flow does not is the claim you are actually making.


Clean up#

DestructiveThis removes real resources. Check which environment you are in first.

Terminal
kubectl delete networkpolicy --all -n platform
kubectl delete hpa --all -n platform
kubectl delete namespace platform

Cost of this lab: Free on kind or minikube with a CNI that enforces policy. On EKS the cluster bills either way; these objects add nothing.

Maintained by others, on Killercoda. Useful for extra repetition on one tool — it does not complete this lab or settle any criterion above.

Success criteria

0 of 4

The concept behind it

Ready to try it without help?Do the challenge

Phase complete · 06 Kubernetes

You can now: The application runs on Kubernetes with storage, routing, scoped permissions, network policy and autoscaling.

Next phase

Lab 40 of 59 on the project path

07 · PackagingCreating a Custom Helm Chart for Django MicroservicesTurn a directory of manifests into a versioned chart you can install into any environment with different values.47 minIntermediate

Previous: Kubernetes RBAC & Service Accounts