Q: How do you check if a service account has permission to create pods?
kubectl auth can-i create pods --as=system:serviceaccount:<namespace>:<sa-name>
Want to master this scenario in a live sandbox? KodeKloud's CKA & CKAD Hands-On Certification Track covers this exact problem with hands-on terminal drills.
🛠️ Production Runbook & Step-by-Step Resolution
Direct Answer: <strong>To check if a service account has permission to create pods, run <code>kubectl auth can-i create pods --as=system:serviceaccount:<namespace>:<name> -n <namespace></code>.</strong> The command asks the API server's authorizer and returns <code>yes</code> or <code>no</code> without creating anything. Interviewers ask this to see whether you can debug <code>403 Forbidden</code> errors systematically: verify the identity, test the verb and resource, trace the RoleBinding, and know that Pod creation can also be blocked by admission (Pod Security, quotas), which is a different layer from RBAC.
Mental model: identity, verb, resource, scope
RBAC is additive allow-only. A request is permitted if any binding grants the verb on the resource in that namespace.
- Identity: a service account is the user
system:serviceaccount:<ns>:<name>, plus the groupssystem:serviceaccountsandsystem:serviceaccounts:<ns>. - Verb and resource: creating a pod is verb
createon resourcepodsin the core API group (empty string). Controllers like Deployments create pods through the controller-manager's identity, not your service account. - Scope: a Role plus RoleBinding applies in one namespace, whereas a ClusterRole bound with a ClusterRoleBinding applies everywhere. A ClusterRole bound via RoleBinding is limited to that namespace.
- Default: a new service account has no permissions beyond those given to the system:authenticated and discovery groups.
- Aggregated roles: built-in ClusterRoles such as
editandadmininclude create on pods, and they can be extended through aggregation labels, so a service account may gain permission indirectly through a role you never edited.
Hands-on: check if a service account has permission to create pods
Use impersonation with kubectl auth can-i. Your own user needs the impersonate permission for serviceaccounts, which cluster admins have.
# Can the SA create pods in 'prod'?
kubectl auth can-i create pods \
--as=system:serviceaccount:prod:deployer -n prod
# yes / no
# Everything the SA may do in that namespace
kubectl auth can-i --list \
--as=system:serviceaccount:prod:deployer -n prod
# Check the related subresource used for exec
kubectl auth can-i create pods/exec \
--as=system:serviceaccount:prod:deployer -n prod
# Check yourself or from inside a pod using the mounted token
kubectl auth can-i create pods -n prod
- Basic test: run the command with
--asand the namespace. - Explain the result: add
-v=6for the API calls, or ask for the full permission list with--listto see everything allowed in that namespace. - Cluster-wide: use
-Aor--all-namespaces, or omit-nfor a cluster-scoped resource check. - Exit status: the command exits 0 for yes and 1 for no, so it fits shell checks and CI.
- Impersonation rights: if
--asreturns forbidden, your own user lacks theimpersonateverb on serviceaccounts. Test from inside a pod using that service account's token instead, which is the most realistic check.
Trace why the answer is yes or no
If the result surprises you, locate the binding and the role rules that grant or deny it.
# Dry-run a real create as the service account (includes admission)
kubectl run probe --image=registry.k8s.io/pause:3.9 \
--as=system:serviceaccount:prod:deployer -n prod --dry-run=server
# Grant it if missing
kubectl -n prod create role pod-creator --verb=create --resource=pods
kubectl -n prod create rolebinding deployer-pod-creator \
--role=pod-creator --serviceaccount=prod:deployer
- Find bindings: list RoleBindings and ClusterRoleBindings and filter for the service account subject, for example with
kubectl get rolebinding,clusterrolebinding -A -o wideand a grep on the name. - Read the role:
kubectl describe roleorclusterroleand look forpodswithcreate. RememberapiGroups: [""]for pods. - Wrong subject namespace: a binding subject must name the service account's namespace. A common mistake is omitting it, so the binding silently matches nothing.
- Real-request test:
kubectl auth can-ievaluates RBAC only. For an end-to-end check, run a server-side dry-run create as the service account. - Reverse lookup: use a plugin such as kubectl-who-can with
kubectl who-can create pods -n prodto list every subject allowed, which is useful during access reviews and incident response.
Failure modes: allowed by RBAC, still rejected
A yes from RBAC does not guarantee pod creation. Other layers evaluate the request after authorization.
- Pod Security Admission: a namespace labelled
pod-security.kubernetes.io/enforce: restrictedrejects privileged or root pods regardless of RBAC. - Quota and limits: ResourceQuota or LimitRange violations return
403style errors with the quota name in the message. - Webhooks and policy engines: Kyverno, OPA Gatekeeper or custom webhooks can deny pods that RBAC allowed.
- Other subresources: creating pods does not cover
pods/exec,pods/portforwardorserviceaccounts/token, which need their own rules. - Authorization modes: RBAC may not be the only authorizer. Webhook authorizers or cloud IAM integrations can add or deny access, so remember that
can-ireports the combined decision of the configured authorizers.
Production practice: least privilege and auditing
Treat pod-creation rights as high privilege, because anyone who can create pods can often mount secrets or escalate.
- Scope narrowly: prefer namespaced Roles, and avoid wildcard verbs. Use
resourceNameswhere possible. - Automate checks: run
kubectl auth can-iassertions in CI after applying RBAC manifests, and fail if privileged accounts gain create on pods unexpectedly. - Audit tools:
kubectl-who-canorrakkessplugins answer the reverse question of who can create pods. - Short-lived tokens: rely on projected, time-bound service account tokens, and disable automounting with
automountServiceAccountToken: falsefor pods that never call the API.
- Use kubectl auth can-i create pods with --as=system:serviceaccount:namespace:name to ask the API server's authorizer directly.
- If the answer is unexpected I trace RoleBindings and ClusterRoleBindings for that subject, then confirm end to end with a server-side dry-run create.
- I automate can-i assertions in CI and keep pod-create rights narrowly scoped, since create pods is effectively a privileged permission.
Senior hiring managers and bar raisers evaluate how you prioritize during outages. Avoid these common anti-patterns:
- Testing with your own admin credentials You get yes because you are cluster-admin, which says nothing about the service account.
- Forgetting the namespace in the binding subject The RoleBinding then references a service account that does not match, and access is silently denied.
- Treating can-i yes as proof pods will be created Pod Security Admission, quotas and webhooks can still reject the request.
- Granting create on pods broadly to fix a 403 Pod creation can be used to mount secrets and escalate, so over-granting is a security risk.