This lab article explores Kubernetes cluster roles and bindings, guiding you through inspecting, creating, and managing custom roles for team members.
In this lab article, we explore Kubernetes cluster roles and cluster role bindings. You will learn how to inspect existing roles and bindings, then create custom roles and bindings for a team member named Michelle. Follow along for step-by-step commands and detailed explanations.
Inspecting Cluster Roles and Cluster Role Bindings
Begin by counting the defined cluster roles. Since these objects are cluster-wide (not namespaced), run the command:
Copy
Ask AI
k get clusterroles --no-headers | wc -l
For example, you might see an output of 69 cluster roles.Next, determine the number of cluster role bindings with a similar command:
Copy
Ask AI
k get clusterrolebindings --no-headers | wc -l
This command returns 54. Note that although the cluster-admin role will appear in the list, both cluster roles and their bindings apply across the entire cluster.
Name: cluster-adminLabels: kubernetes.io/bootstrapping=rbac-defaultsAnnotations: rbac.authorization.kubernetes.io/autoupdate: trueRole: Kind: ClusterRole Name: cluster-adminSubjects: Kind Name Namespace ---- ---- --------- Group system:masters
This output confirms that the cluster-admin role is bound to the system:masters group.To understand the full range of permissions granted, describe the cluster role itself:
Suppose Michelle, a new team member, requires access to view nodes. Even though the cluster-admin role grants full privileges, creating a custom role for node-specific operations can enforce a principle of least privilege.Create a cluster role named michelle-role with the permissions get, list, and watch on nodes:
As Michelle’s responsibilities expand, she now requires access to storage resources, such as persistent volumes and storage classes.First, review available API resources to verify the exact resource names and versions:
Copy
Ask AI
kubectl api-resources
This command lists all available resources along with their short names, API groups, and versions.Create a new cluster role called storage-admin with permissions to list, create, get, and watch on persistent volumes and storage classes:
Copy
Ask AI
k create clusterrole storage-admin --resource=persistentvolumes,storageclasses --verb=list,create,get,watch
In summary, this article guided you through inspecting cluster roles and cluster role bindings in Kubernetes. You examined the cluster-admin role and its binding to the system:masters group, then created a tailored role and binding for Michelle to access nodes. Finally, you extended her permissions to cover storage resources by setting up a storage-admin role and the corresponding binding. These steps illustrate how Kubernetes RBAC can be efficiently adapted to meet evolving team responsibilities.
Leveraging custom roles and bindings not only enhances security by enforcing the principle of least privilege but also simplifies management as responsibilities within the team evolve.