Issue#
An application, CronJob or CI pipeline running inside the cluster calls the Kubernetes API and fails. Its logs, or kubectl run with its token, show:
Error from server (Forbidden): pods is forbidden: User "system:serviceaccount:monitoring:app-reader" cannot list resource "pods" in API group "" in the namespace "default"
The pod is running and the token is valid. Only this API call is rejected.
Cause#
RBAC denies everything by default. The ServiceAccount (app-reader in namespace monitoring) has no Role or ClusterRole bound to it that grants the list verb on pods in the default namespace.
The error message tells you everything you need:
- User:
system:serviceaccount:<namespace>:<name> - Verb:
list - Resource:
pods - API group:
""(core group) - Namespace:
default
A common trap is creating the RoleBinding in the ServiceAccount's own namespace when the access is needed in another namespace. A Role and RoleBinding only grant access inside the namespace they live in.
Solution#
- Confirm the permission is really missing by impersonating the ServiceAccount:
kubectl auth can-i list pods \
--as=system:serviceaccount:monitoring:app-reader \
-n default
It prints no.
- Create a Role in the target namespace (
default) with the required verbs:
kubectl create role pod-reader \
--verb=get,list,watch \
--resource=pods \
-n default
- Bind the Role to the ServiceAccount. The RoleBinding also goes in the target namespace:
kubectl create rolebinding app-reader-pod-reader \
--role=pod-reader \
--serviceaccount=monitoring:app-reader \
-n default
- Or, to keep it in Git, the same thing as YAML:
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
name: pod-reader
namespace: default
rules:
- apiGroups: [""]
resources: ["pods"]
verbs: ["get", "list", "watch"]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
name: app-reader-pod-reader
namespace: default
subjects:
- kind: ServiceAccount
name: app-reader
namespace: monitoring
roleRef:
kind: Role
name: pod-reader
apiGroup: rbac.authorization.k8s.io
kubectl apply -f pod-reader-rbac.yaml
- If the ServiceAccount needs to read pods in all namespaces, use a ClusterRole with a ClusterRoleBinding instead:
kubectl create clusterrole pod-reader-all --verb=get,list,watch --resource=pods
kubectl create clusterrolebinding app-reader-pod-reader-all \
--clusterrole=pod-reader-all \
--serviceaccount=monitoring:app-reader
Tip: For resources outside the core group, use
resource.groupwith--resource, for example--resource=deployments.appsor--resource=ingresses.networking.k8s.io. The API group is shown in the error message.
Verify#
Check the permission again:
kubectl auth can-i list pods \
--as=system:serviceaccount:monitoring:app-reader \
-n default
It should now print yes. To list everything the ServiceAccount can do in the namespace:
kubectl auth can-i --list \
--as=system:serviceaccount:monitoring:app-reader \
-n default
RBAC changes take effect immediately, so you do not need to restart the pod. Re-run the job or watch the application logs to confirm the Forbidden error is gone.