Eine RBAC-Meldung nennt alles: wer fragt (User oder ServiceAccount), welches Verb, welche Ressource, welche API-Gruppe und welcher Scope – „in the namespace …“ oder „at the cluster scope“. Jedes Teil zeigt auf eine Stelle in Role, Binding oder Namespace.
Symptom
kubectl -n tag11 logs deploy/pod-lister --tail=1 Error from server (Forbidden): pods is forbidden: User "system:serviceaccount:tag11:app" cannot list resource "pods" in API group "" in the namespace "tag11"
Der Pod läuft, startet aber immer wieder neu: Der Container endet bei jedem Versuch mit Exit 1 und landet im Backoff. Als Admin fällt das beim Testen nicht auf – die RKE2-Kubeconfig ist system:admin in system:masters, und diese Gruppe umgeht RBAC komplett.
Diagnose
Rechte genau für Wer, Verb und Namespace prüfen und die Bindings suchen, die den Account nennen.
kubectl auth can-i list pods -n tag11 --as=system:serviceaccount:tag11:app kubectl auth can-i list pods -n default --as=system:serviceaccount:tag11:app kubectl get rolebinding -A -o custom-columns=NS:.metadata.namespace,\ NAME:.metadata.name,ROLE:.roleRef.name,SA:.subjects[*].name | grep -E '^NS|app' \ | tr -s ' ' no no - RBAC: role.rbac.authorization.k8s.io "pod-reader" not found NS NAME ROLE SA default pod-reader pod-reader app
Ursachen
in the namespaceRoleBinding im falschen Namespace
Eine RoleBinding gilt nur in ihrem eigenen Namespace und bindet eine Role aus diesem Namespace oder eine ClusterRole. Liegt sie in default, wirkt sie nicht in tag11.
at the cluster scopeRole statt ClusterRole
Nodes, PVs und Namespaces gehören zu keinem Namespace. Eine Role kann sie nennen, gilt aber nur in ihrem Namespace. Nötig sind ClusterRole und ClusterRoleBinding.
kubectl get nodes --as=system:serviceaccount:tag11:app Error from server (Forbidden): nodes is forbidden: User "system:serviceaccount:tag11:app" cannot list resource "nodes" in API group "" at the cluster scope
API groupFalsche API-Gruppe in der Regel
RBAC prüft Gruppe und Ressource zusammen. ConfigMaps liegen in der Kerngruppe "", nicht in apps – eine Regel mit apiGroups [apps] passt auf nichts.
kubectl -n tag11 get role cm-reader -o jsonpath='{.rules}{"\n"}'
kubectl api-resources --api-group='' | grep -w configmaps
[{"apiGroups":["apps"],"resources":["configmaps"],"verbs":["get","list"]}]
configmaps cm v1 true ConfigMapviolates PodSecurityKein RBAC – Pod Security Admission
Auch PSA meldet „is forbidden“, aber beim Anlegen der Pods. Das Event steht am ReplicaSet oder DaemonSet, nicht am Pod.
kubectl -n tag11-logs events --types=Warning | tail -1 1s (x2 over 3s) Warning FailedCreate DaemonSet/log-sammler (combined from similar events): Error creating: pods "log-sammler-vcmsj" is forbidden: violates PodSecurity "baseline:latest": hostPath volumes (volume "logs")
Lösung
kubectl -n default delete rolebinding pod-reader kubectl -n tag11 create rolebinding pod-reader --role=pod-reader \ --serviceaccount=tag11:app kubectl -n tag11 rollout restart deploy/pod-lister kubectl -n tag11 rollout status deploy/pod-lister kubectl auth can-i list pods -n tag11 --as=system:serviceaccount:tag11:app yes
Nicht tun: Dem Account einfach cluster-admin geben. Die Meldung sagt genau, welches Recht fehlt – mehr als das braucht er nicht.
Kubernetes debuggen, Tag 11: RBAC-Meldungen lesen, Rechte mit auth can-i prüfen und von Pod Security abgelehnte Pods finden.
Zu Tag 11 →
Häufige Fragen
Warum sagt auth can-i „yes“, obwohl es trotzdem scheitert?
can-i prüft die Regel formal. Mit -n tag11 und einer Role für nodes antwortet es „yes“ und warnt nur „resource 'nodes' is not namespace scoped“ – Knoten gibt es in keinem Namespace.
Wie sehe ich alle Rechte eines ServiceAccounts?
kubectl auth can-i --list -n <ns> --as=system:serviceaccount:<ns>:<name> zeigt die Summe aller Bindings für den Namespace, auch die über ClusterRoleBindings.