Configuring Kubernetes RBAC
Creating Roles and ClusterRoles
To create a Role or ClusterRole, you need to define the permissions you want to grant. Here's an example of a Role that allows read access to Pods in the "default" namespace:
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
namespace: default
name: pod-reader
rules:
- apiGroups: [""] ## "" indicates the core API group
resources: ["pods"]
verbs: ["get", "list", "watch"]
To create a ClusterRole that allows read access to Nodes across the entire cluster:
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
name: node-reader
rules:
- apiGroups: [""] ## "" indicates the core API group
resources: ["nodes"]
verbs: ["get", "list", "watch"]
Binding Roles and ClusterRoles
After creating the Roles and ClusterRoles, you need to bind them to users, groups, or service accounts. Here's an example of a RoleBinding that grants the "pod-reader" Role to the "alice" user in the "default" namespace:
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
namespace: default
name: read-pods
subjects:
- kind: User
name: alice
apiGroup: rbac.authorization.k8s.io
roleRef:
kind: Role
name: pod-reader
apiGroup: rbac.authorization.k8s.io
And an example of a ClusterRoleBinding that grants the "node-reader" ClusterRole to the "node-readers" group across the entire cluster:
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: read-nodes
subjects:
- kind: Group
name: node-readers
apiGroup: rbac.authorization.k8s.io
roleRef:
kind: ClusterRole
name: node-reader
apiGroup: rbac.authorization.k8s.io
Verifying RBAC Configurations
You can use the kubectl auth can-i
command to verify the permissions granted to a user or service account. For example:
$ kubectl auth can-i get pods --namespace default --as alice
yes
$ kubectl auth can-i get nodes --as system:serviceaccount:default:my-service-account
no
This command checks if the specified user or service account has the "get" permission on Pods in the "default" namespace or Nodes in the cluster.