β CKAD β RBAC Question # 8 (Final Notes, Exam-Style)
π Question
You are given a Deployment running in namespace analytics. It is failing due to RBAC issues.
A task list is provided:
- Create a ServiceAccount named
scout. - In the same namespace, three Role manifests are provided under
/opt/roles/. Examine them and deploy only the correct Role that satisfies the Deploymentβs needs. - Create a RoleBinding in the namespace attaching the correct Role to the
scoutServiceAccount. - Update the Deployment so it uses the new ServiceAccount.
- Roll out the fix and verify that the Deployment logs no longer report RBAC errors.
The Deployment container runs the following command in a loop:
kubectl get pods
π― What the question is REALLY testing
This question evaluates your ability to:
- Read Pod logs to understand RBAC failures
- Identify whether the requirement is Role or ClusterRole
- Identify which verbs and resources are needed
- Choose the correct RBAC manifest from multiple wrong ones
- Bind it via RoleBinding
- Patch the Deployment correctly
- Roll out the fix and verify
π Hidden Traps (Very Common in CKAD)
These must be explicitly remembered:
1. Logs contain βError from server (Forbidden)β + βin namespace β¦β
This means:
- It is NOT cluster-scoped
- You must use Role, NOT ClusterRole
- You must create a RoleBinding, NOT ClusterRoleBinding
2. The wrong Role may:
- Use the wrong verbs (watch, patch, delete)
- Have unrelated resources (events, secrets, deployments)
- Have incomplete resources (
pods/logwithoutpods) - Have only partial verbs
- Have only the sub-resource (
pods/log) which is never valid alone
3. pods/log NEVER works alone.
If a manifest contains:
resources:
- pods/log
But does NOT contain:
- pods
β This Role is invalid. Reject it.
4. The Role manifests may be valid YAML but logically incorrect.
You must match resources + verbs to the Deploymentβs error.
5. Deployment logs are long (10β90 days old).
Use:
kubectl logs deploy/mydeploy -n analytics --since=2m --timestamps
So you see only the recent RBAC errors.
π§ Interpreting the Logs (critical step)
Example log:
Error from server (Forbidden): pods is forbidden:
User "system:serviceaccount:analytics:default"
cannot list resource "pods" in API group "" in the namespace "analytics"
From this we extract:
Required Resource:
pods
Required Verb:
list- (sometimes also
get, butlistis the minimum)
Scope:
- namespaced, because it says:
in the namespace "analytics"
Required Role Type:
- Role only (NOT ClusterRole)
Required Binding Type:
- RoleBinding only.
Required ServiceAccount:
- NOT
default - Must use the newly created one β
scout
π§ͺ Step-by-Step Solution
π Universal Step β Extract Required Permissions From Logs
π This is the step you perform as soon as you inspect the logs.
As soon as you check the Pod logs, you will see one or more Forbidden errors:
Error from server (Forbidden): system:serviceaccount:<namespace>:<service-account>
cannot <verb> resource "<resource>" in API group "" in the namespace "<namespace>"
From this single log line, extract:
- Which ServiceAccount is actually used
- Which resources the Pod is trying to access
- Which verbs (list / get / watch / etc.)
-
Whether the scope is namespace or cluster
-
If log ends with:
in the namespace "X"β Role needed - If log ends with:
at the cluster scopeor uses-Ainternally β ClusterRole needed
π Step 0 β Check Whether the SA Currently Has Those Permissions
Run:
kubectl auth can-i <verb> <resource> --as=system:serviceaccount:<ns>:<sa> -n <ns>
Example:
kubectl auth can-i list pods --as=system:serviceaccount:ops:default -n ops
You will get:
no
This confirms the RBAC issue.
β The goal after fixing the RBAC is:
When you run the exact same command again:
kubectl auth can-i <verb> <resource> --as=system:serviceaccount:<ns>:<sa> -n <ns>
The answer must now be:
yes
This means your ServiceAccount + Role/RoleBinding (or ClusterRoleBinding) are correct.
Step 1 β Create the ServiceAccount
(They may give a YAML, or only key/values.)
kubectl create sa scout -n analytics
Step 2 β Inspect the three Roles provided
The exam provides three Role manifests inside a folder such as:
/opt/roles/
βββ role1.yaml
βββ role2.yaml
βββ role3.yaml
Below are the exact three roles, matching the type you saw in the exam.
πΉ role1.yaml β WRONG
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
name: reader-events
namespace: analytics
rules:
- apiGroups: [""]
resources: ["events"]
verbs: ["get", "list"]
β Why this Role is NOT correct
- The Deployment logs clearly show it is trying to access pods, not events.
- Even though verbs
getandlistare correct, the resource is completely wrong β events.
β‘ Reject this Role immediately.
πΉ role2.yaml β CORRECT
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
name: pod-reader
namespace: analytics
rules:
- apiGroups: [""]
resources: ["pods"]
verbs: ["get", "list"]
β Why this Role IS correct
Matches the log error exactly:
- Resource needed β pods
- Verbs needed β list (and optionally get)
- Scope β namespaced, and this Role is namespaced
β‘ This is the Role you MUST apply.
πΉ role3.yaml β WRONG
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
name: log-reader
namespace: analytics
rules:
- apiGroups: [""]
resources: ["pods/log"]
verbs: ["get", "list"]
β Why this Role is NOT correct
This is one of the most dangerous traps in CKAD & CKA:
-
pods/logCANNOT appear alone Kubernetes RBAC rules require the main resource (pods) before its subresource (pods/log) is allowed. -
A valid subresource rule must look like:
resources:
- pods
- pods/log
- This Role does not allow listing pods, so the Deployment would still fail.
β‘ Reject this Role. It is invalid for the scenario.
π§ Summary Table (quick reference)
| Role File | Resources | Verbs | Valid? | Reason |
|---|---|---|---|---|
| role1.yaml | events | get, list | β | Wrong resource |
| role2.yaml | pods | get, list | β | Matches log error exactly |
| role3.yaml | pods/log | get, list | β | Subresource cannot exist alone |
Step 3 β Apply ONLY the correct Role
kubectl apply -f /opt/roles/role2.yaml -n analytics
Step 4 β Create the RoleBinding
kubectl create rolebinding allow-pods \
--role=role2 \
--serviceaccount=analytics:scout \
-n analytics
Important:
- Must be
RoleBinding - Must reference Role, not ClusterRole
- Must reference correct SA:
scout
Step 5 β Patch the Deployment to use the ServiceAccount
Option 1 (recommended):
kubectl set serviceaccount deploy/analytics-app scout -n analytics
Option 2:
kubectl edit deploy analytics-app -n analytics
Under .spec.template.spec.serviceAccountName: set:
serviceAccountName: scout
Step 6 β Restart Pods so the new SA takes effect
Two methods:
Method A β rollout restart
kubectl rollout restart deploy analytics-app -n analytics
Method B β delete pods by label
kubectl delete pod -l app=analytics-app -n analytics
Step 7 β Verify RBAC is fixed
kubectl logs deploy/analytics-app -n analytics --since=2m
You should see no more Forbidden errors.
π Final Answer (Minimal RBAC YAML)
Below are the correct objects.
Correct Role (role2.yaml)
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
name: pod-reader
namespace: analytics
rules:
- apiGroups: [""]
resources: ["pods"]
verbs: ["get", "list"]
RoleBinding
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
name: allow-pods
namespace: analytics
subjects:
- kind: ServiceAccount
name: scout
namespace: analytics
roleRef:
kind: Role
name: pod-reader
apiGroup: rbac.authorization.k8s.io
π― Summary (short and crisp)
- Logs tell you exactly which resource & verb is missing.
- βin namespaceβ β use Role + RoleBinding
- Ignore Roles that have only
pods/log - Create the ServiceAccount
- Choose correct Role
- Bind it
- Patch Deployment
- Restart pods
- Verify logs β success.