Last active
December 25, 2025 05:02
-
-
Save Aries0d0f/1d412ed2f55c677e602ec942c943e4b1 to your computer and use it in GitHub Desktop.
K3s remote kubeconfig management plugin for quick multiple k3s cluster configurations. Perfect for managing multiple Kubernetes clusters.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| # This plugin provides convenient functions to | |
| # fetch and manage kubeconfig files from remote k3s clusters. | |
| # It simplifies the process of connecting to multiple k3s clusters | |
| # by automatically retrieving, modifying, and loading kubeconfig files | |
| # from remote servers. | |
| k3s-save-remote-config() { | |
| USER=$1 | |
| HOSTNAME=$2 | |
| ALIAS=$3 | |
| PORT=${4:-"22"} | |
| ssh -p $PORT "$USER@$HOSTNAME" \ | |
| 'cat /etc/rancher/k3s/k3s.yaml' |\ | |
| sed -E "s/([^-] name|cluster|current-context): default/\1: $ALIAS/" |\ | |
| sed "s/127.0.0.1/$HOSTNAME/" |\ | |
| tee "$HOME/.kube/config.$ALIAS.yaml" | |
| echo "Kubeconfig saved to $HOME/.kube/config.$ALIAS.yaml" | |
| } | |
| k3s-load-remote-config() { | |
| USER=$1 | |
| HOSTNAME=$2 | |
| ALIAS=$3 | |
| PORT=${4:-"22"} | |
| k3s-save-remote-config "$USER" "$HOSTNAME" "$ALIAS" "$PORT" >/dev/null 2>&1 | |
| export KUBECONFIG="$HOME/.kube/config.$ALIAS.yaml" | |
| echo "KUBECONFIG set to $HOME/.kube/config.$ALIAS.yaml" | |
| echo "Now using context: $ALIAS" | |
| echo "" | |
| kubectl config get-contexts | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
K3s Remote Config Plugin
Overview
This plugin provides convenient functions to fetch and manage kubeconfig files from remote k3s clusters. It simplifies the process of connecting to multiple k3s clusters by automatically retrieving, modifying, and loading kubeconfig files from remote servers.
The plugin includes two main functions:
k3s-save-remote-config- Downloads and saves the kubeconfig from a remote k3s serverk3s-load-remote-config- Downloads, saves, and immediately loads the kubeconfig for useRequirements
sedcommand-line tool (available on Linux and macOS by default)kubectlcommand-line tool (required fork3s-load-remote-config)bashorzshshell/etc/rancher/k3s/k3s.yamlon the remote serverNote
This script works on both Linux and macOS, as it uses standard Unix utilities.
Installation
1. Download the Script
Download
k3s.plugin.shand save it to a convenient location, such as:~/scripts/k3s.plugin.sh~/.local/bin/k3s.plugin.sh2. Source the Plugin
The script is designed as a shell plugin, so you need to source it into your shell to use the functions.
For permanent availability (add to your shell's RC file):
For Zsh users (
~/.zshrc):For Bash users (
~/.bashrcor~/.bash_profile):For one-time use (current session only):
source /path/to/k3s.plugin.shAfter sourcing, reload your shell configuration:
Usage
k3s-save-remote-config
Fetches the kubeconfig from a remote k3s server and saves it locally without changing your current context.
Syntax:
Parameters:
USER- SSH username for the remote serverHOSTNAME- Hostname or IP address of the remote k3s serverALIAS- Friendly name for the cluster (used in kubeconfig)PORT- SSH port (optional, defaults to 22)Examples:
The kubeconfig will be saved to
~/.kube/config.<ALIAS>.yamlk3s-load-remote-config
Fetches the kubeconfig from a remote k3s server, saves it locally, and immediately sets it as the active kubeconfig.
Syntax:
Parameters:
k3s-save-remote-configExamples:
After running this command, your
KUBECONFIGenvironment variable will be set to the downloaded config, and you can immediately usekubectlcommands.How It Works
k3s-save-remote-config
/etc/rancher/k3s/k3s.yaml127.0.0.1with the actual remote hostname~/.kube/config.<ALIAS>.yamlk3s-load-remote-config
k3s-save-remote-configKUBECONFIGenvironment variable to point to the new configkubectl config get-contextsManaging Multiple Clusters
You can save multiple cluster configurations and switch between them:
Troubleshooting
Error: "Permission denied (publickey)"
Error: "cat: /etc/rancher/k3s/k3s.yaml: Permission denied"
/etc/rancher/k3s/k3s.yamlrootuser or a user with appropriate sudo permissionsssh user@host 'sudo cat /etc/rancher/k3s/k3s.yaml'Error: "command not found: kubectl"
apt,yum, etc.)brew install kubectlError: "ssh: connect to host port : Connection refused"
KUBECONFIG not persisting across sessions
export KUBECONFIGonly affects the current shell session~/.zshrcor~/.bashrc~/.kube/configSecurity Considerations
chmod 600 ~/.kube/config.*.yamlAdditional Tips
Set up SSH config for easier access:
Add to
~/.ssh/config:Then use:
Create aliases for frequent clusters:
Add to your shell RC file: