Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add volume backup and restore tutorial #49452

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions content/en/docs/tutorials/_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ Before walking through each tutorial, you may want to bookmark the

* [Running Kubelet in Standalone Mode](/docs/tutorials/cluster-management/kubelet-standalone/)

## Volume Backup

* [Volume Backup Delete and restore](/docs/tutorials/volume-backup/volume-backup-restore/)

## {{% heading "whatsnext" %}}

If you would like to write a tutorial, see
Expand Down
4 changes: 4 additions & 0 deletions content/en/docs/tutorials/storage-management/_index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
title: "Volume Backup, Delete and restore "
weight: 40
---
259 changes: 259 additions & 0 deletions content/en/docs/tutorials/storage-management/volume-backup-restore.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,259 @@
---
title: Volume Backup, Delete and Restore
content_type: tutorial
weight: 10
---

<!-- overview -->
This tutorial provides an introduction for taking backups of a volume and restoring it. It demonstrates how to create, delete, and restore the volume using [gzip](https://www.gnu.org/software/gzip/).

## {{% heading "prerequisites" %}}
Before you begin this tutorial, you should be familiar with the following Kubernetes concepts:

* [Pods](/docs/concepts/workloads/pods/)
* [PersistentVolumes](/docs/concepts/storage/persistent-volumes/)
* The [kubectl](/docs/reference/kubectl/kubectl/) command line tool

## {{% heading "prerequisites" %}}

* You need to have a Kubernetes cluster that has only one Node, and the
{{< glossary_tooltip text="kubectl" term_id="kubectl" >}}
command-line tool must be configured to communicate with your cluster. If you
do not already have a single-node cluster, you can create one by using
[Minikube](https://minikube.sigs.k8s.io/docs/).

* Familiarize yourself with the material in
[Persistent Volumes](/docs/concepts/storage/persistent-volumes/).
Comment on lines +10 to +26
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are there two prerequisites sections?


## {{% heading "objectives" %}}
After this tutorial, you will be familiar with the following:
1. Understanding the importance of volume backups
1. Backing up kubernetes volumes
1. Deleting and recreating volumes
1. Restoring applications with backed-up data
1. Practical workflow for backup and restore

## Configure Persistent Volume and Persistent Volume Claim
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Follow the style guide here if you can.

Use the existing yaml file from the [Configure a Pod to Use a PersistentVolume for Storage](/tasks/configure-pod-container/configure-persistent-volume-storage/) task to configure PersistentVolume (PV) and PersistentVolumeClaim (PVC). Then follow the steps from the beginning, to create a `hostpath` and `index.html` file as well.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure what you mean by "follow the steps from the beginning". I'm already thinking, "Do you mean, from the beginning of this page, or the beginning of the page in the URL?

Here's my suggestion for you.

Suggested change
Use the existing yaml file from the [Configure a Pod to Use a PersistentVolume for Storage](/tasks/configure-pod-container/configure-persistent-volume-storage/) task to configure PersistentVolume (PV) and PersistentVolumeClaim (PVC). Then follow the steps from the beginning, to create a `hostpath` and `index.html` file as well.
Follow the steps in the [Configure a Pod to Use a PersistentVolume for Storage](/tasks/configure-pod-container/configure-persistent-volume-storage/)
task to create an `index.html` file, a PersistentVolume (PV), and PersistentVolumeClaim (PVC).


View information about the PersistentVolume:

```shell
kubectl get pv task-pv-volume
```

The output shows that the PersistentVolume has a `STATUS` of `Available`. This
means it has not yet been bound to a PersistentVolumeClaim.

```
NAME CAPACITY ACCESSMODES RECLAIMPOLICY STATUS CLAIM STORAGECLASS REASON AGE
task-pv-volume 10Gi RWO Retain Available manual 4s
```

After you create the PersistentVolumeClaim, the Kubernetes controlplane looks
for a PersistentVolume that satisfies the claim's requirements. If the controlplane finds a
suitable PersistentVolume with the same StorageClass, it binds the
claim to the volume.

Look again at the PersistentVolume:

```shell
kubectl get pv task-pv-volume
```

Now the output shows a `STATUS` of `Bound`.

```
NAME CAPACITY ACCESSMODES RECLAIMPOLICY STATUS CLAIM STORAGECLASS REASON AGE
task-pv-volume 10Gi RWO Retain Bound default/task-pv-claim manual 2m
```

Look at the PersistentVolumeClaim:

```shell
kubectl get pvc task-pv-claim
```

The output shows that the PersistentVolumeClaim is bound to your PersistentVolume,
`task-pv-volume`.

```
NAME STATUS VOLUME CAPACITY ACCESSMODES STORAGECLASS AGE
task-pv-claim Bound task-pv-volume 10Gi RWO manual 30s
```

## Create a Pod
The next step is to create a Pod that uses your PersistentVolumeClaim as a volume.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
The next step is to create a Pod that uses your PersistentVolumeClaim as a volume.
The next step is to create a Pod running an httpd server that uses your PersistentVolumeClaim as a volume.


Here is the configuration file for the Pod:

{{% code_sample file="pods/storage/pv-pod-backup.yaml" %}}

Notice that the Pod's configuration file specifies a PersistentVolumeClaim, but it does not specify a PersistentVolume. From the Pod's point of view, the claim is a volume.

Create the Pod:

```shell
kubectl apply -f https://k8s.io/examples/pods/storage/pv-pod-backup.yaml
```

Verify that the container in the Pod is running;

```shell
kubectl get pod pv-pod-backup
```

Get a shell to the container running in your Pod:

```shell
kubectl exec -it pv-pod-backup -- /bin/bash
```

In your shell, verify that httpd:2.4 is serving the `index.html` file from the
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not say, "the httpd server", rather than specifying it with the image tag so it's comprehensible?

Suggested change
In your shell, verify that httpd:2.4 is serving the `index.html` file from the
In your shell, verify that the httpd server is serving the `index.html` file from the

hostPath volume:

```shell
# Be sure to run these 3 commands inside the root shell that comes from
# running "kubectl exec" in the previous step
apt update
apt install curl
curl http://localhost/
```

The output shows the text that you wrote to the `index.html` file on the
hostPath volume:

```
Hello from Kubernetes storage
```

If you see that message, you have successfully configured a Pod to
use storage from a PersistentVolumeClaim.

## {{% heading "backup" %}}

### Create Backup

The process involves accessing the pod to interact with its file system, navigating to the mounted volume directory ```(/usr/local/apache2/htdocs)``` containing the data to be backed-up, and compressing the directory contents into a ```.tar.gz``` file stored in the pod's temporary directory ```(/tmp)```. After exiting the pod shell, the backup file is transferred to the local machine using ```kubectl cp```, ensuring the data is securely saved outside the pod environment for restoration or safekeeping.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The process involves accessing the pod to interact with its file system, navigating t
the mounted volume directory (/usr/local/apache2/htdocs) containing the data to be
backed up, and archive the directory contents using tar and gzip. You temporarily store the
archive within a pod's ephemeral storage.
After exiting the pod shell, the backup file is transferred to the local machine using kubectl cp,
ensuring the data is securely saved outside the pod environment for restoration or safekeeping.


Even better:
- at least mention [snapshots](https://kubernetes.io/docs/concepts/storage/volume-snapshots/)
- avoid `kubectl cp`
- make sure that `/tmp` is an `emptyDir` volume when you work with it.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Markdown supports a single backtick for one line of code. You can use triple backticks for a multi-line code.

Suggested change
The process involves accessing the pod to interact with its file system, navigating to the mounted volume directory ```(/usr/local/apache2/htdocs)``` containing the data to be backed-up, and compressing the directory contents into a ```.tar.gz``` file stored in the pod's temporary directory ```(/tmp)```. After exiting the pod shell, the backup file is transferred to the local machine using ```kubectl cp```, ensuring the data is securely saved outside the pod environment for restoration or safekeeping.
The process involves accessing the Pod to interact with its file system, navigating to the mounted volume directory
`(/usr/local/apache2/htdocs)` containing the data to be backed-up, and compressing the directory contents into a
`.tar.gz` file stored in the Pod's temporary directory `(/tmp)`. After exiting the Pod shell, the backup file is transferred to the local machine using `kubectl cp`, ensuring the data is securely saved outside the pod environment for restoration or safekeeping.


**Access the Pod:**
To start the backup process, first, access the pod that has the volume you want to back up:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
To start the backup process, first, access the pod that has the volume you want to back up:
To start the backup process, first, access the Pod that has the volume you want to back up:


```shell
kubectl exec -it pv-pod-backup -- /bin/bash
```

This command opens an interactive shell inside the specified pod (pv-pod-backup).
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
This command opens an interactive shell inside the specified pod (pv-pod-backup).
This command opens an interactive shell inside the specified Pod, `pv-pod-backup`.


**Navigate to the Mounted Volume Directory:**
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be a subheading

Once inside the pod, navigate to the directory where the volume is mounted:
```shell
cd /usr/local/apache2/htdocs
```
This directory contains the data you wish to backup.

**Create a Compressed Backup File (gzip):**
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be a subheading

Use the tar command to create a compressed backup file of the directory's contents:
```shell
tar -czvf /tmp/volume-backup.tar.gz .
```
**Exit the pod:**
After creating the backup file, exit the pod:
```
exit
```
**Copy the Backup to Local Machine:**
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be a subheading

Use ```kubectl cp``` to copy the backup file from the pod to your local system:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Use ```kubectl cp``` to copy the backup file from the pod to your local system:
Use ```kubectl cp``` to copy the backup file from the Pod to your local system:

```shell
kubectl cp pv-pod-backup:/tmp/volume-backup.tar.gz ./volume-backup.tar.gz
```
This saves the backup file ```volume-backup.tar.gz``` in your current local directory.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
This saves the backup file ```volume-backup.tar.gz``` in your current local directory.
This saves the backup file `volume-backup.tar.gz` in your current local directory.


### Delete the data from volume
To understand the backup use, The process involves accessing the pod to navigate to the mounted volume directory ```/usr/local/apache2/htdocs```, deleting all files and subdirectories within it using ```rm -rf *```, and verifying the deletion by listing the directory contents ```ls -l```. Finally, the pod shell is exited after confirming the directory is empty.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
To understand the backup use, The process involves accessing the pod to navigate to the mounted volume directory ```/usr/local/apache2/htdocs```, deleting all files and subdirectories within it using ```rm -rf *```, and verifying the deletion by listing the directory contents ```ls -l```. Finally, the pod shell is exited after confirming the directory is empty.
To understand the backup use, The process involves accessing the pod to navigate to the mounted volume
directory `/usr/local/apache2/htdocs`, deleting all files and subdirectories within it using `rm -rf *`, and verifying the
deletion by listing the directory contents `ls -l`. Finally, the Pod shell is exited after confirming the directory is empty.


**Access the Pod Again:**
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be a subheading

To delete the data from the volume, access the pod that uses the volume:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See the style-guide.

Suggested change
To delete the data from the volume, access the pod that uses the volume:
To delete the data from the volume, access the Pod that uses the volume:

```shell
kubectl exec -it task-pv-pod -- /bin/bash
```
**Navigate to the Mounted Volume Directory:**
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be a subheading

Inside the pod, go to the directory where the volume is mounted:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Inside the pod, go to the directory where the volume is mounted:
Inside the Pod, go to the directory where the volume is mounted:

```shell
cd /usr/local/apache2/htdocs
```
**Delete All Files:**
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be a subheading

Delete all files and subdirectories in the volume:
```shell
rm -rf *
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you make this safer to run?

```
**Verify the Deletion:**
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be a subheading

List the contents of the directory to confirm if it is empty:
```shell
ls -l
```
If the deletion was successful, no files or directories will be listed.

**Exit the Pod:**
After verifying the deletion, exit the pod:
```shell
exit
```

### Restore the Backup Data
The process involves copying the backup file (volume-backup.tar.gz) from the local machine to the pod's temporary directory using ```kubectl cp```. Then, access the pod shell, navigate to the mounted volume directory ```/usr/local/apache2/htdocs```, and extract the backup using ```tar```. Finally, verify the restored files with ```ls -l``` and exit the pod shell.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Long texts in one line are often separated into multiple lines.

Suggested change
The process involves copying the backup file (volume-backup.tar.gz) from the local machine to the pod's temporary directory using ```kubectl cp```. Then, access the pod shell, navigate to the mounted volume directory ```/usr/local/apache2/htdocs```, and extract the backup using ```tar```. Finally, verify the restored files with ```ls -l``` and exit the pod shell.
The process involves copying the backup file, `volume-backup.tar.gz`, from the local machine to the Pod's
temporary directory using `kubectl cp`. Then, accessing the pod's shell, navigating to the mounted volume directory
`/usr/local/apache2/htdocs`, and extracting the backup using `tar`. Finally, the restored files are verified using the `ls -l` command. The Pod can shell can be exited afterward.


**Copy the Backup File to the Pod:**
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be a subheading

Transfer the backup file from your local machine, back to the pod:
```shell
kubectl cp ./volume-backup.tar.gz pv-pod-backup:/tmp/volume-backup.tar.gz
```
This copies the backup file to the /tmp directory of the pv-pod-backup pod.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
This copies the backup file to the /tmp directory of the pv-pod-backup pod.
This copies the backup file to the `/tmp` directory of the `pv-pod-backup` Pod.


**Access the Pod:**
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be a subheading

Open an interactive shell inside the pod:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Open an interactive shell inside the pod:
Open an interactive shell inside the Pod:

```shell
kubectl exec -it task-pv-pod -- /bin/bash
```
**Extract the Backup:**
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be a subheading

Navigate to the mounted volume directory and extract the backup:
```shell
cd /usr/local/apache2/htdocs
tar -xzvf /tmp/volume-backup.tar.gz
```
**Verify the Restoration:**
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be a subheading

List the contents of the directory to confirm the files have been restored:
```shell
ls -l
```
If successful, you should see all the files and directories from the backup.

**Exit the Pod:**
```shell
exit
```
### Test the Restored Data
Now Check the status of the Pod ```pv-pod-backup``` Whether it is in ```Running``` state or not. If it is in ```Running``` state then restoring the volume worked perfectly fine.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Now Check the status of the Pod ```pv-pod-backup``` Whether it is in ```Running``` state or not. If it is in ```Running``` state then restoring the volume worked perfectly fine.
Now Check the status of the `pv-pod-backup` Pod to see whether it is in a `Running` state or not. If it is in a `Running` state, then restoring the volume worked perfectly fine.

```shell
kubectl get pod pv-pod-backup -o wide
```
Now You can again check the output by access the pods
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Now You can again check the output by access the pods
Now You can again check the output by accessing the Pods shell

```shell
kubectl exec -it pv-pod-backup -- /bin/bash
```
To run the restored file run
```shell
# Be sure to run these 3 commands inside the root shell that comes from
# running "kubectl exec" in the previous step
apt update
apt install curl
curl http://localhost/
```
Comment on lines +244 to +251
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're not running the file that has been restored.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The restored file in this context is index.html. To verify its contents, I am using the command:
curl http://localhost This displays the output stored within the file, ensuring that the restoration process was successful.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To run the restored file run

This is misleading or wrong.

## Understanding the importance of volume backups
Some key points about importance of volume backup are:
1. Backups keep your data safe if it's accidentally deleted, corrupted, or lost due to hardware failure.
1. If your system crashes or faces an attack, backups help restore data and reduce downtime.
1. Backups let you transfer data between environments or cloud platforms without losing it.
1. Losing data can stop your work. Backups help avoiding this.
1. Migrate data from development to testing or production environments.
1. Backups allow you to create a safe copy of your production data for Testing new features or bug fixes.
30 changes: 30 additions & 0 deletions content/en/examples/pods/storage/pv-pod-backup.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
apiVersion: v1
kind: Pod
metadata:
name: pv-pod-backup
spec:
volumes:
- name: task-pv-storage
persistentVolumeClaim:
claimName: task-pv-claim
initContainers:
- name: init-volume
image: httpd:2.4
command: ["/bin/bash", "-c"]
args:
- |
echo "Initializing the mounted volume...";
echo "Initialized at: $(date)" >> /usr/local/apache2/htdocs/index.html;

volumeMounts:
- mountPath: "/mnt/data"
name: task-pv-storage
containers:
- name: task-pv-container
image: httpd:2.4
ports:
- containerPort: 80
name: "http-server"
volumeMounts:
- mountPath: "/usr/local/apache2/htdocs"
name: task-pv-storage
3 changes: 3 additions & 0 deletions data/i18n/en/en.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ other = """<p>This page is automatically generated.</p><p>If you plan to report
[blog_post_show_more]
other = "Show More Posts..."

[backup_heading]
other = "Backup, Delete and Restore Volumes"

Comment on lines +18 to +20
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to change this file.

[banner_acknowledgement]
other = "Hide this notice"

Expand Down