Sub topic 7
How kubectl finds your cluster: kubeconfig and contexts
This chapter assumes you have created a cluster with kind or minikube, as in the earlier chapters, and can run basic terminal commands. You should also be able to read YAML.
The kubeconfig file
kubectl does not know where your cluster is until it reads a kubeconfig file. By default this file is ~/.kube/config. Both kind and minikube add entries to it when they create a cluster.
A kubeconfig holds three lists:
- clusters: the API server address and its certificate authority.
- users: the credentials used to authenticate, such as a client certificate.
- contexts: named bundles that pair one cluster with one user, and optionally a default namespace.
The file also has a current-context field. kubectl sends every command to the cluster that this context points to.
Context names
Each tool names the context differently. This is a common source of confusion.
| Tool | Command | Context name |
|---|---|---|
| kind | `kind create cluster --name dev` | `kind-dev` |
| minikube | `minikube start -p dev` | `dev` |
kind adds the prefix kind- to the cluster name. minikube uses the profile name unchanged.
Viewing and switching contexts
kubectl config get-contexts
kubectl config current-context
kubectl config use-context kind-dev
kubectl get pods --context kind-devget-contexts lists every context and marks the active one with *. use-context changes current-context in the file, so it affects every later command, including commands in other terminals. The --context flag applies to one command only and leaves the file unchanged.
Creating a cluster also switches to its context. After you create a second cluster, your next kubectl command goes to the new one.
Setting a default namespace
You can store a namespace in the current context so you do not have to type -n each time:
kubectl config set-context --current --namespace=defaultCommon errors
- Commands hit the wrong cluster. Run
kubectl config current-contextbefore anyapplyordelete. Use--contextfor one-off commands on a different cluster. The connection to the server localhost:8080 was refused.kubectlfound no kubeconfig, or the file has no current context. Check that~/.kube/configexists and that theKUBECONFIGenvironment variable is not pointing elsewhere.- Connection refused on a cluster that exists. The cluster may be stopped, or Docker may not be running. Local clusters often get a new API server port after a restart.
- A deleted cluster leaves a stale context. Delete the leftover entry with
kubectl config delete-context <name>.
Using more than one file
KUBECONFIG can hold a colon-separated list of files. kubectl merges them in memory:
export KUBECONFIG=~/.kube/config:~/.kube/other-config
kubectl config get-contextsTo write a single merged file, run kubectl config view --flatten.
Exercise
You run minikube start -p dev and then kind create cluster --name dev. Which context is active afterward, and how do you run kubectl get nodes against the minikube cluster without switching?
Show answer
The active context is kind-dev, because the most recently created cluster becomes current. To query minikube without switching, run kubectl get nodes --context dev.