Skip to content

Instantly share code, notes, and snippets.

@mattmattox
Last active March 1, 2024 06:36
Show Gist options
  • Star 21 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save mattmattox/d32b3fea4820075c08c6cc2f6d736702 to your computer and use it in GitHub Desktop.
Save mattmattox/d32b3fea4820075c08c6cc2f6d736702 to your computer and use it in GitHub Desktop.
Recovering cluster.yml and cluster.rkestate from kubeconfig
#!/bin/bash
echo "Building cluster_recovery.yml..."
echo "Working on Nodes..."
echo 'nodes:' > cluster_recovery.yml
kubectl --kubeconfig kube_config_cluster.yml -n kube-system get configmap full-cluster-state -o json | jq -r .data.\"full-cluster-state\" | jq -r .desiredState.rkeConfig.nodes | yq r - | sed 's/^/ /' | \
sed -e 's/internalAddress/internal_address/g' | \
sed -e 's/hostnameOverride/hostname_override/g' | \
sed -e 's/sshKeyPath/ssh_key_path/g' >> cluster_recovery.yml
echo "" >> cluster_recovery.yml
echo "Working on services..."
echo 'services:' >> cluster_recovery.yml
kubectl --kubeconfig kube_config_cluster.yml -n kube-system get configmap full-cluster-state -o json | jq -r .data.\"full-cluster-state\" | jq -r .desiredState.rkeConfig.services | yq r - | sed 's/^/ /' >> cluster_recovery.yml
echo "" >> cluster_recovery.yml
echo "Working on network..."
echo 'network:' >> cluster_recovery.yml
kubectl --kubeconfig kube_config_cluster.yml -n kube-system get configmap full-cluster-state -o json | jq -r .data.\"full-cluster-state\" | jq -r .desiredState.rkeConfig.network | yq r - | sed 's/^/ /' >> cluster_recovery.yml
echo "" >> cluster_recovery.yml
echo "Working on authentication..."
echo 'authentication:' >> cluster_recovery.yml
kubectl --kubeconfig kube_config_cluster.yml -n kube-system get configmap full-cluster-state -o json | jq -r .data.\"full-cluster-state\" | jq -r .desiredState.rkeConfig.authentication | yq r - | sed 's/^/ /' >> cluster_recovery.yml
echo "" >> cluster_recovery.yml
echo "Working on systemImages..."
echo 'system_images:' >> cluster_recovery.yml
kubectl --kubeconfig kube_config_cluster.yml -n kube-system get configmap full-cluster-state -o json | jq -r .data.\"full-cluster-state\" | jq -r .desiredState.rkeConfig.systemImages | yq r - | sed 's/^/ /' >> cluster_recovery.yml
echo "" >> cluster_recovery.yml
echo "Building cluster_recovery.rkestate..."
kubectl --kubeconfig kube_config_cluster.yml -n kube-system get configmap full-cluster-state -o json | jq -r .data.\"full-cluster-state\" | jq -r . > cluster_recovery.rkestate
echo "Running rke up..."
rke up --config cluster_recovery.yml
@leoschet
Copy link

This is a life saver! A note for all who will use this in the future: be aware that when running rke up --config <file>.yml rke will consider this as a new cluster unless you have a kube_config_<file>.yml in the local directory. In case you already ran this once, it might make sense to check this answer

@mattmattox
Copy link
Author

We are testing a new tool to fetch the kubconfig and rke state file from the cluster.
PR

git clone https://github.com/rancher/rke && cd rke && git fetch origin pull/2277/head:util && git checkout util && CROSS=true make build

NOTE: This is not for Production yet.

@ferdinandosimonetti
Copy link

ferdinandosimonetti commented Jun 24, 2021

Hello, this is very good... but somehow it seems to retrieve only the informations about the master node of my single-master, three workers cluster.
Anyone noticed?
I must add that the original rke cluster installation (the one of which I lost cluster.yml file) had been performed via non-root user and non-standard SSH private key.
However, I was able to perform successfully an "rke etcd snapshot-save" using the output of the script as cluster.yml (amended for SSH connection parameters).

@dorancemc
Copy link

Thanks, this works for me

@jostmart
Copy link

I needed to change
yq r -

into

yq -P

yq --version
yq (https://github.com/mikefarah/yq/) version 4.21.1

@gfnord
Copy link

gfnord commented Nov 14, 2022

Thanks, this change worked for me.

@litao3rd
Copy link

I encounter the same issue. But in the first step there is no full-cluster-state in config map. crying...

@jrab66
Copy link

jrab66 commented Feb 7, 2024

I do not have full-cluster-state configmap in a working cluster, no idea also ? @litao3rd did you find another option?

@mattmattox
Copy link
Author

@jrab66 You can run the command rke util get-state-file now to pull the state file from a working cluster.

@litao3rd
Copy link

litao3rd commented Mar 1, 2024

I do not have full-cluster-state configmap in a working cluster, no idea also ? @litao3rd did you find another option?

nop...I rebuild the cluster

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment