Managing K3s Remotely: Installing Prometheus & Grafana from Your Windows Workstation
This article goes into deep into how to manage kubernetes remotely from local window computer
So, you've got K3s up and running on your server, perhaps a Raspberry Pi, an old desktop, or a dedicated homelab machine. That's fantastic! Now you're looking to add powerful monitoring tools like Prometheus and Grafana. The natural question arises: should you SSH into your server and install them directly, or can you manage everything from your comfortable local Windows workstation?
The short answer is: You can, and often should, manage your Kubernetes cluster, including deploying applications like Prometheus and Grafana, from your local machine using kubectl and Helm! This is the idiomatic way to interact with Kubernetes clusters and offers a much smoother workflow.
SSHing into your server for every deployment means you're relying on tools and configurations specific to that server. Managing from your local machine leverages your familiar environment, your local kubectl configuration, and the powerful tooling available on your workstation.
This post will guide you through the process of setting up your local Windows machine to connect to your remote K3s cluster, and then touch upon how you'd proceed with installing Prometheus and Grafana.
Step 1: Getting Your K3s Config File to Your Windows Machine
Your K3s cluster's configuration is stored on your server at /etc/rancher/k3s/k3s.yaml. We need to get this file onto your Windows machine, specifically to C:\Users\sajit\.kube\.
While scp is the tool for the job, you might encounter a common permission issue. Let's tackle that head-on.
The Initial Hurdle: Permission Denied
When you try to directly copy /etc/rancher/k3s/k3s.yaml using scp, you'll likely see a "Permission denied" error. This is because the k3s.yaml file is owned by root for security reasons, and your regular SSH user doesn't have read access to it directly.
The Solution: Copy via your Server's Home Directory
We'll use sudo on the server to first copy the file to a location your user does have access to (your home directory), and then scp it from there.
Open a PowerShell or Command Prompt on Windows.
SSH into your server:
PowerShell
ssh [email protected](Replace
your_usernamewith the actual username you use to log in to your server.)On the server (after SSHing in), copy the file and change its ownership:
Bash
sudo cp /etc/rancher/k3s/k3s.yaml ~/k3s.yaml sudo chown $(whoami):$(whoami) ~/k3s.yamlsudo cp /etc/rancher/k3s/k3s.yaml ~/k3s.yaml: This copies thek3s.yamlfile from its restricted location to your user's home directory (~).sudogrants the necessary elevated permissions.sudo chown $(whoami):$(whoami) ~/k3s.yaml: This changes the ownership of the copied file in your home directory to your current user. This allows you toscpit without permission issues.$(whoami)automatically inserts your current username.
Exit the SSH session from your server:
exitYou should now be back on your Windows machine's terminal.
Create the
.kubedirectory on your Windows machine (if it doesn't exist):PowerShell
mkdir C:\Users\sajit\.kubeDownload the file from your server to your Windows machine using
scp:PowerShell
scp [email protected]:~/k3s.yaml C:\Users\sajit\.kube\k3s.yamlYou will be prompted for your server password. This time, the copy should succeed!
Step 2: Modifying the k3s.yaml File for Remote Access
The k3s.yaml file is configured to work locally on the server (using 127.0.0.1). To connect from your Windows machine, you need to change this to your server's actual IP address.
Open the downloaded file (
C:\Users\sajit\.kube\k3s.yaml) in a text editor (like Notepad, VS Code, or Sublime Text).Find the line that looks like this:
YAML
server: https://127.0.0.1:6443Change
127.0.0.1to your server's IP address (192.168.0.120in this case):YAML
server: https://192.168.0.120:6443Save and close the file.
Step 3: Verifying Your Connection with kubectl
Now, let's make sure your local kubectl can talk to your remote K3s cluster. For kubectl to automatically find your configuration, you have a couple of options:
Option A: Rename the file (Simplest)
Rename C:\Users\sajit\.kube\k3s.yaml to C:\Users\sajit\.kube\config.
Then, open a new PowerShell terminal and test the connection:
PowerShell
kubectl get nodes
Option B: Use the KUBECONFIG environment variable
If you prefer to keep the filename as k3s.yaml, you can tell kubectl where to find it by setting an environment variable for your current session:
PowerShell
$env:KUBECONFIG="C:\Users\sajit\.kube\k3s.yaml"
kubectl get nodes
If the command successfully returns information about the nodes in your K3s cluster, congratulations! You have successfully connected your local kubectl to your remote server.
Next Steps: Installing Prometheus and Grafana from Your Windows Machine
Now that your local kubectl is connected, you can deploy Prometheus and Grafana using powerful tools like Helm. Helm is the package manager for Kubernetes and makes installing complex applications like monitoring stacks incredibly easy.
Here's the general approach you would take (assuming you have Helm installed on your Windows machine):
Add the Helm repositories for Prometheus and Grafana:
PowerShell
helm repo add prometheus-community https://prometheus-community.github.io/helm-charts helm repo add grafana https://grafana.github.io/helm-charts helm repo update
2. Install the Prometheus stack (which typically includes Grafana): powershell helm install prometheus prometheus-community/kube-prometheus-stack --namespace monitoring --create-namespace This single command will deploy Prometheus, Grafana, and related components into a monitoring namespace on your K3s cluster. You can customize the installation using a values.yaml file if needed.
Access Grafana: Once deployed, you would typically use
kubectl port-forwardto temporarily expose the Grafana UI to your local machine:PowerShell
kubectl port-forward svc/prometheus-grafana 3000:80 -n monitoringThen, open your web browser to
http://localhost:3000.
By following these steps, you maintain control and management of your K3s cluster from your local Windows machine, making deployments and operations much more efficient. Happy monitoring!