Skip to content

Instantly share code, notes, and snippets.

@adambom
Last active May 27, 2024 07:51
Show Gist options
  • Save adambom/193d923ca51e8ffab325fe8e25fa03aa to your computer and use it in GitHub Desktop.
Save adambom/193d923ca51e8ffab325fe8e25fa03aa to your computer and use it in GitHub Desktop.
Backup Kubernetes Cluster State

Run this in order to backup all you k8s cluster data. It will be saved in a folder bkp. To restore the cluster, you can run kubectl apply -f bkp.

Please note: this recovers all resources correctly, including dynamically generated PV's. However, it will not recover ELB endpoints. You will need to update any DNS entries manually, and manually remove the old ELB's.

Please note: This has not been tested with all resource types. Supported resource types include:

  • services
  • replicationcontrollers
  • secrets
  • deployments
  • horizontal pod autoscalers
  • persistent volumes
  • persistent volume claims
  • quotas
  • limits
  • namespaces
  • storageclasses
  • daemonsets
#!/bin/bash -e
# Adapted from:
# https://github.com/colhom/coreos-docs/blob/cluster-dump-restore/kubernetes/cluster-dump-restore.md
ABSOLUTE_PATH="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/../bkp"
mkdir -p $ABSOLUTE_PATH
kubectl get --export -o=json ns | \
jq '.items[] |
del(.status,
.metadata.uid,
.metadata.selfLink,
.metadata.resourceVersion,
.metadata.creationTimestamp,
.metadata.generation
)' > $ABSOLUTE_PATH/ns.json
echo "" > $ABSOLUTE_PATH/cluster-dump.json
for ns in $(jq -r '.metadata.name' < $ABSOLUTE_PATH/ns.json);do
echo "Namespace: $ns"
kubectl --namespace="${ns}" get --export -o=json svc,rc,secrets,ds,cm,deploy,hpa,pv,pvc,quota,limits,ns,storageclass | \
jq '.items[] |
select(.type!="kubernetes.io/service-account-token") |
del(
.spec.clusterIP, # clusterIP is dynamically assigned
.spec.claimRef, # Throw this out so we can rebind
.metadata.uid,
.metadata.selfLink,
.metadata.resourceVersion,
.metadata.creationTimestamp,
.metadata.generation,
.spec.template.spec.securityContext,
.spec.template.spec.terminationGracePeriodSeconds,
.spec.template.spec.restartPolicy,
.spec?.ports[]?.nodePort? # Delete nodePort from service since this is dynamic
) |
# Set reclaim policy to retain so we can recover volumes
if .kind == "PersistentVolume" then
.spec.persistentVolumeReclaimPolicy = "Retain"
else
.
end' >> $ABSOLUTE_PATH/cluster-dump.json
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment