Tag 0: Trainingsgelände
Ziel: Werkzeuge prüfen, den kind-Cluster
np-labstarten, die Demo-Anwendung ausrollen und mitconncheck.shdie erste Verbindung testen. · Dauer: ca. 45 min · Voraussetzung: keine
Briefing
- Drei kind-Cluster, nie alle gleichzeitig:
np-lab(Teil A),cilium-lab(Teil B),kyverno-lab(Teil C/D). - Eine Demo-Anwendung für alles:
shopmitfrontend→api→db, dazutenant-a,tenant-b,monitoringund ein neutralerclient. - Ein Image für alle Server:
nginxinc/nginx-unprivileged:1.31-alpine(klein, mitcurl,wget,nc). Test-Pods nutzencurlimages/curl:8.22.0. conncheck.shtestet jede Verbindung aus einem Pod heraus und antwortet mit ✔ erlaubt oder ✘ blockiert.
~/guard-lab/
├── code/demo/ Demo-App (00-…40-*.yaml) + conncheck.sh
├── kind/ np-lab.yaml, cilium-lab.yaml, kyverno-lab.yaml
└── tag01/ … deine Arbeitsordner pro Tag
Drill 1: Werkzeuge prüfen
docker info --format '{{.ServerVersion}} {{.MemTotal}}'
kind version
kubectl version --client | head -1
helm version --shortErwartet (gekürzt, kann je nach Version abweichen):
29.5.2 8307167232
kind v0.33.0 go1.27.0 darwin/arm64
Client Version: v1.37.1
v4.3.0+gbec5b06
Die Kyverno-CLI und cosign brauchst du erst ab Tag 7 und Tag 10:
kyverno version | head -1
cosign version | grep GitVersionErwartet (gekürzt):
Version: 1.19.1
GitVersion: v3.1.3
Tipp: Gib Docker mindestens 6 GB RAM. Jeder kind-Node ist ein Container. Ein Node mit Demo-App braucht etwa 1 GB, mit Kyverno oder Cilium eher 2 GB.
Fortschritt speichern? Kostenlos registrieren
Drill 2: Ordner und Demo-Code
mkdir -p ~/guard-lab/{code,kind,tag01,tag02,tag03}
git clone https://github.com/thomaszachmann/kyverno-networkpolicies-zum-anfassen.git \
~/kyverno-networkpolicies-zum-anfassen 2>/dev/null \
|| git -C ~/kyverno-networkpolicies-zum-anfassen pull
cp -R ~/kyverno-networkpolicies-zum-anfassen/demo ~/guard-lab/code/
chmod +x ~/guard-lab/code/demo/conncheck.sh
ls ~/guard-lab/code/demoErwartet:
00-namespaces.yaml 10-shop.yaml 20-tenants.yaml 30-monitoring.yaml
40-client.yaml conncheck.sh
Die Nummern sorgen dafür, dass
kubectl apply -f <ordner> die Namespaces zuerst
anlegt. Ein Blick auf die Labels, die du ab Tag 1 ständig brauchst:
cd ~/guard-lab/code/demo
grep -h -A4 'labels:' 00-namespaces.yaml | grep -v labels
grep -h 'labels: {app' 10-shop.yaml | sort -uErwartet (gekürzt):
guard-lab: demo
team: shop
tenant: a
purpose: monitoring
labels: {app: api, tier: api}
labels: {app: db, tier: data}
labels: {app: frontend, tier: web}
| Namespace | Workload | Labels | Ports |
|---|---|---|---|
| shop | frontend | app=frontend, tier=web |
8080 http |
| shop | api | app=api, tier=api |
8080 http, 9113 metrics |
| shop | db | app=db, tier=data |
5432 postgres |
| tenant-a/-b | api | app=api, tier=api |
8080 http, 9113 metrics |
| monitoring | scraper | app=scraper, tier=monitoring |
– |
| default | client | app=client |
– |
Die Services heißen wie die Deployments. api und
frontend lauschen als Service auf Port 80, db
auf 5432. db ist eine simulierte Datenbank: nginx auf Port
5432. Das spart ein 100-MB-Image, und für NetworkPolicies zählt nur der
Port.
Fortschritt speichern? Kostenlos registrieren
Drill 3: Drei kind-Konfigurationen
cd ~/guard-lab/kind
cat > np-lab.yaml <<'YAML'
# Teil A: kindnet setzt NetworkPolicies selbst durch (seit kind 0.24)
kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
name: np-lab
nodes:
- role: control-plane
- role: worker
- role: worker
YAML
cat > cilium-lab.yaml <<'YAML'
# Teil B: kein Standard-CNI, kein kube-proxy - Cilium übernimmt beides (Tag 4)
kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
name: cilium-lab
networking:
disableDefaultCNI: true
kubeProxyMode: none
nodes:
- role: control-plane
- role: worker
- role: worker
YAML
cat > kyverno-lab.yaml <<'YAML'
# Teil C/D: Kyverno + lokale Registry kind-registry:5000 (Tag 10)
kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
name: kyverno-lab
containerdConfigPatches:
- |-
[plugins."io.containerd.grpc.v1.cri".registry]
config_path = "/etc/containerd/certs.d"
nodes:
- role: control-plane
- role: worker
- role: worker
YAML
lsErwartet:
cilium-lab.yaml kyverno-lab.yaml np-lab.yaml
Warum der containerdConfigPatches-Block? Damit liest
containerd Registry-Spiegel aus /etc/containerd/certs.d. In
Tag 10 legst du dort nur noch eine hosts.toml ab, statt den
Cluster neu zu bauen.
Tipp: Wenig RAM? Lösch die beiden
worker-Zeilen. Alle Drills in Teil A laufen auch auf einem einzelnen Node. Getestet wurde dieses Kapitel single-node.
Fortschritt speichern? Kostenlos registrieren
Drill 4: np-lab starten
kind create cluster --config ~/guard-lab/kind/np-lab.yaml
kubectl get nodes
kubectl -n kube-system get ds kindnet \
-o jsonpath='{.spec.template.spec.containers[0].image}{"\n"}'Erwartet (gekürzt, kann je nach Version abweichen):
✓ Ensuring node image (kindest/node:v1.37.0)
✓ Installing CNI
Set kubectl context to "kind-np-lab"
NAME STATUS ROLES AGE VERSION
np-lab-control-plane Ready control-plane 40s v1.37.0
np-lab-worker Ready <none> 20s v1.37.0
np-lab-worker2 Ready <none> 20s v1.37.0
docker.io/kindest/kindnetd:v20260820-69b56db7
kindnet ist das Standard-CNI von kind. Seit kind 0.24 bringt es einen
eigenen NetworkPolicy-Controller mit
(kube-network-policies). Ob er wirklich greift, beweist
Drill 7.
Fortschritt speichern? Kostenlos registrieren
Drill 5: Demo-Anwendung ausrollen
kubectl apply -f ~/guard-lab/code/demo/
kubectl wait --for=condition=Ready pod --all -A --timeout=300s
kubectl get pods -A -l 'app in (frontend,api,db,scraper,client)'Erwartet (gekürzt):
namespace/shop created
namespace/tenant-a created
...
deployment.apps/scraper created
pod/client created
NAMESPACE NAME READY STATUS RESTARTS AGE
default client 1/1 Running 0 60s
monitoring scraper-7c99db6d96-rw2gv 1/1 Running 0 60s
shop api-5ccd4587ff-gwhbn 1/1 Running 0 60s
shop db-5d6755ff6b-z5vv9 1/1 Running 0 60s
shop frontend-5f9bb6b549-8hpqx 1/1 Running 0 60s
tenant-a api-5ccd4587ff-2x5n2 1/1 Running 0 60s
tenant-b api-5ccd4587ff-f48wx 1/1 Running 0 60s
Der erste Start dauert ein paar Minuten, weil jeder Node die Images zieht. Jetzt die Kette von außen nach innen:
kubectl exec client -- curl -s http://frontend.shop/
kubectl exec client -- curl -s http://frontend.shop/api/orders
kubectl exec client -- curl -s http://api.tenant-a/x
kubectl -n monitoring logs deploy/scraper --tail=3Erwartet:
frontend ok
{"svc":"api","ns":"shop","path":"/api/orders"}
{"svc":"api","ns":"tenant-a","path":"/x"}
ok shop
ok tenant-a
ok tenant-b
Das Frontend reicht /api/… an api.shop
weiter und löst den Namen bei jeder Anfrage per DNS auf. Das wird in Tag
2 wichtig. Der Scraper holt alle 10 s /metrics (Port 9113)
aus allen drei api-Pods und schreibt ok oder
FAIL ins Log.
Fortschritt speichern? Kostenlos registrieren
Drill 6: conncheck.sh kennenlernen
Das Skript macht genau eine Sache: aus einem Quell-Pod heraus eine Verbindung aufbauen und das Ergebnis eindeutig melden.
cd ~/guard-lab/code/demo
sed -n '2,11p' conncheck.shErwartet:
# conncheck.sh - testet eine Verbindung aus einem Pod heraus.
#
# conncheck.sh <ns>/<pod|app> <url> [allow|deny]
#
# <ns>/<pod|app> Quelle: Pod-Name oder Wert des Labels app (erster Running-Pod)
# <url> http://host[:port][/pfad] oder tcp://host:port
# allow|deny optional: Erwartung. Dann Exit 0 = wie erwartet, 1 = Abweichung
#
# Ohne Erwartung: Exit 0 = erlaubt, 1 = blockiert, 2 = Aufruffehler.
Der Kern: kubectl exec in den Quell-Pod, dort
curl (HTTP) oder nc -z (TCP) mit 2 s Timeout.
Der curl-Exit-Code wird übersetzt: 6 = DNS-Fehler, 7 = abgelehnt, 28 =
Timeout. Ein HTTP-Status wie 404 oder 502 zählt als erlaubt, denn die
Verbindung stand.
alias cc=~/guard-lab/code/demo/conncheck.sh
cc shop/frontend http://api.shop
cc default/client tcp://db.shop:5432
cc default/client http://nix.shop.svc
cc shop/frontend http://api.shop:81
cc shop/nope http://x; echo "rc=$?"Erwartet:
✔ erlaubt shop/frontend -> http://api.shop (HTTP 200)
✔ erlaubt default/client -> tcp://db.shop:5432
✘ blockiert default/client -> http://nix.shop.svc (DNS-Fehler)
✘ blockiert shop/frontend -> http://api.shop:81 (Timeout)
✘ Quelle shop/nope nicht gefunden
rc=2
Achtung bei Zeile 4: Der Service api hat keinen Port 81,
also läuft das Paket ins Leere. conncheck.sh meldet
„blockiert”, obwohl gar keine Policy im Spiel ist. Ein ✘ heißt nur:
keine Verbindung. Ob eine Policy schuld ist, prüfst du mit einer
Gegenprobe.
Mit Erwartung wird das Skript zum Test:
cc default/client http://api.shop deny; echo "rc=$?"Erwartet:
✔ erlaubt default/client -> http://api.shop (HTTP 200) <-- erwartet: deny
rc=1
Tipp:
TIMEOUT=5 cc …verlängert die Wartezeit, falls dein Laptop unter Last ist.
Fortschritt speichern? Kostenlos registrieren
Drill 7: Beweis – kindnet erzwingt Policies
Eine NetworkPolicy ohne durchsetzendes CNI ist nur YAML. Also erst beweisen:
cat <<'EOF' | kubectl apply -f -
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: probe-deny
namespace: shop
spec:
podSelector: {}
policyTypes:
- Ingress
EOF
cc default/client http://frontend.shop
kubectl -n shop delete networkpolicy probe-deny
cc default/client http://frontend.shopErwartet:
networkpolicy.networking.k8s.io/probe-deny created
✘ blockiert default/client -> http://frontend.shop (Timeout)
networkpolicy.networking.k8s.io "probe-deny" deleted from shop namespace
✔ erlaubt default/client -> http://frontend.shop (HTTP 200)
Kein ✘ beim ersten Aufruf? Dann setzt dein CNI keine Policies durch. Alter kind-Stand oder ein anderes CNI: Installiere Calico oder nimm direkt Cilium (Tag 4).
Fortschritt speichern? Kostenlos registrieren
Kontrollpunkt
cc=~/guard-lab/code/demo/conncheck.sh
k() { kubectl get "$@" >/dev/null 2>&1; }
kubectl config current-context | grep -q kind-np-lab \
&& echo "✔ Kontext kind-np-lab" || echo "✘ falscher Kontext"
for ns in shop tenant-a tenant-b monitoring; do
k ns "$ns" && echo "✔ Namespace $ns" || echo "✘ Namespace $ns fehlt"
done
[ "$(kubectl get netpol -A --no-headers 2>/dev/null | wc -l)" -eq 0 ] \
&& echo "✔ keine NetworkPolicies" || echo "✘ alte NetworkPolicies vorhanden"
$cc default/client http://frontend.shop allow >/dev/null \
&& echo "✔ client -> frontend" || echo "✘ client -> frontend"
$cc shop/api tcp://db.shop:5432 allow >/dev/null \
&& echo "✔ api -> db" || echo "✘ api -> db"
ls ~/guard-lab/kind/*.yaml | wc -l | grep -q 3 \
&& echo "✔ drei kind-Configs" || echo "✘ kind-Configs fehlen"Challenge
Schreib ~/guard-lab/code/matrix.sh. Es prüft von den
Quellen default/client, shop/frontend und
tenant-a/api aus die Ziele frontend.shop,
api.shop, tcp://db.shop:5432 und
api.tenant-b und druckt alle Ergebnisse. Du brauchst es ab
Tag 1 nach jeder neuen Policy.
Hinweis: Zwei verschachtelte
for-Schleifen und conncheck.sh ohne Erwartung
reichen.
Lösung
cat > ~/guard-lab/code/matrix.sh <<'EOF'
#!/usr/bin/env bash
# matrix.sh - alle Quellen gegen alle Ziele (Lab-Testmatrix)
cc=~/guard-lab/code/demo/conncheck.sh
srcs="default/client shop/frontend tenant-a/api"
dsts="http://frontend.shop http://api.shop tcp://db.shop:5432 http://api.tenant-b"
for s in $srcs; do
for d in $dsts; do
"$cc" "$s" "$d"
done
done
EOF
chmod +x ~/guard-lab/code/matrix.sh
~/guard-lab/code/matrix.sh | head -4Erwartet (gekürzt):
✔ erlaubt default/client -> http://frontend.shop (HTTP 200)
✔ erlaubt default/client -> http://api.shop (HTTP 200)
✔ erlaubt default/client -> tcp://db.shop:5432
✔ erlaubt default/client -> http://api.tenant-b (HTTP 200)
Ohne Policies ist alles erlaubt: Kubernetes-Netz ist von Haus aus flach und offen. Genau das änderst du ab morgen.
Fortschritt speichern? Kostenlos registrieren
Enterprise-Notiz
- Prüfe beim Kunden zuerst, welches CNI läuft und ob es NetworkPolicies durchsetzt (Flannel allein tut es nicht, Canal, Calico, Cilium, OVN-Kubernetes schon).
- RKE2 bringt Canal mit, OpenShift OVN-Kubernetes. Beide setzen Policies durch, die Feinheiten (Logging, Host-Traffic) unterscheiden sich aber.
- Images kommen in Produktion aus einer internen Registry mit Digest statt Tag (Tag 10).
- Ein Prüfskript wie
conncheck.shgehört nach jedem Rollout in die Pipeline (Tag 6). - Lab-Cluster nach dem Tag löschen:
kind delete cluster --name np-lab.