How to switch kubectl clusters between gcloud and minikube

后端 未结 14 1307
再見小時候
再見小時候 2021-01-29 19:01

I have Kubernetes working well in two different environments, namely in my local environment (MacBook running minikube) and as well as on Google\'s Container Engine (GCE, Kubern

14条回答
  •  情深已故
    2021-01-29 19:56

    The canonical answer of switching/reading/manipulating different kubernetes environments (aka kubernetes contexts) is, as Mark mentioned, to use kubectl config, see below:

    $ kubectl config                                                                                                                                                                                                                 
    Modify kubeconfig files using subcommands like "kubectl config set current-context my-context"
    
    Available Commands:
      current-context Displays the current-context
      delete-cluster  Delete the specified cluster from the kubeconfig
      delete-context  Delete the specified context from the kubeconfig
      get-clusters    Display clusters defined in the kubeconfig
      get-contexts    Describe one or many contexts
      rename-context  Renames a context from the kubeconfig file.
      set             Sets an individual value in a kubeconfig file
      set-cluster     Sets a cluster entry in kubeconfig
      set-context     Sets a context entry in kubeconfig
      set-credentials Sets a user entry in kubeconfig
      unset           Unsets an individual value in a kubeconfig file
      use-context     Sets the current-context in a kubeconfig file
      view            Display merged kubeconfig settings or a specified kubeconfig file
    
    Usage:
      kubectl config SUBCOMMAND [options]
    

    Behind the scene, there is a ~/.kube/config YAML file that stores all the available contexts with their corresponding credentials and endpoints for each contexts.

    Kubectl off the shelf doesn't make it easy to manage different kubernetes contexts as you probably already know. Rather than rolling your own script to manage all that, a better approach is to use a mature tool called kubectx, created by a Googler named "Ahmet Alp Balkan" who's on Kubernetes / Google Cloud Platform developer experiences Team that builds tooling like this. I highly recommend it.

    https://github.com/ahmetb/kubectx

    $ kctx --help                                                                                                                                                                                                                  
    USAGE:
      kubectx                       : list the contexts
      kubectx                 : switch to context 
      kubectx -                     : switch to the previous context
      kubectx =     : rename context  to 
      kubectx =.          : rename current-context to 
      kubectx -d  [] : delete context  ('.' for current-context)
                                      (this command won't delete the user/cluster entry
                                      that is used by the context)
    
      kubectx -h,--help         : show this message
    

提交回复
热议问题