Welcome to new things

[Technical] [Electronic work] [Gadget] [Game] memo writing

How to switch between multiple clusters in kubectl

We have added a new Kubernetes cluster and configured it to be switched with the kubectl command.

Once you set it up, you don't have to tweak it, and if you don't, you'll forget how to do it, so I'll write down how to set it up for myself.

structure

To manipulate Kubernetes, you use the kubectl command, but kubectl is independent of the platform on which you built Kubernetes, Whether you build Kubernetes on-prem, GCP, or AWS, a single kubectl command can access all Kubernetes.

To access a cluster from kubectl, information on the "cluster" to be accessed and its "user" is required.

In kubectl, that "cluster" and "user" (and "namespace") are grouped together in a single "context".

Then, when executing commands in kubectl, you can access the desired Kubernetes by obtaining information about the access point from the "context".

In addition, multiple "contexts" can be created, and switching between "contexts" allows one kubectl to access various Kubernetes clusters.

Note that "namespace" is not required, and if omitted, the "default" namespace is accessed. If you set the "namespace", you can create "contexts" with different "namespaces" even in the same cluster, You can create "contexts" with different "namespaces" in the same cluster.

Cluster and user information settings

I need to set the cluster user information into the kubectl context if I want to access new Kukbernetes, The method of configuration depends on the platform on which the cluster was built.

For example, for Google Cloud Platform, use the following command to configure.

gcloud container clusters get-credentials \
    [クラスター名] \
    --project [GCPプロジェクト名] \
    --zone [GCPゾーン名]

Execute the command kubectl config get-contexts to get a list of contexts.

CURRENT NAME    CLUSTER    AHTHINFO    NAMESPACE
*[context_name]    [cluster_name]    [user_name]

and a new "context" is created, with "cluster" and "user" automatically set for it as well.

The cluster connection test is performed at

kubectl cluster-info

Now that the necessary settings for accessing the cluster have been made in the context, we can edit the context to make it easier to use.

The following is a list of commonly used context-related commands.

Show context list

kubectl config get-contexts

Show current context

kubectl config current-context

Activate the specified context

kubectl config set-context [コンテキスト名]

Configure namespace settings

Activate the context you want to set and then do the following

kubectl config set-context --current --namespace=[ネームスペース名]

Edit the context settings file directly

The context settings are described in the file ~/.kube/config.

apiVersion: v1
clusters:
- cluster:
    certificate-authority-data: 
  name: [クラスター1]
- cluster:
    certificate-authority-data:
  name: [クラスター2]
contexts:
- context:
    cluster: [クラスター1]
    user: [ユーザー1]
  name:  [コンテキスト1]
- context:
    cluster: [クラスター2]
    namespace: [ネームスペース1]
    user: [ユーザー2]
  name:  [コンテキスト2]
current-context: [コンテキスト1]
kind: Config
preferences: {}
users:
- name: [ユーザー1]
  user:
    auth-provider:
- name: [ユーザー2]
  user:
    auth-provider:

As you can sort of see, there are definitions of "cluster" and "user," and the "context" refers to that "cluster" and "user" (and "namespace").

I would like to set up various "contexts" so that they are easy to use, but kubectl's context-related commands are not very feature-rich, and there are many things you cannot do if you try to do it only with kubectl commands.

Fortunately, the content of the context settings file is simple, so we edited the context settings file directly.

Points to note

When setting up a context from the platform, existing "contexts," "clusters," and "users" with the same name will be overwritten.

So, when you edit your own configuration files, you should copy them and register them under a new name to prevent them from being overwritten.

Impressions, etc.

I looked for various commands to use if there were any.

When I added a context in GCP, I wanted to shorten the names of "context", "cluster", and "user" because they are long, but I could not do it with just a command, so I edited the configuration file directly.

I wanted to create a different namespace for the same context, but I could do it by copy and paste by editing the file directly.

Well, it is not complicated and editing the file directly is recommended.

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com