Manage cloud infrastructures using Zend Framework

Recently, the Zend Framework community has developed a new component for cloud services: Zend_Cloud_Infrastructure. This component was provided to manage cloud infrastructures, with a common interface for multiple vendors. In this article we will present the basic usage of this new component with some examples and use cases.

Background

“The cloud is for everyone. The cloud is a democracy.”
Marc Benioff (CEO, Salesforce.com)

The cloud business is growing very fast, cloud is everywhere and web applications are using these services more and more frequently. In order to manage cloud services, PHP developers need to use specific API provided by the vendors. That means spend time and energy to become familiar with different PHP libraries. Few years ago, Zend Technologies with the contributions of other companies like IBM, Microsoft, Rackspace, etc, started a new initiative, the Simple Cloud API to produce a general PHP component to manage different APIs, exposing a single interface. The main scope of the Simple Cloud API iniziative is to unlock application from the effect of vendor lock-in, that is make a customer dependent on a vendor for products and services. Using the Simple Cloud API, PHP developers are free to choose the cloud vendors using an unique API to access the cloud services.

Starting from 2 November 2010 the Simple Cloud API has been included in the Zend Framework 1 project, under the Zend_Cloud namespace. This component of ZF implements three generic cloud services, that are:

  • Document (to manage a key/value database);
  • Storage (to manage a store system);
  • Queue (to manage a queue system).

The vendors supported, for these components, are reported in the following table:

  Document Queue Storage
Amazon Web Service x (SimpleDB) x (SQS) x (S3)
Windows Azure x x x
Nirvanix     x

Recently, cloud vendors have relased also infrastructure services and we decided to extend the Zend_Cloud component with the Zend_Cloud_Infrastructure. This new component is able to manage cloud infrastructure based on instances (servers) with specific actions: start/stop/restart an instance, monitoring an instance, run remote commands on an instance (deploy), etc.
So far the Zend_Cloud_Infrastructure supports the follwing adapters: Amazon EC2, and Rackspace. We (the Zend Framework team) are working on the Windows Azure and GoGrid adapters. Moreover, because we released the Rackspace adapter, that is based on OpenStack, we are going also to release the OpenStack infrastructure adapter very soon.

Architecture diagram of Zend_Cloud

If you are using Zend Framework 1 you can start to use the Zend_Cloud_Infrastructure by downloading the latest SVN from trunk (http://framework.zend.com/svn/framework/standard/trunk), and it will be available in a public release starting with 1.12.0, which we plan to ship in a couple months.
If you want to use the ZendCloudInfrastructure for Zend Framework 2 you can download the beta1 release (click here to download).
We provided a packaging system for ZF2, that means you can install just the ZendCloudInfrastructure package if you want. Check the packages.zendframework.com web site to discover how to do that.

Zend_Cloud_Infrastructure

In order to provide a common API for different cloud vendors the Zend_Cloud_Infrastructure abstracts a common set of basic operations for the management of instances (servers).
These operations are:

  • create a new instance;
  • delete a new instance;
  • start/stop an instance;
  • reboot an instance;
  • list of the available instances;
  • get the status of an instance;
  • wait for a status change of an instance;
  • get the public IP or DNS name of the instance;
  • list all the available images for new instances;
  • list all the available geographical zones for new instances;
  • monitor an instance getting the systems information (CPU%, RAM%, DISK%, NETWORK% usage);
  • deploy of an instance (run arbitrary shell script on an instance).

This infrastructure class uses a factory design pattern to manage different cloud vendors (adapters). That means in order to use this class you have to initialize the factory choosing a specific adapters. For instance, to work with the Amazon EC2 adapter you have to initialize the class with following parameters:

Instead, if you are using Rackspace you have to use the following parameters:

After the construct of the $infrastructure object we can start to use it. For instance, if we are using Rackspace, we can create a new instance (server) using the following code:

The $data array depends on the vendor, in the case of Rackspace we have to specify the image of the instance ($data[‘imageId’]), typically the operating system and the flavor ($data[‘flavorId’]), an available hardware configuration for a server. Each flavor has a unique combination of disk space and memory capacity. This parameters are specified using IDs.
We can also add some metadata information to the instance using an associative arrays of keys and values ($data[‘metadata’]).

To get all the attributes of an instance we can use the getAttributes() method. For instance, to retrieve the admin’s password of a Rackspace’s instance we can read the attribute ‘adminPass’ after the createInstance operation.

To start, stop and reboot a server we can use the commands startInstance($id), stopInstance($id), rebootInstance($id), where $id is the id of the instance.
You can check the status of the instance using the statusInstance($id). The Zend_Cloud_Infrastructure provides a method to wait for the status change of an instance, this function is:

where $id is the ID of the instance, $status is the status to wait for and $timeout is the optional time of waiting (in seconds). If the instance changes the status in $status, in maximum $timeout seconds, the waitStatusIstance() returns true, false otherwise. For instance, imagine to reboot an instance and wait for the READY status:

The Zend_Cloud_Infrastructure provides also two special methods to monitoring the usage of instance’s resources and to deploy on an instance (that means execute remote commands on an instance, this feature uses the SSH2 extension of PHP and is available only for GNU/Linux operating systems).
The resources of an instances that the Infrastructure class manages are: CPU, RAM, DISK READ/WRITE, and NETWORK IN/OUT. This monitor features are strictly related to the API exposed by the cloud vendor (for instance, the Amazon AWS provides the CloudWatch services to monitor EC2 instances, and not only). Nonetheless the Zend_Cloud_Infrastructure can retrieve some basic information of the resource usage of an instance even if the cloud vendors doesn’t expose it. For instance, the Rackspace Servers API do not offer a special API to monitor an instance but the Zend_Cloud_Infrastructures does. This extra features is provided using the deploy features of the Infrastrucutre running remote shell commands to retrieve resources usage like the commands: top, df –total, etc (Note: this monitor feature is available only for GNU/Linux instances).

For instance, to get the CPU usage of an instance we can use the following code:

The $cpuUsage is an associative array with the following structure:

where ‘series’ is an array contains the sample of CPU usage (%), got in different timestamps, and ‘average’ is the average of these values.

To execute remote commands (deploy) on an instance we can use the deploy() method. Here, there is an example:

This example retrieves the list of the files stored in the default web root of an instance, using the command:

.
The remote command is specified in the $cmd variable. If you need to execute multiple commands you can also specify an array of commands.
For instance, the following script install a LAMP stack to an Ubuntu instance and generate a PHP test script on the default document root:

More info about the Zend_Cloud_Infrastructure can be found in the simplecloud.org web site (click here) or in the documentation page of Zend Framework.

Conclusion

In this article we just introduced the main features of the new Infrastructure component of Zend_Cloud. Because of the nature of the cloud, this component is very open and “elastic”, that means we would like to extend it in the future according with the new API coming from the vendors and with your feedbacks, coming from the community. In order to get more feedbacks about the cloud components of the Zend Framework project we created a new mailing list, that is zf-cloud@lists.zend.com (to subscribe send an empty email to zf-cloud-subscribe@lists.zend.com). Join us to know more about PHP and cloud!