Skip to main content

Cluster Architecture, Installation & Configuration

How to Configure ClusterRole and ServiceAccount Access in Kubernetes

The DevOpscube team is deploying an application that needs controlled access to Kubernetes resources across the cluster. The application should initially be able to view Pods and Deployments but must not have permission to create resources.

A dedicated ServiceAccount and ClusterRole need to be configured for this application.

Your task is to create a ServiceAccount named devopscube-sa.

Create a ClusterRole named devopscube-clusterrole that allows the ServiceAccount to list Pods and Deployments across all namespaces.

Bind the ClusterRole to the ServiceAccount using a ClusterRoleBinding named devopscube-clusterrolebinding.

Verify that the ServiceAccount can list Pods and Deployments but does not have permission to create Pods.

The application requirements have now changed. Update the existing ClusterRole to allow the ServiceAccount to create Pods, while keeping its existing permissions unchanged.

Verify the updated permissions and confirm that the ServiceAccount can now create Pods.

Use Kubernetes authorization checks to validate the permissions at each stage.

📚
For more details about ClusterRole,ServiceAccount and pods refer to the official Kubernetes documentation.

Create the ServiceAccount

Create a ServiceAccount to provide an identity for the application:

$ kubectl create serviceaccount devopscube-sa

serviceaccount/devopscube-sa created

Verify that it was created successfully:

$ kubectl get serviceaccount devopscube-sa

NAME            AGE
devopscube-sa   31s

The ServiceAccount provides the identity the application can use when making requests to the Kubernetes API.

Create the ClusterRole

Create a ClusterRole that allows get and list operations on Pods and Deployments:

$  kubectl create clusterrole devopscube-clusterrole \
  --verb=get,list \
  --resource=pods,deployments
  
clusterrole.rbac.authorization.k8s.io/devopscube-clusterrole created

Verify the permissions:

$ kubectl describe clusterrole devopscube-clusterrole

Name:         devopscube-clusterrole
Labels:       <none>
Annotations:  <none>
PolicyRule:
  Resources         Non-Resource URLs  Resource Names  Verbs
  ---------         -----------------  --------------  -----
  pods              []                 []              [get list]
  deployments.apps  []                 []              [get list]
root@controlplane:/home/vagrant/test# 

The ClusterRole provides permissions at the cluster level, allowing the role to be used across namespaces when bound appropriately.

Create the ClusterRoleBinding

Bind the devopscube-clusterrole to the devopscube-sa ServiceAccount:

$ kubectl create clusterrolebinding devopscube-clusterrolebinding \
  --clusterrole=devopscube-clusterrole \
  --serviceaccount=default:devopscube-sa
  
clusterrolebinding.rbac.authorization.k8s.io/devopscube-clusterrolebinding created

Verify the binding:

$ kubectl describe clusterrolebinding devopscube-clusterrolebinding 

Name:         devopscube-clusterrolebinding
Labels:       <none>
Annotations:  <none>
Role:
  Kind:  ClusterRole
  Name:  devopscube-clusterrole
Subjects:
  Kind            Name           Namespace
  ----            ----           ---------
  ServiceAccount  devopscube-sa  default

This confirms that the devopscube-sa ServiceAccount is bound to the devopscube-clusterrole and can use the permissions defined by that ClusterRole.

⚠️
Important: default:devopscube-sa means the ServiceAccount named devopscube-sa in the default namespace.

Verify the ServiceAccount Access

Use kubectl auth can-i to verify what the ServiceAccount is allowed to do without actually creating or modifying resources.

Check whether it can list Pods:

$ kubectl auth can-i list pods \
  --all-namespaces \
  --as=system:serviceaccount:default:devopscube-sa

yes
$ kubectl auth can-i list deployments \
  --all-namespaces \
  --as=system:serviceaccount:default:devopscube-sa
  
yes

Both should return:yes

You can also test an action that is not permitted, such as creating a Pod:

kubectl auth can-i create pod \
  --as=system:serviceaccount:default:devopscube-sa

The expected result is:no

Why Use kubectl auth can-i?

kubectl auth can-i is useful for checking whether a specific user or ServiceAccount has permission to perform an action. The --as option allows you to test permissions as a specific ServiceAccount, such as system:serviceaccount:default:devopscube-sa.

Add Create Permission for Pods

Edit the existing ClusterRole:

kubectl edit clusterrole devopscube-clusterrole

Update the rules so that Pods have get, list, and create, while Deployments retain only get and list:

Edit the devopscube-clusterrole
apiVersion: rbac.authorization.k8s.io/v1 kind: ClusterRole metadata: creationTimestamp: "2026-09-16T13:03:14Z" name: devopscube-clusterrole resourceVersion: "1044032" uid: 4bbea8fa-e29c-4728-95bf-5e14d8a2831b rules: - apiGroups: - "" resources: - pods verbs: - get - list - create - apiGroups: - apps resources: - deployments verbs: - get - list
kubectl describe clusterrole devopscube-clusterrole

This confirms that the ServiceAccount can now create Pods while still having read-only access to Deployments.

⚠️
Important: Pods belong to the core API group, represented by apiGroups: [""]. Deployments belong to the apps API group.

Verify Create Permission

Check whether the ServiceAccount can now create Pods:

$ kubectl auth can-i create pod \
  --as=system:serviceaccount:default:devopscube-sa
  
yes

The ServiceAccount now has permission to create Pods, while it still only has get and list permissions for Deployments.

CKA Exam Tips

These tips will help you quickly identify the important Kubernetes concepts and avoid common mistakes during the exam.

1. Remember the RBAC Flow

Think of RBAC as a relationship between the ServiceAccount, ClusterRole, and ClusterRoleBinding. The ServiceAccount provides the identity, the ClusterRole defines the permissions, and the ClusterRoleBinding grants those permissions to the ServiceAccount.

2. kubectl auth can-i Is Very Useful

Use kubectl auth can-i to verify whether a ServiceAccount can perform a specific action:

kubectl auth can-i <verb> <resource> \
  --as=system:serviceaccount:<namespace>:<serviceaccount>

For cluster-wide checks, add --all-namespaces.

3. Know the API Groups

Pods belong to the core API group, while Deployments belong to the apps API group:

pods         → apiGroups: [""]
deployments  → apiGroups: ["apps"]

4. ClusterRole vs ClusterRoleBinding

A ClusterRole defines the permissions, while a ClusterRoleBinding grants those permissions to a subject, such as a ServiceAccount.