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

Conversation

niranjandarshann
Copy link
Contributor

Description

A tutorial about how to do a simple backup of a volume, such as saving a gzip copy of the backing storage, delete the volume and restore it.

Issue

Document Backing Up Volume #43307

@k8s-ci-robot k8s-ci-robot added cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. language/en Issues or PRs related to English language labels Jan 15, 2025
@k8s-ci-robot k8s-ci-robot added the size/L Denotes a PR that changes 100-499 lines, ignoring generated files. label Jan 15, 2025
Copy link

netlify bot commented Jan 15, 2025

Pull request preview available for checking

Built without sensitive environment variables

Name Link
🔨 Latest commit 707f42d
🔍 Latest deploy log https://app.netlify.com/sites/kubernetes-io-main-staging/deploys/678a4e89d55f000008ec44ac
😎 Deploy Preview https://deploy-preview-49452--kubernetes-io-main-staging.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

@niranjandarshann
Copy link
Contributor Author

This PR resolve the issue Document backing Up Container Using Kubernetes . Mostly focused on the #43307 (comment) to do a simple backup of a volume, such as saving a gzip copy of the backing storage, delete the PersistentVolume and its storage, and then restore.

@niranjandarshann
Copy link
Contributor Author

Copy link
Contributor

@network-charles network-charles left a comment

Choose a reason for hiding this comment

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

A few suggestions for now.

@niranjandarshann
Copy link
Contributor Author

@network-charles updated with suggested change PTAL!

Copy link
Contributor

@sftim sftim left a comment

Choose a reason for hiding this comment

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

Thanks

Does the inline feedback make sense here?

@network-charles
Copy link
Contributor

@network-charles updated with suggested change PTAL!

Alright. I think you should mark the suggestions as resolved too.

@k8s-ci-robot k8s-ci-robot added the sig/docs Categorizes an issue or PR as relevant to SIG Docs. label Jan 17, 2025
@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by:
Once this PR has been reviewed and has the lgtm label, please assign nate-double-u for approval. For more information see the Code Review Process.

The full list of commands accepted by this bot can be found here.

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@niranjandarshann
Copy link
Contributor Author

Alright. I think you should mark the suggestions as resolved too.

Thank You @network-charles for the suggestion i marked the resolved one as resolve .

@sftim
Copy link
Contributor

sftim commented Jan 17, 2025

/retitle Add volume backup and restore tutorial

@k8s-ci-robot k8s-ci-robot changed the title Volume Backup and Restore Add volume backup and restore tutorial Jan 17, 2025
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.

Copy link
Contributor

@sftim sftim left a comment

Choose a reason for hiding this comment

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

I'm afraid that this needs a lot of work to become something that we could merge.

Overall, make sure that you understand how to manage Kubernetes storage and manually trigger snapshots; also that you know how to restore data from such a snapshot.

You should acquire or confirm this knowledge even if you explain a different approach, so that you can help readers understand a cloud native way to take a 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.


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

```
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

```
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

### 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.

**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

```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

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

Comment on lines +244 to +251
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/
```
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.

Comment on lines +18 to +20
[backup_heading]
other = "Backup, Delete and Restore Volumes"

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.

Comment on lines +10 to +26
## {{% 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/).
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?

Copy link
Contributor

@network-charles network-charles left a comment

Choose a reason for hiding this comment

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

New suggestions

1. Practical workflow for backup and restore

## Configure Persistent Volume and Persistent Volume Claim
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).

```

## 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.

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


### 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.

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.

```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.

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:**
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:

kubectl exec -it task-pv-pod -- /bin/bash
```
**Navigate to the Mounted Volume Directory:**
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:

This copies the backup file to the /tmp directory of the pv-pod-backup pod.

**Access the Pod:**
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:

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. language/en Issues or PRs related to English language sig/docs Categorizes an issue or PR as relevant to SIG Docs. size/L Denotes a PR that changes 100-499 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants