To check the RoleBindings and ClusterRoleBindings associated with a specific service account in Kubernetes, you can use the following commands and techniques:
Steps to Check Service Account Role Bindings
- List RoleBindings in the Namespace: List all RoleBindings in the namespace where the service account resides.
- List ClusterRoleBindings: List all ClusterRoleBindings in the cluster.
- Describe Specific RoleBindings and ClusterRoleBindings: Use the
kubectl describecommand to get detailed information about specific RoleBindings and ClusterRoleBindings.
Commands
List RoleBindings in the Namespace
To list all RoleBindings in a specific namespace:
kubectl get rolebindings -n <namespace>
Example:
kubectl get rolebindings -n my-namespace
List ClusterRoleBindings
To list all ClusterRoleBindings in the cluster:
kubectl get clusterrolebindings
Describe RoleBindings and ClusterRoleBindings
To get detailed information about a specific RoleBinding or ClusterRoleBinding, use the kubectl describe command.
- Describe a RoleBinding:
kubectl describe rolebinding <rolebinding-name> -n <namespace>
Example:
kubectl describe rolebinding scale-rolebinding -n my-namespace
- Describe a ClusterRoleBinding:
kubectl describe clusterrolebinding <clusterrolebinding-name>
Example:
kubectl describe clusterrolebinding clusterrolebinding-name
Example: Finding RoleBindings and ClusterRoleBindings for a Specific Service Account
You can also filter RoleBindings and ClusterRoleBindings to find those associated with a specific service account using kubectl and jq.
- List and Filter RoleBindings:
kubectl get rolebindings -n my-namespace -o json | jq '.items[] | select(.subjects[]?.kind=="ServiceAccount" and .subjects[]?.name=="cronjob-sa" and .subjects[]?.namespace=="my-namespace") | {name: .metadata.name, roleRef: .roleRef}'
- List and Filter ClusterRoleBindings:
kubectl get clusterrolebindings -o json | jq '.items[] | select(.subjects[]?.kind=="ServiceAccount" and .subjects[]?.name=="cronjob-sa" and .subjects[]?.namespace=="my-namespace") | {name: .metadata.name, roleRef: .roleRef}'
Example Output
The output will list the RoleBindings and ClusterRoleBindings associated with the specified service account. For example:
{ "name": "scale-rolebinding", "roleRef": { "apiGroup": "rbac.authorization.k8s.io", "kind": "Role", "name": "scale-role" } }{ "name": "example-clusterrolebinding", "roleRef": { "apiGroup": "rbac.authorization.k8s.io", "kind": "ClusterRole", "name": "example-clusterrole" } }
Complete Example
Here’s a complete workflow to check the role bindings for a service account named cronjob-sa in the my-namespace namespace.
- List RoleBindings:
kubectl get rolebindings -n my-namespace
- List ClusterRoleBindings:
kubectl get clusterrolebindings
- Describe a Specific RoleBinding:
kubectl describe rolebinding scale-rolebinding -n my-namespace
- Describe a Specific ClusterRoleBinding:
kubectl describe clusterrolebinding example-clusterrolebinding
- Filter RoleBindings for the Service Account:
kubectl get rolebindings -n my-namespace -o json | jq '.items[] | select(.subjects[]?.kind=="ServiceAccount" and .subjects[]?.name=="cronjob-sa" and .subjects[]?.namespace=="my-namespace") | {name: .metadata.name, roleRef: .roleRef}'
- Filter ClusterRoleBindings for the Service Account:
kubectl get clusterrolebindings -o json | jq '.items[] | select(.subjects[]?.kind=="ServiceAccount" and .subjects[]?.name=="cronjob-sa" and .subjects[]?.namespace=="my-namespace") | {name: .metadata.name, roleRef: .roleRef}'
By following these steps, you can identify which roles and role bindings are associated with a specific service account in your Kubernetes cluster.