Saturday, 2 March 2024

Merge/combine multiple kube config files into one

Merge/combine multiple kube config files into one

Sometimes we are working with multiple Kubernetes clusters for example LOCAL development, QA or TESTING environment and PRODUCTION environment. In this case we will have many kubeconfig files. By default, kubectl only looks for a file named config in the $HOME/.kube directory. So, how can we merge many kubeconfig files into one?

Here are the steps.

Steps:

  1. Go to .kube folder in you home directory
    • cd ~/.kube 
  2. Create backup of current config file
    • cp config config_backup
  3. Set the KUBECONFIG environment variable to the path of all the config files
    • export KUBECONFIG=~/.kube/config:~/path/to/first_config_file:~/path/to/second_config_file
  4. Merge all config files into one
    • kubectl config view --flatten > all_in_one_kubeconfig.yaml
  5. Replace the old config file with new merged file
    • mv all_in_one_kubeconfig.yaml config
  6. Verify if it works
    • kubectl config view
    • kubectl config get-contexts
    • kubectl config get-clusters
 

No comments:

Post a Comment