Skip to main content
In this lab we inspect ServiceAccounts and resolve a permission issue so a dashboard application can query the Kubernetes API. Follow the numbered steps below to diagnose the problem, create a dedicated ServiceAccount with the required RBAC, validate access with a token, and update the Deployment so pods automatically mount the correct credentials. Quick reference — common commands used in this exercise: For additional context on RBAC and ServiceAccounts see:

1) How many ServiceAccounts exist in the default namespace?

Run:
Example output:
Interpretation: There are two ServiceAccounts in the default namespace: default and dev.

2) What is the secret token used by the default ServiceAccount?

Describe the default ServiceAccount:
Look under Tokens: in the output. In this environment the Tokens: field shows None, which indicates there is no separate legacy Secret object listed for that ServiceAccount. Modern Kubernetes clusters use projected service account tokens (mounted into pods by the kubelet) instead of Secret-based tokens, so you may see a projected token volume instead of a Secrets entry.

3) Inspect the dashboard Deployment — what image is used?

List deployments and inspect the dashboard deployment:
Relevant portion (trimmed from the describe output):
Conclusion: The web-dashboard Deployment uses the image gcr.io/kodekloud/customimage/my-kubernetes-dashboard.

4) The dashboard UI shows a permission error when trying to list pods

The dashboard UI reports:
This indicates the dashboard app is authenticating as the default ServiceAccount in the default namespace, which lacks permission to list pods. To confirm which account the pod uses, inspect the Deployment or Pod spec and check serviceAccount / serviceAccountName. The error message system:serviceaccount:default:default makes it clear the application is running as the default ServiceAccount.

5) Inspect the dashboard Pod — where are service account credentials mounted?

Identify the dashboard pod:
Describe the pod (use the actual pod name returned above):
Look for these fields in the describe output:
  • Service Account: default (near the top)
  • Under Containers → Mounts: a mount like /var/run/secrets/kubernetes.io/serviceaccount
  • Under Volumes: a projected volume such as kube-api-access-xxxxx with details like TokenExpirationSeconds, ConfigMapName: kube-root-ca.crt, and DownwardAPI.
Example (trimmed):
Key point: ServiceAccount credentials are mounted inside the pod at /var/run/secrets/kubernetes.io/serviceaccount via a projected volume.

6) Create a new ServiceAccount for the dashboard with the appropriate access

Instead of using the cluster default ServiceAccount, create a dedicated ServiceAccount that will be granted the exact permissions the dashboard needs. Create the ServiceAccount:
In this environment the RBAC manifests already exist under /var/rbac/ and define a Role and RoleBinding that grant dashboard-sa permission to read pods:
Apply these manifests if required (in many lab environments they may already be present). The Role should allow reading pods and the RoleBinding should bind the Role to dashboard-sa.

7) Create a token for dashboard-sa and use it in the UI

To validate the new ServiceAccount’s permissions, generate a token you can paste into the dashboard login:
Example output (token truncated):
Copy the token and paste it into the Kubernetes Dashboard token field, then click “Load Dashboard”. If the Role/RoleBinding are correct, the dashboard will be able to list pods and other permitted resources.
A screenshot of a "My Kubernetes Dashboard" web interface. It shows a token input and "Load Dashboard" button above a table listing one running pod (web-dashboard...) with its container count, service account, and IP.

8) Automate token mounting by updating the Deployment to use dashboard-sa

Manually pasting a token into the UI is useful for testing, but for production you should configure the Deployment so pods run as the dashboard-sa ServiceAccount. When serviceAccountName is set in the pod template, Kubernetes mounts the projected ServiceAccount token into the pod automatically. Export the current Deployment YAML:
Edit dashboard.yaml and under the pod template set serviceAccountName in the correct location:
Important: serviceAccountName must be nested under spec.template.spec, not at the top-level spec. Apply the updated manifest:
Note: If the original Deployment was created with kubectl create rather than kubectl apply, applying an edited manifest may show a warning — this is expected and safe for this lab.

9) Verify pods now use dashboard-sa and have credentials mounted

Check the updated Deployment and pods:
Confirm the following in the describe pod output:
  • Service Account: dashboard-sa
  • A volume mount at /var/run/secrets/kubernetes.io/serviceaccount
  • The dashboard no longer requires manually pasted tokens — the projected token is mounted automatically and the dashboard can query the API according to the RBAC permissions granted to dashboard-sa.
Using serviceAccountName in the pod spec mounts the ServiceAccount token automatically into the pod at /var/run/secrets/kubernetes.io/serviceaccount. Ensure the associated Role and RoleBinding grant only the permissions the application needs (principle of least privilege).

Summary
  • We identified that the dashboard was running with the default ServiceAccount and lacked permission to list pods.
  • We created dashboard-sa and ensured it had a Role and RoleBinding granting pod read permissions.
  • We validated access by creating a token and logging into the dashboard.
  • We updated the Deployment to use dashboard-sa, so pods automatically receive the projected token and operate with the intended RBAC permissions.

Watch Video