{"id":501,"date":"2019-05-16T18:05:56","date_gmt":"2019-05-16T18:05:56","guid":{"rendered":"https:\/\/unsrewiki.1sys1.com\/?p=501"},"modified":"2019-05-16T18:26:31","modified_gmt":"2019-05-16T18:26:31","slug":"building-and-scaling-a-website-part-4","status":"publish","type":"post","link":"https:\/\/server-help.org\/index.php\/2019\/05\/16\/building-and-scaling-a-website-part-4\/","title":{"rendered":"Building and Scaling a Website \u2013 Part 4"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">Automated Scaling<\/h2>\n\n\n\n<p>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.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">API<\/h2>\n\n\n\n<p>We will first need to setup our API access<\/p>\n\n\n\n<p>1). Login to your IONOS Cloud Panel <\/p>\n\n\n\n<p>2). Go to Management &gt; Users &gt; Create<\/p>\n\n\n\n<p>3). Make a note of your full username, it should look like:  12345678.youruser <\/p>\n\n\n\n<p>4). Once created select your User and click on Disabled next to API, then click Active: Yes, Apply Changes.<\/p>\n\n\n\n<figure class=\"wp-block-image is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/unsrewiki.1sys1.com\/wp-content\/uploads\/2019\/05\/screen-1024x398.png\" alt=\"\" class=\"wp-image-511\" width=\"726\" height=\"282\" srcset=\"https:\/\/server-help.org\/wp-content\/uploads\/2019\/05\/screen-1024x398.png 1024w, https:\/\/server-help.org\/wp-content\/uploads\/2019\/05\/screen-300x117.png 300w, https:\/\/server-help.org\/wp-content\/uploads\/2019\/05\/screen-768x298.png 768w, https:\/\/server-help.org\/wp-content\/uploads\/2019\/05\/screen.png 1050w\" sizes=\"(max-width: 726px) 100vw, 726px\" \/><\/figure>\n\n\n\n<p>5). Once activated click on &#8220;Show&#8221; next to API Key and make a note of the result. It should look like:  c12e6c9fdc22e77c89c9e3fe3cb09500 <\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Save Money<\/h2>\n\n\n\n<p>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.<\/p>\n\n\n\n<p>With this in mind we will only be running a single Web Server unless a certain performance threshold is reached.<\/p>\n\n\n\n<p>1). Convert your 2nd Web Server to the &#8220;Flex&#8221; option.<\/p>\n\n\n\n<p>2). In your IONOS Cloud Panel go to the Servers page, Select your 2nd WebServer, Click the Blue edit button next to Configuration .<\/p>\n\n\n\n<p>3). Click the link for &#8220;Here, you can customize your configuration individually.&#8221;<\/p>\n\n\n\n<p>4). Select Flex and then Customize Server.<\/p>\n\n\n\n<p>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\u200b 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.<\/p>\n\n\n\n<p>5). Once converted to a &#8220;Flex&#8221; Cloud server go to Actions &gt; Shut Down to power the 2nd server off.<\/p>\n\n\n\n<p>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<\/p>\n\n\n\n<figure class=\"wp-block-image is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/unsrewiki.1sys1.com\/wp-content\/uploads\/2019\/05\/screen2-1024x468.png\" alt=\"\" class=\"wp-image-519\" width=\"662\" height=\"302\" srcset=\"https:\/\/server-help.org\/wp-content\/uploads\/2019\/05\/screen2-1024x468.png 1024w, https:\/\/server-help.org\/wp-content\/uploads\/2019\/05\/screen2-300x137.png 300w, https:\/\/server-help.org\/wp-content\/uploads\/2019\/05\/screen2-768x351.png 768w, https:\/\/server-help.org\/wp-content\/uploads\/2019\/05\/screen2.png 1049w\" sizes=\"(max-width: 662px) 100vw, 662px\" \/><\/figure>\n\n\n\n<p>7). Login to your Web Server 1.<\/p>\n\n\n\n<p>8). mkdir -p \/root\/myscripts<\/p>\n\n\n\n<p>9). vi \/root\/myscripts\/poweron.php <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\n###########################################################################\n# CLOUD SERVER API DOCUMENTATION\n#\n# Name: setStatusServer\n# Language: PHP\n#\n# Description: allows to power on, power off and reboot server\n###########################################################################\n\n#ID access to API\n$TOKEN = \"c12e6c9fdc22e77c89c9e3fe3cb09500\";\n$url = \"https:\/\/cloudpanel-api.ionos.com\/v1\";\n\nfunction setStatusServer($id, $data){\n        global $url;\n        global $TOKEN;\n        $_command = $url . \"\/servers\/$id\/status\/action\";\n        $request = curl_init();\n\n        \/\/Set options\n        curl_setopt($request, CURLOPT_URL, $_command);\n        curl_setopt($request, CURLOPT_RETURNTRANSFER, true);\n        curl_setopt($request, CURLOPT_HTTPHEADER, array(\"X-TOKEN:$TOKEN\", \"Content-type:application\/json\"));\n        curl_setopt($request, CURLOPT_CUSTOMREQUEST, \"PUT\");\n\n        \/\/Add parameters\n        curl_setopt($request, CURLOPT_POSTFIELDS, $data);\n\n\n        \/\/Try to get the response\n        $response = curl_exec($request);\n        if ($response == false){\n                return( curl_error($request));\n        }\n        else{\n                return( $response);\n        }\n\n        curl_close($request);\n}\n\n#PARAMETERS\n$id = \"C11B9BA6E6977F4582BDDAAB51452BE9\";\n$action = \"POWER_ON\";\n$method = \"SOFTWARE\";\n\n$data = array('action'=>$action, 'method'=>$method);\n$data = json_encode($data);\n\necho setStatusServer($id, $data);\n?><\/code><\/pre>\n\n\n\n<p>Make sure to adjust the $TOKEN and $id values with your API token and Server ID.<\/p>\n\n\n\n<p>10). cp \/root\/myscripts\/poweron.php \/root\/myscripts\/poweroff.php <\/p>\n\n\n\n<p>11). vi  \/root\/myscripts\/poweroff.php <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$action = \"POWER_OFF\";<\/code><\/pre>\n\n\n\n<p>We will now configure the 2nd server to Startup or Shutdown based on  either the number of active Web Connections, or the Load average. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Web Connections<\/h2>\n\n\n\n<p>1). vi  \/root\/myscripts\/connections-up.sh <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>limit=100\nconnection=$(netstat -an | grep ESTABLISHED | grep -w 443 | wc -l)\nif [ \"$connection\" \\> \"$limit\" ];\nthen\nphp \/root\/myscripts\/poweron.php > \/dev\/null 2>&amp;1\nfi<\/code><\/pre>\n\n\n\n<p>This script will startup our 2nd server only if the number of connections on port 443 exceed 100.<\/p>\n\n\n\n<p>2). vi  \/root\/myscripts\/connections-down.sh <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>limit=50\nconnection=$(netstat -an | grep ESTABLISHED | grep -w 443 | wc -l)\nif [ \"$connection\" \\&lt; \"$limit\" ];\nthen\nphp \/root\/myscripts\/poweroff.php > \/dev\/null 2>&amp;1\nfi<\/code><\/pre>\n\n\n\n<p>This script will shutdown our 2nd server only if the number of connections on port 443 is less than 50. <\/p>\n\n\n\n<p>3). chmod +x  \/root\/myscripts\/*.sh   <\/p>\n\n\n\n<p>4). crontab -e<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>*\/5 * * * * \/root\/myscripts\/connections-up.sh\n*\/30 * * * * \/root\/myscripts\/connections-down.sh<\/code><\/pre>\n\n\n\n<p>These cronjobs will check the number of connections.<\/p>\n\n\n\n<p>Every 5 minutes if the connections exceeds 100 our 2nd web server will be started.<\/p>\n\n\n\n<p>Every 30 minutes if the number of connections is less than 50 our 2nd server will be shutdown.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Load Average<\/h2>\n\n\n\n<p>1). vi \/root\/myscripts\/load-up.sh<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>limit=1\naload=uptime | cut -d \" \" -f 13 | cut -d \",\" -f 1\nload=\"$((aload))\"\nif [ \"$load\" \\> \"$limit\" ];\nthen\nphp \/root\/myscripts\/poweron.php > \/dev\/null 2>&amp;1\nfi<\/code><\/pre>\n\n\n\n<p>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 &#8211; 1.99 the script will not run, but if the load average is 2.00 or higher it will.<\/p>\n\n\n\n<p>2). vi \/root\/myscripts\/load-down.sh <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>limit=1\naload=uptime | cut -d \" \" -f 13 | cut -d \",\" -f 1\nload=\"$((aload))\"\nif [ \"$load\" \\&lt; \"$limit\" ];\nthen\nphp \/root\/myscripts\/poweroff.php > \/dev\/null 2>&amp;1\nfi<\/code><\/pre>\n\n\n\n<p>This script will shutdown our 2nd server only if the load is less than 1. Meaning a load between 0 &#8211; 0.99 the script will run, but a load 1 or higher it will not.<\/p>\n\n\n\n<p>3). chmod +x  \/root\/myscripts\/*.sh  <\/p>\n\n\n\n<p>4). crontab -e <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>*\/5 * * * * \/root\/myscripts\/load-up.sh\n*\/30 * * * * \/root\/myscripts\/load-down.sh<\/code><\/pre>\n\n\n\n<p>These cronjobs will check the load average.<\/p>\n\n\n\n<p>Every 5 minutes if the load exceeds 1.99 our 2nd web server will be started.<\/p>\n\n\n\n<p>Every 30 minutes if the load is less than 1.00 our 2nd server will be shutdown.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion of Part 4<\/h2>\n\n\n\n<p>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.<\/p>\n\n\n\n<p>You can also continue to grow and expand on this idea by using additional servers beyond the 2 Web Servers you currently have.<\/p>\n\n\n\n<p>For information and ideas on additional things you can do with the IONOS Cloud API look at the links below:<\/p>\n\n\n\n<div class=\"wp-block-button\"><a class=\"wp-block-button__link\" href=\"https:\/\/cloudpanel-api.ionos.com\/documentation\/index_en.html\">Documentation<\/a><\/div>\n\n\n\n<p><\/p>\n\n\n\n<div class=\"wp-block-button\"><a class=\"wp-block-button__link\" href=\"https:\/\/cloudpanel-api.ionos.com\/documentation\/v1\/en\/api\/examples.html\">Examples<\/a><\/div>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 &gt; Users &gt; Create 3).&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12,4,1],"tags":[],"_links":{"self":[{"href":"https:\/\/server-help.org\/index.php\/wp-json\/wp\/v2\/posts\/501"}],"collection":[{"href":"https:\/\/server-help.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server-help.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server-help.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server-help.org\/index.php\/wp-json\/wp\/v2\/comments?post=501"}],"version-history":[{"count":15,"href":"https:\/\/server-help.org\/index.php\/wp-json\/wp\/v2\/posts\/501\/revisions"}],"predecessor-version":[{"id":520,"href":"https:\/\/server-help.org\/index.php\/wp-json\/wp\/v2\/posts\/501\/revisions\/520"}],"wp:attachment":[{"href":"https:\/\/server-help.org\/index.php\/wp-json\/wp\/v2\/media?parent=501"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server-help.org\/index.php\/wp-json\/wp\/v2\/categories?post=501"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server-help.org\/index.php\/wp-json\/wp\/v2\/tags?post=501"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}