Fix RBAC Permission Denied Errors
Debug RBAC forbidden and unauthorized errors in Kubernetes. Covers ClusterRole vs Role scope and service account permissions.
π‘ Quick Answer: Debug forbidden and unauthorized errors in Kubernetes RBAC. Covers ClusterRole vs Role scope, RoleBinding targeting, service account tokens, and permission auditing.
The Problem
This is a common issue in Kubernetes security that catches both beginners and experienced operators.
The Solution
Step 1: Identify the Exact Error
# Typical RBAC error
# Error from server (Forbidden): pods is forbidden: User "system:serviceaccount:default:myapp"
# cannot list resource "pods" in API group "" in the namespace "production"Parse the error:
- Who:
system:serviceaccount:default:myapp(ServiceAccountmyappin namespacedefault) - What:
list pods - Where: namespace
production
Step 2: Check Current Permissions
# Can this SA do the thing?
kubectl auth can-i list pods -n production --as=system:serviceaccount:default:myapp
# no
# What CAN this SA do?
kubectl auth can-i --list --as=system:serviceaccount:default:myapp -n productionStep 3: Fix β Create the Right Binding
# Role (namespace-scoped)
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
name: pod-reader
namespace: production # Must match target namespace
rules:
- apiGroups: [""]
resources: ["pods"]
verbs: ["get", "list", "watch"]
---
# RoleBinding
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
name: myapp-pod-reader
namespace: production # Must match Role namespace
subjects:
- kind: ServiceAccount
name: myapp
namespace: default # SA's home namespace
roleRef:
kind: Role
name: pod-reader
apiGroup: rbac.authorization.k8s.ioKey gotcha: A RoleBinding in namespace production can reference a ServiceAccount from namespace default. The RoleBindingβs namespace determines WHERE the permissions apply.
Common Mistakes
# Wrong: ClusterRoleBinding when you need namespace-scoped
# ClusterRoleBinding grants permissions in ALL namespaces
# Wrong: Role in namespace A, RoleBinding in namespace B
# The Role and RoleBinding must be in the SAME namespace
# Wrong: Forgot to set serviceAccountName on the pod
kubectl get pod myapp-abc123 -o jsonpath='{.spec.serviceAccountName}'
# "default" β using the default SA which has no permissionsBest Practices
- Monitor proactively with Prometheus alerts before issues become incidents
- Document runbooks for your teamβs most common failure scenarios
- Use
kubectl describeand events as your first debugging tool - Automate recovery where possible with operators or scripts
Key Takeaways
- Always check events and logs first β Kubernetes tells you whatβs wrong
- Most issues have clear error messages pointing to the root cause
- Prevention through monitoring and proper configuration beats reactive debugging
- Keep this recipe bookmarked for quick reference during incidents

Recommended
Kubernetes Recipes β The Complete Book100+ production-ready patterns with detailed explanations, best practices, and copy-paste YAML. Everything in one place.
Get the Book βLearn by Doing
CopyPasteLearn β Hands-on Cloud & DevOps CoursesMaster Kubernetes, Ansible, Terraform, and MLOps with interactive, copy-paste-run lessons. Start free.
Browse Courses βπ Deepen Your Skills β Hands-on Courses
Courses by CopyPasteLearn.com β Learn IT by Doing
