kubernetes lost ~/.kube/config

2/23/2019

Unfortunately I lost my local

~/.kube/config

where I had configuration for my namespace.

Is there a way to get this config if I have access to master nodes?

Thanks in advance

-- kpazik
devops
kubernetes

2 Answers

2/23/2019

I believe you're using kubeadm to start your kubernetes cluster, you can generate the new kubeconfig file using following command:

kubeadm alpha phase kubeconfig admin --kubeconfig-dir /etc/kubernetes --cert-dir /etc/kubernetes/pki

This will generate a new config file in /etc/kubernetes/admin.conf. Then you can copy the file in following way:

mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
-- Prafull Ladha
Source: StackOverflow

4/21/2020

Thanks to @PrafullLadha, there's a similar solution for OpenShift:

  1. Install kubeadm with snap: sudo snap install kubeadm --classic

  2. cd to your openshift cluster installation directory.

  3. Make a copy of your TLS certificate: cp ./tls/journal-gatewayd.crt ./tls/ca.crt

  4. Make a copy of your TLS key: cp ./tls/journal-gatewayd.key ./tls/ca.key

  5. Run kubeadm as follow:

    kubeadm init phase kubeconfig admin --kubeconfig-dir ./auth --cert-dir "${PWD}/tls"

    It should output: [kubeconfig] Writing "admin.conf" kubeconfig file

  6. vi ./auth/admin.conf - see that the certificates were added, and make sure the server address (https://api.your-cluster:6443) is correct.

  7. Rename: mv ./auth/admin.conf ./auth/kubeconfig and you're all set.

-- Noam Manos
Source: StackOverflow