RBAC
Control who can do what in your cluster using ServiceAccounts, Roles, and RoleBindings.
Time: ~10 minutes Difficulty: Intermediate
What You Will Learn
Section titled “What You Will Learn”- ServiceAccounts: identities for pods and automation
- Roles: named sets of permissions (verbs on resources)
- RoleBindings: connecting ServiceAccounts to Roles
- Namespace-scoped (Role) vs cluster-scoped (ClusterRole) permissions
- Testing permissions with
kubectl auth can-i
Deploy
Section titled “Deploy”Navigate to the demo directory:
cd demos/rbackubectl apply -f manifests/namespace.yamlkubectl apply -f manifests/serviceaccounts.yamlkubectl apply -f manifests/roles.yamlkubectl apply -f manifests/rolebindings.yamlkubectl apply -f manifests/test-pods.yamlUnderstand the Setup
Section titled “Understand the Setup”Two ServiceAccounts with different permission levels:
| ServiceAccount | Role | Can Do |
|---|---|---|
pod-reader | pod-reader | Get, list, watch pods |
pod-admin | pod-admin | Get, list, watch, create, delete pods and pod logs. Read services and deployments. |
Test Permissions
Section titled “Test Permissions”Check what each ServiceAccount can do
Section titled “Check what each ServiceAccount can do”# pod-reader can list podskubectl auth can-i list pods \ --as=system:serviceaccount:rbac-demo:pod-reader -n rbac-demo
# pod-reader CANNOT delete podskubectl auth can-i delete pods \ --as=system:serviceaccount:rbac-demo:pod-reader -n rbac-demo
# pod-admin CAN delete podskubectl auth can-i delete pods \ --as=system:serviceaccount:rbac-demo:pod-admin -n rbac-demo
# pod-admin CANNOT create deploymentskubectl auth can-i create deployments \ --as=system:serviceaccount:rbac-demo:pod-admin -n rbac-demoTry operations as each ServiceAccount
Section titled “Try operations as each ServiceAccount”# pod-reader lists pods (works)kubectl get pods -n rbac-demo \ --as=system:serviceaccount:rbac-demo:pod-reader
# pod-reader tries to delete a pod (denied)kubectl delete pod -l app=sample-app -n rbac-demo \ --as=system:serviceaccount:rbac-demo:pod-reader
# pod-admin lists pods (works)kubectl get pods -n rbac-demo \ --as=system:serviceaccount:rbac-demo:pod-admin
# pod-admin creates a pod (works)kubectl run test-pod --image=busybox:1.36 --command -- sleep 10 \ -n rbac-demo --as=system:serviceaccount:rbac-demo:pod-adminCheck cross-namespace access
Section titled “Check cross-namespace access”# pod-reader CANNOT list pods in default namespace (Role is namespace-scoped)kubectl auth can-i list pods \ --as=system:serviceaccount:rbac-demo:pod-reader -n defaultWhat is Happening
Section titled “What is Happening”manifests/ namespace.yaml # rbac-demo namespace serviceaccounts.yaml # pod-reader and pod-admin identities roles.yaml # Permission definitions (verbs on resources) rolebindings.yaml # Links ServiceAccounts to Roles test-pods.yaml # Sample app to test againstRBAC model:
ServiceAccount ──> RoleBinding ──> Role (who) (link) (what they can do)A Role defines allowed operations:
rules: - apiGroups: [""] # core API group resources: ["pods"] # what resource verbs: ["get", "list"] # what operationsA RoleBinding connects a subject (ServiceAccount, user, or group) to a Role. The binding is namespace-scoped, so pod-reader can only read pods in rbac-demo, not in other namespaces.
Experiment
Section titled “Experiment”-
List all permissions for a ServiceAccount:
Terminal window kubectl auth can-i --list \--as=system:serviceaccount:rbac-demo:pod-admin -n rbac-demo -
Create a ClusterRole and ClusterRoleBinding for cluster-wide read access:
Terminal window kubectl create clusterrole cluster-pod-reader \--verb=get,list,watch --resource=podskubectl create clusterrolebinding cluster-pod-reader-binding \--clusterrole=cluster-pod-reader \--serviceaccount=rbac-demo:pod-reader# Now pod-reader can list pods in ANY namespacekubectl auth can-i list pods \--as=system:serviceaccount:rbac-demo:pod-reader -n default -
Clean up the cluster-scoped resources:
Terminal window kubectl delete clusterrolebinding cluster-pod-reader-bindingkubectl delete clusterrole cluster-pod-reader
Cleanup
Section titled “Cleanup”kubectl delete namespace rbac-demoFurther Reading
Section titled “Further Reading”See docs/deep-dive.md for a detailed explanation of the RBAC authorization model, aggregated ClusterRoles, the default ServiceAccount, token projection, and least-privilege patterns for production.
Next Step
Section titled “Next Step”Move on to CRDs & Operators to learn how to extend Kubernetes with custom resources.