Over a million developers have joined DZone.

Windows Azure and scaling: how? (PHP)

DZone's Guide to

Windows Azure and scaling: how? (PHP)

· Cloud Zone
Free Resource

Production-proven Mesosphere DC/OS is now even better with GPU scheduling, pods, troubleshooting, enhanced security, and over 100+ integrated services deployed in one-click.

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:

2 // Set include path
3 $path = array('./library/', get_include_path());
4 set_include_path(implode(PATH_SEPARATOR, $path));
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, '');
4 echo "Uploading new configuration...\r\n";
6 $managementClient->setInstanceCountBySlot($serviceName, $slot, $roleName, $instanceCount);
8 echo "Finished.\r\n";

Here’s the full script:

2 // Set include path
3 $path = array('./library/', get_include_path());
4 set_include_path(implode(PATH_SEPARATOR, $path));
6 // Microsoft_WindowsAzure_Management_Client
7 require_once 'Microsoft/WindowsAzure/Management/Client.php';
9 // Some commercial info :-)
10 echo "AutoScale - (c) 2011 Maarten Balliauw\r\n";
11 echo "\r\n";
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 }
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];
32 // Do the magic
33 $managementClient = new Microsoft_WindowsAzure_Management_Client($subscriptionId, $certificateFile, '');
35 echo "Uploading new configuration...\r\n";
37 $managementClient->setInstanceCountBySlot($serviceName, $slot, $roleName, $instanceCount);
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)).

Simply build, test, and deploy. Mesosphere DC/OS is the best way to run containers and big data anywhere offering production-proven flexibility and reliability.


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

Opinions expressed by DZone contributors are their own.


Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

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


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

{{ parent.tldr }}

{{ parent.urlSource.name }}