Google Cloud Persistent Disk Tutorial
Introduction to Persistent Disk
Persistent Disk is a durable and high-performance block storage service provided by Google Cloud. It is designed to store data that needs to be accessed frequently and consistently. Persistent Disks can be attached to virtual machine (VM) instances in Google Cloud, allowing them to store and access data even after the VM is stopped or restarted.
Types of Persistent Disks
Google Cloud offers several types of Persistent Disks:
- Standard Persistent Disk (pd-standard): Cost-effective and suitable for sequential read/write operations.
- SSD Persistent Disk (pd-ssd): High-performance option for I/O intensive applications.
- Balanced Persistent Disk (pd-balanced): A middle ground between standard and SSD disks, offering balanced performance and cost.
- Extreme Persistent Disk (pd-extreme): Provides the highest performance for latency-sensitive and high IOPS workloads.
Creating a Persistent Disk
To create a Persistent Disk in Google Cloud, follow these steps:
- Go to the Google Cloud Console.
- Navigate to the "Disks" section under the "Compute Engine" menu.
- Click on "Create Disk" and fill in the required details, such as disk name, type, size, and zone.
- Click "Create" to provision the Persistent Disk.
Example: Creating a Persistent Disk using gcloud CLI
NAME ZONE SIZE_GB TYPE STATUS example-disk us-central1-a 100 pd-standard READY
Attaching a Persistent Disk to a VM Instance
After creating a Persistent Disk, you can attach it to a VM instance:
- Go to the "VM instances" section under the "Compute Engine" menu in the Google Cloud Console.
- Click on the name of the VM instance you want to attach the disk to.
- Click on the "Edit" button at the top of the VM instance details page.
- Scroll down to the "Additional disks" section and click "Add item".
- Select the Persistent Disk you created from the list and click "Save".
Example: Attaching a Persistent Disk using gcloud CLI
NAME ZONE MACHINE_TYPE PREEMPTIBLE INTERNAL_IP EXTERNAL_IP STATUS example-instance us-central1-a n1-standard-1 10.128.0.2 35.202.123.456 RUNNING
Formatting and Mounting a Persistent Disk
Once the Persistent Disk is attached to a VM instance, you need to format and mount it:
- SSH into the VM instance using the Google Cloud Console or gcloud CLI.
- List the available disks to identify the newly attached disk:
sudo lsblk
- Create a filesystem on the disk (e.g., ext4):
sudo mkfs.ext4 -m 0 -F -E lazy_itable_init=0,lazy_journal_init=0,discard /dev/sdb
- Create a mount point and mount the disk:
sudo mkdir -p /mnt/disks/example-disk
sudo mount -o discard,defaults /dev/sdb /mnt/disks/example-disk - Update /etc/fstab to mount the disk automatically on reboot:
sudo nano /etc/fstab
/dev/sdb /mnt/disks/example-disk ext4 discard,defaults 0 2
Resizing a Persistent Disk
To resize a Persistent Disk, follow these steps:
- Go to the "Disks" section under the "Compute Engine" menu in the Google Cloud Console.
- Click on the name of the disk you want to resize.
- Click on the "Edit" button at the top of the disk details page.
- Enter the new size for the disk and click "Save".
After resizing the disk, you need to resize the filesystem on the VM instance:
- SSH into the VM instance.
- Resize the partition using the
resize2fs
command (for ext4 filesystem):sudo resize2fs /dev/sdb
Example: Resizing a Persistent Disk using gcloud CLI
NAME ZONE SIZE_GB TYPE STATUS example-disk us-central1-a 200 pd-standard READY
Backing Up and Restoring Persistent Disks
To back up a Persistent Disk, you can create snapshots:
- Go to the "Snapshots" section under the "Compute Engine" menu in the Google Cloud Console.
- Click on "Create Snapshot".
- Select the disk you want to back up and fill in the required details.
- Click "Create" to create the snapshot.
To restore a Persistent Disk from a snapshot:
- Go to the "Disks" section under the "Compute Engine" menu in the Google Cloud Console.
- Click on "Create Disk".
- Select "Snapshot" as the source and choose the snapshot you want to restore from.
- Fill in the required details and click "Create".
Example: Creating a Snapshot using gcloud CLI
NAME DISK_NAME DISK_SIZE_GB STATUS example-snapshot example-disk 200 READY
Conclusion
In this tutorial, we covered the basics of Persistent Disks in Google Cloud, including creating, attaching, formatting, mounting, resizing, and backing up disks. Persistent Disks provide a reliable and scalable storage solution for your applications running on Google Cloud.