Learn about role-based access control in Kubernetes, including creating roles, binding them to users, and managing permissions for secure cluster access.
In this lesson, you’ll learn about role-based access control (RBAC) in Kubernetes. RBAC enables you to define roles with specific permissions and bind those roles to users or groups, ensuring secure and controlled access within your cluster. We’ll walk through creating a role, binding it to a user, verifying configurations, and restricting access to specific resources.
After saving the YAML file (for instance, as developer-role.yaml), create the role with:
Copy
Ask AI
kubectl create -f developer-role.yaml
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.
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.
Below is an example that binds the “developer” role to the user “dev-user”:
Copy
Ask AI
apiVersion: rbac.authorization.k8s.io/v1kind: RoleBindingmetadata: name: devuser-developer-bindingsubjects:- kind: User name: dev-user apiGroup: rbac.authorization.k8s.ioroleRef: kind: Role name: developer apiGroup: rbac.authorization.k8s.io
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:
Copy
Ask AI
kubectl get roles
Example output:
Copy
Ask AI
NAME AGEdeveloper 4s
List all role bindings:
Copy
Ask AI
kubectl get rolebindings
Example output:
Copy
Ask AI
NAME AGEdevuser-developer-binding 24s
To view detailed information about a specific role, use:
Copy
Ask AI
kubectl describe role developer
This command displays details such as allowed resources and permissions, for example:
You can verify if you or another user have access to particular resources using the kubectl auth can-i command. For example:To check if you can create deployments:
Copy
Ask AI
kubectl auth can-i create deployments
Example output:
Copy
Ask AI
yes
To check if you have permissions to delete nodes:
Copy
Ask AI
kubectl auth can-i delete nodes
Example output:
Copy
Ask AI
no
If you want to check permissions for a different user (like “dev-user”) without switching accounts, use the --as flag. For example, if the dev user has permission to create pods but not deployments, these commands will reflect that:
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 the resourceNames field. For example, to allow a user to interact only with pods named “blue” and “orange”, define the role as follows:
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 kubectl commands.
Checking and testing user permissions.
Restricting access to specific resources for enhanced security.
For additional information on Kubernetes RBAC, consider reviewing the Kubernetes Documentation. Continue practicing by applying these concepts to secure your cluster effectively.