In this guide, we explore Role-Based Access Controls (RBAC) in Kubernetes, including how to create and manage roles and role bindings effectively. RBAC provides a robust mechanism for managing access to resources within a Kubernetes cluster.Documentation Index
Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
Use this file to discover all available pages before exploring further.
Creating a Role
A Role in Kubernetes encapsulates a set of permissions for resources within a namespace. To create a role, define a role object in a YAML file with the following essential components:- Set the API version to
rbac.authorization.k8s.io/v1 - Specify the
kindasRole - Provide a metadata name (for example, “developer”)
- List the rules that define the API groups, resources, and permitted verbs (actions)
Both roles and role bindings in Kubernetes are namespace-scoped. In this example, the
developer role applies only to the default namespace. To apply a role in another namespace, add the namespace field within the metadata section of your YAML file.Creating a Role Binding
After defining a role, bind it to a specific user using a RoleBinding. A role binding connects a user to a role within a namespace, thereby granting the user the permissions defined in that role. Below is an example YAML configuration that creates a role binding named “devuser-developer-binding”. This binding assigns the “developer” role to the userdev-user:
Viewing and Verifying Roles and Bindings
Listing Roles and Role Bindings
You can verify that your roles and role bindings have been created correctly using thekubectl commands below:
Describing Specific Resources
To get detailed information about a specific role, use:Verifying User Permissions
Use thekubectl auth can-i command to verify if a user has permission to perform a specific action on a resource. For instance, to check if you can create deployments or delete nodes, execute:
--as flag. For example, to check if dev-user can create deployments or pods, run:
--namespace flag to specify the namespace for these commands.
Restricting Access to Specific Resources
Sometimes it’s necessary to restrict a role’s permissions to only specific resource instances. For example, if a user should only have access to pods named “blue” and “orange”, you can utilize theresourceNames field in the role’s rule: