Fixing “Config not found” Error When Using kubectl
with k3s on Linux
When attempting to use the command:
kubectl version
you encounter a config issue. The output is as follows:
W0730 19:46:32.066086 17273 loader.go:222] Config not found: /home/ecarroll/.kube/config
The connection to the server localhost:8080 was refused - did you specify the right host or port?
This indicates that kubectl
cannot find its configuration file, which is necessary for communicating with your k3s cluster. Here’s how to resolve this problem quickly and effectively!
Understanding Kubeconfig Problem Solutions
The error message hints at an environmental misconfiguration of the KUBECONFIG variable—a path pointing towards a non-existent configuration file or directory on your system. To address it:
- Confirm that
KUBECONFIG
is correctly set by running in terminal:echo $KUBECONFIG
- If the above command reveals an incorrect path, you can regenerate a new kubeconfig file for your k3s setup with these steps (make sure to use appropriate sudo commands if necessary):
- Create or verify existence of
.kube
directory:mkdir ~/.kube > /dev/null chmod 700 ~/.kube
- Generate a new kubectl configuration with current context details from your cluster using the following command (assuming
sudo
is not needed):k3s kubectl config view --raw > $KUBECONFIG
Remember to adjust the certificate path as necessary if your setup uses TLS for secure communication with kubelet onmkdir ~/.kube; chmod go-rwx ~/.kube && sudo -H chown $(whoami):$(id -R) .kube/config > $KUBECONFIG ``` 3. Set the generated kubectl configuration as your `KUBECONFIG`: ```shell KUBECONFIG=$HOME/.kube/config sudo kubectl version --tls-cert-file=/var/run/secrets/kubernetes.io/serviceaccount/cert:44be0b38-2e71-11ea-96a5-ccebc9bfd3dd
kubectl
client nodes. - Create or verify existence of
After setting up correctly, try running:
kubectl version
This should now successfully display information about both cluster and local configurations without any error messages related to missing config files!