Zum Inhalt springen
Trainingslager IT-Trainingslager

Tag 4: Cilium – L7, FQDN und Hubble

Ziel: cilium-lab mit Cilium und Hubble bauen und Policies schreiben, die NetworkPolicy nicht kann: HTTP-Methoden und Pfade, Domains, Entities. · Dauer: ca. 120 min · Voraussetzung: Tag 3

Briefing

  • Cilium ersetzt kindnet und kube-proxy durch eBPF. Jeder Pod bekommt eine Sicherheits-Identität aus seinen Labels, nicht aus seiner IP.
  • CiliumNetworkPolicy (CNP) kann alles, was NetworkPolicy kann, plus L7-Regeln (HTTP), toFQDNs, Entities (world, host, kube-apiserver) und explizite Denies.
  • L7- und DNS-Regeln laufen über einen Proxy (Envoy bzw. den DNS-Proxy im Agent). Ein L7-Verbot ist kein Timeout, sondern HTTP 403.
  • Hubble zeigt jede Entscheidung: FORWARDED, DROPPED, Policy-Verdicts, HTTP-Flows.
 client ──► frontend ──GET /api/*──► api ──► db        Hubble Relay ◄── Agent (pro Node)
                  ✘ POST, /admin (403)                      │
 client ──► example.com ✔   kubernetes.io ✘          hubble observe / Hubble UI

Drill 1: Cluster ohne CNI

Die Konfiguration ~/guard-lab/kind/cilium-lab.yaml hast du an Tag 0 angelegt: disableDefaultCNI: true, kubeProxyMode: none, ein Control-Plane- und zwei Worker-Nodes.

mkdir -p ~/guard-lab/tag04 ~/guard-lab/code/cilium && cd ~/guard-lab/tag04
kind create cluster --config ~/guard-lab/kind/cilium-lab.yaml
kubectl get nodes
kubectl -n kube-system get ds

Erwartet (gekürzt, kann je nach Version abweichen):

 ✓ Ensuring node image (kindest/node:v1.37.0)
Set kubectl context to "kind-cilium-lab"
NAME                       STATUS     ROLES           AGE   VERSION
cilium-lab-control-plane   NotReady   control-plane   45s   v1.37.0
cilium-lab-worker          NotReady   <none>          25s   v1.37.0
cilium-lab-worker2         NotReady   <none>          25s   v1.37.0
No resources found in kube-system namespace.

NotReady ist richtig: Ohne CNI kann kein Pod-Netz entstehen. Und es gibt keinen kube-proxy-DaemonSet – Services funktionieren erst, wenn Cilium sie übernimmt.

Fortschritt speichern? Kostenlos registrieren

Drill 2: Cilium per Helm mit Hubble

helm repo add cilium https://helm.cilium.io
helm repo update cilium
cat > ~/guard-lab/code/cilium/values-kind.yaml <<'EOF'
# Cilium 1.20 für kind (cilium-lab), Hubble mit Relay und UI
kubeProxyReplacement: true
k8sServiceHost: cilium-lab-control-plane   # API-Server direkt, kein kube-proxy
k8sServicePort: 6443
ipam:
  mode: kubernetes          # Pod-CIDR aus dem Node-Objekt (kind: 10.244.x.0/24)
operator:
  replicas: 1               # Lab: ein Operator reicht
hubble:
  enabled: true
  relay:
    enabled: true
  ui:
    enabled: true
EOF
helm install cilium cilium/cilium --version 1.20.2 -n kube-system \
  -f ~/guard-lab/code/cilium/values-kind.yaml

Warum k8sServiceHost? Ohne kube-proxy gibt es beim Start noch keine Service-IP für kubernetes.default. Der Agent muss den API-Server direkt finden. Der Container-Name cilium-lab-control-plane ist im kind-Docker-Netz per DNS auflösbar.

Die CLIs cilium und hubble holst du als Binary (macOS, Apple Silicon):

cd /tmp
curl -sLO https://github.com/cilium/cilium-cli/releases/download/v0.20.1/\
cilium-darwin-arm64.tar.gz
curl -sLO https://github.com/cilium/hubble/releases/download/v1.20.2/\
hubble-darwin-arm64.tar.gz
tar xzf cilium-darwin-arm64.tar.gz && tar xzf hubble-darwin-arm64.tar.gz
export PATH=/tmp:$PATH
cilium status --wait

Erwartet (gekürzt, kann je nach Version abweichen):

    /¯¯\
 /¯¯\__/¯¯\    Cilium:             OK
 \__/¯¯\__/    Operator:           OK
 /¯¯\__/¯¯\    Envoy DaemonSet:    OK
 \__/¯¯\__/    Hubble Relay:       OK
    \__/       ClusterMesh:        disabled

DaemonSet              cilium                   Desired: 3, Ready: 3/3
DaemonSet              cilium-envoy             Desired: 3, Ready: 3/3
Deployment             cilium-operator          Desired: 1, Ready: 1/1
Deployment             hubble-relay             Desired: 1, Ready: 1/1
Deployment             hubble-ui                Desired: 1, Ready: 1/1
kubectl get nodes
kubectl -n kube-system exec ds/cilium -c cilium-agent -- \
  cilium-dbg status | grep -E 'KubeProxyReplacement|Hubble|Proxy Status'

Erwartet (gekürzt, kann je nach Version abweichen):

NAME                       STATUS   ROLES           AGE   VERSION
cilium-lab-control-plane   Ready    control-plane   4m    v1.37.0
cilium-lab-worker          Ready    <none>          4m    v1.37.0
cilium-lab-worker2         Ready    <none>          4m    v1.37.0
KubeProxyReplacement:    True   [eth0   172.18.0.3 (Direct Routing)]
Proxy Status:            OK, ip 10.244.0.95, 0 redirects active on ports 10000-20000
Hubble:                  Ok   Current/Max Flows: 4095/4095 (100.00%)

Envoy läuft seit Cilium 1.16 als eigener DaemonSet cilium-envoy. Im Agent heißt das Debug-Werkzeug cilium-dbg, nicht mehr cilium.

Jetzt die Demo-App wie gewohnt:

kubectl apply -f ~/guard-lab/code/demo/
kubectl wait --for=condition=Ready pod --all -A --timeout=300s
alias cc=~/guard-lab/code/demo/conncheck.sh
cc default/client http://api.shop

Erwartet (gekürzt, kann je nach Version abweichen):

✔ erlaubt   default/client -> http://api.shop (HTTP 200)

Fortschritt speichern? Kostenlos registrieren

Drill 3: CiliumNetworkPolicy auf L3/L4

Dieselbe Regel wie an Tag 1, nur in Cilium-Syntax: shop/api nimmt frontend auf 8080 und den scraper aus monitoring auf 9113 an.

cat > ~/guard-lab/code/cilium/api-l4.yaml <<'EOF'
apiVersion: cilium.io/v2
kind: CiliumNetworkPolicy
metadata:
  name: api-l4
  namespace: shop
spec:
  endpointSelector:
    matchLabels:
      app: api
  ingress:
  - fromEndpoints:
    - matchLabels:
        app: frontend          # ohne Namespace-Label: nur aus shop
    toPorts:
    - ports:
      - port: "8080"
        protocol: TCP
  - fromEndpoints:
    - matchLabels:
        k8s:io.kubernetes.pod.namespace: monitoring
        app: scraper
    toPorts:
    - ports:
      - port: "9113"
        protocol: TCP
EOF
kubectl apply -f ~/guard-lab/code/cilium/api-l4.yaml
kubectl -n shop get cnp
cc shop/frontend http://api.shop
cc default/client http://api.shop
cc monitoring/scraper http://api.shop:9113/metrics
cc monitoring/scraper http://api.shop

Erwartet (gekürzt, kann je nach Version abweichen):

ciliumnetworkpolicy.cilium.io/api-l4 created
NAME     AGE   VALID
api-l4   2s    True
✔ erlaubt   shop/frontend -> http://api.shop (HTTP 200)
✘ blockiert default/client -> http://api.shop (Timeout)
✔ erlaubt   monitoring/scraper -> http://api.shop:9113/metrics (HTTP 200)
✘ blockiert monitoring/scraper -> http://api.shop (Timeout)
NetworkPolicy CiliumNetworkPolicy
podSelector endpointSelector
from[].podSelector fromEndpoints[].matchLabels
namespaceSelector + podSelector Label k8s:io.kubernetes.pod.namespace
ports[].port: 8080 toPorts[].ports[].port: "8080" (String)

Warum ohne Namespace-Label „nur aus shop”? Eine namespaced CNP ergänzt fromEndpoints automatisch um ihren eigenen Namespace. Namespace-Labels selektierst du mit dem Präfix k8s:io.cilium.k8s.namespace.labels. (z. B. …labels.purpose: monitoring).

Fortschritt speichern? Kostenlos registrieren

Drill 4: Hubble – wer wurde verworfen?

hubble observe -P baut den Port-Forward zum Relay selbst auf.

cc default/client http://api.shop >/dev/null
hubble observe -P --verdict DROPPED --namespace shop --last 5

Erwartet (gekürzt, kann je nach Version abweichen):

Oct 10 08:12:03.512: default/client:51234 (ID:23412) <> shop/api-5ccd4587ff-gwhbn:8080
  (ID:4012) Policy denied DROPPED (TCP Flags: SYN)
Oct 10 08:12:04.530: default/client:51234 (ID:23412) <> shop/api-5ccd4587ff-gwhbn:8080
  (ID:4012) Policy denied DROPPED (TCP Flags: SYN)

Obwohl der Client api.shop:80 aufruft, siehst du Port 8080: Cilium übersetzt die Service-IP schon beim connect() im Client-Pod. Policies gelten immer für den Ziel-Port des Pods. Jetzt die Policy-Entscheidungen selbst, erlaubt und verboten:

cc shop/frontend http://api.shop >/dev/null
hubble observe -P --type policy-verdict --to-namespace shop --last 4 \
  --print-policy-names

Erwartet (gekürzt, kann je nach Version abweichen):

... default/client:51234 (ID:23412) <> shop/api-5ccd4587ff-gwhbn:8080 (ID:4012)
  policy-verdict:none INGRESS DENIED (TCP Flags: SYN)
... shop/frontend-5f9bb6b549-8hpqx:40112 (ID:9921) -> shop/api-5ccd4587ff-gwhbn:8080
  (ID:4012) policy-verdict:L3-L4 INGRESS ALLOWED (TCP Flags: SYN)

policy-verdict:none heißt: keine Regel hat gepasst, Default Deny hat gegriffen. L3-L4 nennt die Ebene der Regel, die erlaubt hat. --print-policy-names hängt zusätzlich die Namen der beteiligten Policies an. Nützliche Filter:

Filter Zweck
--from-pod default/client nur Flows dieses Quell-Pods
--to-port 8080 nur ein Ziel-Port
--protocol http nur L7-HTTP-Flows (Drill 5)
-f live mitlesen

Tipp: Die IDs sind Sicherheits-Identitäten. kubectl -n kube-system exec ds/cilium -c cilium-agent -- cilium-dbg identity get 4012 zeigt die Labels dahinter.

Fortschritt speichern? Kostenlos registrieren

Drill 5: L7 – nur GET auf /api/*

Das frontend soll die api nur lesend unter /api/… nutzen. Gleicher Policy-Name, also ersetzt apply die L4-Version.

cat > ~/guard-lab/code/cilium/api-l7.yaml <<'EOF'
apiVersion: cilium.io/v2
kind: CiliumNetworkPolicy
metadata:
  name: api-l4                 # gleicher Name: ersetzt die L4-Regel
  namespace: shop
spec:
  endpointSelector:
    matchLabels:
      app: api
  ingress:
  - fromEndpoints:
    - matchLabels:
        app: frontend
    toPorts:
    - ports:
      - port: "8080"
        protocol: TCP
      rules:
        http:
        - method: GET
          path: "/api/.*"      # Regex, verankert auf den ganzen Pfad
        - method: GET
          path: "/healthz"
  - fromEndpoints:
    - matchLabels:
        k8s:io.kubernetes.pod.namespace: monitoring
        app: scraper
    toPorts:
    - ports:
      - port: "9113"
        protocol: TCP
EOF
kubectl apply -f ~/guard-lab/code/cilium/api-l7.yaml
cc shop/frontend http://api.shop/api/orders
cc shop/frontend http://api.shop/admin
kubectl -n shop exec deploy/frontend -- \
  curl -s -X POST http://api.shop/api/orders
cc default/client http://frontend.shop/api/orders

Erwartet (gekürzt, kann je nach Version abweichen):

ciliumnetworkpolicy.cilium.io/api-l4 configured
✔ erlaubt   shop/frontend -> http://api.shop/api/orders (HTTP 200)
✔ erlaubt   shop/frontend -> http://api.shop/admin (HTTP 403)
Access denied
✔ erlaubt   default/client -> http://frontend.shop/api/orders (HTTP 200)

Achtung bei Zeile 2: conncheck.sh meldet „erlaubt”, denn die TCP-Verbindung stand. Die Ablehnung kommt als HTTP 403 vom Envoy-Proxy. Für L7-Tests prüfst du den Statuscode. Der letzte Aufruf geht über das frontend, das intern GET /api/orders an die api schickt – erlaubt.

hubble observe -P --protocol http --to-namespace shop --last 4

Erwartet (gekürzt, kann je nach Version abweichen):

... shop/frontend-5f9bb6b549-8hpqx:40500 (ID:9921) -> shop/api-5ccd4587ff-gwhbn:8080
  (ID:4012) http-request DROPPED (HTTP/1.1 POST http://api.shop/api/orders)
... shop/frontend-5f9bb6b549-8hpqx:40512 (ID:9921) -> shop/api-5ccd4587ff-gwhbn:8080
  (ID:4012) http-request FORWARDED (HTTP/1.1 GET http://api.shop/api/orders)
... shop/api-5ccd4587ff-gwhbn:8080 (ID:4012) <- shop/frontend-5f9bb6b549-8hpqx:40512
  (ID:9921) http-response FORWARDED (HTTP/1.1 200 1ms (GET http://api.shop/api/orders))

Mit L7-Regeln siehst du in Hubble Methode, URL und Statuscode – ohne Sidecar und ohne App-Logs.

Achtung: Allow-Regeln addieren sich auch in Cilium. Gibt eine zweite Policy denselben Port auf L4 frei, ist die L7-Regel wirkungslos: L4-Allow erlaubt alle Pfade.

Fortschritt speichern? Kostenlos registrieren

Drill 6: toFQDNs – Egress nach Domain

Der client darf nach außen, aber nur zu example.com und *.github.com auf 443.

cat > ~/guard-lab/code/cilium/client-fqdn.yaml <<'EOF'
apiVersion: cilium.io/v2
kind: CiliumNetworkPolicy
metadata:
  name: client-fqdn
  namespace: default
spec:
  endpointSelector:
    matchLabels:
      app: client
  egress:
  - toEndpoints:               # DNS über den DNS-Proxy von Cilium
    - matchLabels:
        k8s:io.kubernetes.pod.namespace: kube-system
        k8s-app: kube-dns
    toPorts:
    - ports:
      - port: "53"
        protocol: ANY
      rules:
        dns:
        - matchPattern: "*"
  - toFQDNs:
    - matchName: example.com
    - matchPattern: "*.github.com"
    toPorts:
    - ports:
      - port: "443"
        protocol: TCP
EOF
kubectl apply -f ~/guard-lab/code/cilium/client-fqdn.yaml
TIMEOUT=5 cc default/client https://example.com
TIMEOUT=5 cc default/client https://api.github.com
TIMEOUT=5 cc default/client https://kubernetes.io
cc default/client http://frontend.shop

Erwartet (gekürzt, kann je nach Version abweichen):

ciliumnetworkpolicy.cilium.io/client-fqdn created
✔ erlaubt   default/client -> https://example.com (HTTP 200)
✔ erlaubt   default/client -> https://api.github.com (HTTP 200)
✘ blockiert default/client -> https://kubernetes.io (Timeout)
✘ blockiert default/client -> http://frontend.shop (Timeout)

Warum braucht es die DNS-Regel mit rules.dns? Cilium lernt die IPs zu einem Namen nur, wenn die DNS-Antwort durch seinen DNS-Proxy läuft. Ohne rules.dns bleibt toFQDNs leer. Der Proxy füllt einen Cache aus Name → IP mit TTL:

kubectl -n kube-system exec ds/cilium -c cilium-agent -- \
  cilium-dbg fqdn cache list | grep -E 'example|github'
hubble observe -P --from-pod default/client --verdict DROPPED --last 2

Erwartet (gekürzt, kann je nach Version abweichen):

12    example.com.      23.192.228.80    2026-10-10T08:31:02Z   ...
12    api.github.com.   140.82.121.6     2026-10-10T08:30:45Z   ...
... default/client:39822 (ID:23412) -> kubernetes.io:443 (world) Policy denied
  DROPPED (TCP Flags: SYN)

fqdn cache list zeigt nur Einträge des Nodes, auf dem der ds/cilium-Pod läuft, den kubectl exec gewählt hat. Liegt der client woanders, nimm den Agent auf seinem Node.

Fortschritt speichern? Kostenlos registrieren

Drill 7: Entities statt IP-Listen

An Tag 2 hast du den API-Server per ipBlock freigegeben – mit fester IP. Cilium kennt den API-Server als Entity, egal welche IP er hat.

cc default/client https://kubernetes.default
cat > ~/guard-lab/code/cilium/client-entities.yaml <<'EOF'
apiVersion: cilium.io/v2
kind: CiliumNetworkPolicy
metadata:
  name: client-entities
  namespace: default
spec:
  endpointSelector:
    matchLabels:
      app: client
  egress:
  - toEntities:
    - kube-apiserver           # egal, welche IP der API-Server gerade hat
    toPorts:
    - ports:
      - port: "6443"
        protocol: TCP
  - toEntities:
    - host                     # der eigene Node (z. B. node-local Agents)
EOF
kubectl apply -f ~/guard-lab/code/cilium/client-entities.yaml
cc default/client https://kubernetes.default

Erwartet (gekürzt, kann je nach Version abweichen):

✘ blockiert default/client -> https://kubernetes.default (Timeout)
ciliumnetworkpolicy.cilium.io/client-entities created
✔ erlaubt   default/client -> https://kubernetes.default (HTTP 403)

HTTP 403 kommt vom API-Server selbst (anonymer Zugriff), die Verbindung steht. Der Port ist 6443, nicht 443: Auch hier zählt der Port nach der Service-Übersetzung. Die wichtigsten Entities:

Entity Bedeutung
world alles außerhalb des Clusters (praktisch 0.0.0.0/0)
cluster alle Endpoints im Cluster inkl. Nodes
host / remote-node der eigene Node / alle anderen Nodes
kube-apiserver der API-Server, auch außerhalb des Clusters

Sicherheit: toEntities: [world] ist bequem und fast immer zu weit. Für Egress ins Internet nimm toFQDNs (Drill 6) oder toCIDR mit konkreten Netzen.

Fortschritt speichern? Kostenlos registrieren

Kontrollpunkt

cc=~/guard-lab/code/demo/conncheck.sh
kubectl config current-context | grep -q kind-cilium-lab \
  && echo "✔ Kontext kind-cilium-lab" || echo "✘ falscher Kontext"
cilium status >/dev/null 2>&1 && echo "✔ Cilium OK" || echo "✘ Cilium nicht OK"
kubectl -n kube-system get ds kube-proxy >/dev/null 2>&1 \
  && echo "✘ kube-proxy läuft noch" || echo "✔ kein kube-proxy"
while read -r src url want; do
  "$cc" "$src" "$url" "$want" >/dev/null \
    && echo "✔ $src -> $url ($want)" || echo "✘ $src -> $url (erwartet: $want)"
done <<'EOF'
shop/frontend http://api.shop/api/x allow
default/client http://api.shop deny
monitoring/scraper http://api.shop:9113/metrics allow
default/client https://example.com allow
default/client https://kubernetes.io deny
EOF
code=$(kubectl -n shop exec deploy/frontend -- curl -s -o /dev/null \
  -w '%{http_code}' -X POST http://api.shop/api/x)
[ "$code" = 403 ] && echo "✔ POST -> 403" || echo "✘ POST -> $code"

Challenge

Der scraper soll auf Port 9113 nur noch GET /metrics dürfen. GET / auf 9113 muss mit 403 enden, die api auf 8080 bleibt wie in Drill 5.

Hinweis: Schreib keine zweite Policy dazu – die L4-Freigabe für 9113 in api-l4 würde deine L7-Regel aushebeln.

Lösung

cd ~/guard-lab/code/cilium
cp api-l7.yaml scraper-l7.yaml
cat >> scraper-l7.yaml <<'EOF'
      rules:
        http:
        - method: GET
          path: "/metrics"
EOF
kubectl apply -f scraper-l7.yaml
cc monitoring/scraper http://api.shop:9113/metrics
cc monitoring/scraper http://api.shop:9113/

Erwartet (gekürzt, kann je nach Version abweichen):

ciliumnetworkpolicy.cilium.io/api-l4 configured
✔ erlaubt   monitoring/scraper -> http://api.shop:9113/metrics (HTTP 200)
✔ erlaubt   monitoring/scraper -> http://api.shop:9113/ (HTTP 403)

Das cat >> hängt die rules an den letzten toPorts-Eintrag an – den für 9113. Prüf die Einrückung mit tail -12 scraper-l7.yaml, bevor du anwendest.

Fortschritt speichern? Kostenlos registrieren

Enterprise-Notiz

  • Cilium-Version, Chart-Values und Envoy-Modus gehören ins Git; Upgrades nur Minor-für-Minor mit cilium status und cilium connectivity test dazwischen.
  • toFQDNs hängt am DNS-Proxy. Fällt der Agent aus oder ist der Proxy überlastet, scheitert DNS für alle Pods mit FQDN-Regeln – Proxy-Metriken überwachen.
  • L7-Regeln kosten Latenz und CPU im Envoy. Nur dort einsetzen, wo Methode oder Pfad wirklich zählen (Admin-Endpunkte, Schreibzugriffe).
  • Hubble-Flows sind sensible Daten (URLs, IPs). Relay und UI nur mit TLS und Zugriffskontrolle betreiben, Export in ein SIEM mit Aufbewahrungsfrist.
  • Aufräumen: kind delete cluster --name cilium-lab – oder laufen lassen für Tag 5 und 6.