DEV Community

Cover image for Understanding Kubernetes ClusterRoles and ClusterRoleBindings
Adeoye Malumi
Adeoye Malumi

Posted on

Understanding Kubernetes ClusterRoles and ClusterRoleBindings

Yesterday I learned about Roles and RoleBindings. Today I explored ClusterRoles and ClusterRoleBindings for cluster-scoped resources like Nodes.

Why a Role Wasn't Enough

kubectl auth can-i get nodes
Enter fullscreen mode Exit fullscreen mode

Output:

Warning: resource 'nodes' is not namespace scoped
no
Enter fullscreen mode Exit fullscreen mode

Nodes are cluster-scoped resources, so a namespace Role cannot grant access.

Creating a ClusterRole

kubectl create clusterrole node-reader --verb=get,list,watch --resource=nodes
Enter fullscreen mode Exit fullscreen mode

Verify:

kubectl get clusterrole | grep node-reader
Enter fullscreen mode Exit fullscreen mode

Creating a ClusterRoleBinding

I first made a mistake:

kubectl create clusterrolebinding reader-binding --clusterrole=cluster-reader --user=adeoye
Enter fullscreen mode Exit fullscreen mode

Checking permissions:

kubectl auth can-i get nodes --as adeoye
Enter fullscreen mode Exit fullscreen mode

returned:

no - RBAC: clusterrole.rbac.authorization.k8s.io "cluster-reader" not found
Enter fullscreen mode Exit fullscreen mode

The binding referenced a ClusterRole that didn't exist.

I fixed it by deleting and recreating the binding:

kubectl delete clusterrolebinding reader-binding
kubectl create clusterrolebinding reader-binding --clusterrole=node-reader --user=adeoye
Enter fullscreen mode Exit fullscreen mode

Now:

kubectl auth can-i get nodes --as adeoye
Enter fullscreen mode Exit fullscreen mode

returned:

yes
Enter fullscreen mode Exit fullscreen mode

Testing as adeoye

kubectl config use-context adeoye
kubectl get nodes
Enter fullscreen mode Exit fullscreen mode

Listing nodes worked.

Describing a node:

kubectl describe node cka-cluster3-worker
Enter fullscreen mode Exit fullscreen mode

showed most information, but RBAC blocked access to leases and pods because the ClusterRole only granted access to nodes.

Trying to delete a node:

kubectl delete node cka-cluster3-worker
Enter fullscreen mode Exit fullscreen mode

returned Forbidden because the ClusterRole only included get, list, and watch.

Key Takeaways

  • Roles are namespace-scoped.
  • ClusterRoles are cluster-scoped.
  • ClusterRoleBindings grant cluster-wide permissions.
  • RBAC permissions are resource-specific.
  • Kubernetes follows the principle of least privilege.

Top comments (0)