This article explains how to implement Role-Based Access Controls in Kubernetes, including creating roles, role bindings, and verifying permissions.
In this lesson, we dive into Kubernetes Role-Based Access Controls (RBAC) to help you manage permissions effectively. You’ll learn how to create roles, bind them to users, and verify permissions within a namespace.
To define a role, create a YAML file that sets the API version to rbac.authorization.k8s.io/v1 and the kind to Role. In this example, we create a role named developer to grant developers specific permissions. The role includes a list of rules where each rule specifies the API groups, resources, and allowed verbs. For resources in the core API group, provide an empty string ("") for the apiGroups field.For instance, the following YAML definition grants developers permissions on pods (with various actions) and allows them to create ConfigMaps:
Both roles and role bindings are namespace-scoped. This example assumes usage within the default namespace. To manage access in a different namespace, update the YAML metadata accordingly.
After defining a role, you need to bind it to a user. A role binding links a user to a role within a specific namespace. In this example, we create a role binding named devuser-developer-binding that grants the user dev-user the developer role.Below is the combined YAML definition for both creating the role and its corresponding binding:
After applying your configurations, it’s important to verify that the roles and role bindings have been created correctly.To list all roles in the current namespace, execute:
Copy
Ask AI
kubectl get roles
Example output:
Copy
Ask AI
NAME AGEdeveloper 4s
Next, list all role bindings:
Copy
Ask AI
kubectl get rolebindings
Example output:
Copy
Ask AI
NAME AGEdevuser-developer-binding 24s
For detailed information about the developer role, run:
You can test whether you have the necessary permissions to perform specific actions by using the kubectl auth can-i command. For example, to check if you can create deployments, run:
Copy
Ask AI
kubectl auth can-i create deployments
This command might return:
Copy
Ask AI
yes
Similarly, to verify if you can delete nodes:
Copy
Ask AI
kubectl auth can-i delete nodes
Expected output:
Copy
Ask AI
no
To test permissions for a specific user without switching user contexts, use the --as flag. Although the developer role does not permit creating deployments, it does allow creating pods:
In some scenarios, you may want to restrict user access to a select group of resources. For example, if you have multiple pods in a namespace but only intend to provide access to pods named “blue” and “orange,” you can utilize the resourceNames field in the role rule.Start with a basic role definition without any resource-specific restrictions:
This lesson provided an in-depth look at implementing Role-Based Access Controls in Kubernetes. You learned how to create roles and role bindings, verify permissions, and restrict access to specific resources. Practicing these exercises will enhance your grasp of RBAC and help you manage Kubernetes security effectively.For additional details on Kubernetes RBAC, refer to the Kubernetes Documentation and explore best practices for securing your clusters.