Question - 8
π Question (Exam Style)
A Deployment named mydeploy is running in namespace analytics. It is failing due to RBAC errors.
Look at the Pod logs and you will see messages similar to:
Error from server (Forbidden): system:serviceaccount:analytics:default
cannot list resource "pods" in API group "" in the namespace "analytics"
You are provided with:
- A requirement to create a new ServiceAccount named
pantherin the namespaceanalytics. - Three Role manifest files located in:
/opt/roles/
Their contents differ in:
- resources: (
events,pods,pods/log) - verbs: (
get,list) - No RoleBindings are provided β you must create one.
-
The question explicitly states:
-
βDo NOT modify any of the provided Role manifest files.β
π§ Your tasks:
- Create the ServiceAccount
pantherin theanalyticsnamespace. - Inspect all three provided Roles and select the only Role that satisfies the Podβs RBAC requirements.
- Apply ONLY the correct Role file.
-
Create a new RoleBinding in the same namespace that:
-
Binds the selected Role
- To the
pantherServiceAccount - Update the Deployment
scannerso that it uses thepantherServiceAccount. - Restart the Deployment so the new Pod picks up the new RBAC permissions.
- Confirm that the logs no longer show Forbidden errors.
π 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.
1οΈβ£ Understand the Question
A Deployment is running inside a specific namespace. Its logs show RBAC failures:
Error from server (Forbidden): system:serviceaccount:<namespace>:<sa-name>
cannot list resource "pods" in API group "" in the namespace "<namespace>"
This tells us:
- The app is trying to list pods
- The service account inside the pod does NOT have correct RBAC access
- The scope is namespaced (because logs say in the namespaceβ¦)
We must fix it using:
- One new ServiceAccount (given by question)
- Three provided Role manifests (we must choose the correct one)
- A new RoleBinding we must create
Goal: make the Deployment able to list pods successfully.
2οΈβ£ Step-by-Step Breakdown (Exam Strategy)
Step 1 β Create the ServiceAccount
If the question gives:
Create a ServiceAccount named panther in namespace analytics
Then:
kubectl create sa panther -n analytics
β Step 2 β Inspect the three provided Roles
Inside /opt/roles/ the exam provides three Role manifests.
We must:
- Read all of them
- Identify which Role matches the RBAC error
- Reject the invalid ones
Provided Role Manifests
role1.yaml β WRONG
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
name: reader-events
namespace: analytics
rules:
- apiGroups: [""]
resources: ["events"]
verbs: ["get", "list"]
β Wrong because:
- Resource is
events - Deployment needs access to
pods
role2.yaml β CORRECT
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
name: pod-reader
namespace: analytics
rules:
- apiGroups: [""]
resources: ["pods"]
verbs: ["get", "list"]
β Correct because:
- Resource =
pods - Verbs =
get,list - Scope is namespaced β matches error message
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"]
β Invalid Role because:
- RBAC cannot allow
pods/logalone - Must include pods as a resource:
Correct form would be:
resources:
- pods
- pods/log
Since the provided Role does not include pods, it can never satisfy the Deployment's error.
β‘ Therefore: The ONLY correct Role is role2.yaml.
We apply it:
kubectl apply -f /opt/roles/role2.yaml
Step 3 β Create the RoleBinding
The RoleBinding needs:
- Role: pod-reader
- ServiceAccount: gorilla
- Namespace: analytics
kubectl create rolebinding panther-rb \
--role=pod-reader \
--serviceaccount=analytics:panther \
-n analytics
Step 4 β Patch the Deployment to Use This ServiceAccount
Two valid methods:
Method A β Imperative (Fastest for CKAD)
kubectl set serviceaccount deployment/mydeploy panther -n analytics
Method B β kubectl edit
kubectl edit deployment mydeploy -n analytics
Add under .spec.template.spec:
serviceAccountName: panther
Step 5 β Restart Pods so RBAC Takes Effect
Two valid methods:
A β Rollout restart
kubectl rollout restart deployment mydeploy -n analytics
B β Delete pods manually
kubectl delete pod -l app=mydeploy -n analytics
Step 6 β Verify Fix
kubectl logs deploy/mydeploy -n analytics --since=1m --timestamps
You should now see no RBAC errors, and the pod successfully lists pods.