Creating and attaching a persistent disk
As we discussed in the previous sections, a persistent disk is the storage option that is associated with the VM instances in GCE, by default. This section will walk you through creating and attaching a persistent disk.
Let's say we have already created a VM instance called test-instance in the US zone central1-f. We now want to expand the storage that is available to this VM instance, which can be done by creating a new persistent disk. We will use the command line for this:
gcloud compute disks create test-disk --size=100GB --zone us-central1-f
- gcloud compute disks create is the command
- test-disk is the name of our disk
- The size is 100GB
- The zone is us-central1-f
The persistent disk that you set up has to be in the same zone as your instance. You can't create a persistent disk in another zone and attach it to an instance that lives in a completely different zone, because persistent disks have to be connected to the instance with really high-speed connections—and that is only possible in the same zone. Note that ideally when you are creating a persistent disk, there should be at least 200 GB in size for optimal read and write performance or you'll get a warning.
- Click on your test instance VM to edit your settings and let's attach this particular disk to your VM instance.
- Click on add item under Additional disks and, in the drop-down list, you will find test-disk, the standard persistent disk that we set up.
- Let us use the command line to attach this disk to our instance. The command line is especially preferable if this is a repeated operation. Use the gcloud compute instances attach-disk command to specify the instance and the disk you want attached and the instance and the zone where they both live and hit Enter:
gcloud compute instances attach-disk test-instance --disk test-disk --zone us-central1-f
- Once the command has returned, hit REFRESH on your web console and go into the test-instance to see whether the disk has indeed been attached. Note that test-disk is now attached to this VM instance.
- You can also SSH into your test instance to confirm that the disk is indeed part of the instance now. You can view all the hard disks that are associated with this instance by running ls -l /dev/disk/by-id. This will give you the list of hard disks attached. You will see an SCSI disk called Google persistent disk-1, which is the new disk that we just attached.
- Once the status of your disk creation is ready, the disk needs to be formatted.