Managing multiples Kubernetes clusters with kubectl

kubernetes Jan 18, 2023

Learn how to use kubectl CLI to manage multiple Kubernetes clusters. Import new config, add cluster, user, and context. Switch between contexts to manage different clusters.

Kubernetes is an open-source platform for managing containerized workloads and services, allowing you to easily deploy and run applications in a cloud-native environment. The kubectl command-line interface (CLI) is a key component of Kubernetes, providing a powerful and flexible way to manage clusters and deploy applications.

One useful feature of kubectl is the ability to manage multiple clusters using a single CLI. In this post, I'll show you how to import a new Kubernetes config file and use the kubectl CLI to manage multiple clusters.


Make a backup

Assume that something can go wrong and you will lose all your configuration data.

So, be cautious and backup your default configuration file:

cp ~/.kube/config /path/to/BACKUP_FILE

(option 1) Import an existent config file

To import an existing Kubernetes config file using kubectl, you can use the kubectl config command.

Here's an example of how you can do it:

First, copy the config file to your local machine, and make sure it is in the right location (e.g., ~/.kube/config).

Run the following command to view the current contexts in your config:

kubectl config get-contexts

This will list the contexts that are currently defined in your config.

  1. Run the following command to import the new config file:
IMPORTANT: You must have a different context name or your configuration will be replaced!
KUBECONFIG=~/.kube/config:/path/to/NEW_CONFIG_FILE kubectl config view --flatten > ~/.kube/config

Replace NEW_CONFIG_FILE with the path to your new config file.

This will merge the new config file with your existing config, adding any new contexts, users, or clusters that are defined in the file.

You can then use the kubectl config use-context command to switch between the different contexts and manage the different clusters.

For example:

kubectl config use-context NEW_CONTEXT_NAME

This will set the NEW_CONTEXT_NAME context as the current context, and you can use kubectl to manage the cluster associated with that context.

(option 2) Import using a certificate file

IMPORTANT: I didn't reviewed this second option. Let me know in the comments if I made some mistake. Sorry in advance.

To import a new Kubernetes config file using kubectl, you can use the kubectl config command.

Here's an example of how you can do it:

First, copy the config file to your local machine, and make sure it is in the right location (e.g., ~/.kube/config).

Run the following command to add the new config to your Kubernetes config:

kubectl config set-cluster NEW_CLUSTER_NAME --server=NEW_CLUSTER_API_SERVER_URL --certificate-authority=CA_CERT_FILE

Replace NEW_CLUSTER_NAME, NEW_CLUSTER_API_SERVER_URL, and CA_CERT_FILE with the appropriate values for your new cluster.

  1. Run the following command to add a new user to the config:
kubectl config set-credentials NEW_USER_NAME --client-certificate=CLIENT_CERT_FILE --client-key=CLIENT_KEY_FILE

Replace NEW_USER_NAME, CLIENT_CERT_FILE, and CLIENT_KEY_FILE with the appropriate values for your new user.

  1. Finally, run the following command to add a new context to the config:
kubectl config set-context NEW_CONTEXT_NAME --cluster=NEW_CLUSTER_NAME --user=NEW_USER_NAME

Replace NEW_CONTEXT_NAME and NEW_CLUSTER_NAME with the appropriate values for your new context and cluster.

You can then use the kubectl config use-context command to switch between the different contexts and manage the different clusters.

For example:

kubectl config use-context NEW_CONTEXT_NAME

This will set the NEW_CONTEXT_NAME context as the current context, and you can use kubectl to manage the cluster associated with that context.

References

Tags

Luiz Costa

I am a software engineer at Red Hat / Ansible. I love automation tools, games, and coffee. I am also an active contributor to open-source projects on GitHub.