Home KubernetesKubernetes RBAC: Roles, ClusterRoles and ServiceAccounts

Kubernetes RBAC: Roles, ClusterRoles and ServiceAccounts

by Knowledge Base Linux
Kubernetes RBAC roles and service accounts security illustration

By default, a Kubernetes cluster is a locked door with no keys issued: the API server authenticates every request but authorizes almost nothing unless you explicitly grant it. Kubernetes RBAC (Role-Based Access Control) is the mechanism that decides who can do what inside your cluster — which users, groups, and ServiceAccounts may list Pods, delete Deployments, or read Secrets, and in which namespaces. Misconfigured RBAC is one of the most common ways clusters get compromised, from overly broad cluster-admin bindings handed to CI bots to service accounts that can read every Secret in every namespace.

This guide explains Kubernetes RBAC from the ground up: the four core objects (Role, ClusterRole, RoleBinding, ClusterRoleBinding), how ServiceAccounts fit in, how to write least-privilege policies with kubectl, how to verify permissions before you deploy, and how to fix the mistakes everyone makes. If you are still getting comfortable with everyday cluster operations, keep our kubectl commands cheat sheet handy while you work through the examples below.

Prerequisites

To follow the Kubernetes RBAC examples hands-on, you need:

  • A working Kubernetes cluster — any recent version (1.28+) works: kind, minikube, k3s, or a managed cluster. RBAC is enabled by default on all of them.
  • kubectl configured with cluster-admin access for the initial setup steps, so you can create roles and bindings.
  • Basic familiarity with YAML manifests and namespaces. You will create test namespaces like dev and staging to see how bindings scope permissions.
  • Read access to Secrets awareness: the examples reference Secrets, so skim our Kubernetes ConfigMaps and Secrets guide if those objects are new to you.

How Kubernetes RBAC Works: The Four Core Objects

Kubernetes RBAC separates the question of what is allowed from who gets it. Two objects define permissions (the “what”), and two objects grant those permissions to subjects (the “who”):

  • Role — a namespaced set of permissions. A Role named pod-reader in namespace dev only ever grants rights inside dev.
  • ClusterRole — a cluster-wide set of permissions. It can grant access to cluster-scoped resources (Nodes, PersistentVolumes) or be bound into specific namespaces like a reusable template.
  • RoleBinding — grants a Role (or a ClusterRole, namespaced down) to subjects within one namespace.
  • ClusterRoleBinding — grants a ClusterRole to subjects across the entire cluster.

Subjects come in three flavors: Users (humans authenticated via certificates, OIDC, etc.), Groups (collections of users), and ServiceAccounts (non-human identities for workloads, formatted as system:serviceaccount:<namespace>:<name>).

Kubernetes RBAC flow showing subjects, bindings, roles, and protected cluster resources

Step 1: Create a Namespace-Scoped Role

Start with the principle of least privilege: grant only what is needed, where it is needed. This Role allows reading Pods and listing Deployments, but only inside the dev namespace:

kubectl create namespace dev

cat <<'EOF' | kubectl apply -f -
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
  namespace: dev
  name: pod-reader
rules:
- apiGroups: [""]
  resources: ["pods"]
  verbs: ["get", "list", "watch"]
- apiGroups: ["apps"]
  resources: ["deployments"]
  verbs: ["get", "list"]
EOF

Note the anatomy of a rule: apiGroups selects the API (empty string means the core API where Pods live), resources names the object types, and verbs lists the allowed actions. There is no “deny” in Kubernetes RBAC — permissions are purely additive, so keep each Role narrow.

Step 2: Bind the Role to a User with a RoleBinding

A Role does nothing until a binding connects it to a subject. This RoleBinding grants pod-reader to the user alice, but only in dev:

cat <<'EOF' | kubectl apply -f -
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
  name: read-pods-dev
  namespace: dev
subjects:
- kind: User
  name: alice
  apiGroup: rbac.authorization.k8s.io
roleRef:
  kind: Role
  name: pod-reader
  apiGroup: rbac.authorization.k8s.io
EOF

The roleRef is immutable after creation — if you point a binding at the wrong Role, you must delete and recreate the binding. Alice can now run kubectl get pods -n dev but gets Forbidden for kubectl get pods -n staging or kubectl delete pod -n dev.

Step 3: Use ClusterRoles for Reusable and Cluster-Wide Permissions

When several namespaces need the same permission set, define it once as a ClusterRole and bind it per namespace with RoleBindings — the ClusterRole acts as a template whose permissions are still confined to each binding’s namespace:

cat <<'EOF' | kubectl apply -f -
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
  name: deployment-manager
rules:
- apiGroups: ["apps"]
  resources: ["deployments", "replicasets"]
  verbs: ["get", "list", "watch", "create", "update", "patch", "delete"]
EOF

kubectl create rolebinding dev-deploys \
  --clusterrole=deployment-manager \
  --user=bob --namespace=dev

kubectl create rolebinding staging-deploys \
  --clusterrole=deployment-manager \
  --user=bob --namespace=staging

