Backup And Restore Methods
- Backup Candidates:
- Resource configuration - pod definition files etc/
- ETCD Cluster - where all cluster-related information is stored.
- Persistent Volumes
- Imperative Commands:
kubectl create namespace new-namespacekubectl create secretkubectl create configmap
- Declarative Approach:
- Creating a definition file.
- Then run
kubectl apply -f <filename> - Ideally store these on source code repositories such as GitHub.
- Don’t need to stick to certain standards.
- A good way is to query the
kube-api server.- Can save all definition files for all objects created on the cluster as a copy.
- A good command to use in a backup script:
kubectl get all -all-namespaces -o yaml > all-deploy-services.yaml - Many other resource groups that must be considered.
- Useful tools such as Velero that can do this for you and takes backups using the
kube-apiserver. ETCDcontains information about the state of the cluster.- Can backup the
ETCDserver. - The
ETCDcluster is hosted on the Master Nodes. - In the
etcd.servicefile, everything will be stored in the--data-dir=/var/lib/etcdby default.- This can be backupped by a backup tool.
- Can also take snapshots of the
ETCDdata using the following commandETCDCTL_API=3 etcdctl \ snapshot save snapshot.db - The output file is the following:
snapshot.db - Can view the status of the backup using the
snapshot statuscommand:ETCDCTL_API=3 etcdctl \ snapshot status snapshot.db - To restore the cluster from a later point in time:
- Stop the ETCD service with:
sudo service kube-apiserver stop
- Stop the ETCD service with:
- Then run the
snapshot restorecommand and set the path to the backup file:ETCDCTL_API=3 etcdctl \ snapshot restore snapshot.db --data-dir /var/lib/etcd-from-backup - When ETCD is restored from backup, it initialises a new cluster configuration.
- It also prevents new ETCD members from joining an existing cluster.
- Then reconfigure the
etcd.servicefile to use the new--data-dirdirectory. - Reload the service daemon:
sudo systemctl daemon-reload - Restart the
etcdservice:sudo service etcd restart - Start the
kube-apiserverservice:sudo service kube-apiserver start - With all
etcdcommand, specify the certificate for authentication:ETCDCTL_API=3 etcdctl \ snapshot save snapshot.db \ --endpoints=https://127.0.0.1:2379 --cacert=/etc/etcd/ca.crt \ --cert=/etc/etcd/etcd-server.crt \ --key=/etc/etcd/etcd-server.key - If using a managed kubernetes environment, likely do not have access to the
etcdcluster.- In that case, backup by querying the
kube-apiserveris the best way.
- In that case, backup by querying the