kubeconfig Anatomy
kubeconfig Anatomy
A kubeconfig answers two questions: which cluster, and which identity.
clusters:
- name: prod
cluster:
server: https://api.prod:6443
certificate-authority-data: <base64 CA>
users:
- name: alice
user: { client-certificate-data: <base64 cert>, client-key-data: <base64 key> }
contexts:
- name: prod-alice
context: { cluster: prod, user: alice, namespace: payments }
current-context: prod-alice
kubectl reads $KUBECONFIG (colon-separated paths merged left to right) and then ~/.kube/config. Only current-context is active. A context binds one cluster to one user plus a default namespace; user entries may hold client certs, bearer tokens, or an exec plugin such as the AWS or gcloud credential helper.
Multi-Cluster Workflows
Multi-Cluster Workflows
kubectl config get-contexts
kubectl config use-context prod-alice
kubectl --context=staging get nodes
KUBECONFIG=~/.kube/prod:~/.kube/staging kubectl config get-contexts
kubectl config set-context --current --namespace=payments
kubectl config delete-cluster old-ephemeral
For automation, prefer short-lived credentials: cloud helpers refresh tokens on demand through exec plugins instead of embedding long-lived secrets. Keep the file private — chmod 600 ~/.kube/config — because it often carries cluster-admin certificates.
A naming scheme like env-role-team on contexts prevents applying staging manifests to production during a stressful incident.
Pitfalls and Debugging
Pitfalls and Debugging
Pitfall 1 — certificate SAN mismatches. "x509: certificate signed by unknown authority" or "certificate is valid for ... not this host" usually means the wrong CA or an apiserver certificate missing the endpoint's SAN — common when the API server is first reached through a new load balancer DNS name. Compare kubectl config view --minify -o jsonpath='{.clusters[0].cluster.server}' against the certificate SANs.
Pitfall 2 — stale exec-plugin tokens. Expired cached tokens surface as 401 or 403; re-run the cloud login before blaming RBAC.
Interview-ready: explain the cluster/user/context split, then your debug order: current-context, token or certificate expiry, kubectl auth can-i list pods --namespace=prod, and finally apiserver audit logs for the rejected request.
Quiz
1. Which part of kubeconfig selects the API server endpoint?
2. kubectl returns 403 on a valid, unexpired token. What is the likely cause?
3. How do you run one command against a different cluster without switching context?
Flashcards
Question
kubeconfig sections
Click to reveal answer
Answer
clusters (endpoints + CA), users (credentials), contexts (cluster + user + namespace), current-context.
Question
KUBECONFIG merge order
Click to reveal answer
Answer
Colon-separated paths merge left to right; later files fill gaps; then ~/.kube/config.
Question
x509 unknown authority fix
Click to reveal answer
Answer
Verify the CA data and the apiserver cert SANs match the server URL you dial.
Question
Guarding kubeconfig
Click to reveal answer
Answer
chmod 600, short-lived exec-plugin credentials, and context names that encode the environment.
Revision Notes
Key Takeaways
- 1. Context = cluster + user + namespace; check it before every destructive action
- 2. Auth failures split into 401 (identity) and 403 (permissions)
- 3. Exec plugins keep credentials short-lived and out of the file
Interview Tips
- • Describe your multi-cluster setup with concrete context names
- • Walk the auth debug ladder: context → expiry → can-i → audit logs
Cheat Sheet
clusters=server+CA, users=credentials, context=cluster+user+ns. Merge via KUBECONFIG. Debug: current-context → token/cert expiry → auth can-i → audit log.