Kubeconfig Files and Contexts
What you'll learn: How kubectl knows which Kubernetes cluster to talk to and how you switch between different clusters or accounts.
Understanding the Kubeconfig Structure
When you run kubectl commands, the tool needs to know where to send those requests (which cluster), who you are (authentication), and what permissions you have. All this information lives in a kubeconfig file—typically located at ~/.kube/config.
Think of kubeconfig like a contact list in your phone. Each entry contains the server address, your credentials, and a nickname for quick access.
Three Key Components
The kubeconfig file contains three main sections:
- Clusters: Define the API server endpoints (where the cluster lives)
- Users: Store authentication credentials (certificates, tokens, or cloud provider auth)
- Contexts: Combine a cluster + user + optional namespace into a named profile
How Contexts Work
A context is simply a convenient label that bundles together which cluster you want to access, which user credentials to use, and optionally which namespace to default to.
When you run kubectl get pods, kubectl looks at your current context to determine where to send that request. You can switch contexts to immediately target a different cluster or use different credentials—no need to edit files manually.
Analogy: If clusters are different office buildings and users are your ID badges, contexts are saved shortcuts like "Building-A-with-Admin-Badge" or "Building-B-with-Developer-Badge."
Key Takeaway: Kubeconfig files organize cluster addresses, user credentials, and contexts—allowing kubectl to target the right cluster with the right identity simply by switching the active context.