Skip to content

Instantly share code, notes, and snippets.

@adrian-pino
Last active November 26, 2024 11:42
Show Gist options
  • Select an option

  • Save adrian-pino/d5a7c687d18337dcd601d621aaa25697 to your computer and use it in GitHub Desktop.

Select an option

Save adrian-pino/d5a7c687d18337dcd601d621aaa25697 to your computer and use it in GitHub Desktop.
Add additional kubeconfigs

How to add an additional kubernetes config file to your current one

How to add an additional kubernetes config file to your current one (located in ~./kube/config)

  1. Backup the existing kubeconfig

    Before making any changes, it's essential to back up your existing kubeconfig file.

    cp ~/.kube/config ~/.kube/config.backup
  2. Set the new names

    Define the new user name, cluster name, and the path to the new kubeconfig file. Update these variables as needed.

    USER_NAME="user-name"
    CLUSTER_NAME="cluster-name"
    NEW_CLUSTER_CONFIG_FILE="path-to-new-config-file-and-file-name"
  3. Update the kubeconfig file

    Replace the default values in the new kubeconfig file with the specified names using the following sed command:

    sed -i -e "s/kubernetes-admin@kubernetes/${USER_NAME}@${CLUSTER_NAME}/g" \
           -e "s/kubernetes-admin/${USER_NAME}/g" \
           -e "s/kubernetes/${CLUSTER_NAME}/g" $NEW_CLUSTER_CONFIG_FILE
  4. Set the KUBECONFIG environment variable

    Include both the existing and new kubeconfig files in the KUBECONFIG environment variable:

    export KUBECONFIG=~/.kube/config:$NEW_CLUSTER_CONFIG_FILE
  5. Merge the configurations

    Merge the configurations into a temporary file:

    kubectl config view --merge --flatten > /tmp/merged-kubeconfig
  6. Check the temporary merged config file

    Verify the contexts in the temporary merged kubeconfig file:

    kubectl config get-contexts --kubeconfig /tmp/merged-kubeconfig

    Ensure the output is correct before proceeding.

  7. Move the merged config to overwrite the existing kubeconfig

    If the temporary merged config file is correct, move it to overwrite the existing kubeconfig file:

    mv /tmp/merged-kubeconfig ~/.kube/config
  8. Unset the KUBECONFIG environment variable

    Unset the KUBECONFIG environment variable to revert to the default configuration:

    unset KUBECONFIG
  9. Verify the new kubeconfig

    Check the contexts in the merged kubeconfig file to ensure it has been updated correctly:

    kubectl config get-contexts
    CURRENT  NAME                                   CLUSTER                               AUTHINFO                               NAMESPACE
             admin-6gxr@6g-xr                       6g-xr                                 admin-6gxr                                                       
             admin-gn5-1@geant-dev                  geant-dev                             admin-gn5-1                                                   
             admin-cilium@kind-cluster-cilium       kind-cluster-cilium                   admin-cilium                                         
    *        admin-kind@kind-kind                   kind-kind                             admin-kind                                                   
  10. Switch to another context (optional)

    You can switch to another context using the following command:

    kubectl config use-context <CONTEXT_NAME>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment