Setting Up Access to the Kubernetes Cluster
After installing the K3s cluster, you need to enable access to the cluster from your local machine, which can run any major operating system — Windows, Linux, or macOS. To do this, copy the Kubeconfig file from any server node to your local machine and update the server IP address in the file to match your cluster’s external IP. The kubectl and Helm command-line tools automatically read this file to obtain the connection details and credentials needed to communicate with the cluster.
To set up access to the Kubernetes cluster from a local machine:
-
Verify that the $HOME/.kube directory exists on your local machine.
-
If you already manage another Kubernetes cluster, back up your existing config file:
mv $HOME/.kube/config $HOME/.kube/config.backup -
Copy the Kubeconfig file, located at /etc/rancher/k3s/k3s.yaml, from any K3s server node to your local machine:
scp <user>@<server_node_ip>:/etc/rancher/k3s/k3s.yaml $HOME/.kube/config -
Open $HOME/.kube/config and replace the value of the server field with the IP address or hostname of your K3s server node.
-
Verify that you can access the cluster.
kubectl get nodes -
(Optional): Merge Kubeconfig files.
If you backed up an existing Kubeconfig file in step 2, you can merge it with the new one by using one of the following commands:In Windows (PowerShell):
mv $HOME\.kube\config $HOME\.kube\k3s $ENV:KUBECONFIG = "$HOME\.kube\config.backup;$HOME\.kube\k3s" kubectl config view --flatten | Out-File -FilePath "$HOME\.kube\config" kubectl config use-context defaultIn Linux or macOS (Bash/Zsh):
mv ~/.kube/config $HOME/.kube/k3s KUBECONFIG=~/.kube/config.backup:~/.kube/k3s kubectl config view --flatten > ~/.kube/config kubectl config use-context default
What's Next?