Kubernetes RBAC Deep Dive: From Basics to ABAC
You deployed a cluster. You gave your team cluster-admin because “it’s easier.” Six months later, a compromised dev laptop deletes production namespaces. Kubernetes RBAC exists to prevent this, but its reputation for complexity drives people to over-permission.
This post takes you from RBAC fundamentals to building an Attribute-Based Access Control (ABAC) layer on top of RBAC. You will learn to write least-privilege policies, debug access issues, and implement a scalable authorization model. All examples tested on Kubernetes 1.29+ (EKS, GKE, AKS, kind).
Prerequisites
- Kubernetes cluster 1.29+ (kind for local testing:
kind create cluster) kubectlconfigured- Basic understanding of Kubernetes resources (Pods, Deployments, Services)
# Verify cluster version
kubectl version --short
# Client Version: v1.29.0
# Kustomize Version: v5.0.4
# Server Version: v1.29.0
RBAC Fundamentals: The Mental Model
Kubernetes RBAC has four core concepts:
| Concept | Resource | Purpose |
|---|---|---|
| Subject | User, Group, ServiceAccount | Who is making the request |
| Role / ClusterRole | Rules (verbs + resources) | What actions are allowed |
| RoleBinding / ClusterRoleBinding | Subjects + RoleRef | Who gets what permissions |
| Resource | Pods, Deployments, Secrets, etc. | What is being accessed |
Key distinction:
- Role + RoleBinding: Namespace-scoped. Only grants access within one namespace.
- ClusterRole + ClusterRoleBinding: Cluster-scoped. Grants access across all namespaces.
- ClusterRole + RoleBinding: Namespace-scoped grant of a cluster-wide role definition.
Core Verbs and Resources
Standard verbs:
get,list,watch— Read operationscreate,update,patch,delete,deletecollection— Write operationsimpersonate,bind,escalate— Special verbs (dangerous)
Resource naming:
# Core resources
pods, services, configmaps, secrets, endpoints, persistentvolumes, persistentvolumeclaims
# Apps resources
deployments, replicasets, statefulsets, daemonsets
# Batch resources
jobs, cronjobs
# RBAC resources
roles, rolebindings, clusterroles, clusterrolebindings
# Custom resources (CRDs)
. # e.g., certificates.cert-manager.io
Level 1: Least-Privilege Role for Developers
Goal: Developers can deploy and debug their apps in dev namespace, nothing else.
# dev-developer-role.yaml
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
namespace: dev
name: developer
rules:
# Read access to most resources
- apiGroups: [""]
resources: ["pods", "services", "configmaps", "secrets", "endpoints", "persistentvolumeclaims", "events"]
verbs: ["get", "list", "watch"]
- apiGroups: ["apps"]
resources: ["deployments", "replicasets", "statefulsets", "daemonsets"]
verbs: ["get", "list", "watch", "create", "update", "patch", "delete"]
- apiGroups: ["batch"]
resources: ["jobs", "cronjobs"]
verbs: ["get", "list", "watch", "create", "update", "patch", "delete"]
# Port-forward, exec, logs
- apiGroups: [""]
resources: ["pods/exec", "pods/portforward", "pods/log"]
verbs: ["create", "get"]
# NO: nodes, persistentvolumes, clusterroles, clusterrolebindings
# NO: create/update/delete secrets (read-only)
# NO: access to other namespaces
# dev-developer-binding.yaml
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
name: developer-binding
namespace: dev
subjects:
- kind: Group
name: dev-team
apiGroup: rbac.authorization.k8s.io
roleRef:
kind: Role
name: developer
apiGroup: rbac.authorization.k8s.io
Level 2: CI/CD Service Account (Deploy Only)
Goal: CI pipeline can update images in prod namespace, cannot read secrets or exec into pods.
# cicd-sa.yaml
apiVersion: v1
kind: ServiceAccount
metadata:
name: cicd-deployer
namespace: prod
---
# cicd-role.yaml
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
namespace: prod
name: cicd-deployer
rules:
# Only update deployments (image updates)
- apiGroups: ["apps"]
resources: ["deployments"]
verbs: ["get", "list", "patch", "update"]
# Scale deployments
- apiGroups: ["apps"]
resources: ["deployments/scale"]
verbs: ["get", "update", "patch"]
# Rollback
- apiGroups: ["apps"]
resources: ["deployments/rollback"]
verbs: ["create"]
# NO: secrets, configmaps, pods/exec, pods/portforward
# NO: create, delete (only patch/update for image changes)
# cicd-binding.yaml
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
name: cicd-deployer-binding
namespace: prod
subjects:
- kind: ServiceAccount
name: cicd-deployer
namespace: prod
roleRef:
kind: Role
name: cicd-deployer
apiGroup: rbac.authorization.k8s.io
Level 3: Read-Only Auditor Access (Cluster-Wide)
Goal: Security team can read everything, modify nothing.
# auditor-role.yaml
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
name: cluster-auditor
rules:
# Read all core resources
- apiGroups: [""]
resources: ["*"]
verbs: ["get", "list", "watch"]
# Read all apps resources
- apiGroups: ["apps"]
resources: ["*"]
verbs: ["get", "list", "watch"]
# Read all batch resources
- apiGroups: ["batch"]
resources: ["*"]
verbs: ["get", "list", "watch"]
# Read RBAC (but not bind/escalate)
- apiGroups: ["rbac.authorization.k8s.io"]
resources: ["roles", "rolebindings", "clusterroles", "clusterrolebindings"]
verbs: ["get", "list", "watch"]
# Read custom resources
- apiGroups: ["*"]
resources: ["*"]
verbs: ["get", "list", "watch"]
# NO: create, update, patch, delete, deletecollection
# NO: impersonate, bind, escalate
# auditor-binding.yaml
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: cluster-auditor-binding
subjects:
- kind: Group
name: security-auditors
apiGroup: rbac.authorization.k8s.io
roleRef:
kind: ClusterRole
name: cluster-auditor
apiGroup: rbac.authorization.k8s.io
Level 4: Namespace Admin (Full Control Within Namespace)
Goal: Team lead has full control over staging namespace, including RBAC within it.
# ns-admin-role.yaml
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
namespace: staging
name: namespace-admin
rules:
# Full access to all resources in namespace
- apiGroups: ["*"]
resources: ["*"]
verbs: ["*"]
# Manage RBAC within namespace
- apiGroups: ["rbac.authorization.k8s.io"]
resources: ["roles", "rolebindings"]
verbs: ["*"]
# ns-admin-binding.yaml
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
name: namespace-admin-binding
namespace: staging
subjects:
- kind: User
name: [email protected]
apiGroup: rbac.authorization.k8s.io
roleRef:
kind: Role
name: namespace-admin
apiGroup: rbac.authorization.k8s.io
Debugging RBAC: The auth can-i Workflow
When access is denied, debug systematically:
# Check if user can do something
kubectl auth can-i create deployments --namespace=dev
# yes/no
# Check as another user (requires impersonate permission)
kubectl auth can-i create deployments --namespace=dev [email protected]
# Check with full resource attributes
kubectl auth can-i create deployments --namespace=dev [email protected] -v=6
# Shows the exact authorization decision
# List all permissions for a user in a namespace
kubectl auth can-i --list --namespace=dev [email protected]
# List all cluster-wide permissions
kubectl auth can-i --list [email protected]
Common Debugging Scenarios
| Symptom | Likely Cause | Fix |
|---|---|---|
| “forbidden: cannot create resource” | Missing verb or resource in Role | Add create verb for resource |
| “forbidden: cannot bind role” | Missing bind verb on clusterrole |
Add bind verb (carefully!) |
| “forbidden: attempt to grant extra privileges” | Escalation attempt | User tries to create role with more perms than they have |
| ServiceAccount works locally but not in CI | Different SA or namespace | Check kubectl get sa in CI namespace |
| CRD access denied | CRD not in Role rules | Add resources: ["myresources.mygroup.io"] |
Advanced: Impersonation and Delegation
Allow a user to act as another user (for debugging, support):
# impersonator-role.yaml
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
name: user-impersonator
rules:
- apiGroups: [""]
resources: ["users", "groups", "serviceaccounts"]
verbs: ["impersonate"]
- apiGroups: [""]
resources: ["users", "groups", "serviceaccounts"]
verbs: ["get", "list"]
Usage:
kubectl auth can-i create pods --namespace=dev [email protected]
# The impersonator checks if dev-user CAN do it
Advanced: Aggregated ClusterRoles
Compose ClusterRoles from smaller pieces. Kubernetes does this automatically for labels:
# Custom aggregated role
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
name: custom-admin
aggregationRule:
clusterRoleSelectors:
- matchLabels:
rbac.example.com/aggregate-to-custom-admin: "true"
rules: [] # Empty - populated by aggregation
Then create component roles with the label:
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
name: custom-admin-network
labels:
rbac.example.com/aggregate-to-custom-admin: "true"
rules:
- apiGroups: ["networking.k8s.io"]
resources: ["networkpolicies", "ingresses"]
verbs: ["*"]
Building ABAC on Top of RBAC
RBAC is static (role = fixed permissions). ABAC adds dynamic attributes: time, location, resource labels, request context.
Kubernetes doesn’t have native ABAC (removed in 1.6). We implement it with:
- Admission Webhooks — Intercept requests, evaluate attributes
- Kyverno / OPA Gatekeeper — Policy engines with attribute access
- Custom authorizer — Full control (complex)
ABAC Example with Kyverno: Time-Based Access
Developers can only deploy during business hours (9 AM – 6 PM UTC):
# time-based-deploy.yaml
apiVersion: kyverno.io/v1
kind: ClusterPolicy
metadata:
name: business-hours-deploy
spec:
validationFailureAction: Enforce
background: false
rules:
- name: check-business-hours
match:
any:
- resources:
kinds: ["Deployment"]
operations: ["CREATE", "UPDATE", "PATCH"]
validate:
message: "Deployments only allowed during business hours (09:00-18:00 UTC)"
pattern:
request:
object:
metadata:
annotations:
deploy-time: "^([0-1][0-9]|2[0-3]):[0-5][0-9]$"
deny:
conditions:
- key: "{{ time_now('UTC') }}"
operator: NotIn
value: ["09", "10", "11", "12", "13", "14", "15", "16", "17"]
ABAC Example with Kyverno: Label-Based Access
Users can only manage resources with matching team label:
# team-label-isolation.yaml
apiVersion: kyverno.io/v1
kind: ClusterPolicy
metadata:
name: team-label-isolation
spec:
validationFailureAction: Enforce
rules:
- name: enforce-team-label
match:
any:
- resources:
kinds: ["Deployment", "Service", "ConfigMap", "Secret"]
operations: ["CREATE", "UPDATE", "PATCH"]
validate:
message: "Resource must have team label matching your group"
pattern:
request:
object:
metadata:
labels:
team: "{{ request.user_info.groups | regex_replace_all('.*team-([^,]+).*', '') }}"
ABAC Example: Emergency Break-Glass Access
Allow emergency access with audit trail and time limit:
# break-glass.yaml
apiVersion: kyverno.io/v1
kind: ClusterPolicy
metadata:
name: break-glass-access
spec:
validationFailureAction: Enforce
rules:
- name: require-break-glass-annotation
match:
any:
- resources:
kinds: ["*"]
operations: ["CREATE", "UPDATE", "PATCH", "DELETE"]
validate:
message: "Emergency access requires break-glass annotation with justification"
deny:
conditions:
- key: "{{ request.user_info.username }}"
operator: NotIn
value: ["system:admin", "cluster-admin-user"]
- key: "{{ request.object.metadata.annotations.break-glass-justification }}"
operator: ""
context:
- name: break-glass-annotations
configMap:
name: break-glass-config
namespace: kyverno
RBAC for Multi-Tenancy: The Hierarchical Model
For multi-tenant clusters, organize RBAC hierarchically:
# Tenant namespace template
apiVersion: v1
kind: Namespace
metadata:
name: tenant-acme
labels:
tenant: acme
environment: production
---
# Tenant admin (full control within tenant namespaces)
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
name: tenant-admin
rules:
- apiGroups: ["*"]
resources: ["*"]
verbs: ["*"]
- apiGroups: ["rbac.authorization.k8s.io"]
resources: ["roles", "rolebindings"]
verbs: ["*"]
---
# Bind to tenant namespaces only
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
name: acme-tenant-admin
namespace: tenant-acme
subjects:
- kind: Group
name: acme-admins
apiGroup: rbac.authorization.k8s.io
roleRef:
kind: ClusterRole
name: tenant-admin
apiGroup: rbac.authorization.k8s.io
Use Hierarchical Namespaces (HNC) for automatic inheritance.
Security Hardening: Dangerous Permissions to Avoid
| Permission | Why Dangerous | Alternative |
|---|---|---|
verbs: ["*"] on resources: ["*"] |
Full cluster control | Explicit verbs and resources |
impersonate on users/groups/SA |
Act as any identity | Restrict to specific SAs |
bind on clusterroles |
Create bindings to any role | Use RoleBinding, not ClusterRoleBinding |
escalate on clusterroles |
Create roles with more perms than you have | Never grant |
Secrets create/update/delete |
Inject malicious credentials, steal data | Read-only for most roles |
| Pods/exec, pods/portforward | Arbitrary code execution in pods | Restrict to specific namespaces/roles |
CertificateSigningRequests approve/sign |
Issue valid certs for any identity | Restrict to cert-manager only |
Automated RBAC Audit
Run regular audits with kube-bench and rbac-tool:
# Install rbac-tool
go install github.com/rancher/rbac-tool@latest
# Analyze current RBAC
rbac-tool analyze --kubeconfig ~/.kube/config
# Find over-privileged subjects
rbac-tool lookup --subject-kind User --subject-name [email protected]
# Generate least-privilege roles from audit logs
rbac-tool generate --kubeconfig ~/.kube/config --audit-log /var/log/kubernetes/audit.log
# kube-bench CIS benchmark
docker run --pid=host -v /etc/kubernetes:/etc/kubernetes:ro -v /var/lib/kubelet:/var/lib/kubelet:ro aquasec/kube-bench:latest --version 1.29
Tools Cross-References
- YAML Formatter — Format RBAC manifests, Kyverno policies
- JSON Formatter — Format rbac-tool output, audit logs
- JWT Decoder — Inspect OIDC tokens for RBAC groups
Related Posts
- Kubernetes Network Policies: Zero-Trust Networking Guide — Network-level isolation
- Kubernetes Security Hardening: CIS Benchmarks Guide — CIS benchmark compliance
- systemd Service Hardening — Node-level hardening
- eBPF for Container Security — Runtime RBAC violation detection
Conclusion
RBAC is not “too complex”—it is explicitly designed for least privilege. The complexity comes from trying to retrofit least privilege onto clusters that started with cluster-admin everywhere.
Start with the four levels above: Developer (namespace read/write), CI/CD (deploy-only), Auditor (cluster read-only), Namespace Admin (full namespace control). Add ABAC via Kyverno for dynamic policies: time-based, label-based, break-glass. Audit quarterly with rbac-tool and kube-bench.
The goal is not perfect RBAC on day one. The goal is: every principal has exactly the permissions they need, no more, and you can prove it.
Next steps:
- Audit current bindings:
rbac-tool analyze - Create the four baseline roles for your namespaces
- Migrate one team off
cluster-adminto Developer role - Deploy Kyverno for ABAC policies
- Add break-glass process for emergencies
- Schedule quarterly RBAC reviews
RBAC done right is invisible—it just works. RBAC done wrong is a breach waiting to happen.