Skip to content

Forbidden: why the service account isn't allowed

Kubernetestested on RKE2 v1.37.2

An RBAC message names everything: who is asking (user or service account), which verb, which resource, which API group and which scope – "in the namespace …" or "at the cluster scope". Each part points to a place in the Role, the binding or the 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"

The pod runs but keeps restarting: the container exits with code 1 on every attempt and ends up in backoff. As an admin you never notice while testing – the RKE2 kubeconfig is system:admin in system:masters, and that group bypasses RBAC entirely.

Diagnosis

Check the permission for exactly that who, verb and namespace, and find the bindings that name the account.

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

Causes

in the namespaceRoleBinding in the wrong namespace

A RoleBinding only applies in its own namespace and binds a Role from that namespace or a ClusterRole. If it lives in default, it has no effect in tag11.

at the cluster scopeRole instead of ClusterRole

Nodes, PVs and namespaces don't belong to any namespace. A Role can name them but only applies inside its namespace. You need a ClusterRole and a 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 groupWrong API group in the rule

RBAC checks group and resource together. ConfigMaps live in the core group "", not in apps – a rule with apiGroups [apps] matches nothing.

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         ConfigMap

violates PodSecurityNot RBAC – Pod Security Admission

PSA also says "is forbidden", but when the pods are created. The event is on the ReplicaSet or DaemonSet, not on a 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")

Fix

Put the binding into the namespace where the Role lives and roll the pod out again: permissions take effect immediately, the backoff doesn't. For cluster-wide resources create a ClusterRole plus ClusterRoleBinding; for a wrong group fix the rule in the file.
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

Don't: Just giving the account cluster-admin. The message says exactly which permission is missing – it needs nothing more than that.

▶
Break it yourself and find it – free
Debugging Kubernetes, day 11: read RBAC messages, check permissions with auth can-i and find pods rejected by Pod Security.
Go to day 11 →

FAQ

Why does auth can-i say "yes" when it still fails?

can-i checks the rule formally. With -n tag11 and a Role for nodes it answers "yes" and only warns "resource 'nodes' is not namespace scoped" – nodes don't exist in any namespace.

How do I see all permissions of a service account?

kubectl auth can-i --list -n <ns> --as=system:serviceaccount:<ns>:<name> shows the sum of all bindings for the namespace, including those via ClusterRoleBindings.

Related error patterns

More