Backup & Restore Kubernetes Cluster

Backup & Restore Kubernetes Cluster

In a real-time scenario, we must encounter failures related to the accidental deletion of resources-definition.yaml files or we want to use earlier resources as it was. so how to recover or back up our kubernetes cluster as it was before is the most important need while working with kubernetes.

Let's get into the real thing and learn about how this can be achieved using ETCD.

Our main requirement is to save or backup the information that we are using while running a kubernetes cluster for hosting the application on it. Here ETCD comes in.

ETCD cluster is where all cluster-related information is stored, also we can see the above picture that defines the meaning and uses of etcd.

As all the cluster-level information is stored in the etcd server so we will backup the etcd server itself is a good approach. As the etcd cluster is hosted on the master node, and when we had configured the etcd cluster earlier we had specified a location where all the data would be stored (data directory).

--data-dir=/var/lib/etcd

This is the directory that can be configured to be backed up by a backup tool.

As etcd also comes with a Built-in snapshot solution, we can take a snapshot of the etcd database at that time by using the etcd control utility's snapshot save command.

> ETCDCTL_API=3 etcdctl snaphot save my_snapshot.db

This command will help us to take a snapshot of etcd at this time and the name of that snapshot will be my_snapshot.db.

> ETCDCTL_API=3 etcdctl snaphot status my_snapshot.db

This command will help us to show the status of the backup created.

Now, how to restore these backups in the future is the main thing we wanted to do. there are a few steps to do this so let's have a look below.

[1]. Stop the Kube API server service, as the restore process will require to restart the etcd cluster and the Kube API server depends on it.

> service kube-apiserver stop

[2]. Run the etcd restore snapshot command with the path of the backup file present.

> ETCDCTL_API=3 etcdctl snapshot restore my_snapshot.db --data-dir /var/lib/etcd

[3]. Reload the daemon service & restart etcd service.

> systemctl daemon-reload
> service etcd restart

[4]. Start the Kube API server service.

> service kube-apiserver start

Now, we have successfully backed up the cluster from the snapshot we had created earlier.

Also please remember with all the etcdctl commands we have used above, we have to also specify the certificate files for authentication, the endpoint to the etcd cluster, CS certificate, the etcd server certificate, and the key.

Thank you so much for taking your valuable time for reading.

I have tried my level best to explain as much information as possible in the easiest manner. Any feedback for further improvement will be highly appreciated!

We can connect at Rahul Kumar Verma | LinkedIn Rahul Kumar Verma (@RahulKu28171925) / Twitter