Exam speed tip:kubectl config current-context confirms where you are before running destructive commands. Many CKA tasks begin with a context switch, and answering in the wrong cluster is an instant zero. Source: CKA Day 22
Multiple Kubeconfig Files
You can merge or select kubeconfig files via the KUBECONFIG environment variable:
This is useful for separating personal, staging, and production credentials. The files are merged in memory; no file is modified unless you explicitly write back with --merge and redirection.
Context in CI/CD and Automation
In automated pipelines, ServiceAccount tokens are preferred over long-lived client certificates. The typical pattern:
Create a dedicated ServiceAccount (e.g., deployer)
Grant it a minimal Role or ClusterRole (e.g., edit in a single Namespace)
Extract the token from the ServiceAccount’s Secret or use kubectl create token for short-lived tokens
Inject the token into the CI/CD runner as a kubeconfig user.token field
For direct REST API calls from outside the cluster, you can also pass the ServiceAccount bearer token with curl instead of client certificates:
This pattern is increasingly preferred over long-lived client certificates for automation because tokens can be scoped and rotated easily. Source: CKA Day 25
This avoids storing human admin certificates in build systems and limits blast radius via least-privilege RBAC. Source: CKA Day 22
Adding a New User: Practical Workflow
When creating a new user via client certificates (common in CKA security tasks), three commands wire the identity into kubeconfig:
# 1. Add the user's credentials (embeds cert/key as base64 data)kubectl config set-credentials krishna \ --client-certificate=./krishna.crt \ --client-key=./krishna.key# 2. Create a context binding user + clusterkubectl config set-context krishna \ --cluster=kind-cka-cluster-2 \ --user=krishna# 3. Switch to the new contextkubectl config use-context krishna# Verifykubectl config current-contextkubectl config view --raw
Certificate expiry trap: If the client certificate expires (e.g., set with 1-day validity during CSR creation), kubectl returns Unauthorized or certificate errors. Regenerate via the CSR workflow: create CSR → kubectl certificate approve → extract signed cert → re-run set-credentials with the new certificate. Source: CKA Day 23
Common Pitfalls
Pitfall
Symptom
Fix
Wrong context
Commands affect unexpected cluster
kubectl config current-context and kubectl config use-context
Expired certificate
x509: certificate has expired
Renew certs with kubeadm certs renew all or regenerate client cert
Ensure certificate-authority-data matches the cluster CA
Token in shell history
Security leak
Use exec plugins or credential helpers instead of inline tokens
Practical Practice
Exam-style hands-on tasks for this topic. Complete each task before reviewing the solution. Time yourself — CKA tasks average 5–7 minutes.
Task 1: Add a New Cluster, User, and Context
You are asked to add cluster prod-cluster (server https://prod.example.com:6443, CA at /etc/kubernetes/ca.crt) and user admin (client cert /etc/kubernetes/admin.crt, key /etc/kubernetes/admin.key) to the kubeconfig, then create and switch to context prod-admin.
Requirements: Use imperative kubectl config commands only.
Verification:kubectl config current-context returns prod-admin.
Solution:
Task 2: Switch Context and Verify
You are asked to switch to the existing context named staging and confirm the active context.
Requirements: Do not modify the kubeconfig file directly.
Verification:kubectl config current-context outputs staging.
Solution:
Task 3: Fix an Unable-to-Connect Error
A kubectl command fails with Unable to connect to the server: dial tcp ...: connect: connection refused. You inspect the current context and discover the server URL points to an old IP.
Requirements: Update the server URL in the current context to https://192.168.1.10:6443.
Verification:kubectl cluster-info succeeds.
Solution:
# View current cluster serverkubectl config view --minify | grep server# Update the cluster server for the current context's clusterkubectl config set-cluster $(kubectl config view --minify -o jsonpath='{.contexts[0].context.cluster}') --server=https://192.168.1.10:6443kubectl cluster-info