This article explores cluster roles and bindings, detailing how to inspect, create, and assign permissions for users in a Kubernetes environment.
In this lesson, we will explore the concepts of cluster roles and cluster role bindings. We will inspect the existing cluster roles and their bindings, count them, and review their permissions before creating new roles and bindings for a new team member named Michelle.
Begin by examining the currently defined cluster roles in your cluster. These roles include cluster-admin, system-specific roles, and others. Run the following command to list them:
You can count these roles manually by piping the output to wc -l. For example:
Copy
k get clusterroles --no-headers | wc -l
On one system, this command produced an output of 6, but keep in mind that this example shows a truncated list. In another instance, a total of 69 roles might be expected using a different counting method.
Cluster roles are cluster-scoped and are not limited to any namespace.
Next, verify the cluster role bindings by counting them with the following command:
Copy
k get clusterrolebindings --no-headers | wc -l
This command returned 54 on the system in question, indicating that there are 54 cluster role bindings.To illustrate further, here is an excerpt displaying some cluster roles and their creation times:
Copy
k get clusterrolesNAME CREATED ATcluster-admin 2022-04-18T00:01:23Zsystem:discovery 2022-04-18T00:01:23Zsystem:monitoring 2022-04-18T00:01:23Zsystem:basic-user 2022-04-18T00:01:23Zsystem:public-info-viewer 2022-04-18T00:01:23Zsystem:aggregate-to-admin 2022-04-18T00:01:23Zsystem:aggregate-to-edit 2022-04-18T00:01:23Zsystem:aggregate-to-view 2022-04-18T00:01:23Zsystem:heapster 2022-04-18T00:01:23Zsystem:node 2022-04-18T00:01:23Z...
Both cluster roles and cluster role bindings are applied across the entire cluster and are not namespace-specific.
The cluster-admin role represents the highest permission level and is bound to specific user groups via a ClusterRoleBinding. To identify the binding for the cluster-admin role, run:
You can then inspect the details of the cluster-admin binding with:
Copy
k describe clusterrolebindings cluster-admin
This command produces output similar to:
Copy
Name: cluster-adminLabels: kubernetes.io/bootstraping=rbac-defaultsAnnotations: rbac.authorization.kubernetes.io/autoupdate: trueRole: Kind: ClusterRole Name: cluster-adminSubjects: Kind Name Namespace ----- ------------------- ----------- Group system:masters
This confirms that the cluster-admin role is bound by default to the system:masters group, thereby granting all possible operations (denoted by [*] for all actions on all resources).
Exercise caution when modifying cluster roles as they affect permissions across the entire cluster.
Michelle’s roles are expanding to include storage management. To grant her access to storage-related resources, first verify the available API resources by running:
Copy
kubectl api-resources
This command lists resources along with their short names, API versions, and scope. For storage management, the key resources are persistent volumes and storage classes.
Bind the storage-admin role to Michelle with the following command:
Copy
k create clusterrolebinding michelle-storage-admin --user=michelle --clusterrole=storage-admin
Verify this binding:
Copy
k describe clusterrolebinding michelle-storage-admin
Expected output:
Copy
Name: michelle-storage-adminLabels: <none>Annotations: <none>Role: Kind: ClusterRole Name: storage-adminSubjects: Kind Name Namespace ---- ---- --------- User michelle
Michelle should now have the necessary permissions to manage storage resources such as persistent volumes and storage classes.This concludes the lesson on inspecting, creating, and binding cluster roles and cluster role bindings. By following these steps, you learned how to review existing roles, grant specific permissions to a user, and expand those permissions as job responsibilities change.