Chapter 9. Compute Engine: virtual machines
This chapter covers
- What are virtual machines (VMs)?
- Using persistent storage with virtual machines
- How auto-scaling works
- Spreading traffic across multiple machines with a load balancer
- Compute Engine’s pricing structure
virtual machines are chopped-up pieces of a single physical system that are shared between several people.
decouple the virtual machine from the physical machine, so physical machines can be taken offline for maintenance while the virtual machine is live-migrated elsewhere, all without any downtime or significant changes in performance.
automatic scaling, where the hosting provider can automatically provision more or fewer virtual machines based on incoming traffic or CPU usage,
performance characteristics (particularly those that might seem counterintuitive), and walk you through the more advanced features (like automatic scaling).
Google Compute Engine (GCE) is an enormous system with almost 40 API resources
most common and useful things you can do with GCE
all you need is a simple VM that you can SSH into and run a server or two,
customize your virtual machines or balance server requests across a set of many machines
tarting with a simplified version of the scary diagram that’s much easier to digest (


disk storage makes up the base of your instance
9.2. Block storage with Persistent Disks
a persistent disk in varying sizes (for example, 100 GB or 1 TB)
storage service that looked and acted like a regular disk but was replicated and highly available.
Persistent Disk
create and manage disks separately from VMs
At any time, a persistent disk can be in one of three states:
- Unattached—You’ve created the disk, but it’s not mounted on any VMs.
- Attached in read-only mode—The VM can only read from the disk.
- Attached in read-write mode—The VM can both read and write to the disk.
attached–read-write state because that’s been the default when creating a VM.
the default value when creating a disk in GCE is the unattached state

attach disks to a VM in two different modes (read-only and read-write)
create a disk and take it through the different states.
Compute Engine section and choose Disks in the left-side navigation
Hit Create Disk
to be attached to an instance, a disk must live in the same zone as that instance
choose a disk type,
standard disks acting a lot like traditional hard drives and SSD disks acting like solid-state drives.
SSDs have much faster random operations, and traditional drives are adequate for large sequential operations.
Cost vs. Performance
create an empty disk resource, and choose any size you want for the disk; for example, 500 GB.
Input/output operations per second (IOPS, pronounced eye-ops) is an input/output performance measurement used to characterize computer storage devices like hard disk drives (HDD), solid state drives (SSD), and storage area networks (SAN). Like benchmarks, IOPS numbers published by storage device manufacturers do not directly relate to real-world application performance
disk size and performance are directly related, such that larger disks can handle more input/output operations per second (IOPS).
applications that don’t store a lot of data but have heavy access patterns (lots of reads and writes) will provision a larger disk, not for the storage capacity but for the performance characteristics.
you enter a size (in GB), you can see the estimated performance below the field.
when you create an instance, GCE also has to create a disk,
disk is in the unattached state.
'Cloud Console and look at instance-1. If you scroll down the page a bit, you’ll see a section called Additional Disks,
Edit the VM instance
Attach existing disk
choose your new disk (disk-1) from the list, but be certain you choose to attach it in Read Only mode!
disk is in the attached–read-only state, which means that it can continue to be attached to other VMs,
attach that same disk to instance-2, again in read-only mode. This time, you can do so using the attach-disk subcommand.
Cloud Console and look at your list of disks, you’ll see that disk-1 is in use by both instance-1 and instance-2.
9.2.3. Using your disks
plugging your external hard drive into the VM.
mount the disk device and then format it.
mount command as well as by calling the mkfs.ext4 shortcut to format the disk with the ext4 file system.
get your disk into read-write mode, which you can do by detaching the disk from both instances and then reattaching the disk to instance-1 in read-write mode:
arrow over pencil and use can to remove disk
john_iacovacci1@gcelab:~$ ls -l /dev/disk/by-id total 0 lrwxrwxrwx 1 root root 9 Sep 22 21:26 google-gcelab -> ../../sda lrwxrwxrwx 1 root root 10 Sep 22 21:26 google-gcelab-part1 -> ../../sda1 lrwxrwxrwx 1 root root 9 Sep 22 21:26 scsi-0Google_PersistentDisk_gcelab -> ../../sda lrwxrwxrwx 1 root root 10 Sep 22 21:26 scsi-0Google_PersistentDisk_gcelab-part1 -> ../../sda1 john_iacovacci1@gcelab:~$
formatting the disk using its device ID (/dev/disk/by-id/google-disk-1)
re-attach as read/write
Once you have disk-1 attached to instance-1 only, and in read-write mode, SSH into instance-1 and look at the disks
formatting the disk using its device ID (/dev/disk/by-id/google-disk-1)
9.2.4. Resizing disks
resize a disk for a variety of reasons
running out of space
size of the disk directly correlates to its speed: the bigger the disk, the faster it is
increase the size of the virtual disk in the Cloud Console
Edit on the disk and then typing in the new size
you can always make a disk larger by increasing the size, but you can’t make a disk smaller.
resize your file system
resize2fs command on an unmounted disk:
jjg@instance-1:~$ sudoumount /mnt/disks/disk-1/
jjg@instance-1:~$ sudo e2fsck -f /dev/disk/by-id/google-disk-1
e2fsck 1.42.12 (29-Aug-2014)
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
/dev/disk/by-id/google-disk-1: 13/36044800 files (0.0%
non-contiguous), 2312826/144179200 blocks
jjg@instance-1:~$ sudo resize2fs /dev/disk/by-id/google-disk-1
resize2fs 1.42.12 (29-Aug-2014)
Resizing the filesystem on /dev/disk/by-id/google-disk-1 to
157286400 (4k) blocks.
The filesystem on /dev/disk/by-id/google-disk-1 is now 157286400
(4k) blocks long.
remount the disk,
jjg@instance-1:~$ sudo mount -o discard,defaults
/dev/disk/by-id/google-disk-1 /mnt/disks/disk-1
jjg@instance-1:~$ df -h | grep disk-1
/dev/sdb 591G 70M 561G 1% /mnt/disks/disk-1
freeze your computer at a point in time and be able to jump right to that checkpoint?
act as those checkpoints for the data on your disk,
allowing you to jump around in time by restoring a snapshot to a disk
end up costing you much less than a full backup.
snapshots use differential storage
storing only what’s changed from one snapshot to the next.
create a snapshot, change one block of data
second snapshot will only store the difference
create and delete them at any time
once you have a snapshot of a disk, you can create a new disk based on the content from the snapshot.
1. Start with disk-1 attached to your instance.
2. Create a snapshot (snapshot-1) from disk-1.
3. Change some data on the mounted copy of disk-1.
4. Create a new instance based on your snapshot and mount it to your instance.

wo versions of disk-1 attached to the VM
One of the disks will be the old version reflecting the snapshot from step 1, and the other the current version with the data you modified in step 2.
jjg@instance-1:~$ cd /mnt/disks/disk-1/workspace/
jjg@instance-1:/mnt/disks/disk-1/workspace$ echo "This is changed
after the snapshot!" > test.txt
jjg@instance-1:/mnt/disks/disk-1/workspace$ cat test.txt
This is changed after the snapshot!
you can mount it to your machine like any other disk.
create a disk instance from your snapshot
you can mount it to your machine like any other disk.
navigating to the list of disks and choosing Create Disk
choose Snapshot as the source type, then choose disk-1 as your source snapshot.
you can attach that disk to your VM
$ gcloud compute instances attach-disk instance-1
--zone us-central1-a --disk disk-1-from-snapshot
Updated [https://www.googleapis.com/compute/v1/projects/
your-project-id-here/zones/us-central1-a/instances/instance-1].
$ gcloud compute ssh --zone us-central1-a instance-1
# ...
Last login: Mon Sep 5 19:46:06 2016 from 104.132.34.72
jjg@instance-1:~$ sudomkdir -p /mnt/disks/disk-1-from-snapshot
jjg@instance-1:~$ sudo mount -o discard,defaults
/dev/disk/by-id/google-disk-1-from-snapshot
/mnt/disks/disk-1-from-snapshot
both disks mounted to the same machine, where disk-1-from-snapshot holds the data you had before you modified it, and disk-1 holds the data from afterward.
jjg@instance-1:~$ cat /mnt/disks/disk-1-from-snapshot/
workspace/test.txt
This is a test
jjg@instance-1:~$ cat /mnt/disks/disk-1/workspace/test.txt
This is changed after the snapshot!
Snapshot consistency
if you’re writing to your disk, and you take a snapshot in between two important disk operations?
fundamentally low-level in that the problem arises because computers tend to cache things in memory instead of always writing the data to your hard drive.
tell your virtual machine to flush any data that’s stored in memory but not yet on the disk.
to avoid a potentially disastrous snapshot, you should shut down your applications that are writing data
flush your disk buffers (using the sync command), freeze the disk (using fsfreeze)
jjg@instance-1:~$
jjg@instance-1:~$ sudo sync
jjg@instance-1:~$ sudofsfreeze -f /mnt/disks/disk-1
jjg@instance-1:~$
jjg@instance-1:~$ sudofsfreeze -u /mnt/disks/disk-1
any attempts to write to the disk will wait until the disk is unfrozen.
Snapshots can protect your data over time
9.2.6. Images
Images are similar to snapshots in that both can be used as the source of content when you create a new disk.
images are meant as starting templates for your disk,
snapshots are meant as a form of backup to pinpoint your disk’s content at a particular time.
create a new VM from a base operating system, you’re using an image under the hood.
an image doesn’t rely on differential storage like snapshots do,
create a disk that’s larger than you need for storage if you want to meet performance requirements.
several classes of persistent disk (Standard, SSD, and Local SSD) are available, each with slightly different performance characteristics
An SSD (solid-state drive) is a type of nonvolatile storage media that stores persistent data on solid-state flash memory. ... Unlike a hard disk drive (HDD), an SSD has no moving parts to break or spin up or down. A traditional HDD consists of a spinning disk with a read/write head on a mechanical arm called an actuator.
Local SSD disks provide the most performance
SSD and Standard disks have two performance profiles,
Standard disks are great if you need lots of space and don’t need super-high performance
SSDs are great for super-fast reads and writes.
storing data in the cloud brings different risks than storing data locally on your home computer
unauthorized access to your data v
prevent such a scenario, Google encrypts the data stored on your disks,
copy the bytes directly, they’d be useless without the encryption keys.
Google comes up with its own random encryption key for your disk and stores that in a secure place with access logged,
you can elect to keep these keys for yourself and give Google the key only when you need to decrypt the disk (such as when you first attach it to a VM).
creating an encrypted disk where you manage the keys yourself.
you’ll use /dev/urandom in Linux combined with the tr command to put a random chunk of bytes into a file called key.bin.
you can decide either to use RSA encryption to wrap your key or leave the key as is.
wrapping a key involves encrypting it with a public encryption key so that it can only be decrypted by the corresponding private key.
ensure your secret is only able to be decrypted by Google Cloud Platform systems.
$ base64 key.bin
ZSqykgCqS0FG+dkpBslgv2lwL9nIgNFKQbN8C09N1vk=
create a disk like you usually would, but choose Customer Supplied from the Encryption drop-down and leave the box for Wrapped Key unchecked (because you’re leaving the key as plain text)
9.3. Instance groups and dynamic resources
autoscaling, I mean the ability to expand or contract the number of VMs running to handle requests based on how much traffic is being sent to them,

experiences a request load that varies over the course of the day
start of the day, the system sees around 1,000 queries every second, growing quickly until about noon, and it only slows as it approaches 3,000 queries per second.
he perfect world, where your system grows and shrinks to meet your demands, GCE’s setup can use the concept of autoscaling.

you might not be able to get as close as you’d like to the line, like the one in figure 9.21, but you can get much closer than the block shape shown in figure 9.20, where a specific number of machines is always on regardless of the traffic sent to them
you teach GCE how to turn on instances configured to your liking.
monitor the overall CPU usage of the currently running instances and decide whether to turn on more,
two new concepts: instance groups and instance templates.
Instance templates are like the recipe for your instances
contain all of the information needed to turn on a new VM instance
instance group acts as a container for these managed instances
Great Article Keep on sharing
ReplyDeleteGCP Training Online
Online GCP Training