Kubernetes Security - ClusterRoles and ClusterRoleBindings: Practical RBAC Guide with Examples
Hello DevOps, DevSecOps Engineers, SREs, and Kubernetes Administrators!
As Kubernetes environments grow, managing access becomes an important part of cluster administration. Not every engineer needs full cluster-admin access. In many situations, we need to provide users with access to specific cluster-wide resources such as Nodes, PersistentVolumes, StorageClasses, or other non-namespaced resources.
This is where Kubernetes RBAC (Role-Based Access Control) becomes extremely useful.
In this article, we will explore ClusterRoles and ClusterRoleBindings with practical examples. We will create permissions for users, bind those permissions to users, validate access, and clean up the configuration.
The examples are based on the practical Kubernetes administration scenarios I have worked with and used while practicing Kubernetes RBAC concepts.
What We Will Learn
In this article, we will cover:
- What is a ClusterRole?
- What is a ClusterRoleBinding?
- ClusterRole vs Role
- ClusterRoleBinding vs RoleBinding
- Listing ClusterRoles and ClusterRoleBindings
- Creating a ClusterRole using the imperative approach
- Binding a ClusterRole to a user
- Creating ClusterRole YAML declaratively
- Granting access to Nodes
- Granting access to PersistentVolumes and StorageClasses
- Validating permissions with
kubectl auth can-i - Cleaning up RBAC resources
- Kubernetes RBAC security best practices
| Kubernetes User Access Control with ClusterRoleBindings to ClusterRole |
Prerequisite:
Before starting, you should have:
- A Kubernetes cluster up and running
kubectlconfigured- Basic understanding of Kubernetes RBAC
- Administrative permissions to create ClusterRoles and ClusterRoleBindings
Listing ClusterRoles
kubectl get clusterrole # Get the Count kubectl get clusterrole --no-headers |wc -l
Listing ClusterRoleBindings
To know about the api-resources that have clusterrole and clusterrolebindings.k api-resources |grep clusterTo veiew the clusterrolebindings available in this Kubernetes Cluster
kubectl get clusterrolebindings # Get the Count kubectl get clusterrolebindings --no-headers |wc -l
Imperative way
Practical Example 1: Create a ClusterRole for DaemonSets
Let's start with a simple example. Suppose user krish needs permission to list DaemonSets across the cluster.
First, verify that the user currently does not have the required access:
# Initial check kubectl get daemonsets --all-namespaces --as krishAfter confirming, let's create ClusterRole for 'krish' user.
kubectl create clusterrole list-ds-role \ --resource=daemonsets \ --verb=list kubectl describe clusterrole list-ds-role
Now create clusterrolebinding list-ds-rb for user 'krish' to map that clusterrole list-ds which created above.
kubectl create clusterrolebinding \ list-ds-rb \ --clusterrole=list-ds-role \ --user=krishAfter ClusterRoleBinding assigned to krish again check access:
kubectl get ds --as krish
![]() |
| Create ClusterRole, ClusterRoleBinding imperative way |
Cleanup for ClusterRoles
kubectl delete clusterrolebinding list-ds-rb kubectl delete clusterrole list-ds
| Cleanup ClusterRole and ClusterRoleBindings |
ClusterRole are Kubernetes Cluster wide and they are not part of any namespace. To know about user or groups are associated with cluster-admin role, use ClusterRoleBindings and describe it. Where we can see in the subject section that will reveals you about user/groups.
kubectl describe clusterrolebinding cluster-adminTo inspect the clusterrole 'cluster-admin' privileges describe will show the PolicyRules where what resources can be used? and what you can do? The '*' astriek is to indicate that 'all'. If you want to get all resources access then '*.*' should be given. And same way to indicate all actions such as create, delete, list, watch, get use '*'. A new user mahi joined the Kubernetes Administrtors team. She will be focusing on the nodes in the cluster. Let's create a ClusterRole and ClusterRoleBindings so that she gets access to the nodes .
Initially we will check that she is able to access the nodes or not.
kubectl create clusterrole node-admin --verb=get,list,watch --resource=nodes --dry-run=client -o yaml > node-admin.yaml kubectl apply -f node-admin.yaml kubectl describe clusterrole node-adminLet's bind the node-admin clusterrole to mahi user using clusterrolebinding.
kubectl create clusterrolebinding mahi-rb --clusterrole=node-admin --user=mahi --dry-run=client -o yaml > mahi-rb.yaml kubectl create -f node-admin-rb.yaml kubectl describe clusterrolebindings node-admin-rb # Check michelle have the access to nodes kubectl --as mahi get nodesIf a user responsibilities are growing as they are into the organization for atime being. Here Maheshwari(mahi) user got more responsibilities for maintaining storge that used for Kubernetes cluster. Create the required ClusterRole and ClusterRoleBindings to allow her access Storage. Requirements:
ClusterRole: storage-admin Resource: persistentvolumes Resource: storageclasses ClusterRoleBinding: mahi-storage-admin ClusterRoleBinding Subject: mahi ClusterRoleBinding Role: storage-adminNow you know all the steps how to proceed on the clusterrole, clusterrolebindings
kubectl create clusterrole storage-admin \ --verb=* --resource=persistentvolumes --resource=storageclasses \ --dry-run=client -o yaml > storage-admin.yaml kubectl apply -f storage-admin.yaml kubectl describe clusterrole storage-admin kubectl create clusterrolebinding mahi-storage-admin-rb \ --clusterrole=storage-admin --user=mahi --dry-run=client -o yaml > mahi-storage-admin-rb.yaml kubectl create -f mahi-storage-admin-rb.yaml kubectl describe clusterrolebinding mahi-storage-admin-rb # Validate that authentication given for mahi user to access storage kubectl get pv --as mahi kubectl get sc --as mahiHere the last execution of fetching the storageclasses using 'mahi' is successful.
Production RBAC Best Practices
As an Automation Architect, I recommend keeping the following principles in mind when designing Kubernetes RBAC.
1. Follow Least Privilege
Grant only the resources and verbs required for the user's responsibility.
2. Avoid cluster-admin for Daily Operations
cluster-admin provides extremely broad privileges. Use dedicated roles wherever possible.
3. Prefer Declarative RBAC
Store RBAC YAML in source control and manage it through your normal review and deployment process.
4. Validate With kubectl auth can-i
Before handing access to a user, test the exact permissions:
kubectl auth can-i <verb> <resource> --as <user>5. Be Careful With Wildcards
Avoid unnecessarily broad rules such as:
verbs:
- "*"or:
resources:
- "*"unless the requirement genuinely calls for them.
6. Review ClusterRoleBindings Regularly
Over time, users change teams and responsibilities.
Unused ClusterRoleBindings should be reviewed and removed according to your organization's access-management process.
Reader Challenge
Let's turn this into a real Kubernetes administration exercise.
A new Kubernetes administrator, mahi, has the following responsibilities:
Requirement 1
She should be able to:
get
list
watchKubernetes Nodes.
Requirement 2
She should be able to view:
PersistentVolumes
StorageClassesRequirement 3
She must not be able to:
delete nodes
delete persistentvolumes
create clusterrolesCreate the appropriate ClusterRoles and ClusterRoleBindings.
Then validate every permission using:
kubectl auth can-iBonus challenge:
Run:
kubectl auth can-i --list --as mahiReview the resulting permissions and determine whether the user has received any permissions beyond the original requirements.
This is a good exercise to understand how Kubernetes RBAC, ClusterRoles, ClusterRoleBindings, and least-privilege access work together.

Comments