Kubernetes: kubeconfig and Contexts
kubeconfig is the file where kubectl keeps its cluster connections. A context is one named connection inside that file.
List your contexts
Each context is one cluster you can talk to. The star marks the active one.
kubectl config get-contextsSee the active context
One command shows the context in use right now.
kubectl config current-contextSwitch to another cluster
Switching changes the target of every later command. Nothing on either cluster changes.
kubectl config use-context kind-practiceSwitch back
Switch back the same way when you want minikube again.
kubectl config use-context minikubeSet the namespace for a context
A context also remembers a namespace. Commands then list that namespace unless you name another one.
kubectl config set-context --current --namespace=shopExample
kubectl config get-contexts
kubectl config use-context kind-practice
kubectl config current-context
kubectl config use-context minikubeKey points
- kubeconfig: the file kubectl reads to find clusters, users and contexts.
- Context: a named group of one cluster, one user and one namespace.
- Use kubectl config get-contexts to list every context you have.
- Use kubectl config use-context to switch to a different cluster.
- Use kubectl config current-context to see which cluster you are talking to.
- The default kubeconfig file lives at ~/.kube/config on your machine.
- A namespace is a separate area inside one cluster for one team or one app.
In this example, get-contexts lists minikube and kind-practice. use-context switches to kind-practice. current-context confirms the switch. The last command switches back to minikube.
Note
Switching context changes the cluster every later command talks to. Check it before you run a command that changes something.
Try it Yourself
Edit the code, then press Run.
Running is simulated · Rust Playground
Exercise
What does kubectl config use-context kind-practice do?