How to switch kubectl clusters between gcloud and minikube

后端 未结 14 1122
再見小時候
再見小時候 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:41

    yes, i think this is what your asking about. To view your current config, use kubectl config view. kubectl loads and merges config from the following locations (in order)

    --kubeconfig=/path/to/.kube/config command line flag
    KUBECONFIG=/path/to/.kube/config env variable
    $HOME/.kube/config  - The DEFAULT
    

    i use --kubeconfig since i switch alot between multiple clusters. its slightly cumbersome but it works well.

    see these for more info. https://kubernetes.io/docs/tasks/administer-cluster/share-configuration/ and https://kubernetes.io/docs/concepts/cluster-administration/authenticate-across-clusters-kubeconfig/

    0 讨论(0)
  • 2021-01-29 19:42

    In case you might be looking for a simple way to switch between different contexts maybe this will be of help.

    I got inspired by kubectx and kswitch scripts already mentioned, which I can recommend for most use-cases. They are helping with solving the switching task, but are breaking for me on some bigger or less standard configurations of ~/.kube/config. So I created a sys-exec invocation wrapper and a short-hand around kubectl.

    If you call k without params you would see an intercepted prompt to switch context.

    Switch kubectl to a different context/cluster/namespace.
    Found following options to select from:
     >>> context: [1] franz
     >>> context: [2] gke_foo_us-central1-a_live-v1
     >>> context: [3] minikube
     --> new num [?/q]:
    

    Further, k continues to act as a short-hand. The following is equivalent:

    kubectl get pods --all-namespaces
    k get pods -A
    k p -A
    
    0 讨论(0)
  • 2021-01-29 19:46

    List contexts

    kubectl config get-contexts
    

    Switch contexts

    kubectl config set current-contex MY-CONTEXT
    
    0 讨论(0)
  • 2021-01-29 19:47

    Check also the latest (docker 19.03) docker context command.

    Ajeet Singh Raina ) illustrates it in "Docker 19.03.0 Pre-Release: Fast Context Switching, Rootless Docker, Sysctl support for Swarm Services"

    A context is essentially the configuration that you use to access a particular cluster.

    Say, for example, in my particular case, I have 4 different clusters – mix of Swarm and Kubernetes running locally and remotely.
    Assume that I have a default cluster running on my Desktop machine , 2 node Swarm Cluster running on Google Cloud Platform, 5-Node Cluster running on Play with Docker playground and a single-node Kubernetes cluster running on Minikube and that I need to access pretty regularly.

    Using docker context CLI I can easily switch from one cluster(which could be my development cluster) to test to production cluster in seconds.

    $ sudo docker context --help
    Usage:  docker context COMMAND
    Manage contexts
    Commands:
      create      Create a context
      export      Export a context to a tar or kubeconfig file
      import      Import a context from a tar file
      inspect     Display detailed information on one or more contexts
      ls          List contexts
      rm          Remove one or more contexts
      update      Update a context
      use         Set the current docker context
    Run 'docker context COMMAND --help' for more information on a command.
    

    For example:

    [:)Captain'sBay=>sudo docker context ls
    NAME                DESCRIPTION                               DOCKER ENDPOINT               KUBERNETES ENDPOINT                 ORCHESTRATOR
    default *           Current DOCKER_HOST based configuration   unix:///var/run/docker.sock   https://127.0.0.1:16443 (default)   swarm
    swarm-context1 
    
    0 讨论(0)
  • 2021-01-29 19:48

    I use kubeswitch (disclaimer: I wrote the tool) that can be used just like kubectx, but is designed for a large number of kubeconfig files. If you have to deal with hundreds or thousands of kubeconfig files, this tool might be useful to you, otherwise kubectx or kubectl config use-context might be sufficient.

    For instance, it adds capabilities like reading from vault, hot reload while searching, and an index to speed up subsequent searches.

    You can install it from here.

    0 讨论(0)
  • 2021-01-29 19:48

    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.

    0 讨论(0)
提交回复
热议问题