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
kubectl get pods -n kube-systemshows Calico or Cilium. With flannel alone, policies are accepted but not enforced.- Make the namespace:
kubectl create ns shop.
Tasks
Tick a task when it is done. Only your browser remembers it.
- Create three deployments in
shop:frontend(nginx),api(nginx) anddb(postgres:16-alpinewithPOSTGRES_PASSWORD=lab). Label themtier=frontend|api|db. - Expose
api(port 80) anddb(port 5432) with ClusterIP Services. Exposefrontendwith a NodePort. - From a test pod, reach
apiby short name, by FQDN (api.shop.svc.cluster.local) and check the Service endpoints. - Apply a default-deny policy for ingress and egress in
shop. Show that the test now fails, also DNS. - Allow egress to DNS (kube-dns in
kube-system, UDP and TCP 53) for all pods inshop. - Allow
frontend→apion TCP 80, andapi→dbon TCP 5432 (ingress and egress rules). - 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 printsblocked; api → db printsopen.
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 labelkubernetes.io/metadata.name. - The nginx image has
bashandcurl, so you can test from it.
Solution
Show the solution
- 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 - 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" - 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
dbstill blocks it, because onlytier=apimay come in. - Run the three checks from Check your work:
200,blocked,open. Clean up withkubectl delete ns shop.
My notes
No notes yet.