my-kube-playground:6443, you can use curl with the client certificate/key pair (and the CA certificate) to query the API:
~/.kube/config. If you create the file there, you do not need to pass --kubeconfig on the command line.
By default, kubectl uses the file at
~/.kube/config. You can override this with --kubeconfig=<path> or by setting the KUBECONFIG environment variable.What is in a kubeconfig?
A kubeconfig is a YAML file with three primary sections:clusters: Definitions of Kubernetes API servers (endpoints + CA information).users: Credentials (client identities) used to authenticate to clusters.contexts: Mappings that tie auserto aclusterand optionally anamespace.
How the earlier curl/kubectl example maps to kubeconfig:
- The API server address and CA certificate belong in a
clustersentry. - The admin user’s key and certificate belong in a
usersentry. - A
contextmaps themy-kube-adminuser to themy-kube-playgroundcluster so kubectl can pick them together.
Example kubeconfig (simplified)
A real kubeconfig might look like this:Which context does kubectl use?
Set a default context withcurrent-context at the top level of the kubeconfig:
current-context is set to dev-user@google, kubectl will use the dev-user credentials against the google cluster by default — unless you override this on the command line.
Viewing and switching contexts
Use kubectl subcommands to view and modify kubeconfig files.- Print the kubeconfig kubectl is currently using:
- Ask kubectl to read a different file:
- Change the current context:
kubectl config view will show current-context: prod-user@production. You can also add, delete, and update kubeconfig entries with other kubectl config subcommands — see kubectl config --help for details.
Namespaces in contexts
Each context can include anamespace field so kubectl commands default to that namespace when the context is active:
admin@production context active, kubectl commands run without -n will operate in the finance namespace by default.
File paths vs. inline data for certificates
Kubeconfig supports two ways to provide certificate credentials:-
File paths
- Fields:
certificate-authority,client-certificate,client-key - Example:
certificate-authority: /etc/kubernetes/pki/ca.crt
- Fields:
-
Inline base64-encoded contents
- Fields:
certificate-authority-data,client-certificate-data,client-key-data - The value must be the base64 encoding of the file contents (no newlines).
- Fields:
*-data fields is convenient for a single portable kubeconfig that contains all credentials, but embedding secrets in a file increases risk if the file is exposed.
Embedding client keys or certificates directly in a kubeconfig (
*-data fields) makes the file self-contained but also increases the risk of credential leakage. Protect files containing credentials using OS-level file permissions and secure storage.Summary
- Kubeconfig centralizes cluster endpoints and user credentials for kubectl.
- The file contains
clusters,users, andcontextssections;current-contextselects the default. - Contexts map a
userto aclusterand can set a defaultnamespace. - Certificates can be referenced by file path or embedded as base64 data via
*-datafields. - kubectl provides
kubectl configsubcommands to view and modify kubeconfig files.
Practice
Create multiple contexts for different clusters/users, switch between them withkubectl config use-context, and experiment with both file-based and inline credentials to learn the trade-offs.
Example of decoding a certificate (corrected):