Back to Blog
Lesson 8 of the Kubernetes: Kubernetes Concepts & Your First Pod course
KubernetesJuly 26, 20264 min read

Managing Kubeconfig and Contexts: Secure Cluster Switching

Master Kubeconfig and contexts to safely switch between dev and production Kubernetes clusters. Avoid dangerous mistakes with these essential CLI skills.

KubernetesKubeconfigDevOpsClustersCLI
Blue plastic wires with white tips connected to server and provide access to information

Previously in this course, we learned how to explore cluster status with kubectl and how to organize resources using namespaces. While namespaces keep your resources tidy within one cluster, you will eventually need to work with multiple clusters—like a local development instance and a remote production environment.

Today, we dive into the kubeconfig file, the command-line equivalent of a keychain that holds the keys to your various Kubernetes kingdoms.

What is a Kubeconfig?

At its core, a kubeconfig file is a YAML file that tells kubectl three critical pieces of information:

  1. Clusters: The API server URL and security certificates for your different environments.
  2. Users: The credentials (tokens, certificates, or usernames) used to authenticate with those clusters.
  3. Contexts: The "active" combination of a specific user and a specific cluster.

Think of it this way: a Cluster is the destination, a User is your identity, and a Context is the specific bridge you take to connect the two.

Viewing Your Current Context

A detailed view of hands holding eyeglasses, with a serene seascape in the background.

By default, kubectl looks for your configuration at ~/.kube/config. To see which cluster you are currently talking to, run:

Bash
kubectl config current-context

If you have multiple clusters, the output will return the alias of the one currently "active." If you want to see all available contexts, use:

Bash
kubectl config get-contexts

The output will look something like this:

CURRENTNAMECLUSTERAUTHINFONAMESPACE
*dev-clusterlocal-devadmin-userdefault
prod-clusterprod-cloudprod-user

The asterisk (*) indicates your active context.

Switching Between Clusters

Switching contexts is a daily task for a DevOps engineer. To move from your local environment to production, you use the use-context command:

Bash
kubectl config use-context prod-cluster

Now, every subsequent command you run (like kubectl get pods) will automatically target the production cluster.

Adding New Cluster Configs

When you join a new project or set up a new remote cluster, you will often be provided with a file containing the connection details. You can merge this into your existing config without overwriting your current data:

Bash
KUBECONFIG=~/.kube/config:~/Downloads/new-cluster.yaml kubectl config view --flatten > ~/.kube/config.new && mv ~/.kube/config.new ~/.kube/config

This command treats KUBECONFIG as a list of files, merges them, and saves the result. It’s the safest way to import new credentials.

Hands-on Exercise: Context Safety

To ensure you don't accidentally run a "delete" command on the wrong cluster, follow these steps:

  1. List your current contexts: kubectl config get-contexts.
  2. Rename your context to something descriptive if it's hard to read: kubectl config rename-context old-name new-name.
  3. Try switching to a context, then run kubectl get nodes. Verify you are indeed looking at the nodes of the expected cluster.

Common Pitfalls

  • The "Global" Mistake: Never rely on the default context without checking. Always alias your shell or use a tool like kubectx to visually confirm your current cluster in your terminal prompt.
  • Leaking Credentials: Your kubeconfig contains sensitive tokens. Never commit this file to Git. If you have been managing secrets in your CI/CD pipelines, ensure those are stored in a vault, not a local config file.
  • Overwriting Configs: The --flatten flag is your best friend when merging. Without it, you might find yourself with a corrupted or incomplete configuration file.

Frequently Asked Questions

Q: Can I set a default namespace for a context? A: Yes. Use kubectl config set-context --current --namespace=my-app. This saves you from typing -n my-app on every command.

Q: What if I lose my kubeconfig? A: Your cluster access will be revoked. You will need to re-authenticate via your cloud provider (e.g., aws eks update-kubeconfig or gcloud container clusters get-credentials) to regenerate the file.

Q: Is it safe to have multiple contexts in one file? A: It is standard practice. The file is local to your machine and is the primary way to manage multi-cluster workflows.

Recap

Managing kubeconfig effectively is the difference between a smooth deployment and a disaster. By understanding how clusters, users, and contexts interact, you gain full control over your Kubernetes infrastructure. Always verify your context before executing high-stakes commands, and keep your config file clean and updated.

Up next: We will begin our journey into application deployment by defining The Pod: The Smallest Unit of Execution.

Similar Posts