Kubernetes backup and restore Kubernetes backup and restore kubernetes kubernetes

Kubernetes backup and restore


You can find some resources about backup's in the web. It is important to choose a strategy that will work in your scenario.

In general Kubernetes stores its state in etcd, also managed clusters like GKE, AKS and EKS take care of your etcd (and master node) so you can't access this directly. I can't advise anything in particular as you did not provide enough details, so I will briefly try to introduce a few scenarios. Also, please remember that migrating persistent volumes across cloud providers might be tricky (last time I checked Velero did not support that - but not sure about current state).

One easy way is to get the yaml of the object you want to backup (also works for edits)

kubectl get *object_name* -n *namespace* --export -o yam

ex.kubectl get daemonset.apps/fluentd-gcp-v3.2.0 -n kube-system --export -o yamlThan just apply it in other place. If you want an advanced options, like judging by tag cron scheduled, automatic backups or event-based snapshots you would have to reach to more advanced tools - example is mentioned by Shouichi - Heptio Velero , kube-backup or kaptaind.

You can also find good explanation in this article and there is a section on automating single master backup with Kubernetes CronJob or this one with small adjustments you can easily migrate to a different cloud. If you will meet any issues, feel free to ask and I will try to help - just add more information like how did you create the cluster, is it managed service and what applications are inside.


I like to use this script: https://gist.github.com/irraz/d23ea066b08c4ff2e5652bd4d62e937e

Basically it exports in json format and the metadata with jq is deleted


Got two solutions.

Solution 1 - Bash script I threw together to export, practically, everything using kubectl.

#!/bin/bash# NAMESPACED EXPORTSfor ns in $(kubectl get ns --no-headers | cut -d " " -f1); do  kubectl --namespace="${ns}" get -o=json bindings,cm,ep,ev,limits,pvc,po,podtemplates,rc,quota,secrets,sa,svc,controllerrevisions,ds,deploy,rs,sts,localsubjectaccessreviews,hpa,cj,jobs,leases,ev,ds,deploy,ing,netpol,rs,pods,netpol,pdb,roles,rolebindings | \    jq '.items[] |    select(.type!="kubernetes.io/service-account-token") |    del(        .spec.clusterIP,        .metadata.uid,        .metadata.selfLink,        .metadata.resourceVersion,        .metadata.creationTimestamp,        .metadata.generation,        .status,        .spec.template.spec.securityContext,        .spec.template.spec.dnsPolicy,        .spec.template.spec.terminationGracePeriodSeconds,        .spec.template.spec.restartPolicy    )' >> "./${ns}.json"done# NON-NAMESPACED EXPORTSkubectl get -o=json cs,ns,no,pv,mutatingwebhookconfigurations,validatingwebhookconfigurations,crds,apiservices,tokenreviews,selfsubjectaccessreviews,selfsubjectrulesreviews,subjectaccessreviews,csr,psp,nodes,psp,clusterrolebindings,clusterroles,pc,sc,volumeattachments | \    jq '.items[] |    select(.type!="kubernetes.io/service-account-token") |    del(        .spec.clusterIP,        .metadata.uid,        .metadata.selfLink,        .metadata.resourceVersion,        .metadata.creationTimestamp,        .metadata.generation,        .status,        .spec.template.spec.securityContext,        .spec.template.spec.dnsPolicy,        .spec.template.spec.terminationGracePeriodSeconds,        .spec.template.spec.restartPolicy    )' >> "./cluster_non-namespaced_export.json"

Solution 2 - Using the Helm Backup Plugin

helm plugin install https://github.com/maorfr/helm-backuphelm backup {NAMESPACE}

Then to restore:

helm backup --restore {NAMESPACE}.tgz

Or if you're lazy like me, auto loop each ns

for ns in $(kubectl get ns --no-headers | cut -d " " -f1); do  helm backup $nsdone