6

I understand kubectl gets the kubeconfig file in the order

  1. command line option --kubeconfig
  2. environment variable KUBECONFIG=
  3. default path ~/.kube/config

But is there a way to get the kubeconfig path/file details from the kubectl which one being currently used?

Something like kubectl config path

Logu
  • 904
  • 9
  • 15
  • 1
    [kubectl: Get location of KubeConfig file in use [closed]](https://stackoverflow.com/questions/64346151/kubectl-get-location-of-kubeconfig-file-in-use) – Lei Yang Jun 29 '21 at 04:34

1 Answers1

15

Question: But is there a way to get the kubeconfig path/file details from the kubectl which one being currently used?

Yes, you can run any kubectl command with verbose level 6+ to see the kubeconfig in use.

kubectl get pod   -v6                                             
I0629 04:48:25.050954   14444 loader.go:379] Config loaded from file:  /home/ps/.kube/config
I0629 04:48:25.146072   14444 round_trippers.go:445] GET https://kubemaster:6443/api/v1/namespaces/default/pods?limit=500 200 OK in 10 milliseconds
No resources found in default namespace.

Few examples demonstrating the same:

kubectl get pod   -v6 2>&1 |awk  '/Config loaded from file:/{print $NF}'
/home/ps/.kube/config

Changed thekubeconfig to /tmp/config

export KUBECONFIG=/tmp/config    
kubectl get pod   -v6 2>&1 |awk  '/Config loaded from file:/{print $NF}'
/tmp/config

Remove the awk command to see the whole output.

Windows output:

enter image description here

P....
  • 17,421
  • 2
  • 32
  • 52
  • It seems this is either already broken, or never worked properly on Windows. I'm using v1.22.0 of `kubectl`, and no verbosity level of any command prints that first line =/ – Fulluphigh Aug 27 '21 at 21:07
  • I just checked it is working, adding the screenshot for reference – P.... Aug 27 '21 at 21:15
  • 1
    worked for me as well. There was a .kube folder in my windows user home directory, but the kubectl config was loading from the home directory of the SSH client i was using. This command helped me find out the additional .kube folder in the SSH client's home directory. – Ravi Kumar CH Feb 13 '22 at 05:59