Assigning Roles to Users
To assign a role to a user in Kubernetes, you can use the RoleBinding
or ClusterRoleBinding
resource. The main difference between the two is that RoleBinding
is scoped to a specific namespace, while ClusterRoleBinding
is cluster-wide.
Assigning a Role to a User
- Create a
RoleBinding
or ClusterRoleBinding
resource:
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
name: user-reader-role
namespace: default
subjects:
- kind: User
name: john
roleRef:
kind: Role
name: reader
apiGroup: rbac.authorization.k8s.io
In this example, we're assigning the reader
role to the user john
in the default
namespace.
- Apply the
RoleBinding
resource:
kubectl apply -f role-binding.yaml
Verifying the Role Assignment
To verify that the role has been assigned to the user, you can use the kubectl auth can-i
command:
kubectl auth can-i list pods --as john
This command will check if the user john
has the permission to list pods in the default
namespace.
Assigning a ClusterRole to a User
To assign a ClusterRole
to a user, you can use the ClusterRoleBinding
resource:
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: user-cluster-admin
subjects:
- kind: User
name: jane
roleRef:
kind: ClusterRole
name: cluster-admin
apiGroup: rbac.authorization.k8s.io
In this example, we're assigning the cluster-admin
ClusterRole
to the user jane
, which grants them full administrative access to the entire cluster.
By understanding how to assign roles to users, you can effectively control and manage access to your Kubernetes cluster.