How to add an additional kubernetes config file to your current one (located in ~./kube/config)
-
Backup the existing kubeconfig
Before making any changes, it's essential to back up your existing kubeconfig file.
cp ~/.kube/config ~/.kube/config.backup
-
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"
-
Update the kubeconfig file
Replace the default values in the new kubeconfig file with the specified names using the following
sedcommand: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
-
Set the KUBECONFIG environment variable
Include both the existing and new kubeconfig files in the
KUBECONFIGenvironment variable:export KUBECONFIG=~/.kube/config:$NEW_CLUSTER_CONFIG_FILE
-
Merge the configurations
Merge the configurations into a temporary file:
kubectl config view --merge --flatten > /tmp/merged-kubeconfig -
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.
-
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 -
Unset the KUBECONFIG environment variable
Unset the
KUBECONFIGenvironment variable to revert to the default configuration:unset KUBECONFIG -
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 -
Switch to another context (optional)
You can switch to another context using the following command:
kubectl config use-context <CONTEXT_NAME>