Dealing with new .kube/config

Video: https://youtu.be/oBF-dUXZwrA

Once you get a new config from a remote kubernetes installation you need to integrate it to existing local .kube/config file.

Sometime, you or others, are doing experiments with kubernetes, so repeat the .kube/config integration steps over and over again, dealing with -data blobs

List of ingredients

  • existing .kube/config
  • config file to be added, with: new cluster infos, user infos, context (binding user to cluster)
  • utility bash scripts:
    • extract_kconfig.sh for extrac infos and add cluster, context and user
    • kuco_import.sh for extracting BLOB into .crt and .key files

Getting the scripts

Retrieve the scripts by:

curl https://raw.githubusercontent.com/danielecr/selfhosted/refs/heads/main/kubernetes/kuco_import.sh kuco_import.sh
curl https://raw.githubusercontent.com/danielecr/selfhosted/refs/heads/main/kubernetes/extract_kconfig.sh extract_kconfig.sh

Make those executable:

chmod +x extract_kconfig.sh
chmod +x kuco_import.sh

First import

First, copy locally the remote config for kubectl:

scp remote.host:/home/user/.kube/config newremotekube

then run the extract_kconfig.sh by:

extract_kconfig newkubeconfig

it is an interactive script. First output is some like:


contexts:
- context:
cluster: kubernetes
user: kubernetes-admin
name: kubernetes-admin@kubernetes
current-context: kubernetes-admin@kubernetes
users:
- name: kubernetes-admin
user:

creating:
- a cluster named kubernetes
- a user named kubernetes-admin
- a context named kubernetes-admin@kubernetes

Please, check for conflicts in .kube/config before going on!

Enter host URL:

Hit <return> without input. Check for conflict in current .kube/config file. If there are no conflict, run it again and insert the right host URL, i.e. http://remote.host:6443

At the end the suggested command is:

Now run:
kuco_import.sh newkubeconfig newkubeconfig

Run it. Extracted files are stored in the folder $HOME/.kube/

Edit the .kube/config and remove the path from added certs and key filenames, i.e.:

    certificate-authority: /current/path/to/newkubeconfig-cluster-cert.crt
#becomes
certificate-authority: newkubeconfig-cluster-cert.crt

now change current context by:

kubectl use-context kubernetes-admin@kubernetes

And check

kubectl get nodes

Same server new keys

This is the quick part, every time the server changes certificate and user key, the commands to execute are:

scp remote.host:/home/user/.kube/config newremotekube
kuco_import.sh newkubeconfig newkubeconfig

The .kube/config is untouched

Bonus

Now, kuco_import.sh is good for extracting also blob from existing .kube/config file by:

cp .kube/config kindserv
kuco_import kindserv

Then edit the .kube/config relevant part by replacing -data with filename references