<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Benly Walter</title>
    <description>The latest articles on DEV Community by Benly Walter (@benlywalter).</description>
    <link>https://dev.to/benlywalter</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1047540%2F0b907acf-c6b4-4007-89dd-182fa42bfe6e.jpg</url>
      <title>DEV Community: Benly Walter</title>
      <link>https://dev.to/benlywalter</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/benlywalter"/>
    <language>en</language>
    <item>
      <title>Automate kubeconfig from Rancher with PowerShell</title>
      <dc:creator>Benly Walter</dc:creator>
      <pubDate>Sun, 13 Jul 2025 22:56:55 +0000</pubDate>
      <link>https://dev.to/benlywalter/automate-kubeconfig-from-rancher-with-powershell-4578</link>
      <guid>https://dev.to/benlywalter/automate-kubeconfig-from-rancher-with-powershell-4578</guid>
      <description>&lt;p&gt;If you're like me and manage multiple Kubernetes clusters using &lt;a href="https://www.rancher.com/" rel="noopener noreferrer"&gt;Rancher&lt;/a&gt;, you've probably run into situations where you needed to copy the kubeconfig from Rancher to your local machine — whether for debugging with &lt;code&gt;kubectl&lt;/code&gt; or accessing clusters with &lt;a href="https://k9scli.io/" rel="noopener noreferrer"&gt;k9s&lt;/a&gt;. Personally, I prefer managing everything through &lt;code&gt;k9s&lt;/code&gt; and leaving the Rancher UI for developers or support teams who don’t interact with Kubernetes as deeply as I do.&lt;/p&gt;

&lt;p&gt;In this post, I’ll show you how I automated this process with a PowerShell script so you can update your local kubeconfig for all clusters in one go.&lt;/p&gt;

&lt;h2&gt;
  
  
  What does this script do?
&lt;/h2&gt;

&lt;p&gt;✅ Connects to multiple Rancher environments&lt;br&gt;
✅ Lists all clusters (or filters by name/provider)&lt;br&gt;
✅ Optionally backs up your existing kubeconfig&lt;br&gt;
✅ Merges or recreates kubeconfig automatically&lt;br&gt;
✅ Leaves you ready to &lt;code&gt;kubectl get pods&lt;/code&gt; everywhere&lt;/p&gt;

&lt;h2&gt;
  
  
  Requirements
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;PowerShell Core&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;kubectl&lt;/code&gt; CLI installed and available in your PATH&lt;/li&gt;
&lt;li&gt;Rancher API key. You can create a key with no scope which lets you manage all clusters where you have access.&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Rancher module with helper functions:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;Get-RancherClusterAll&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;Get-RancherClusterInfo&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;Get-RancherKubeConfig&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;Merge-KubeConfigFromRancher&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;h2&gt;
  
  
  Why PowerShell Core?
&lt;/h2&gt;

&lt;p&gt;PowerShell Core works on Windows, macOS, and Linux. It’s perfect for cross-platform automation, and it makes interacting with APIs and files super smooth.&lt;/p&gt;

&lt;h2&gt;
  
  
  Example usage
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight powershell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$rancherAccounts&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;@{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="s2"&gt;"dev"&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;@{&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="nx"&gt;url&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"https://rancher-dev.example.com"&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="nx"&gt;token&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"token-xxxxxx"&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="s2"&gt;"prod"&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;@{&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="nx"&gt;url&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"https://rancher-prod.example.com"&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="nx"&gt;token&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"token-yyyyyy"&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;

&lt;/span&gt;&lt;span class="c"&gt;# Backup and recreate existing Kubeconfig File&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;\rancher-set-kubeconfig.ps1&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nt"&gt;-rancherAccounts&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nv"&gt;$rancherAccounts&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nt"&gt;-backupExisting&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nt"&gt;-recreateConfig&lt;/span&gt;&lt;span class="w"&gt;

&lt;/span&gt;&lt;span class="c"&gt;# Specify Backup Path&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;\rancher-set-kubeconfig.ps1&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nt"&gt;-rancherAccounts&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nv"&gt;$rancherAccounts&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nt"&gt;-backupExisting&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nt"&gt;-backupPath&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"C:\Backups\KubeconfigBackups"&lt;/span&gt;&lt;span class="w"&gt;

&lt;/span&gt;&lt;span class="c"&gt;# Specify Custom Kubeconfig File Path on Windows&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;\rancher-set-kubeconfig.ps1&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nt"&gt;-rancherAccounts&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nv"&gt;$rancherAccounts&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nt"&gt;-kubeConfigFilePath&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"C:\Users\&amp;lt;user&amp;gt;\.kube\custom-config"&lt;/span&gt;&lt;span class="w"&gt;

&lt;/span&gt;&lt;span class="c"&gt;# Select Specific Clusters&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;\rancher-set-kubeconfig.ps1&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nt"&gt;-rancherAccounts&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nv"&gt;$rancherAccounts&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nt"&gt;-clusters&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;@(&lt;/span&gt;&lt;span class="s2"&gt;"cluster1"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"cluster2"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"cluster3"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt;

&lt;/span&gt;&lt;span class="c"&gt;# Filter by Provider&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;\rancher-set-kubeconfig.ps1&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nt"&gt;-rancherAccounts&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nv"&gt;$rancherAccounts&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nt"&gt;-provider&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"aks"&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  How it works
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Takes in a list of Rancher accounts and optional filters (clusters, provider)&lt;/li&gt;
&lt;li&gt;Figures out your OS-specific kubeconfig location&lt;/li&gt;
&lt;li&gt;Backs up or removes existing config (if you want)&lt;/li&gt;
&lt;li&gt;Fetches and merges all selected cluster configs from Rancher&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;This script has saved me hours of boring manual work and avoided nasty copy-paste errors in my kubeconfig.&lt;br&gt;
If you'd like to try it, &lt;a href="https://github.com/Benly-walter/dev.to/blob/main/scripts/powershell/rancher-set-kubeconfig.ps1"&gt;grab the full script here&lt;/a&gt;. Please note that the script references the &lt;code&gt;rancher.psm1&lt;/code&gt; module saved in the same path.&lt;br&gt;
So far, the script has only been tested on Windows. If you find it useful or have ideas to improve it, feel free to share your thoughts in the comments or share it with others!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Automate your workflow, sit back, and sip some tea while it does the work for you!&lt;/strong&gt; 🎉&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fe24np0gt51eu7t8pynkw.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fe24np0gt51eu7t8pynkw.gif" alt="Sip some tea" width="186" height="256"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>rancher</category>
      <category>k8s</category>
      <category>powershell</category>
      <category>kubeconfig</category>
    </item>
  </channel>
</rss>
