Difference between revisions of "K8s backup"

From UVOO Tech Wiki
Jump to navigation Jump to search
(Created page with "https://github.com/vmware-tanzu/velero")
 
 
(4 intermediate revisions by the same user not shown)
Line 1: Line 1:
 +
 +
https://microk8s.io/docs/velero
 +
 
https://github.com/vmware-tanzu/velero
 
https://github.com/vmware-tanzu/velero
 +
 +
https://velero.io/docs/main/migration-case/
 +
 +
https://velero.io/docs/v0.11.0/get-started/
 +
 +
 +
```
 +
Step 6: Backing up a Cluster
 +
To take full backup run the below command
 +
 +
velero backup create my-full-backup
 +
The velero backup create command creates a backup.
 +
my-full-backup is the name of the backup.
 +
Above command will take backup of all namespaces and all resources.
 +
 +
Backup specific namespace:
 +
 +
velero backup create backup-all-specific-namespace --include-namespaces my-namespace
 +
The velero backup create command creates a backup.
 +
The backup-all-specific-namespace is the name of the backup.
 +
The --include-namespaces flag specifies the namespace(s) to include in the backup. In this case, it's the my-namespace namespace.
 +
Backup a specific resource in some namespace:
 +
 +
velero backup create backup-specific-resource-namespace --include-resources deployment.apps/nginx --include-namespaces some-namespace
 +
The velero backup create command creates a backup.
 +
The backup-specific-resource-namespace is the name of the backup.
 +
The --include-resources flag specifies the resource(s) to include in the backup. In this case, it's the deployment.apps/nginx deployment.
 +
The --include-namespaces flag specifies the namespace(s) to include in the backup. In this case, it's the some-namespace namespace.
 +
Step 7: Restoring a Cluster
 +
To Restore from backup
 +
 +
velero restore create --from-backup my-full-backup
 +
The velero restore create command creates a restore operation.
 +
The --from-backup flag specifies the backup to restore from. In this case, it's the my-full-backup backup.
 +
Restore without persistent volume
 +
 +
velero restore create --from-backup my-full-backup --restore-volume-snapshots=false
 +
The --restore-volume-snapshots flag controls whether to restore Persistent Volumes and their snapshots. In this case, it's set to false, meaning that Persistent Volumes and snapshots will not be restored.
 +
 +
Step 8: Schedule a Backup
 +
You can schedule a Velero backup to run regularly using a Kubernetes CronJob resource. A CronJob runs a job on a schedule defined in a cron expression.
 +
 +
Here’s an example of how to create a CronJob that runs a Velero backup every day at midnight:
 +
 +
apiVersion: batch/v1beta1
 +
kind: CronJob
 +
metadata:
 +
  name: daily-backup
 +
spec:
 +
  schedule: "0 0 * * *"
 +
  jobTemplate:
 +
    spec:
 +
      template:
 +
        spec:
 +
          containers:
 +
          - name: backup
 +
            image: velero/velero:v1.10.0
 +
            command:
 +
            - /velero
 +
            args:
 +
            - backup
 +
            - create
 +
            - daily-backup
 +
            - --include-namespaces
 +
            - default
 +
          restartPolicy: OnFailure
 +
The apiVersion and kind specify that this is a CronJob resource.
 +
The metadata section contains the name of the CronJob, in this case daily-backup.
 +
The spec section contains the schedule for the CronJob defined in the schedule field. In this case, it's 0 0 * * *, which means run every day at midnight.
 +
The jobTemplate section specifies the Job resource that the CronJob will run.
 +
The template section specifies the Pod that will run the Velero command.
 +
The containers section specifies the container that will run the Velero command. It uses the velero/velero image, and runs the velero backup create command with the daily-backup backup name and includes resources in the default namespace.
 +
The restartPolicy is set to OnFailure, which means the Pod will not be restarted if it fails.
 +
Once you have defined the CronJob, you can create it in your Kubernetes cluster using the kubectl apply command:
 +
 +
kubectl apply -f cronjob.yaml
 +
Where cronjob.yaml is the file containing the CronJob definition.
 +
 +
Step 9: Deleting Backups
 +
You can delete a Velero backup using the following command:
 +
 +
velero backup delete <backup-name>
 +
The velero backup delete command deletes a Velero backup.
 +
The <backup-name> is the name of the backup you want to delete.
 +
Note that deleting a backup will permanently remove all of the backed up data and it cannot be recovered. Before deleting a backup, you may want to verify that you have a copy of the data stored elsewhere.
 +
 +
Bonus
 +
1. List backups
 +
 +
velero backup get
 +
The command will display information about each backup, including its name, creation time, and status.
 +
 +
2. Check backup status
 +
 +
velero backup describe <backup-name>
 +
This command will display information about the backup, including its status, creation time, and the resources included in the backup. The status of the backup will be one of the following:
 +
 +
3. Check the status of restore
 +
 +
velero restore describe <restore-name>
 +
The velero restore describe command retrieves information about a restore.
 +
The <restore-name> is the name of the restore you want to check the status of.
 +
This command will display information about the restore, including its status, creation time, and the resources included in the restore. The status of the restore will be one of the following:
 +
 +
4. Check all restore(s)
 +
 +
velero restore get
 +
The command will display information about each restore, including its name, creation time, and status.
 +
```