Bob can manage Deployments in dev and staging but has no rights anywhere else. Reserve ClusterRoleBinding for genuinely cluster-scoped needs — reading Nodes, managing PersistentVolumes, or granting cluster-admin to a break-glass account:

kubectl create clusterrolebinding break-glass-admin \
  --clusterrole=cluster-admin \
  --user=oncall-admin

Treat every ClusterRoleBinding as a security review item: a single one can expose the whole cluster. For the authoritative reference on which verbs and resources exist, see the official Kubernetes RBAC documentation.

Step 4: Grant Permissions to ServiceAccounts

Workloads need identities too. A ServiceAccount is a namespaced account that Pods use to talk to the API server — for example, a monitoring agent that lists Pods, or a CI deployer that updates Deployments. Create a dedicated ServiceAccount per workload (never reuse default) and bind a least-privilege Role to it:

kubectl create serviceaccount deploy-bot -n dev

cat <<'EOF' | kubectl apply -f -
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
  namespace: dev
  name: deploy-bot-role
rules:
- apiGroups: ["apps"]
  resources: ["deployments"]
  verbs: ["get", "list", "watch", "update", "patch"]
EOF

kubectl create rolebinding deploy-bot-binding \
  --role=deploy-bot-role \
  --serviceaccount=dev:deploy-bot \
  --namespace=dev

A Pod that runs as deploy-bot (via spec.serviceAccountName: deploy-bot) can now roll Deployments in dev but cannot read Secrets, touch other namespaces, or delete anything. Two hardening tips for Kubernetes RBAC with ServiceAccounts:

  • Disable auto-mounting where it is not needed: set automountServiceAccountToken: false on Pods and ServiceAccounts that never call the API. Every mounted token is a credential an attacker can steal.
  • Prefer short-lived tokens. Modern clusters issue bound, audience-scoped tokens via the TokenRequest API instead of long-lived Secret tokens. Avoid manually creating kubernetes.io/service-account-token Secrets unless a legacy integration demands it.

Step 5: Verify Permissions with kubectl auth can-i

Never guess whether a policy works — Kubernetes RBAC ships with a built-in dry-run checker. Test from the subject’s perspective before you hand over credentials:

# As user alice
kubectl auth can-i list pods --namespace dev --as alice
# yes

kubectl auth can-i delete pods --namespace dev --as alice
# no

# As the deploy-bot ServiceAccount
kubectl auth can-i update deployments --namespace dev \
  --as system:serviceaccount:dev:deploy-bot
# yes

kubectl auth can-i get secrets --namespace dev \
  --as system:serviceaccount:dev:deploy-bot
# no

Add --list to dump everything a subject may do in a namespace — an excellent audit primitive:

kubectl auth can-i --list --namespace dev --as alice

Run these checks in CI after every RBAC change: a policy that drifts silently is worse than no policy at all, because it creates false confidence.

Troubleshooting Kubernetes RBAC

Error: “User cannot list resource pods … Forbidden”

The classic symptom of a missing or mis-scoped binding. Walk the chain: does the Role exist in the right namespace (kubectl get role -n dev)? Does a RoleBinding in that same namespace reference it and name the right subject (kubectl get rolebinding -n dev -o yaml)? Remember that RoleBindings only grant permissions inside their own namespace — a binding in dev never helps in staging.

ClusterRoleBinding to a Role, or RoleBinding to a ClusterRole — which is legal?

Three of the four combinations are valid: RoleBinding→Role, RoleBinding→ClusterRole (namespaced down), ClusterRoleBinding→ClusterRole. The invalid one is ClusterRoleBinding→Role — the API server rejects it because a Role’s permissions are namespace-scoped and cannot be meaningfully granted cluster-wide.

ServiceAccount still gets 403 after binding

Check three things: the binding’s subject uses kind: ServiceAccount with both name and namespace set (a missing namespace silently targets the wrong account); the Pod actually runs as that account (kubectl get pod -o jsonpath='{.spec.serviceAccountName}'); and no stale long-lived token Secret is overriding the projected token.

Someone granted cluster-admin “temporarily” and forgot

Audit aggressively. List every ClusterRoleBinding and who it binds, then trim:

kubectl get clusterrolebindings -o custom-columns=NAME:.metadata.name,ROLE:.roleRef.name,SUBJECTS:.subjects[*].name | sort

Any binding to cluster-admin that is not a break-glass account or a platform component deserves a ticket and a deadline. Kubernetes RBAC follows least privilege only if you enforce it.

Conclusion

Kubernetes RBAC is simple at its core: Roles and ClusterRoles declare what is allowed, RoleBindings and ClusterRoleBindings decide who gets it, and ServiceAccounts give workloads their own least-privilege identities. The discipline that keeps clusters safe is equally simple — default to namespace-scoped Roles, reserve ClusterRoleBindings for cluster-scoped resources, create one ServiceAccount per workload with auto-mounting off, and verify every change with kubectl auth can-i. Adopt those habits and RBAC stops being a compliance checkbox and becomes the guardrail that lets teams move fast without handing every script the keys to the cluster.

You may also like

KbLinux

Typically replies within a day

Hello, Welcome to the site. Please click below button for chatting me through Telegram.