Articles, Blog

Creating Custom Images for Google Compute Engine

November 4, 2019


KATHRYN HURLEY: Hi, everyone. My name is Kathryn Hurley. I’m a Developer Programs
Engineer at Google. And in this session, I’m going
to show you how to create custom images for your Google
Compute Engine instances. So when are custom
images useful? I find them useful when I’m
creating an application, and this application starts
instances on demand, and all these instances, they all need
the same packages and software installed on them. So I actually have two options
for installing the necessary packages and software
on my instances. I can use the start up script,
and in the start up script do an apt-get install to install
the packages that my application needs. Or I can use the custom image. The benefit of using a custom
image is that the boot time is a bit faster, since you don’t
have to wait for the package to install before the machine
does its work. A custom image also
gives you more control over the instance. When you use a start up script
to apt-get install package, there might be changes to that
package from time to time. With a custom image, you know
exactly what you’re getting. All right, so let’s
jump into a demo. In this demo, I’m going to
show you how to start an instance, install some software
on that instance, create an image of that
instance, and finally start a new instance using that image. I switched over to
my terminal now. I use the gcutil addinstance
command to start up a new instance called myinstance. And notice how I add a service
account scope flag with a value of storage full. This flag gives my instance full
access to Google Cloud Storage, which will be important
later when I transfer the image I create
to Cloud Storage. Now, at this point, I select
a zone, a machine type, and Compute Engine will start
up that instance. Now that the operation
is done, I use gcutil listinstances to list
out my instances. I have one instance,
myinstance, with a status of staging. Once the instance is running,
I can SSH into that machine. Looks like it’s still staging. Let’s try one more time. All right. Now that the instance is
running, I SSH into the instance using gcutil ssh and
provide the instance name. As you can see, this is
a Linux machine called myinstance that is running
Ubuntu 12.04, one of the default images in
Compute Engine. Let’s install OpenJDK on this
instance because I want to create a custom image
with Java available. I can then use this image when
I start up other instances that also require
Java installed. As you can see, it does take
some time for OpenJDK to install on this instance. Imagine running this
at the beginning of your start up script. It would take this long
before the machine could do any real work. So this is a good reason
to use custom images. Now that OpenJDK is installed
on my instance, I can make other changes such as adding
users, or downloading files. And all these changes would
be available on my image. However, I just need Java
installed, so I’m going to make the image now. To do so, I run the image bundle
program stored on all Compute Engine instances. This command saves the image
to the temp directory and saves the log output to a
file named abc dot log. Now that our image has been
created, let’s take a look at the output of the image
bundle program. I’ll cd to the temp directory. And here you can see the
abc log file and the tar image file. The tar image file needs
to be uploaded to Google Cloud Storage. Compute Engine loads the image
from Cloud Storage during instance startup. Before uploading the file to
Cloud Storage, I’m first going to create a bucket
on Cloud Storage. And this is the part where my
service account scope flag comes in handy. I don’t need to authenticate
to Cloud Storage. I can immediately start using
the gsutil command. I run gsutil mb command to
make the bucket, and I’m naming the bucket
gce-java-image. Once the bucket is created, I
copy the image to the bucket using the gsutil cp command. I’ll give the image
a name that’s much easier to remember. Let’s call it java.image.tar.gz. As you can see, the transfer
speed is amazingly fast. And it completes in
under a minute. I’m going to log out
of my instance now. And I want to add my new
image to the images available for my project. I use gcutil addimage
to do so. I provide a name
for the image. I’ll call it java. And I specify its location
on Cloud Storage. Once the operation is complete,
I can use gcutil listimages to see my
list of images. I want to now use my new image
when starting an instance and run a Java program
on that instance. I have a short hello world Java
program that prints out “Hello World!” You
can see it here. And the compiled Java code
is on Cloud Storage. I also created a little start up
script for my instance that downloads the compiled Java code
from Cloud Storage, and then runs the program. So let’s fire up this
new instance. I use the gcutil addinstance
command again. This time, I specify some
metadata, the start up script, the image– my Java image– and finally, service account
scope, which gives this instance access to download my
compiled Java hello world code from Cloud Storage. I choose the zone and the
machine type again. And Compute Engine starts
up this instance. When the operation is complete,
I list my instances. And you can see my new
Java instance in the list, and it’s staging. And I’ll try to SSH
into the machine. It looks like the instance is
still in staging, so I’m going to try again. It looks like it’s working
this time. Now, let’s take a look
at the start up logs of this instance. To do so, I print the tail of
the google.log file in war slash log directory. Here you can see that the
hello world class was downloaded from Cloud Storage,
and it was successfully run displaying the message
“Hello World!” If you’re interested in learning
more about Compute Engine images, I’ve provided
the link here. And this was, of course, a very
basic example of what you can do with custom images. There are so many more cool
things that can be done, and I really look forward to seeing
the awesome ideas that you come up with. Cheers.

9 Comments

  • Reply NodePoint August 29, 2012 at 4:49 am

    Since the command that has been used ro emulatea linux terminal, can Linux users do the same?

  • Reply Radu Ghelmeci August 29, 2012 at 9:33 am

    Hello World

  • Reply Radu Ghelmeci August 29, 2012 at 9:34 am

    Hello World

  • Reply davedave9 August 29, 2012 at 10:07 am

    ma tu chiar esti retardat…

  • Reply Dan Brickley October 27, 2012 at 11:04 pm

    Just FYI, I had to do an 'apt-get update' before the jdk installation.

  • Reply allyourcode November 4, 2012 at 1:09 am

    Hmm. I cannot run apt-get install without sudo, but what is my sudo password??

  • Reply allyourcode November 4, 2012 at 1:09 am

    Which command are you talking about? When you gcutil ssh, you are in a real Linux machine.

  • Reply allyourcode November 12, 2012 at 4:49 am

    Can you delete the image from Google Cloud Storage after you have added it via gcutil addimage?

  • Reply Ivan Rosa December 17, 2018 at 3:11 am

    what about a Windows 10 image?

  • Leave a Reply