This is an old revision of the document!
In this exercise, we will instantiate a virtual machine using the gcloud compute engine. This may not be as straightforward as you expect. The reason for this is that there are many aspects to consider. For example, in what datacenter do we want our instance to reside. Do we want a public IP address assigned to it? Looking at the gcloud compute instances create command, it may first appear intimidating. Let's take it step-by-step and discover what we need to create a VM. With each step, make sure to write down the parameters that you'll need later.
Google Cloud offers a number of services, none of which are enabled by default. One of them is the compute service (i.e.: compute.googleapis.com
) that lets us create VM instances. When running a command that requires a certain service that was not previously enabled, you get a prompt asking you if you want to enable it then and there. In this case, we'll do it manually. Note that this may take a bit of time, up to a couple of minutes.
# get full list of available services $ gcloud services list --available # enable the compute service $ gcloud services enable compute.googleapis.com
Once your VM is up and running, you will want to be able to SSH into it. For this to happen, you will have to configure a public SSH key. This key will be automatically copied into ~/.ssh/authorized_keys at creation. If you still don't have a SSH keypair generated, now's a good a time as any (see ssh-keygen).
# upload your public SSH key to gcloud # --ttl 0 means that the key does not have an expiration date $ gcloud compute os-login ssh-keys add --ttl 0 --key-file ~/.ssh/id_rsa.pub
First thing first. What OS do we want to run on our machine? A Windows server? Maybe CentOS? Nay – let's look for something familiar: Ubuntu.
# list available base VM images
$ gcloud compute images list
All cloud providers worth their salt will offer you a number of physical locations (datacenters) where to deploy your instance. Locality is very important when offering web services. Normally, this is a difficult task. Can you imagine YouTube running on a single server somewhere in the US and you accessing it from SEA? Many people use Content Delivery Networks (CDN) for this task. Even DigitalOcean, a rather important cloud provider uses CloudFlare as a proxy for their HTTP servers.
You can read up on Google's regions and zones. When working with your own funds and not with free tier accounts or education credits, you will want to consult their regional pricing model. Usually, US-based datacenters are much cheaper.
# show available regions $ gcloud compute regions list # show zones in selected region $ gcloud compute zones list --filter ${REGION}
When selecting the number of Virtual CPUs (vCPU) and RAM for your VM, you will have to choose from a list of presets. These presets may vary depending on the region.
# show available flavors for your selected zone $ gcloud compute machine-types list --zones "${YOUR_ZONE}"
Storage options refer to HDDs/SSDs and can be separated from the instance you create. Meaning that you can delete the instance and still keep your storage image intact, in case you may want to mount it to another instance in the future (think moving a USB stick between PCs). For this exercise, we won't dive into this specific use-case and delete the virtual storage device together with the instance (at the end of the lab). A 10G disk should be more than sufficient. Again, the storage device selection is specific to each zone.
# select storage type for selected zone $ gcloud compute disk-types list --zones "${YOUR_ZONE}"
Finally, it's time to start up our VM. In addition to all the parameters that you've selected until now, you will also have to give the instance a name.
# create the instance $ gcloud compute instances create ${INSTANCE_NAME} \ --image-family ${IMAGE_FAMILY} \ --image-project ${IMAGE_PROJECT} \ --zone ${ZONE} \ --machine-type ${FLAVOR} \ --boot-disk-type ${DISK_TYPE} \ --boot-disk-size ${DISK_SIZE} \ --metadata enable-oslogin=TRUE # show active instances $ gcloud compute instances list
Using the username that was generated for you (see Task B) and the Public (External) IP address of your VM, connect to it via SSH. The Ubuntu image comes with a pre-configured SSH server. But remember that after you created the instance, it still requires ~1m to boot and start up the services.
# connect to your instance $ ssh ${USERNAME}@${PUBLIC_IP}