Black Friday · 27 Nov 2026

Up to 90% off selected certifications.

One time only offer for a very precious career goal. Limited coupons, waitlist first.

Free lesson · CKA Prep Program · Module 2: Cluster architecture, installation and configuration

Certificates, kubeconfig Contexts and RBAC from Scratch

Access control is three layers that compose: who you are (certificate), where you point (kubeconfig context), and what you may do (RBAC). This lesson builds all three by hand so denials become readable.

15 min reading

Objectives

  • Mint a client certificate and wrap it in a kubeconfig context
  • Switch contexts without editing files by hand
  • Write a Role plus RoleBinding that grants exactly one verb on one resource
  • Read any RBAC denial and trace it to the missing rule

Why this matters

Half of all "cannot do X" errors in the exam are RBAC, and the other half are pointing at the wrong cluster. Both are five-minute fixes once you see the layers separately. The exam loves this topic because it rewards precision: exactly the needed verbs, exactly the needed resources, nothing borrowed from cluster-admin.

Concepts

Identity starts with a key and a certificate signing request. Generate a private key, build a CSR with the username in CN and the groups in O, sign it against the cluster CA (or have the cluster sign it), and you have an identity the API server trusts. The kubeconfig file then bundles three things per context: the cluster (server plus CA), the user (certificate plus key), and the namespace. kubectl config use-context switches the live triple; kubectl config get-contexts shows the table. Never hand-edit these files when a command exists.

RBAC grants verbs on resources inside rules, binds rules to subjects, and stops at namespace boundaries unless you use the cluster-scoped kinds. A Role holds rules for one namespace; a RoleBinding attaches them to users, groups or service accounts in that namespace. ClusterRole plus ClusterRoleBinding is the cluster-wide pair. The API server denies by default, so every denial means no bound rule matched: read the resource, the verb and the namespace from the error, then write exactly that rule.

Worked example

A read-only auditor for one namespace, no cluster-admin anywhere near it:

# identity
openssl genrsa -out auditor.key 2048
openssl req -new -key auditor.key -out auditor.csr -subj "/CN=auditor"
# sign with the cluster CA, then build the context
kubectl config set-credentials auditor \
  --client-certificate=auditor.crt --client-key=auditor.key
kubectl config set-context auditor-ctx \
  --cluster=kubernetes --user=auditor --namespace=demo
# least privilege: get and list pods in demo only
kubectl create role pod-reader \
  --verb=get,list --resource=pods -n demo
kubectl create rolebinding auditor-read \
  --role=pod-reader --user=auditor -n demo
kubectl auth can-i list pods -n demo --as=auditor
# yes
kubectl auth can-i delete pods -n demo --as=auditor
# no

kubectl auth can-i --as is the exam superpower: it tests any subject's rights without switching identity.

Exam notes

Forgetting the namespace binds the rule in default while the workload lives elsewhere: namespace mismatch is the top RBAC trap. --as checks rights but does not authenticate as that user for real calls, which is exactly what you want for testing. And service accounts need the same treatment: bind roles to them explicitly instead of widening defaults.

Quick check

A 4-question self-check. Your answers stay on your device, are never stored or sent. Only a passing score (for example 3 of 4) is saved, and only to unlock lesson progress: at least 3 correct is required before a lesson counts as complete. It never counts toward any certification assessment.

Lesson feedback

No published feedback yet.

Log in and complete the lesson to leave feedback.

Exercise

Create a kubeconfig context for a fictional teammate with access to one namespace only, plus the Role and RoleBinding that allow get and list on configmaps there and nothing else. Show the two auth can-i checks that prove the boundary, one yes and one no.

Pass criteria

Context carries cluster, user and namespace; Role holds exactly get plus list on configmaps; binding targets the right namespace; yes and no checks both shown with correct expectations.

Sources

Log in to track progressFree account: stores only your lesson progress and quiz results.