|
| 1 | +--- |
| 2 | +title: Kubeconfig merging on Windows with Powershell |
| 3 | +date: 2024-04-15 |
| 4 | +categories: [How-to, Kubernetes, Powershell] |
| 5 | +tags: [kubernetes,k8s,k3s,howto,Powershell] # TAG names should always be lowercase |
| 6 | +--- |
| 7 | + |
| 8 | +# Merging kubeconfig Files in Windows Using PowerShell |
| 9 | + |
| 10 | +Managing Kubernetes configurations can be a crucial aspect of working efficiently with Kubernetes clusters. In this guide, we will walk through the process of merging a new kubeconfig file into the main kubeconfig file on Windows using PowerShell. |
| 11 | + |
| 12 | +Feel free to customize the instructions below to suit your specific environment and preferences. Following these steps will enable you to efficiently manage kubeconfig files on Windows using PowerShell. |
| 13 | + |
| 14 | +Happy Kubernetizing! 🚀🔧 |
| 15 | + |
| 16 | +## Prerequisites |
| 17 | + |
| 18 | +Before proceeding, ensure you have the following: |
| 19 | + |
| 20 | +- PowerShell installed on your Windows machine. |
| 21 | +- kubectl command-line tool installed and configured. |
| 22 | +- Access to the kubeconfig files you wish to merge. |
| 23 | + |
| 24 | +## Procedure |
| 25 | + |
| 26 | +Replace `<username>` and `<new-config>` with your username and new config file you want to merge. |
| 27 | +```powershell |
| 28 | +# make a backup |
| 29 | +cd ~/.kube/ |
| 30 | +cp config config.bak |
| 31 | +
|
| 32 | +# merge both kube config files |
| 33 | +$ENV:KUBECONFIG = "C:\Users\<username>\.kube\config;C:\Users\<username>\.kube\<new-config>" |
| 34 | +
|
| 35 | +# verify that the variable is set |
| 36 | +$ENV:KUBECONFIG |
| 37 | +
|
| 38 | +# output to temp file |
| 39 | +kubectl config view --flatten > config-merged |
| 40 | +
|
| 41 | +# verify that config-merged is correct |
| 42 | +kubectl --kubeconfig=config-merged config get-clusters |
| 43 | +
|
| 44 | +# delete backup |
| 45 | +rm config |
| 46 | +
|
| 47 | +# move merged file to config |
| 48 | +mv config-merged config |
| 49 | +
|
| 50 | +# remove (optional) |
| 51 | +rm config.bak |
| 52 | +``` |
| 53 | + |
| 54 | +## Managing Contexts |
| 55 | + |
| 56 | +### List All Clusters |
| 57 | + |
| 58 | +To list all clusters configured in your kubeconfig file, use the following command: |
| 59 | + |
| 60 | +```powershell |
| 61 | +kubectl config get-clusters |
| 62 | +``` |
| 63 | + |
| 64 | + |
| 65 | +### Deleting a Cluster by Name |
| 66 | + |
| 67 | +To delete a cluster from your kubeconfig file by its name, execute the following command: |
| 68 | + |
| 69 | +Replace `<cluster_name>` with the name of the cluster you wish to delete. |
| 70 | +```powershell |
| 71 | +kubectl config delete-cluster <cluster_name> |
| 72 | +``` |
0 commit comments