Answers: <p>This problem is likely caused by a bad <code>~/.kube/config</code> perhaps you have a file from a different kubernetes install (minikube) or an older k3s. If the server is local you can fix this by running  these commands,</p>
  <pre><code>mkdir ~/.kube
  sudo k3s kubectl config view --raw | tee ~/.kube/config
  chmod 600 ~/.kube/config
  </code></pre>
  <p>The contents of <code>~/.kube/config</code> need to have the same information as <code>/etc/rancher/k3s/k3s.yaml</code> when the server was started (the keys, ip, and ports).</p>
  <hr />
  <p>Note in order to tell k3s to use this config file <a href="https://devops.stackexchange.com/a/16044/18965">you'll want to configure <code>KUBECONFIG</code></a>.</p>
  <pre><code>export KUBECONFIG=~/.kube/config
  </code></pre>
  <p>You should persist this by having it set in <code>~/.profile</code> or <code>~/.bashrc</code></p>
  
Answers: CC BY-SA 4.0
Answers: 2024-10-18T20:15:14.663Z
Answers: Is there a way to fix the problem cause of having a bad ~/.kube/config?  Like removing the old config?
Answers: engineer-x
Answers: CC BY-SA 4.0
Answers: 2022-05-24T17:25:45.973Z
Answers: 2022-05-31T19:55:39.130Z
Answers: 2022-05-31T19:55:39.130Z
Answers: Evan Carroll
Body: <p>When I run any <code>kubectl</code> commands, I'm getting</p>
  <blockquote>
  <p>The connection to the server localhost:8080 was refused - did you specify the right host or port?</p>
  </blockquote>
  
Tags: <kubernetes><k3s>
Title: k3s: The connection to the server localhost:8080 was refused - did you specify the right host or port?
UserDisplayName: Evan Carroll
ThreadText: # k3s: The connection to the server localhost:8080 was refused - did you specify the right host or port?
  
  (Asked by: username_0 on 2022-05-24)
  
  When I run any `kubectl` commands, I'm getting
  
  > The connection to the server localhost:8080 was refused - did you specify the right host or port?
  
  **Tags:** kubernetes, k3s
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_0 on 2022-05-24. Score: 10 | Accepted answer
  
  This problem is likely caused by a bad `~/.kube/config` perhaps you have a file from a different kubernetes install (minikube) or an older k3s. If the server is local you can fix this by running these commands,
  
  ```
  mkdir ~/.kube
  sudo k3s kubectl config view --raw | tee ~/.kube/config
  chmod 600 ~/.kube/config
  
  ```
  
  The contents of `~/.kube/config` need to have the same information as `/etc/rancher/k3s/k3s.yaml` when the server was started (the keys, ip, and ports).
  
  ---
  
  Note in order to tell k3s to use this config file [you'll want to configure `KUBECONFIG`](https://devops.stackexchange.com/a/16044/18965).
  
  ```
  export KUBECONFIG=~/.kube/config
  
  ```
  
  You should persist this by having it set in `~/.profile` or `~/.bashrc`
  
  > **Comments:**
  > **username_1**: Is there a way to fix the problem cause of having a bad ~/.kube/config?  Like removing the old config?  
  
  ---
  
  