-1

My helm install was giving some errors and I used this command to try to fix it. Now my cluster is gone

kubectl config view --raw >~/.kube/config

The error is:

localhost:8080 was refused - did you specify the right host or port?
Andrew
  • 3,912
  • 17
  • 28
user101
  • 1
  • 1

1 Answers1

0

You can't reverse it because you have basically overwritten the existing file, the only way to get it back is to restore from backups.

See this answer for more details on why it happened: https://stackoverflow.com/a/6696881/3066081

It boils down to the way bash handles shell redirects - > overwrites file before kubectl has a chance to access it, and after seeing an empty file, it creates the following yaml:

apiVersion: v1
clusters: null
contexts: null
current-context: ""
kind: Config
preferences: {}
users: null

localhost:8080 is a default address kubectl tries to connect to if kubeconfig is empty.

As for proper way of doing that in future you should:

  1. Backup your current ~/.kube/config to ~/.kube/config.bak, just in case

    cp ~/.kube/config{,.bak}
    
  2. Create a new, temporary file, containing output of your command, i.e.

    kubectl config view --raw > raw_config
    
  3. Replace kubeconfig with new one

    mv raw_config ~/.kube/config
    
Andrew
  • 3,912
  • 17
  • 28
  • Thank you, I recovered the contents of config file, by copying the contents from admin.conf file and pasted onto config file. – user101 Jul 12 '22 at 15:44