Kubernetes RBAC
Master Kubernetes Role-Based Access Control: Roles, ClusterRoles, RoleBindings, ClusterRoleBindings, ServiceAccounts, and authorization.
Kubernetes Role-Based Access Control (RBAC) regulates who (subjects: Users, Groups, and ServiceAccounts) can perform which operations (verbs: get, list, create, update, delete) on which resources (Pods, Deployments, Secrets) within a specific namespace or across the entire cluster. Mastering RBAC is essential for establishing least-privilege security boundaries and multi-tenant isolation.
RBAC Architecture: Roles vs ClusterRoles
The Kubernetes RBAC model is composed of four primary API resources defined in rbac.authorization.k8s.io/v1:
| Resource Kind | Scope | Bound By | Primary Purpose |
|---|---|---|---|
| Role | Namespaced | RoleBinding |
Grants permissions within a single target namespace (e.g., list Pods in dev) |
| ClusterRole | Cluster-wide | ClusterRoleBinding or RoleBinding |
Grants permissions cluster-wide (e.g., inspect Nodes) OR serves as a reusable template |
| RoleBinding | Namespaced | Associates subjects with a Role or ClusterRole |
Confines granted permissions strictly to the target namespace |
| ClusterRoleBinding | Cluster-wide | Associates subjects with a ClusterRole |
Grants permissions across all namespaces and cluster-scoped resources |
Key API Verbs and Resource Groups
When writing RBAC rules, permissions are declared using standard API groups, resources, and HTTP-aligned verbs:
- Resource Verbs:
get,list,watch,create,update,patch,delete,deletecollection. - Non-Resource Verbs:
get,post(used for health endpoints like/healthzor/metrics). - Resource Names: Constrains permissions to specific named objects (e.g., only Secret named
db-credentials).
RBAC Manifests
1. Namespaced Role and RoleBinding
The following manifest grants a CI/CD service account permission to deploy and inspect applications within the staging namespace:
apiVersion: v1
kind: ServiceAccount
metadata:
name: deployment-bot
namespace: staging
---
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
name: deployment-manager
namespace: staging
rules:
- apiGroups: ["apps"]
resources: ["deployments", "statefulsets"]
verbs: ["get", "list", "watch", "create", "update", "patch"]
- apiGroups: [""]
resources: ["pods", "pods/log", "services"]
verbs: ["get", "list", "watch"]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
name: bind-deployment-manager
namespace: staging
subjects:
- kind: ServiceAccount
name: deployment-bot
namespace: staging
roleRef:
kind: Role
name: deployment-manager
apiGroup: rbac.authorization.k8s.io
2. ClusterRole and ClusterRoleBinding for Global Monitoring
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
name: node-metrics-reader
rules:
- apiGroups: [""]
resources: ["nodes", "nodes/metrics", "nodes/proxy"]
verbs: ["get", "list", "watch"]
- nonResourceURLs: ["/metrics"]
verbs: ["get"]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: bind-node-metrics-reader
subjects:
- kind: ServiceAccount
name: prometheus-k8s
namespace: monitoring
roleRef:
kind: ClusterRole
name: node-metrics-reader
apiGroup: rbac.authorization.k8s.io
Testing and Auditing Permissions with kubectl
# Verify if current user can create deployments in production
kubectl auth can-i create deployments --namespace=production
# Output: yes / no
# Impersonate a ServiceAccount to test its granted permissions
kubectl auth can-i delete pods --as=system:serviceaccount:staging:deployment-bot -n staging
# Output: no
# Check what verbs a user or service account can perform across a namespace
kubectl auth can-i --list --as=system:serviceaccount:staging:deployment-bot -n staging
Common Mistakes
- Using wildcard asterisks (
*) in production: Grantingresources: ["*"]orverbs: ["*"]gives administrative control and destroys least-privilege boundaries. - Using ClusterRoleBinding instead of RoleBinding: Binding a
ClusterRolewith aClusterRoleBindinggrants cluster-wide access across all namespaces rather than restricting permissions to a single namespace. - Granting permissions on Secrets unnecessarily: Granting
verbs: ["get", "list"]onresources: ["secrets"]allows the subject to read plaintext database passwords, certificates, and tokens. - Mounting default ServiceAccount tokens into Pods: Leaving
automountServiceAccountToken: trueallows compromised containers to interact with the API server.
Best Practices
- Follow the Principle of Least Privilege: only grant the specific verbs and resources required.
- Reuse cluster-wide
ClusterRoledefinitions with namespacedRoleBindingobjects across multiple developer namespaces. - Regularly audit permissions using
kubectl auth can-i --list. - Use dedicated ServiceAccounts per workload rather than sharing the
defaultServiceAccount.
Interview Questions
1. What is the difference between a Role and a ClusterRole?
A Role is scoped strictly to a single namespace and can only grant access to namespaced resources (such as Pods, ConfigMaps, or Services) within that specific namespace. A ClusterRole is cluster-scoped and can grant access to cluster-level resources (such as Nodes, PersistentVolumes, or Namespaces), non-resource endpoints (/metrics), or serve as a reusable permission template across multiple namespaces.
2. What happens when you bind a ClusterRole using a RoleBinding?
When a ClusterRole is referenced inside a RoleBinding, the permissions defined in the ClusterRole are scoped strictly to the namespace of that RoleBinding. This powerful pattern allows cluster administrators to define global role templates (such as view or edit) once and bind them independently within individual tenant namespaces.
3. How do you verify if a ServiceAccount has permission to perform an action?
You can test permissions using kubectl auth can-i <verb> <resource> --as=system:serviceaccount:<namespace>:<serviceaccount-name> -n <target-namespace>.
4. What are the three subject types supported in RBAC RoleBindings?
The three subject types are:
User: External individual user identities authenticated via OIDC, certificates, or cloud IAM.Group: Collections of users provided by the identity provider.ServiceAccount: Machine identities managed natively by Kubernetes for workloads and Pods.
5. Why is deletecollection considered a dangerous verb in RBAC?
The deletecollection verb allows an actor to delete multiple resources simultaneously using label selectors (e.g., deleting all Pods or all ConfigMaps in a namespace with a single API call). It should only be granted to high-privilege administrative automation.
6. What is the difference between Authentication (AuthN) and Authorization (AuthZ) in Kubernetes?
Authentication (AuthN) verifies who the caller is (using X.509 client certificates, OIDC tokens, or webhook tokens). Authorization (AuthZ)—most commonly RBAC—determines what the authenticated identity is allowed to do against the API server.
Summary
- Roles define namespaced permissions; ClusterRoles define cluster-wide or reusable permissions.
- RoleBindings confine access to a single namespace; ClusterRoleBindings apply globally.
- Use
kubectl auth can-ito audit and debug access control issues. - Avoid wildcard
*permissions and grant minimal verbs on sensitive resources like Secrets.
Continue Learning
Previous: Kubernetes Security | Next: Spring Boot on Kubernetes
Was this guide helpful?
CodeWithVenu is free to read, with no paywall. If this guide saved you time or helped you prepare, a small voluntary contribution helps me write more practical tutorials, architecture guides, and interview-prep content.