Finding the kubeconfig file being used
Asked Answered
B

1

7

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

Basil answered 29/6, 2021 at 4:30 Comment(1)
[kubectl: Get location of KubeConfig file in use [closed]](#64346651)Pyrimidine
H
18

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

Hesperidin answered 29/6, 2021 at 4:50 Comment(3)
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 =/Wilscam
I just checked it is working, adding the screenshot for referenceHesperidin
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.Talishatalisman

© 2022 - 2024 — McMap. All rights reserved.