Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to switch kubectl clusters between gcloud and minikube

People also ask

How do I switch between clusters in kubectl?

After your clusters, users, and contexts are defined in one or more configuration files, you can quickly switch between clusters by using the kubectl config use-context command. Note: A file that is used to configure access to a cluster is sometimes called a kubeconfig file.

How do I switch to Minikube?

To start the minikube, type “minikube start” into the terminal. A virtual machine capable of running a single node cluster will be constructed, and the Kubernetes cluster will be launched. It also works with the kubectl setup.

How do I change the current context in kubectl?

What this says is that you can create or modify contexts in your kubeconfig file with the command kubectl config set-context. This command also accepts the name of the context to be changed (or --current if you want to change the current context), as well as --user, --cluster, and --namespace options.


You can switch from local (minikube) to gcloud and back with:

kubectl config use-context CONTEXT_NAME

to list all contexts:

kubectl config get-contexts

You can create different enviroments for local and gcloud and put it in separate yaml files.


A faster shortcut to the standard kubectl commands is to use kubectx:

  • List contexts: kubectx
    • Equivalent to kubectl config get-contexts
  • Switch context (to foo): kubectx foo
    • Equivalent to kubectl config use-context foo

To install on macOS: brew install kubectx

The kubectx package also includes a similar tool for switching namespaces called kubens.

These two are super convenient if you work in multiple contexts and namespaces regularly.

More info: https://ahmet.im/blog/kubectx/


List contexts

kubectl config get-contexts

Switch contexts

kubectl config set current-context MY-CONTEXT

If you're looking for a GUI-based solution for Mac and have the Docker Desktop installed, you can use the Docker Menu Bar icon. Here you can find "Kubernetes" menu with all the contexts you have in your kubeconfig and easily switch between them.


Latest 2020 answer is here,

A simple way to switch between kubectl context,

kubectl top nodes **--context=**context01name

kubectl top nodes --context=context02name

You can also store the context name as env like context01name=gke_${GOOGLE_CLOUD_PROJECT}_us-central1-a_standard-cluster-1