Tag 12: Rechte, Feature Gates, Metriken
Ziel: kro darf nur noch, was du ihm ausdrücklich erlaubst. Teams dürfen NotesApps anlegen, aber keine Deployments. Dazu Events bei jedem Zustandswechsel und ein Blick in die Metriken des Controllers. · Dauer: ca. 60 min · Voraussetzung: Tag 11
Briefing
- Im Modus
unrestrictedhat kro eine ClusterRole mit*auf alles. Wer eine RGD anlegen darf, kann damit jedes Objekt im Cluster erzeugen, ist also praktisch Cluster-Admin. - Im Modus
aggregationbekommt kro nur das Nötigste für sich selbst. Für jede Art, die eine RGD erzeugt oder liest, legst du eine ClusterRole mit dem Labelrbac.kro.run/aggregate-to-controller: "true"an. Kubernetes fasst alle zu einer Rolle zusammen. - Für die Teams legt kro keine Rollen an. Wer eine NotesApp anlegen darf, bestimmst du mit normalem RBAC.
- Feature Gates schalten Alpha-Funktionen ein:
DeletionPolicy(Tag 6),InstanceConditionEvents,InstanceConditionMetrics,CELOmitFunction,ConservativeCRDComparison,GraphKind(Tag 14).
Drill 1: Wer darf was?
mkdir -p ~/kro-lab/tag12
cd ~/kro-lab/tag12
kubectl get clusterrole | grep kro
kubectl get clusterrole kro-cluster-role -o jsonpath='{.rules}{"\n"}'Erwartet:
kro-cluster-role 2026-11-11T17:28:01Z
kro-edit 2026-11-11T17:28:01Z
kro-view 2026-11-11T17:28:01Z
[{"apiGroups":["*"],"resources":["*"],"verbs":["*"]}]
kro-cluster-roleist die Rolle des Controllers: alles, überall.kro-editundkro-viewhängen sich an die eingebauten Rollenedit,adminundview. Wereditals ClusterRoleBinding hat, darf damit RGDs anlegen.
Fortschritt speichern? Kostenlos registrieren
Drill 2: Auf aggregation umstellen
Erweitere die Werte-Datei aus Tag 6 um den Modus und ein zweites Feature Gate:
cat > ~/kro-lab/kro-values.yaml <<'EOF'
rbac:
mode: aggregation
config:
featureGates:
DeletionPolicy: true
InstanceConditionEvents: true
EOF
helm upgrade kro oci://registry.k8s.io/kro/charts/kro -n kro-system \
--version 0.10.0 -f ~/kro-lab/kro-values.yaml
kubectl -n kro-system rollout status deploy/kro
kubectl get clusterrole | grep kroErwartet (gekürzt):
Release "kro" has been upgraded. Happy Helming!
STATUS: deployed
REVISION: 3
deployment "kro" successfully rolled out
kro-edit 2026-11-11T17:28:01Z
kro-view 2026-11-11T17:28:01Z
kro:controller 2026-11-11T18:05:34Z
kro:controller:static 2026-11-11T18:05:34Z
kro-cluster-role ist weg. kro:controller
sammelt alle Rollen mit dem Aggregations-Label ein, im Moment nur
kro:controller:static mit den Rechten für RGDs, CRDs und
Leases. Was jetzt passiert:
kubectl get notesapp -A
kubectl -n kro-system logs deploy/kro --since=1m | grep -m1 forbiddenErwartet:
NAMESPACE NAME REPLICAS AVAILABLE PORT STATE
app notes 1 1 30080 ACTIVE
gelb-dev notes 1 1 31000 ACTIVE
gelb-test notes 1 1 31001 ACTIVE
... ERROR dynamic-controller Error syncing item, requeuing with rate limit {"item":
{"name":"notes","namespace":"app"}, "error": "failed loading instance:
notesapps.lab.example.com \"notes\" is forbidden: User
\"system:serviceaccount:kro-system:kro\" cannot get resource \"notesapps\" in API
group \"lab.example.com\" in the namespace \"app\""}
Die Instanzen sagen ACTIVE, aber das ist ein alter
Stand. kro darf sie nicht einmal lesen. Der Beweis:
kubectl -n app scale deploy notes --replicas=2
kubectl -n app get deploy notesErwartet:
deployment.apps/notes scaled
NAME READY UP-TO-DATE AVAILABLE AGE
notes 1/2 2 1 14m
Keine Drift-Korrektur mehr. Ein STATE in der Instanz
sagt nur, was kro zuletzt geschrieben hat. Ob kro gerade arbeitet,
verraten die Logs.
Fortschritt speichern? Kostenlos registrieren
Drill 3: Rechte für die NotesApp
cat > kro-notes-rbac.yaml <<'EOF'
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
name: kro:controller:notes
labels:
rbac.kro.run/aggregate-to-controller: "true"
rules:
- apiGroups: ["lab.example.com"]
resources:
- notesapps
- notesapps/status
- notesfleets
- notesfleets/status
- notessettings
- notessettings/status
verbs: ["*"]
- apiGroups: [""]
resources:
- configmaps
- secrets
- services
- persistentvolumeclaims
- namespaces
verbs: ["*"]
- apiGroups: ["apps"]
resources: ["deployments", "statefulsets"]
verbs: ["*"]
- apiGroups: ["batch"]
resources: ["cronjobs"]
verbs: ["*"]
- apiGroups: ["networking.k8s.io"]
resources: ["networkpolicies"]
verbs: ["*"]
- apiGroups: ["policy"]
resources: ["poddisruptionbudgets"]
verbs: ["*"]
EOF- Die eigenen Arten mit
/status: kro schreibt den Status per Server-Side Apply und braucht dafür das Subresource. configmapsauch für die externen Referenzen aus Tag 9: Lesen braucht Rechte wie Schreiben.namespacesfür die Flotte.
kubectl auth can-i get notesapps -n app --as=system:serviceaccount:kro-system:kro
kubectl apply -f kro-notes-rbac.yaml
kubectl auth can-i get notesapps -n app --as=system:serviceaccount:kro-system:kroErwartet:
no
clusterrole.rbac.authorization.k8s.io/kro:controller:notes created
yes
Nach dem nächsten Versuch des Controllers (bis zu einer Minute):
kubectl -n app get deploy notesErwartet:
NAME READY UP-TO-DATE AVAILABLE AGE
notes 1/1 1 1 15m
kro arbeitet wieder und hat replicas zurückgesetzt.
Fortschritt speichern? Kostenlos registrieren
Drill 4: Rechte für das Team
Ein Entwickler-Konto (hier ein ServiceAccount) darf in
app NotesApps verwalten, sonst nichts:
cat > entwickler.yaml <<'EOF'
apiVersion: v1
kind: ServiceAccount
metadata:
name: entwickler
namespace: app
---
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
name: notesapp-nutzer
namespace: app
rules:
- apiGroups: ["lab.example.com"]
resources: ["notesapps"]
verbs: ["get", "list", "watch", "create", "update", "patch", "delete"]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
name: entwickler-notesapp
namespace: app
subjects:
- kind: ServiceAccount
name: entwickler
namespace: app
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: Role
name: notesapp-nutzer
EOF
kubectl apply -f entwickler.yaml
A=system:serviceaccount:app:entwickler
kubectl auth can-i create notesapps -n app --as=$A
kubectl auth can-i create deployments -n app --as=$AErwartet:
serviceaccount/entwickler created
role.rbac.authorization.k8s.io/notesapp-nutzer created
rolebinding.rbac.authorization.k8s.io/entwickler-notesapp created
yes
no
Jetzt als Entwickler eine zweite App:
cat > notes2.yaml <<'EOF'
apiVersion: lab.example.com/v1alpha1
kind: NotesApp
metadata:
name: notes2
namespace: app
spec:
apiKey: lab-api-key-456
nodePort: 30082
EOF
kubectl apply -f notes2.yaml --as=$A
kubectl -n app get deploy --as=$A
kubectl -n app wait notesapp/notes2 --for=condition=Ready --timeout=120s --as=$A
kubectl -n app get notesapp --as=$AErwartet:
notesapp.lab.example.com/notes2 created
Error from server (Forbidden): deployments.apps is forbidden: User
"system:serviceaccount:app:entwickler" cannot list resource "deployments" in API
group "apps" in the namespace "app"
notesapp.lab.example.com/notes2 condition met
NAME REPLICAS AVAILABLE PORT STATE
notes 1 1 30080 ACTIVE
notes2 1 1 30082 ACTIVE
Der Entwickler darf kein Deployment sehen, hat aber gerade eins anlegen lassen, dazu eine Datenbank, ein Volume und einen NodePort. kro handelt mit seinen Rechten. Die NotesApp ist damit eine Rechte-Erweiterung, und zwar genau so weit, wie die RGD es vorsieht. Das ist der Sinn der Sache, aber du musst es wissen.
Aufräumen:
kubectl -n app delete notesapp notes2 --as=$A
kubectl -n app get pvcErwartet:
notesapp.lab.example.com "notes2" deleted from app namespace
NAME STATUS VOLUME CAPACITY ...
notes-db-data Bound pvc-ea46db97-7ea6-44ea-979a-0cecbef22a32 1Gi ...
notes2-db-data Bound pvc-bcca5153-f4fb-437f-9f24-ce13f3966ef7 1Gi ...
Das Volume von notes2 bleibt dank Orphaned
stehen. Das darf nur jemand mit eigenen Rechten auf PVCs löschen:
kubectl -n app delete pvc notes2-db-dataFortschritt speichern? Kostenlos registrieren
Drill 5: Events bei jedem Wechsel
Das Feature Gate InstanceConditionEvents ist seit Drill
2 an. Lös einen Wechsel aus:
kubectl -n app patch notesapp notes --type merge -p '{"spec":{"replicas":2}}'
kubectl -n app wait notesapp/notes --for=condition=Ready --timeout=60s
kubectl -n app get events --field-selector involvedObject.kind=NotesAppErwartet:
notesapp.lab.example.com/notes patched
notesapp.lab.example.com/notes condition met
LAST SEEN TYPE REASON OBJECT MESSAGE
23s Warning Ready notesapp/notes True -> False: Lifecycle
18s Normal Ready notesapp/notes False -> True: Lifecycle
Während die zweite Replika startete, war die Instanz fünf Sekunden
nicht bereit (readyWhen aus Tag 5). Lifecycle
ist der reason aus deiner eigenen Condition. Events landen
in jedem Werkzeug, das Kubernetes-Events sammelt. Zurück auf eine
Replika:
kubectl -n app patch notesapp notes --type merge -p '{"spec":{"replicas":1}}'Fortschritt speichern? Kostenlos registrieren
Drill 6: Metriken
kro liefert Prometheus-Metriken auf Port 8078:
kubectl -n kro-system port-forward deploy/kro 8078:8078 >/dev/null &
sleep 2
curl -s localhost:8078/metrics | grep '^instance_reconcile_total'
curl -s localhost:8078/metrics | grep '^dynamic_controller_gvr_count'
curl -s localhost:8078/metrics | grep '^instance_reconcile_errors_total'
kill %1Erwartet (Zahlen weichen ab):
instance_reconcile_total{gvr="lab.example.com/v1alpha1, Resource=notesapps"} 116
instance_reconcile_total{gvr="lab.example.com/v1alpha1, Resource=notesfleets"} 19
instance_reconcile_total{gvr="lab.example.com/v1alpha1, Resource=notessettings"} 1
dynamic_controller_gvr_count 3
instance_reconcile_errors_total{gvr="lab.example.com/v1alpha1, Resource=notesapps"} 12
| Metrik | Frage |
|---|---|
dynamic_controller_gvr_count |
Für wie viele Arten läuft ein Controller? |
instance_reconcile_errors_total |
Wie oft ist ein Abgleich gescheitert? |
instance_state_transitions_total |
Wie oft wechseln Instanzen ihren Zustand? |
rgd_graph_build_errors_total |
Wie oft ist eine RGD durch die Prüfung gefallen? |
dynamic_controller_queue_length |
Staut sich Arbeit? |
Die zwölf Fehler bei notesapps sind die
forbidden-Meldungen aus Drill 2. Ein Alarm auf einen
steigenden Fehlerzähler hätte dir den Rechte-Fehler gemeldet, auch wenn
alle Instanzen ACTIVE zeigen. Im Chart schaltest du einen
Service und einen ServiceMonitor dafür mit
metrics.service.create und
metrics.serviceMonitor.enabled ein.
Fortschritt speichern? Kostenlos registrieren
Kontrollpunkt
helm get values kro -n kro-systemErwartet:
USER-SUPPLIED VALUES:
config:
featureGates:
DeletionPolicy: true
InstanceConditionEvents: true
rbac:
mode: aggregation
kubectl auth can-i create cronjobs -n app --as=system:serviceaccount:kro-system:kroErwartet: yes.
kubectl auth can-i create pods -n app --as=system:serviceaccount:kro-system:kroErwartet: no. kro legt keine Pods an, also braucht es
das Recht nicht.
diff kro-notes-rbac.yaml ~/kro-lab/code/loesung/tag12/kro-notes-rbac.yamlErwartet: keine Ausgabe.
Challenge
Nimm cronjobs aus kro:controller:notes
heraus und wende an. Was zeigt die Instanz notes, was
zeigen die Logs, und welcher Check kommt nicht mehr an? Stell die Rolle
danach wieder her.
Hinweis: Tag 8 hat die CronJobs angelegt.
Lösung
Diesmal merkt es die Instanz, denn kro darf sie lesen und ihren Status schreiben:
kubectl -n app get notesapp notes
kubectl -n app get notesapp notes \
-o jsonpath='{.status.conditions[?(@.type=="Ready")].message}{"\n"}'Erwartet:
NAME REPLICAS AVAILABLE PORT STATE
notes 1 1 30080 ERROR
resource reconciliation failed: apply "checkJobs": get current state for CronJob
"app/notes-check-health": cronjobs.batch "notes-check-health" is forbidden: User
"system:serviceaccount:kro-system:kro" cannot get resource "cronjobs" in API group
"batch" in the namespace "app"
Fehlt nur ein Teil der Rechte, landet der Fehler im Status. Fehlen die Rechte auf die Instanz selbst (Drill 2), sieht man nichts davon. Zurück:
kubectl apply -f kro-notes-rbac.yamlFortschritt speichern? Kostenlos registrieren
Enterprise-Notiz
- aggregation ist Pflicht: In jedem gemeinsam genutzten Cluster. Die zusätzlichen ClusterRoles gehören zur RGD und werden mit ihr ausgeliefert, am besten im selben Git-Ordner.
- Wer darf RGDs anlegen? Eine RGD ist eine API für den ganzen Cluster. Behandle das Recht darauf wie das Recht, CRDs anzulegen: nur das Plattform-Team.
- Instanzen absichern: Was in eine NotesApp darf,
steht im Schema. Was darüber hinaus gilt (keine NodePorts in
prod, Pflicht-Labels), gehört in eine Admission-Policy, siehe „Kyverno und NetworkPolicies zum Anfassen“.