Search code examples
kubectl

How does kubectl know which KUBECONFIG config file to use?


In the past, I used $HOME/.kube/config and downloaded the kubeconfig file from Rancher. There was only one kubeconfig file (see docs) so I never wondered about which file was being used.

Yesterday, I switched to using the KUBECONFIG environment variable because there are several k8s clusters I'm now using (prod, stg, dev), and that's what the devops team told me to do, so I did it (and it worked). For the sake of discussion, the command was:

export KUBECONFIG=$HOME/.kube/config-prod:$HOME/.kube/config-stage:$HOME/.kube/config-dev

So now I can use following commands to use the staging K8S cluster.

kubectl config current-context
# verify I'm in the using the right k8s config, and switch if necessary
kubectl config use-context dev    <<< WHERE DOES kubectl store this setting???
kubectl config set-context --current --namespace MyDeploymentNamespace
kubectl get pods

My question is How does does kubectl know what kube config file is being used? Where does it store the currently selected context?

or instead

Does kubectl logically concatenate all of the kubeconfig files defined in the KUBECONFIG environment variable to get access to any of the defined Kubernetes clusters?

Searching for answer

I found several answers to similar questions but not quite this question.

Which explains how to the file

kubectl config current-context -v6
I0203 14:59:05.926287   30654 loader.go:375] Config loaded from file:  /home/user1/.kube/config
dev

I'm asking where is this information stored?

The only guess I have is in the $HOME/.kube/cache directory in which I'm finding scores of files.

Some Kubernetes documentation


Solution

  • See:

    which says:

    Otherwise, if the KUBECONFIG environment variable is set, use it as a list of files that should be merged. Merge the files listed in the KUBECONFIG environment variable according to these rules: (see link for details)

    So to answer your question,

    Does kubectl logically concatenate all of the kubeconfig files defined in the KUBECONFIG environment variable . . .

    The answer is; the files are merged.

    kubeconfig files contain sections for clusters,contexts and users and there's a current-context property too which stores the value of kubectl config current-context and is revised by kubectl config use-context.

    Per the rules in Merging kubeconfig files, the first occurrence of e.g. current-context is the value that wins.