Kubernetes backup and restore

2/6/2019

How to take a backup of the kubernetes cluster and restore them on another cluster.? I have setup kubenetes cluster on Azure & I want to take backup every day 00.00.00

-- Raghunath Babalsure
cron
kubernetes

3 Answers

12/10/2019

Got two solutions.

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

#!/bin/bash

# NAMESPACED EXPORTS
for 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 EXPORTS
kubectl 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-backup
helm 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 $ns
done
-- Timothy Perez
Source: StackOverflow

2/7/2019

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 yaml Than 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.

-- aurelius
Source: StackOverflow

5/3/2019

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

-- Sergio Jiménez
Source: StackOverflow