RBAC, service accounts & kubeconfig
Grant a user or service account exactly the permissions it needs and verify with auth can-i.
By Invicly Tech, , in Kubernetes Administrator (CKA). About 8 minutes to read.
In short
RBAC grants permissions by binding roles to subjects. A Role or ClusterRole lists allowed verbs on resources, and a RoleBinding or ClusterRoleBinding attaches it to users, groups or service accounts. Permissions are purely additive, and kubectl auth can-i verifies the result.
Explained simply
RBAC (role-based access control) decides who may do what in a cluster. You never give permissions to a person directly. Instead you write a role, which is a list of allowed actions, and then bind that role to a user, a group or a service account.
It works like key cards in an office building. A role is a key card type, such as "can open meeting rooms on floor 3". A binding hands that card type to specific people. If nobody gave you a card for a door, the door stays shut.
Service accounts are identities for programs running in Pods, like a CI tool or a monitoring agent. They get permissions the same way as people. Granting the smallest set that still works, called least privilege, limits the damage if a token ever leaks.
Press Play on a diagram to walk through it one step at a time.
- Role
- Namespaced rules
- verbs on resources
- ClusterRole
- Cluster-wide or reusable
- Nodes, PVs, all namespaces
- RoleBinding
- Grants in one namespace
- Can reference a ClusterRole
- ClusterRoleBinding
- Grants across the cluster
- Subjects
- User
- Group
- ServiceAccount
- 1Request arrives
user jane: list pods in dev
- 2Find bindings
RoleBindings in dev, all ClusterRoleBindings
- 3Collect rules
From every bound Role or ClusterRole
- 4Match
apiGroup, resource, verb, namespace
- 5Decide
Any match allows; none means 403
- New user to Admin: Send CSR file (key stays private)1Send CSR file (key stays private)
- Admin to API server: Create CertificateSigningRequest2Create CertificateSigningRequest
- Admin to API server: kubectl certificate approve3kubectl certificate approve
- API server to Admin: Signed certificate in status4Signed certificate in status
- Admin to New user: Certificate + kubeconfig5Certificate + kubeconfig
In depth
What a rule really matches
Each rule in a Role lists apiGroups, resources and verbs. The core group is written as an empty string, so Pods use apiGroups: [""], while Deployments use ["apps"]. kubectl api-resources shows the group and the short name of every resource.
Verbs map to HTTP actions: get, list and watch for reading, create, update, patch and delete for changes. Subresources are separate: reading logs needs pods/log, and kubectl exec needs create on pods/exec. A rule can also be narrowed to specific object names with resourceNames.
- Read-only set: get, list, watch
- Logs: resource pods/log, verb get
- Exec: resource pods/exec, verb create
- kubectl api-resources -o wide shows supported verbs
- apiGroups
- "" for core (pods, services)
- "apps" for deployments
- resources
- pods
- pods/log
- pods/exec
- verbs
- get, list, watch
- create, update, patch, delete
- resourceNames
- Optional: only named objects
Built-in ClusterRoles you can reuse
Kubernetes ships with useful ClusterRoles: view (read most things except Secrets), edit (change most things, but not roles), admin (full control within a namespace, including RoleBindings) and cluster-admin (everything everywhere).
Binding a built-in ClusterRole with a RoleBinding is a quick, safe way to give a team admin rights in just their namespace. Use cluster-admin rarely; it bypasses every limit.
- 1cluster-admin
Everything, everywhere
- 2admin
Full control in a namespace, incl. RoleBindings
- 3edit
Change most things, but not roles
- 4view
Read most things, not Secrets
Users come from certificates, not from Kubernetes
Kubernetes has no User object. A user is just a name the API server trusts from a credential, most simply a client certificate signed by the cluster CA. The certificate's Common Name (CN) becomes the username and Organization (O) fields become groups.
To create one, generate a key and a certificate request with openssl, submit it as a CertificateSigningRequest object with signerName kubernetes.io/kube-apiserver-client, approve it with kubectl certificate approve, and read back the signed certificate. Then put the key and certificate into a kubeconfig.
Service account tokens today
Since Kubernetes 1.24, service accounts no longer get a long-lived token Secret created automatically. Pods receive a short-lived token through a projected volume, rotated by the kubelet. For a quick token outside a Pod, kubectl create token SA_NAME prints one that expires after an hour by default.
You can stop a Pod from receiving a token at all with automountServiceAccountToken: false, which is good practice for Pods that never call the API.
Learn it in 5 short steps
About 50m each. Track them with a timer in your plan.
- 1Create Roles and RoleBindings~50m
- 2Create ClusterRoles and ClusterRoleBindings~50m
- 3Bind a role to a service account~50m
- 4Verify access with kubectl auth can-i~50m
- 5Create a kubeconfig for a new user~50m
Key points
- RBAC has no deny rules; anything not allowed is forbidden.
- A RoleBinding to a ClusterRole limits that ClusterRole to one namespace.
- Cluster-scoped resources like nodes need a ClusterRole and ClusterRoleBinding.
- Test as another identity with kubectl auth can-i --as.
- Service account subjects use the form system:serviceaccount:NAMESPACE:NAME.
Key terms
Tap a card to flip it. 0/5 known
Examples
kubectl create namespace dev
kubectl create serviceaccount app -n dev
kubectl create role pod-reader -n dev \
--verb=get,list,watch --resource=pods
kubectl create rolebinding app-reads-pods -n dev \
--role=pod-reader --serviceaccount=dev:app
kubectl auth can-i list pods -n dev \
--as=system:serviceaccount:dev:app # yes
kubectl auth can-i delete pods -n dev \
--as=system:serviceaccount:dev:app # noA CI service account that may only deploy
Task: in namespace ci, service account deployer may create, update and get Deployments, nothing else. Step 1: create the service account. Step 2: create a Role with those verbs on deployments in the apps group. Step 3: bind it. Step 4: check allowed and denied actions with can-i.
k -n ci create sa deployer
k -n ci create role deploy-only --verb=get,create,update,patch \
--resource=deployments.apps
k -n ci create rolebinding deployer-rb --role=deploy-only \
--serviceaccount=ci:deployer
k -n ci auth can-i create deployments --as=system:serviceaccount:ci:deployer # yes
k -n ci auth can-i delete deployments --as=system:serviceaccount:ci:deployer # no
k -n ci auth can-i get secrets --as=system:serviceaccount:ci:deployer # noGiving user jane a working kubeconfig
Step 1: create a key and CSR with CN=jane and O=dev. Step 2: submit and approve it. Step 3: extract the certificate. Step 4: add credentials and a context to a kubeconfig. Step 5: test with the new context.
openssl genrsa -out jane.key 2048
openssl req -new -key jane.key -subj "/CN=jane/O=dev" -out jane.csr
cat <<EOF | k apply -f -
apiVersion: certificates.k8s.io/v1
kind: CertificateSigningRequest
metadata: {name: jane}
spec: {request: $(base64 -w0 jane.csr), signerName: kubernetes.io/kube-apiserver-client, usages: [client auth]}
EOF
k certificate approve jane
k get csr jane -o jsonpath='{.status.certificate}' | base64 -d > jane.crt
k config set-credentials jane --client-key=jane.key --client-certificate=jane.crt --embed-certs
k config set-context jane --cluster=kubernetes --user=jane --namespace=devQuick reference
- Core group is ""; Deployments are apps; Ingress is networking.k8s.io
- k create role NAME --verb=get,list,watch --resource=pods -n NS
- k create rolebinding NAME --clusterrole=view --user=jane -n NS
- k auth can-i --list --as=jane -n NS shows everything jane can do
- SA subject: system:serviceaccount:NAMESPACE:NAME
- k create token SA prints a short-lived token for a service account
- Logs need pods/log get; exec needs pods/exec create
Common mistakes
- Using a ClusterRoleBinding when access should be limited to one namespace.
- Writing --serviceaccount=app without the namespace prefix.
Quick quiz
Pick an answer to see if you're right and why.
Practice exercises
Try each one yourself first. Use the hint if you get stuck, then compare with the solution.
- Exercise 1easy
Give user bob read-only access to Pods and their logs in namespace web.
- Exercise 2medium
Service account monitor in namespace ops must list nodes and Pods in every namespace. Grant it and verify.
- Exercise 3hard
Create user alice in group qa with a working kubeconfig file /opt/alice.kubeconfig that can only view resources in namespace qa.
Check yourself
Answer in your head, reveal, then be honest with yourself.
Feel confident about this topic?
Frequently asked questions
- Can I deny a single action with RBAC?
- No, RBAC only allows. To block something, remove the rule or binding that grants it, or use an admission policy for deny-style checks.
- How do I see who has access to Secrets?
- kubectl auth can-i tests one identity at a time. To audit everyone, inspect RoleBindings and ClusterRoleBindings or use a plugin such as rbac-tool or kubectl who-can.
- Why does a Pod get 403 when calling the API?
- It runs as its namespace's default service account unless you set serviceAccountName, and the default account has almost no permissions. Bind a role to the account the Pod actually uses.