Managing multiple Kubernetes clusters efficiently can often seem daunting, especially when dealing with separate configuration files for each cluster. In this post, I’ll walk you through a straightforward process to merge new Kubernetes cluster configurations with your existing setup, ensuring smooth transitions between different contexts.
Thank me by sharing on Twitter 🙏
Introduction
As a developer, I frequently interact with multiple Kubernetes clusters for various projects. Initially, managing separate configurations for each cluster was tedious and error-prone. However, I discovered a seamless method to merge and manage these configurations, significantly simplifying my workflow. In this guide, I’ll share my process of merging Kubernetes configurations and selecting new contexts.
Prerequisites
Before diving into the merging process, ensure you have the following:
- Kubernetes CLI (
kubectl
) installed. - Existing Kubernetes configuration file (typically located at
~/.kube/config
). - New Kubernetes configuration file for the cluster you wish to add.
Merging Kubernetes Configurations
Step 1: Setting the KUBECONFIG Environment Variable
To begin, we need to set the KUBECONFIG
environment variable to include paths to both the existing and new configuration files. This environment variable allows kubectl
to reference multiple configuration files.
For Unix-like systems (Linux, macOS):
USB Type-C to A Cable 5pack 6ft Braided Fast Charging 3A Quick Charger Cord, 6 Foot Compatible iPhone 16/15,Samsung Galaxy S10 S9 S8 Plus, Note 10 9 8, LG V50 V40 G8 G7(Grey)
$9.99 (as of January 22, 2025 11:32 GMT +00:00 - More infoProduct prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on [relevant Amazon Site(s), as applicable] at the time of purchase will apply to the purchase of this product.)Logitech MK270 Wireless Keyboard And Mouse Combo For Windows, 2.4 GHz Wireless, Compact Mouse, 8 Multimedia And Shortcut Keys, For PC, Laptop - Black
$22.99 (as of January 22, 2025 11:32 GMT +00:00 - More infoProduct prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on [relevant Amazon Site(s), as applicable] at the time of purchase will apply to the purchase of this product.)TAKAGI for iPhone Charger, [MFi Certified] Lightning Cable 3PACK 6FT Nylon Braided USB Charging Cable High Speed Transfer Cord Compatible with iPhone 14/13/12/11 Pro Max/XS MAX/XR/XS/X/8/iPad
$9.99 (as of January 22, 2025 11:32 GMT +00:00 - More infoProduct prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on [relevant Amazon Site(s), as applicable] at the time of purchase will apply to the purchase of this product.)export KUBECONFIG=~/.kube/config:/path/to/new/kubeconfig
For Windows:
set KUBECONFIG=%USERPROFILE%\.kube\config;C:\path\to\new\kubeconfig
Step 2: Merging the Configurations
Next, we use kubectl config view --merge --flatten
to merge the configurations. This command reads the configuration files specified in the KUBECONFIG
environment variable, merges them, and outputs a single configuration file.
kubectl config view --merge --flatten > ~/.kube/config-merged
Step 3: Replacing the Current Config with the Merged One
After merging, we need to replace our current configuration file with the newly merged configuration. It’s a good practice to back up the original file before making any replacements.
mv ~/.kube/config ~/.kube/config.bak
mv ~/.kube/config-merged ~/.kube/config
Step 4: Verifying the Merge
Finally, we verify the merge by listing all available contexts. This helps confirm that contexts from both the original and new configurations are present.
kubectl config get-contexts
You should see a list of contexts that includes entries from both your existing and newly added configurations.
Selecting a New Context
Once the configurations are merged, switching between different contexts becomes necessary. Here’s how to do it:
Step 1: Listing Available Contexts
First, list all available contexts to identify the name of the new context you wish to switch to.
kubectl config get-contexts
The output will show a list of contexts along with their details.
Step 2: Selecting the Desired Context
After identifying the desired context, use the kubectl config use-context
command followed by the context name to switch to it.
kubectl config use-context <context-name>
Replace <context-name>
with the name of the context you want to select.
Step 3: Verifying the Selected Context
To ensure the new context is selected, list the contexts again. The current context will be indicated with an asterisk (*
).
kubectl config get-contexts
This output confirms that the desired context is now active.
Conclusion
Managing multiple Kubernetes clusters doesn’t have to be a cumbersome task. By merging your configurations and efficiently switching contexts, you can streamline your development workflow and focus more on building great applications.
In this guide, I covered:
- Setting the
KUBECONFIG
environment variable to reference multiple configuration files. - Merging configurations using
kubectl config view --merge --flatten
. - Replacing the current configuration file with the merged one.
- Verifying the merge and switching contexts.
Following these steps, you can effortlessly manage multiple Kubernetes clusters and maintain a clean, organized configuration setup. With this approach, my workflow has become much more efficient, allowing me to handle multiple projects with ease.