How to switch kubectl clusters between gcloud and minikube
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
Kubernetes is a popular container orchestration system, with kubectl serving as its command-line interface for interacting with clusters. Developers and DevOps engineers often work with multiple Kubernetes clusters, such as Google Cloud's GKE (Google Kubernetes Engine) and local environments like Minikube. Efficiently switching between these clusters using kubectl is crucial for streamlined development and deployment processes. This article covers the technical steps and considerations for switching kubectl contexts between GKE and Minikube.
Prerequisites
Before diving into the specifics of switching clusters, ensure you have the following prerequisites:
- kubectl: The Kubernetes command-line tool, installed and configured on your system.
- Google Cloud SDK (gcloud): This allows you to interact with Google Cloud services including GKE.
- Minikube: A tool to run Kubernetes locally.
If any of these tools are not installed, you can find installation guides on their respective documentation pages.
Understanding kubectl Contexts and Configurations
Kubernetes’ kubectl manages its connection to clusters through a configuration file, typically located at ~/.kube/config. This file includes information about clusters, users, and contexts, enabling kubectl to function across multiple environments seamlessly.
- Cluster: Represents the Kubernetes cluster itself.
- User: Authentication and authorization information.
- Context: A combination of cluster, user, and namespace that allows you to switch between environments.
Here's a snippet of a kubectl configuration file:
Switching with kubectl between GKE and Minikube
Setting Up GKE with kubectl
To interact with a GKE cluster, perform the following steps:
- Authenticate with GCloud: Ensure you are logged into your Google Cloud account.
- Set the Project and Compute Zone: Specify the Google Cloud project and zone for your GKE cluster.
- Get GKE Credentials: Retrieve the cluster credentials to set up the
kubectlcontext.
Setting Up Minikube with kubectl
Minikube acts as another context within the kubectl configuration:
- Start Minikube: Launch Minikube with the desired settings.
Minikube will automatically update kubectl to point to Minikube’s context.
Switching Contexts with kubectl
List all available contexts using:
Switch to a specific context using:
The command will move your active kubectl context to the desired cluster, allowing you to perform operations on the correct environment.
Key Points and Considerations
Switching between Kubernetes clusters is simplified using kubectl contexts. Here is a summary table of commands related to managing these contexts:
| Operation | Command |
| List current contexts | kubectl config get-contexts |
| Use specific context | kubectl config use-context [CONTEXT_NAME] |
| Set project and zone for GKE | gcloud config set project [PROJECT_ID]
gcloud config set compute/zone [COMPUTE_ZONE] |
| Get credentials for GKE cluster | gcloud container clusters get-credentials [CLUSTER_NAME] |
| Start Minikube and update context | minikube start |
Additional Tips
- Aliases and Convenience Scripts:
- Consider creating shell aliases or scripts to quickly switch between frequently used contexts.
- Verification:
- After switching contexts, verify the active context to avoid unintended actions:
- Namespace Scope:
- Be mindful of the default namespace in use, as this can vary between clusters and affect Kubernetes operations.
- Security:
- Regularly update your
kubectl,gcloud, and Minikube versions to benefit from security patches and improvements.
By understanding and effectively managing kubectl contexts, you can optimize your workflow in multi-cluster Kubernetes environments. Whether working locally with Minikube or on a GKE production environment, these techniques streamline operations and enhance cluster management efficiency.
Related reading
- How to switch namespace in kubernetes
- How to troubleshoot metrics-server on kubeadm?
- how to undo a kubectl port-forward
- how to uninstall minikube from ubuntu, i get an ''Unable to load cached images'' error
- How to update a set of pods running in kubernetes?
- How to update Kubernetes Dashboard in hosted Kubernetes on GKE?
- How to update nginx-ingress controller so that latest ingress paths are used?
- How to upgrade kubectl client version

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.