- Default kubeconfig location and format
- Viewing and counting clusters/users/contexts
- Using an alternate kubeconfig without replacing the default
- Making another kubeconfig the default
- Troubleshooting certificate path mismatches and fixing credentials
Where is the default kubeconfig located?
By default, kubectl reads the kubeconfig from$HOME/.kube/config. Confirm your home and the file:
- Number of clusters defined (here: 1)
- Number of users defined (here: 1)
- Number of contexts defined (here: 1)
- Current context name (here:
kubernetes-admin@kubernetes) - Cluster name (here:
kubernetes) - User referenced by the current context (here:
kubernetes-admin)
user field in a context references a user entry in the users: section of the file. Context names are metadata (often user@cluster by convention).
Inspecting another kubeconfig: /root/my-kube-config
There is a separate kubeconfig at/root/my-kube-config that contains multiple clusters, contexts, and users. Relevant excerpts:
- Number of clusters: 4
- Number of contexts: 4
- Initial
current-context:test-user@development - The
researchcontext mapstest-cluster-1to userdev-user. - The
dev-usercredential block referencesdeveloper-user.crtanddev-user.key— this will be important for troubleshooting since the actual files on disk may differ. - The
aws-userentry referencesaws-user.crtfor the client certificate.
Use the research context without replacing the default kubeconfig
You can test the research context from this alternate kubeconfig without touching your default config by using --kubeconfig or setting KUBECONFIG. Example:
Make /root/my-kube-config the default kubeconfig
If you’d like/root/my-kube-config to become the default for this user so you no longer need --kubeconfig, back up the existing default and move the file into place:
KUBECONFIG=/root/my-kube-config in your environment instead of moving the file.
Always back up your original kubeconfig before replacing it (for example
cp -a ~/.kube/config ~/.kube/config.backup), so you can restore it if needed.Troubleshooting: access fails when using dev-user
Withcurrent-context: research (which uses dev-user) you may see authentication errors when running kubectl:
users: block in the kubeconfig and the certificate/key files on disk.
Truncated users section from the kubeconfig:
developer-user.crt while the directory contains dev-user.crt. That path mismatch prevents kubectl from locating the client certificate and authenticating.
Fix the client-certificate path for dev-user
Usekubectl config set-credentials to update the user entry in the kubeconfig to point to the correct certificate and key.
If /root/my-kube-config has already been moved to ~/.kube/config (the default), run:
/root/my-kube-config without replacing your default, pass --kubeconfig (place it immediately after kubectl):
Verify access
Retry the previously failing command:Useful commands quick reference
Links and references
- Kubernetes kubeconfig documentation: https://kubernetes.io/docs/concepts/configuration/organize-cluster-access-kubeconfig/
- kubectl config commands: https://kubernetes.io/docs/reference/generated/kubectl/kubectl-commands#config
Summary
- Default kubeconfig:
~/.kube/config. - Use
--kubeconfigorKUBECONFIGto point kubectl at a non-default kubeconfig file. - To make another file the default, back up and replace
~/.kube/config. - If authentication fails, check the
users:entries and ensureclient-certificateandclient-keypaths in the kubeconfig match the files on disk. - Use
kubectl config set-credentialsto correct credentials entries in the kubeconfig.