Latest revision as of 12:46, 17 April 2023

https://microk8s.io/docs/velero

https://github.com/vmware-tanzu/velero

https://velero.io/docs/main/migration-case/

https://velero.io/docs/v0.11.0/get-started/

Step 6: Backing up a Cluster
To take full backup run the below command

velero backup create my-full-backup
The velero backup create command creates a backup.
my-full-backup is the name of the backup.
Above command will take backup of all namespaces and all resources.

Backup specific namespace:

velero backup create backup-all-specific-namespace --include-namespaces my-namespace
The velero backup create command creates a backup.
The backup-all-specific-namespace is the name of the backup.
The --include-namespaces flag specifies the namespace(s) to include in the backup. In this case, it's the my-namespace namespace.
Backup a specific resource in some namespace:

velero backup create backup-specific-resource-namespace --include-resources deployment.apps/nginx --include-namespaces some-namespace
The velero backup create command creates a backup.
The backup-specific-resource-namespace is the name of the backup.
The --include-resources flag specifies the resource(s) to include in the backup. In this case, it's the deployment.apps/nginx deployment.
The --include-namespaces flag specifies the namespace(s) to include in the backup. In this case, it's the some-namespace namespace.
Step 7: Restoring a Cluster
To Restore from backup

velero restore create --from-backup my-full-backup
The velero restore create command creates a restore operation.
The --from-backup flag specifies the backup to restore from. In this case, it's the my-full-backup backup.
Restore without persistent volume

velero restore create --from-backup my-full-backup --restore-volume-snapshots=false
The --restore-volume-snapshots flag controls whether to restore Persistent Volumes and their snapshots. In this case, it's set to false, meaning that Persistent Volumes and snapshots will not be restored.

Step 8: Schedule a Backup
You can schedule a Velero backup to run regularly using a Kubernetes CronJob resource. A CronJob runs a job on a schedule defined in a cron expression.

Here’s an example of how to create a CronJob that runs a Velero backup every day at midnight:

apiVersion: batch/v1beta1
kind: CronJob
metadata:
  name: daily-backup
spec:
  schedule: "0 0 * * *"
  jobTemplate:
    spec:
      template:
        spec:
          containers:
          - name: backup
            image: velero/velero:v1.10.0
            command:
            - /velero
            args:
            - backup
            - create
            - daily-backup
            - --include-namespaces
            - default
          restartPolicy: OnFailure
The apiVersion and kind specify that this is a CronJob resource.
The metadata section contains the name of the CronJob, in this case daily-backup.
The spec section contains the schedule for the CronJob defined in the schedule field. In this case, it's 0 0 * * *, which means run every day at midnight.
The jobTemplate section specifies the Job resource that the CronJob will run.
The template section specifies the Pod that will run the Velero command.
The containers section specifies the container that will run the Velero command. It uses the velero/velero image, and runs the velero backup create command with the daily-backup backup name and includes resources in the default namespace.
The restartPolicy is set to OnFailure, which means the Pod will not be restarted if it fails.
Once you have defined the CronJob, you can create it in your Kubernetes cluster using the kubectl apply command:

kubectl apply -f cronjob.yaml
Where cronjob.yaml is the file containing the CronJob definition.

Step 9: Deleting Backups
You can delete a Velero backup using the following command:

velero backup delete <backup-name>
The velero backup delete command deletes a Velero backup.
The <backup-name> is the name of the backup you want to delete.
Note that deleting a backup will permanently remove all of the backed up data and it cannot be recovered. Before deleting a backup, you may want to verify that you have a copy of the data stored elsewhere.

Bonus
1. List backups

velero backup get
The command will display information about each backup, including its name, creation time, and status.

2. Check backup status

velero backup describe <backup-name>
This command will display information about the backup, including its status, creation time, and the resources included in the backup. The status of the backup will be one of the following:

3. Check the status of restore

velero restore describe <restore-name>
The velero restore describe command retrieves information about a restore.
The <restore-name> is the name of the restore you want to check the status of.
This command will display information about the restore, including its status, creation time, and the resources included in the restore. The status of the restore will be one of the following:

4. Check all restore(s)

velero restore get
The command will display information about each restore, including its name, creation time, and status.