Skip to content

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:

  1. Confirm that KUBECONFIG is correctly set by running in terminal:
    echo $KUBECONFIG
    
  2. 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):
    1. Create or verify existence of .kube directory:
      mkdir ~/.kube > /dev/null
      chmod 700 ~/.kube
      
    2. 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
      
    If you’re behind sudo, ensure proper permissions:
      mkdir ~/.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
    
    Remember to adjust the certificate path as necessary if your setup uses TLS for secure communication with kubelet on kubectl client nodes.

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!


Previous Post
appears there was a misunderstanding in your init
Next Post
Nginx as a Webserver Outside an Ingress Controller