Attaching Persistent Disk to an Instance on Google Cloud Platform (GCP)
Introduction:
Google Cloud Platform (GCP) offers a flexible and scalable infrastructure for deploying applications and services. As workloads grow, data persistence becomes critical to ensure data availability and integrity. Persistent disks in GCP provide reliable, durable, and high-performance storage that can be attached to virtual machine instances. In this blog, we will explore how to attach a persistent disk to an instance on GCP, using both the GCP Console and the Command-Line Interface (CLI), and understand the significance of using persistent disks.
Why Do We Need Persistent Disks?
When working with virtual machines, the underlying storage is ephemeral, meaning the data is lost once the instance is terminated or moved. To retain data across instance restarts or migrations, persistent disks come into play. They provide a reliable and independent storage solution that decouples data from the compute resources, allowing for easy data management and data durability.
Benefits of Using Persistent Disks:
- Data Durability: Persistent disks are designed to provide high durability, ensuring your data remains safe even in the face of hardware failures or other disruptions.
- Data Persistence: Your data is retained across instance restarts, so you don’t need to worry about losing valuable information during instance maintenance or migrations.
- High Performance: Persistent disks deliver consistent and predictable performance, enabling your applications to run efficiently.
- Independent Management: You can attach and detach persistent disks from instances as needed, giving you the flexibility to move your data between instances or resize disks without affecting your application.
Step-by-Step Guide: Attaching a Persistent Disk to an Instance using GCP GUI Console and with CLI as well.
Prerequisites:
- A GCP account with a project set up.
- An instance already created in the project.
Using GCP Console:
Step 1: Create a Persistent Disk
- Navigate to the GCP Console (https://console.cloud.google.com/).
- Click on “Compute Engine” > “Disks” in the left-side menu.
- Click on the “Create Disk” button.
- Provide a name for the disk, select the size and type (SSD or HDD), and choose the region and zone for data storage.
- Click on the “Create” button to create the persistent disk.
Step 2: Attach the Persistent Disk to an Instance
- Navigate to “Compute Engine” > “VM instances” in the left-side menu.
- Locate the instance you want to attach the disk to and click on the “Edit” button (pencil icon).
- In the “Additional disks” section, click on the “Add new disk” button.
- Select the persistent disk you created from the dropdown menu.
- Choose the “Device name” for the disk (e.g.,
/dev/sdb
) and click on the "Save" button.
Step 3: Mount the Disk and Format it (Linux Instance)
- SSH into the instance using the GCP Console or a terminal.
- List the available disks using the command:
lsblk
. - Identify the newly attached disk (usually
/dev/sdb
) and format it using a file system (e.g., ext4):sudo mkfs.ext4 /dev/sdb
. - Create a mount point for the disk:
sudo mkdir /mnt/data
. - Mount the disk to the mount point:
sudo mount /dev/sdb /mnt/data
. - Make the mount persistent across reboots by adding an entry in
/etc/fstab
:/dev/sdb /mnt/data ext4 defaults 0 0
.
Step 4: Verify the Disk Attachment
Use the df -h
command to verify that the new disk is mounted and available for use.
Using GCP CLI:
Step 1: Create a Persistent Disk
gcloud compute disks create [DISK_NAME] — size=[DISK_SIZE] — type=[DISK_TYPE] — zone=[ZONE]
Step 2: Attach the Persistent Disk to an Instance
gcloud compute instances attach-disk [INSTANCE_NAME] — disk=[DISK_NAME] — device-name=[DEVICE_NAME] — zone=[ZONE]
Step 3: Mount the Disk and Format it (Linux Instance)
SSH into the instance and execute the following commands:
>lsblk # List available disks and identify the newly attached disk (e.g., /dev/sdb).
>sudo mkfs.ext4 /dev/sdb # Format the disk with ext4 file system.
>sudo mkdir /mnt/data # Create a mount point.
>sudo mount /dev/sdb /mnt/data # Mount the disk.
>sudo bash -c ‘echo “/dev/sdb /mnt/data ext4 defaults 0 0” >> /etc/fstab’ # Make the mount persistent across reboots.
Step 4: Verify the Disk Attachment
>df -h