Managing a single Kubernetes cluster is simple with
kubectl
, but things get complicated when handling multiple clusters. It’s easy to mix up credentials, contexts, and configurations, leading to errors that can disrupt operations. That’s where the kubeconfig file comes in. As we’ve seen in this post, this file holds credentials, cluster details, and user settings, acting as our key to Kubernetes. It tells
kubectl
which cluster to talk to, what credentials to use, and how to authenticate requests. Without it, we can't access our clusters efficiently. Whether we're handling production workloads or testing configurations, managing kubeconfig files properly ensures smooth operations. As Kubernetes admins, we rely on it to connect securely to different clusters. Knowing how to manage this file is essential. In this post, we will explore the basic kubectl commands to manage kubeconfig files effectively.
This command displays the active kubeconfig settings, including clusters, users, and contexts.
Each context represents a cluster and a user. This command switches to the specified context.
This shows the active context, helping us confirm which cluster we’re working with.
A quick way to see all configured contexts in the kubeconfig file.
This adds a new cluster entry. The
This creates a user entry with authentication details.
Contexts link clusters to users, making it easier to switch between them.
This removes a context from the kubeconfig file.
This removes a cluster entry from the kubeconfig file, ensuring it's no longer accessible.
This deletes a user entry, removing their credentials from kubeconfig.
This combines multiple kubeconfig files into one, allowing us to manage multiple clusters seamlessly.
12. Set a Default Namespace for
This ensures that
1. View Current Configuration
kubectl config view
2. Set a Context (Switch Clusters)
kubectl config use-context <context-name>
3. Get the Current Context
kubectl config current-context
4. List All Available Contexts
kubectl config get-contexts
5. Add a New Cluster to Kubeconfig
kubectl config set-cluster <cluster-name> --server=<server-url> --certificate-authority=<path-to-ca>
--server
flag specifies the API server's address, while --certificate-authority
provides the CA certificate.6. Add a New User
kubectl config set-credentials <user-name> --client-certificate=<path-to-cert> --client-key=<path-to-key>
7. Create a Context (Cluster + User)
kubectl config set-context <context-name> --cluster=<cluster-name> --user=<user-name>
8. Delete an Unused Context
kubectl config delete-context <context-name>
9. Delete a Cluster from Kubeconfig
kubectl config delete-cluster <cluster-name>
10. Delete a User from Kubeconfig
kubectl config unset users.<user-name>
11. Merge Multiple Kubeconfig Files
export KUBECONFIG=file1.yaml:file2.yaml:file3.yaml
kubectl config view --merge
12. Set a Default Namespace for kubectl
kubectl config set-context --current --namespace=<namespace>
kubectl
commands run in the specified namespace by default.