lunedì 12 maggio 2014

JClouds APIs - HOW TO DEPLOY A VM ON OPENSTACK USING JCLOUDS

Today I want to wirte a short, but I hope clear, tutorial on how to use jclouds with Openstack.
I’m pretty sure this will be the first article of a series, explaining every time a new feauture. All these posts will be gather together at the end for an awesome article. 

Apache jclouds is an open source multi-cloud toolkit for the Java platform that gives you the freedom to create applications that are portable across clouds while giving you full control to use cloud-specific features (http://jclouds.apache.org/).

 This tutorial is focused on the Compute Service and on how to deploy a VM on openstack using jclouds. Before I deepen the specific argument , if you are asking if there any other APIs that can do the same operation the answer is YES! If you go to openstack page, you will find what you are looking for.

Let’s start with the tutorial: Requirements:

  •  Java Programming Skill 
  • A working openstack with credential and endpoints
  • An Imagae stored in glance

If you are planning to have a maven project (and that is a good thing) you can just add the jcloud dependency:
 <dependency>  
    <groupid>org.jclouds.driver</groupid>  
    <artifactid>jclouds-log4j</artifactid>  
    <version>1.6.0</version>  
   </dependency>  
   <dependency>  
    <groupid>org.apache.jclouds</groupid>  
    <artifactid>jclouds-core</artifactid>  
    <version>1.6.1-incubating</version>  
   </dependency>  
   <dependency>  
    <groupid>org.jclouds.driver</groupid>  
    <artifactid>jclouds-slf4j</artifactid>  
    <version>1.6.0</version>  
   </dependency>  
   <dependency>  
    <groupid>org.jclouds.api</groupid>  
    <artifactid>openstack-nova</artifactid>  
    <version>1.6.0</version>  
   </dependency>  
I used two classes to show how to deploy a VM on openstack using jclouds:

  • Deployer
  • Configuration
Deployer contains all the code to deploy the VM, instead Configuration all the information about the openstack connection.


Let's take a look to Configuration.java:
 public class Configuration {  
      public static final String KEYSTONE_AUTH_URL = "http://172.25.27.16:35357/v2.0";  
      public static final String KEYSTONE_USERNAME = "admin";  
      public static final String KEYSTONE_PASSWORD = "password";  
      public static final String KEYSTONE_ENDPOINT = "http://172.25.27.16:35357/v2.0";  
      public static final String TENANT_NAME = "admin";  
      public static final String NOVA_ENDPOINT = "http://172.25.27.16:5000/v2";  
      public static final String CEILOMETER_ENDPOINT = "";  
 }  


You have to change the value with your configuration.


Once the configuration is done you have to connect to openstack:

 ComputeServiceContext context = ContextBuilder.newBuilder("openstack-nova")
                  .endpoint(Configuration.NOVA_ENDPOINT)
                   .credentials(identity, credential)
            .modules(modules)
            .overrides(overrides)
         .buildView(ComputeServiceContext.class);


overrides is an empty Property Object, identity and password are string containing the openstack creadentials (tenant:admin and password). The builder specifies to create a ContextBuilder for openstack-nova!


Now you can get the ComputeService:
 ComputeService computeService = context.getComputeService();  

To instantiate a VM in openstack we have to create its template.
Jclouds provides a template builder to map the given template with all the different specifics of the supported clouds.

In the example, I deployed a VM with at least 512Mb of Ram and using the specific Image "RegionOne/f72885a5-e681-4219-b931-bb062d74c591". All the Images have to be specified using the approach <openstackZone>/<imageID>.

 TemplateBuilder tb = computeService.templateBuilder();  
        tb.minRam(Integer.parseInt("512"));  
        tb.imageId("RegionOne/f72885a5-e681-4219-b931-bb062d74c591");  
        NodeMetadata node1=null;  
           try {  
                node1 = getOnlyElement(computeService.createNodesInGroup(groupName, 1, tb.build()));  
           } catch (RunNodesException e) {  
                e.printStackTrace();  
           }  
        System.out.printf("<< node %s: %s%n", node1.getId(),  
         concat(node1.getPrivateAddresses(), node1.getPublicAddresses()));  

We should get the information about the VM if everything run well.

The Complete example is hosted at FilippoGaudenzi github. Inside Deloyer there are other operations that may help you to understand how to interact with the ComputeService interface.

Enjoy


Nessun commento:

Posta un commento