Mastering Kubectl: Essential Commands to Manage Kubeconfig Files


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.

1. View Current Configuration

kubectl config view

This command displays the active kubeconfig settings, including clusters, users, and contexts.


2. Set a Context (Switch Clusters)

kubectl config use-context <context-name>

Each context represents a cluster and a user. This command switches to the specified context.


3. Get the Current Context

kubectl config current-context

This shows the active context, helping us confirm which cluster we’re working with.


4. List All Available Contexts

kubectl config get-contexts

A quick way to see all configured contexts in the kubeconfig file.


5. Add a New Cluster to Kubeconfig

kubectl config set-cluster <cluster-name> --server=<server-url> --certificate-authority=<path-to-ca>

This adds a new cluster entry. The 
--server flag specifies the API server's address, while --certificate-authorityprovides the CA certificate.


6. Add a New User

kubectl config set-credentials <user-name> --client-certificate=<path-to-cert> --client-key=<path-to-key>

This creates a user entry with authentication details.


7. Create a Context (Cluster + User)

kubectl config set-context <context-name> --cluster=<cluster-name> --user=<user-name>

Contexts link clusters to users, making it easier to switch between them.


8. Delete an Unused Context

kubectl config delete-context <context-name>

This removes a context from the kubeconfig file.


9. Delete a Cluster from Kubeconfig

kubectl config delete-cluster <cluster-name>

This removes a cluster entry from the kubeconfig file, ensuring it's no longer accessible.


10. Delete a User from Kubeconfig

kubectl config unset users.<user-name>

This deletes a user entry, removing their credentials from kubeconfig.


11. Merge Multiple Kubeconfig Files

export KUBECONFIG=file1.yaml:file2.yaml:file3.yaml
kubectl config view --merge

This combines multiple kubeconfig files into one, allowing us to manage multiple clusters seamlessly.


12. Set a Default Namespace for kubectl

kubectl config set-context --current --namespace=<namespace>

This ensures that 
kubectl commands run in the specified namespace by default.

Previous Post Next Post