Swiftorial Logo
Home
Swift Lessons
Tutorials
Learn More
Career
Resources

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:

  1. Go to the Google Cloud Console.
  2. Navigate to the "Disks" section under the "Compute Engine" menu.
  3. Click on "Create Disk" and fill in the required details, such as disk name, type, size, and zone.
  4. Click "Create" to provision the Persistent Disk.

Example: Creating a Persistent Disk using gcloud CLI

gcloud compute disks create example-disk --size=100GB --type=pd-standard --zone=us-central1-a
Output:
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:

  1. Go to the "VM instances" section under the "Compute Engine" menu in the Google Cloud Console.
  2. Click on the name of the VM instance you want to attach the disk to.
  3. Click on the "Edit" button at the top of the VM instance details page.
  4. Scroll down to the "Additional disks" section and click "Add item".
  5. Select the Persistent Disk you created from the list and click "Save".

Example: Attaching a Persistent Disk using gcloud CLI

gcloud compute instances attach-disk example-instance --disk=example-disk --zone=us-central1-a
Output:
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:

  1. SSH into the VM instance using the Google Cloud Console or gcloud CLI.
  2. List the available disks to identify the newly attached disk:
    sudo lsblk
  3. 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
  4. 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
  5. 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:

  1. Go to the "Disks" section under the "Compute Engine" menu in the Google Cloud Console.
  2. Click on the name of the disk you want to resize.
  3. Click on the "Edit" button at the top of the disk details page.
  4. Enter the new size for the disk and click "Save".

After resizing the disk, you need to resize the filesystem on the VM instance:

  1. SSH into the VM instance.
  2. Resize the partition using the resize2fs command (for ext4 filesystem):
    sudo resize2fs /dev/sdb

Example: Resizing a Persistent Disk using gcloud CLI

gcloud compute disks resize example-disk --size=200GB --zone=us-central1-a
Output:
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:

  1. Go to the "Snapshots" section under the "Compute Engine" menu in the Google Cloud Console.
  2. Click on "Create Snapshot".
  3. Select the disk you want to back up and fill in the required details.
  4. Click "Create" to create the snapshot.

To restore a Persistent Disk from a snapshot:

  1. Go to the "Disks" section under the "Compute Engine" menu in the Google Cloud Console.
  2. Click on "Create Disk".
  3. Select "Snapshot" as the source and choose the snapshot you want to restore from.
  4. Fill in the required details and click "Create".

Example: Creating a Snapshot using gcloud CLI

gcloud compute disks snapshot example-disk --snapshot-names=example-snapshot --zone=us-central1-a
Output:
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.