Beruflich Dokumente
Kultur Dokumente
Summary
The cloud is rapidly becoming an essential tool that allows organizations, entrepreneurs
and scientists to innovate without a large, upfront cost for computing and storage
infrastructure. Anyone can start virtual machines (VMs) in the cloud and use them to
create the next critical business application, killer web application or simulation to
predict hurricanes. To support that innovation this tutorial is for Java developers looking
to control both a Eucalyptus cloud and the Amazon Elastic Compute Cloud and access
the virtual machines within them programmatically using Java APIs.
Objective
At the end of this tutorial you should be able to:
Overview
When developers want to harness the full capability of the cloud (in the IaaS sense of the
word) they need to be able to control it programmatically from within their applications.
When it comes to Java there are a number of options available to developers for choosing
an API that will allow them to control multiple cloud providers. There are:
typica
jclouds
Deltacloud
Dasein
libcloud
Our requirements for the CESWP project are to support a Eucalyptus cloud and the
1 of 9
Control the Cloud Programmatically with Java at CESWP Tech Blog
Amazon Elastic Compute Cloud (EC2). typica has initially been selected because it
supports both clouds out-of-the-box.
jclouds support for Eucalyptus is only in the nightly snapshot builds. A little too bleeding
edge for me and there is no documentation on using jclouds with Eucalyptus. Neither
Deltacloud nor Dasein explicitly support Eucalyptus. Although Eucalyptus claims to be
compatible with the EC2 API I’ve found that unless it’s documented, tested and officially
supported by the API then it probably won’t work at all. libcloud (a Java port of the
python libcloud) is still in the Apache incubator but it looks promising.
To this end I’ve begun a Java Cloud (Agnostic) API Survey that we may add to, if we
evaluate and experiment with other APIs.
What follows is a tutorial on using typica to control virtual machines and their associated
storage in the cloud. One final piece of the puzzle for controlling VMs in the cloud is an
SSH library to run commands directly on the VMs. For this I’ve used the excellent sshj.
The relevant versions used in this tutorial are:
typica 1.7.2
sshj 0.3.0
Eucalyptus 2.0.0
The code in this tutorial is not production ready. The code is only here to illustrate
control of the cloud. Most error handling has been omitted to make the code clearer and
is left as an exercise for the reader.
One or both of the accounts below will be need for the tutorial.
Eucalyptus
Cost: Free
Most people won’t have the time, resources or inclination to build their own
Eucalyptus cloud. Instead I suggest you apply for an account on the Eucalyptus
Community Cloud (ECC). Follow the instructions to get an account. It may take a
day or longer to get your account approved so start as early as possible. Once you
have your account follow the guide Getting Started Using Eucalyptus 2.0 guide.
Important! Make a note of the key name and where you saved the private key
when creating your key pair. Do not skip the euca-authorize step!
Note that you cannot create storage and attach it to the VM in ECC. It’s a limitation
Eucalyptus put into this particular cloud to prevent abuse because the accounts are
free.
2 of 9
Control the Cloud Programmatically with Java at CESWP Tech Blog
Cost: Get your credit card out (but prices are very low for short lived VMs like we’ll
be using)
Simply follow the directions in the Get Started with EC2 guide. Important! Make a
note of the key name and where you saved the private key when creating your
key pair. As part of this tutorial when you’re in the AWS Management Console click
on Security Groups. Choose default and in the window below add the Connection
Method SSH and click Save.
Tutorial
Get and Compile the Code
Eclipse
It’s done as an Eclipse project so for those of you using Eclipse the quickest way to get
started is:
1. Fire up Eclipse
2. File > New > Other > type “svn” > Checkout Projects from SVN > Next
3. Create a new repository location > Next
4. Select the URL > Finish
This will checkout and compile the code for you. Done.
Non-Eclipse
3 of 9
Control the Cloud Programmatically with Java at CESWP Tech Blog
To get and compile the source code run the following commands:
Configuration
Now we need to setup the configuration files that contain the cloud specific parameters
for Eucalyptus and EC2. Open both typica-tutorial/conf/aws.properties and typica-
tutorial/conf/euca.properties.
aws.properties
cloud.accessId – Look at your AWS Security Credentials and it’s the one labelled
Access Key ID
cloud.secretKey – Look at your AWS Security Credentials and it’s the one labelled
Secret Access Key
cloud.username – Look at your AWS Security Credentials and it’s the one labelled
AWS Account ID (near the bottom of the page). Important! Remove the hyphens!
launchConfig.imageID – The ID of the Amazon Machine Image (AMI) you want to
run. You can use ami-ff6c8596, it’s a CentOS 5.5 image. A good place to find more
images is the cloud market.
launchConfig.availabilityZone – The zone where your AMI will run. You can set this
to “us-east-1d” (no quotes).
launchConfig.keyName – The key name from when you got started with your
Amazon account in the Before You Start section.
launchConfig.minCount=1 – the minimum number of VMs to run. Leave it as 1.
launchConfig.maxCount=1 – the maximum number of VMs to run. Leave it as 1.
attachVolume.deviceName=/dev/sdf – This is the Linux device you’ll attached your
storage to. Leave it as /dev/sdf.
vmAuth.username – The username you’ll use to login to your VM. If you use the
suggest AMI above set this to “root” (no quotes). If it was an Ubuntu image you’d
most likely use “ubuntu”.
vmAuth.privateKeyLocation – The absolute path to the private key from when you
got started with your Amazon account in the Before You Start section.
vmAuth.sudo – Set this property to “sudo” (no quotes) if your username is anything
other than root.
euca.properties
cloud.accessId – Look at your ECC Credentials and it’s the one labelled Query ID
cloud.secretKey – Look at your ECC Credentials and it’s the one labelled Secret Key
cloud.URL – Set this property to “ecc.eucalyptus.com” (no quotes)
cloud.username – The username you used to sign up with ECC
4 of 9
Control the Cloud Programmatically with Java at CESWP Tech Blog
Note that the source and Javadoc of the primary dependencies typica and sshj are
included in typica-tutorial/lib/sources so you can see what’s going on under the hood.
TypicaTutorial.TypicaTutorial()
TypicaTutorial.start()
TypicaTutorial.describeImages()
TypicaTutorial.runInstance()
TypicaTutorial.createAttachAndMountVolume()
5 of 9
Control the Cloud Programmatically with Java at CESWP Tech Blog
doesn’t work on the Eucalyptus Community Cloud). Waits for the storage to be ready
and the calls Jec2.attachVolume() to attach that storage to the VM. Waits for the
storage to be attached and then runs the commands via SSH to make a filesystem
and mount the storage in the VM.
TypicaTutorial.doInterestingStuff()
This is where you come in. Right now the tutorial just runs a simple ping
command over SSH. Change the command and recompile! Experiment what you can
do with a complete virtual machine at your disposal. Run multiple commands over
during one connection like is done when the storage is mounted. For example, install
subversion (yum install -y subversion), download some code from the web, compile
and execute it.
TypicaTutorial.unmountDetachAndDeleteVolume()
First unmounts the storage so it can be detached without corrupting the storage
(even though were just going to delete it anyway). Calls Jec2.detachVolume() to
detach the storage from the VM and waits for it to detach. Calls Jec2.deleteVolume()
to delete the volume but doesn’t wait for it to be deleted because this can take a
while and we’re not dependant on the storage being deleted.
TypicaTutorial.terminateInstance()
Calls Jec2.terminateInstances to kill the running VM but doesn’t wait for it to die
because this can take a while and we’re not dependant on the VM being terminated.
One thing to note is that the call to terminate the instance goes through but it looks
like Eucalyptus returns a null value and typica chokes on it so we ignore the
Exception (see http://code.google.com/p/typica/issues/detail?id=105).
Note that it can take a long time for VMs to start running or for storage to be created so
be patient with the process.
Eclipse
6 of 9
Control the Cloud Programmatically with Java at CESWP Tech Blog
Non-Eclipse
1. cd typica-tutorial
2. For Eucalyptus: java -cp conf:bin:”lib/*” org.cybera.TypicaTutorial euca (on
Windows change the / to \ and : to ; )
3. For EC2: java -cp conf:bin:”lib/*” org.cybera.TypicaTutorial aws (on Windows
change the / to \ and : to ; )
References
typica Google Group
sshj Google Group
Eucalyptus support forum
Future Work
Building a library on top of typica for coarser grained functionality (e.g. a single method
to start a VM and attach and mount X GBs of storage).
Try different Java Cloud APIs and add our findings to Java Cloud (Agnostic) API Survey.
Experiment with Java APIs that control storage in the cloud (e.g. S3 on AWS and Walrus
on Eucalyptus) and write a similar tutorial for that.
Posted in Uncategorized
« Troubleshooting Eucalyptus
Eucalyptus User-data »
Leave a Reply
Name (required)
Website
7 of 9
Control the Cloud Programmatically with Java at CESWP Tech Blog
Submit Comment
Pages
About
Project Tools
Search
search
Blogroll
CESWP Blog
Archives
April 2011
January 2011
December 2010
October 2010
September 2010
June 2010
May 2010
April 2010
January 2010
December 2009
November 2009
Categories
Hypervisors
Sys Admin
Uncategorized
Meta
8 of 9
Control the Cloud Programmatically with Java at CESWP Tech Blog
Log in
Site Feed
Comments Feed
Back to top
9 of 9