Building and Scaling a Website – Part 4

Automated Scaling

In part 4 we will look into using the IONOS Cloud API to allow for our setup to automatically scale up and down as resources demand it.

API

We will first need to setup our API access

1). Login to your IONOS Cloud Panel

2). Go to Management > Users > Create

3). Make a note of your full username, it should look like: 12345678.youruser

4). Once created select your User and click on Disabled next to API, then click Active: Yes, Apply Changes.

5). Once activated click on “Show” next to API Key and make a note of the result. It should look like: c12e6c9fdc22e77c89c9e3fe3cb09500

Save Money

In this Scenario we will assume that you have completed the setup of at least part 2 of this guide. Meaning that you currently have 2 Web Servers.

With this in mind we will only be running a single Web Server unless a certain performance threshold is reached.

1). Convert your 2nd Web Server to the “Flex” option.

2). In your IONOS Cloud Panel go to the Servers page, Select your 2nd WebServer, Click the Blue edit button next to Configuration .

3). Click the link for “Here, you can customize your configuration individually.”

4). Select Flex and then Customize Server.

You will notice that now the monthly cost of the Cloud S will go from $4.99 per month to $12.96. However, while a Flex Server is powered down you will only pay for the storage which in this case is $2.16​ per month. If over time you determine that your usage of the extra server costs more than $4.99 you will then want to switch it back to its original Cloud type and keep the server running 100% of the time instead.

5). Once converted to a “Flex” Cloud server go to Actions > Shut Down to power the 2nd server off.

6). Go to Management > Logs and select the task you just performed from the list. Make a note of the value next to ID, it should look like: C11B9BA6E6977F4582BDDAAB51452BE9

7). Login to your Web Server 1.

8). mkdir -p /root/myscripts

9). vi /root/myscripts/poweron.php

<?php
###########################################################################
# CLOUD SERVER API DOCUMENTATION
#
# Name: setStatusServer
# Language: PHP
#
# Description: allows to power on, power off and reboot server
###########################################################################

#ID access to API
$TOKEN = "c12e6c9fdc22e77c89c9e3fe3cb09500";
$url = "https://cloudpanel-api.ionos.com/v1";

function setStatusServer($id, $data){
        global $url;
        global $TOKEN;
        $_command = $url . "/servers/$id/status/action";
        $request = curl_init();

        //Set options
        curl_setopt($request, CURLOPT_URL, $_command);
        curl_setopt($request, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($request, CURLOPT_HTTPHEADER, array("X-TOKEN:$TOKEN", "Content-type:application/json"));
        curl_setopt($request, CURLOPT_CUSTOMREQUEST, "PUT");

        //Add parameters
        curl_setopt($request, CURLOPT_POSTFIELDS, $data);


        //Try to get the response
        $response = curl_exec($request);
        if ($response == false){
                return( curl_error($request));
        }
        else{
                return( $response);
        }

        curl_close($request);
}

#PARAMETERS
$id = "C11B9BA6E6977F4582BDDAAB51452BE9";
$action = "POWER_ON";
$method = "SOFTWARE";

$data = array('action'=>$action, 'method'=>$method);
$data = json_encode($data);

echo setStatusServer($id, $data);
?>

Make sure to adjust the $TOKEN and $id values with your API token and Server ID.

10). cp /root/myscripts/poweron.php /root/myscripts/poweroff.php

11). vi /root/myscripts/poweroff.php

$action = "POWER_OFF";

We will now configure the 2nd server to Startup or Shutdown based on either the number of active Web Connections, or the Load average.

Web Connections

1). vi /root/myscripts/connections-up.sh

limit=100
connection=$(netstat -an | grep ESTABLISHED | grep -w 443 | wc -l)
if [ "$connection" \> "$limit" ];
then
php /root/myscripts/poweron.php > /dev/null 2>&1
fi

This script will startup our 2nd server only if the number of connections on port 443 exceed 100.

2). vi /root/myscripts/connections-down.sh

limit=50
connection=$(netstat -an | grep ESTABLISHED | grep -w 443 | wc -l)
if [ "$connection" \< "$limit" ];
then
php /root/myscripts/poweroff.php > /dev/null 2>&1
fi

This script will shutdown our 2nd server only if the number of connections on port 443 is less than 50.

3). chmod +x /root/myscripts/*.sh

4). crontab -e

*/5 * * * * /root/myscripts/connections-up.sh
*/30 * * * * /root/myscripts/connections-down.sh

These cronjobs will check the number of connections.

Every 5 minutes if the connections exceeds 100 our 2nd web server will be started.

Every 30 minutes if the number of connections is less than 50 our 2nd server will be shutdown.

Load Average

1). vi /root/myscripts/load-up.sh

limit=1
aload=uptime | cut -d " " -f 13 | cut -d "," -f 1
load="$((aload))"
if [ "$load" \> "$limit" ];
then
php /root/myscripts/poweron.php > /dev/null 2>&1
fi

This script will startup our 2nd server only if the load average is greater than 1. Please note that this only looks at the whole value of the load and not the decimal. Meaning if the load average is 0 – 1.99 the script will not run, but if the load average is 2.00 or higher it will.

2). vi /root/myscripts/load-down.sh

limit=1
aload=uptime | cut -d " " -f 13 | cut -d "," -f 1
load="$((aload))"
if [ "$load" \< "$limit" ];
then
php /root/myscripts/poweroff.php > /dev/null 2>&1
fi

This script will shutdown our 2nd server only if the load is less than 1. Meaning a load between 0 – 0.99 the script will run, but a load 1 or higher it will not.

3). chmod +x /root/myscripts/*.sh

4). crontab -e

*/5 * * * * /root/myscripts/load-up.sh
*/30 * * * * /root/myscripts/load-down.sh

These cronjobs will check the load average.

Every 5 minutes if the load exceeds 1.99 our 2nd web server will be started.

Every 30 minutes if the load is less than 1.00 our 2nd server will be shutdown.

Conclusion of Part 4

You have now configured your 2nd Web Server to startup and shutdown when your site needs additional resources. In the process you have saved money when the resources are not required.

You can also continue to grow and expand on this idea by using additional servers beyond the 2 Web Servers you currently have.

For information and ideas on additional things you can do with the IONOS Cloud API look at the links below:

Marv has written 28 articles

One thought on “Building and Scaling a Website – Part 4

  1. sirgliofrei says:

    We are a group of volunteers and opening a new scheme in our community. Your site offered us with valuable information to work on. You’ve done a formidable job and our entire community will be grateful to you.

Cancel reply

Leave a Reply to sirgliofrei

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>