Over a million developers have joined DZone.

Windows Azure and scaling: how? (PHP)

· Cloud Zone

Build fast, scale big with MongoDB Atlas, a hosted service for the leading NoSQL database on AWS. Try it now! Brought to you in partnership with MongoDB.

One of the key ideas behind cloud computing is the concept of scaling.Talking to customers and cloud enthusiasts, many people seem to be unaware about the fact that there is great opportunity in scaling, even for small applications. In this blog post series, I will talk about the following:

Creating and uploading a management certificate

In order to keep things DRY (Don’t Repeat Yourself), I’ll just link you to the previous post (Windows Azure and scaling: how? (.NET)) for this one.

For PHP however, you’ll be needing a .pem certificate. Again, for the lazy, here’s mine (management.pfx (4.05 kb), management.cer (1.18 kb) and management.pem (5.11 kb)). If you want to create one yourself, check this site where you can convert and generate certificates.

Building a small command-line scaling tool (in PHP)

In order to be able to scale automatically, let’s build a small command-line tool in PHP. The idea is that you will be able to run the following command on a console to scale to 4 instances:

1 php autoscale.php "management.cer" "subscription-id0" "service-name" "role-name" "production" 4 

Or down to 2 instances:

1 php autoscale.php "management.cer" "subscription-id" "service-name" "role-name" "production" 2

Will this work on Linux? Yup! Will this work on Windows? Yup! Now let’s get started.

The Windows Azure SDK for PHP will be quite handy to do this kind of thing. Download the latest source code (as the Microsoft_WindowsAzure_Management_Client class we’ll be using is not released officially yet).

Our script starts like this:

<?php
2 // Set include path
3 $path = array('./library/', get_include_path());
4 set_include_path(implode(PATH_SEPARATOR, $path));
5
6 // Microsoft_WindowsAzure_Management_Client
7 require_once 'Microsoft/WindowsAzure/Management/Client.php';

This is just making sure all necessary libraries have been loaded. next, call out to the Microsoft_WindowsAzure_Management_Client class’ setInstanceCountBySlot() method to set the instance count to the requested number. Easy! And in fact even easier than Microsoft's .NET version of this.

// Do the magic
2 $managementClient = new Microsoft_WindowsAzure_Management_Client($subscriptionId, $certificateFile, '');
3
4 echo "Uploading new configuration...\r\n";
5
6 $managementClient->setInstanceCountBySlot($serviceName, $slot, $roleName, $instanceCount);
7
8 echo "Finished.\r\n";

Here’s the full script:

<?php
2 // Set include path
3 $path = array('./library/', get_include_path());
4 set_include_path(implode(PATH_SEPARATOR, $path));
5
6 // Microsoft_WindowsAzure_Management_Client
7 require_once 'Microsoft/WindowsAzure/Management/Client.php';
8
9 // Some commercial info :-)
10 echo "AutoScale - (c) 2011 Maarten Balliauw\r\n";
11 echo "\r\n";
12
13 // Quick-and-dirty argument check
14 if (count($argv) != 7)
15 {
16 echo "Usage:\r\n";
17 echo " AutoScale <certificatefile> <subscriptionid> <servicename> <rolename> <slot> <instancecount>\r\n";
18 echo "\r\n";
19 echo "Example:\r\n";
20 echo " AutoScale mycert.pem 39f53bb4-752f-4b2c-a873-5ed94df029e2 bing Bing.Web production 20\r\n";
21 exit;
22 }
23
24 // Save arguments to variables
25 $certificateFile = $argv[1];
26 $subscriptionId = $argv[2];
27 $serviceName = $argv[3];
28 $roleName = $argv[4];
29 $slot = $argv[5];
30 $instanceCount = $argv[6];
31
32 // Do the magic
33 $managementClient = new Microsoft_WindowsAzure_Management_Client($subscriptionId, $certificateFile, '');
34
35 echo "Uploading new configuration...\r\n";
36
37 $managementClient->setInstanceCountBySlot($serviceName, $slot, $roleName, $instanceCount);
38
39 echo "Finished.\r\n";

Now schedule or cron this (when needed) and enjoy the benefits of scaling your Windows Azure service.

So you’re lazy? Here’s my sample project (AutoScale-PHP.zip (181.67 kb)) and the certificates used (management.pfx (4.05 kb), management.cer (1.18 kb) and management.pem (5.11 kb)).

Now it's easier than ever to get started with MongoDB, the database that allows startups and enterprises alike to rapidly build planet-scale apps. Introducing MongoDB Atlas, the official hosted service for the database on AWS. Try it now! Brought to you in partnership with MongoDB.

Topics:

Published at DZone with permission of Maarten Balliauw, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}