Creating a Role
A role in Kubernetes is defined in a YAML file that outlines the permitted actions under the following key elements:- apiVersion: Must be set to
rbac.authorization.k8s.io/v1. - kind: Should be
Role. - metadata.name: The name of your role (e.g., “developer”).
- rules: A list describing the API groups, resources, and verbs (actions) that are allowed.
developer-role.yaml), create the role with:
Roles and role bindings are namespaced. In the example above, the role is created in the default namespace unless you specify otherwise within the metadata.
Binding the Role to a User
To grant the permissions defined in the role to a user, you need to create a role binding. A role binding links a user (or group) to a role. The YAML for a role binding includes:- metadata.name: A unique name for the role binding (e.g., “devuser-developer-binding”).
- subjects: The user, group, or service account to which permissions are granted.
- roleRef: A reference to the role created previously.
Verifying Roles and Bindings
To confirm that the role and its binding have been created successfully, you can list them using the following commands: List all roles in the current namespace:Checking Permissions
You can verify if you or another user have access to particular resources using thekubectl auth can-i command. For example:
To check if you can create deployments:
--as flag. For example, if the dev user has permission to create pods but not deployments, these commands will reflect that:
--namespace flag if needed.
Restricting Access to Specific Resources
RBAC in Kubernetes allows you to fine-tune permissions at a granular level. Instead of granting permissions universally to a resource type, you can restrict them to specific resources using theresourceNames field. For example, to allow a user to interact only with pods named “blue” and “orange”, define the role as follows:
Conclusion
In this lesson, we covered how to configure RBAC in Kubernetes by:- Creating a role with defined permissions.
- Binding a user to that role using a role binding.
- Verifying roles and permissions through
kubectlcommands. - Checking and testing user permissions.
- Restricting access to specific resources for enhanced security.