Hey all,

Hope you are safe and healthy during pandemic.

Continuing the post series that we started some days ago, this is the fourth post about AutoScaling. Today we will continue Hands-On to create an environment with AutoScaling.

You can follow this post series here:

Scaling Challenges in On-Premises Workloads – Post 1

AutoScaling on Oracle Cloud Infrastructure – Post 2

AutoScaling Hands-On (Part 1) – Post 3

In the last post, we created the following resources:

  • Compartment creation;
  • VCN/Subnets creation;
  • Load Balancer creation;
  • Security List updates on Ingress Rules to enable AutoScaling to attach an Instance on Load Balancer;
  • Instance creation.

Today we’ll continue creating the following resources:

  • Customization of instance created;
  • Custom Image creation;
  • Termination of Instance created;
  • Creation of an Instance using the Custom Image;
  • Creation of an Instance Configuration;
  • Creation of 2 Instance Pools;
  • Creation of 2 AutoScaling Configurations.

Instance Configuration

Now with instance created, access it via SSH with user opc using the SSH key that you already have or create during the instance creation:

ssh opc@<instancde_ip>
sudo su - root

Now, execute the following commands:

yum install -y stress httpd 
yum install -y https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
yum install -y http://rpms.remirepo.net/enterprise/remi-release-7.rpm
yum install -y yum-utils
yum-config-manager --enable remi-php73
yum install -y php php-mcrypt php-cli php-gd php-curl php-mysql php-ldap php-zip php-fileinfo
systemctl enable httpd
firewall-cmd --permanent --add-port=80/tcp
firewall-cmd --reload
systemctl start httpd

The above commands will install some tools and will enable a rule in iptables to allow connections on port 80.

Then, create the file /var/www/html/index.html with the following content:

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><title></title><meta http-equiv="refresh" content="0;url=oci.php/" /></head><body></body></html>

Now with file index.html created, create the file /var/www/html/oci.php with the following content:

<html>
<head>
<title>OCI - Webserver</title>
</head>
<body>
<h1 style="text-align: center;"><span style="color: #ff0000;"><strong>ViniciusDBA Config AutoScale on OCI</strong></span></h1>
<p>&nbsp;</p>
<p><img src="https://www.viniciusdba.com.br/blog/wp-content/uploads/2018/07/Logo_Blog-2.png" alt="" width="335" height="79" /></p>

<p>&nbsp;</p>
<img src="https://dka575ofm4ao0.cloudfront.net/pages-transactional_logos/retina/41865/Z9Ik6huVSQC0sSJf8To4"><br>
<h1 style="color: #5e9ca0;"><span style="color: #000000;">Welcome to OCI Webserver!</span></h1><br>
<?php

$json = file_get_contents("http://169.254.169.254/opc/v1/instance/");
$obj = json_decode($json);
$displayName = $obj->displayName;
$shape = $obj->shape;
$ad = $obj->availabilityDomain;
$region = $obj->region;
$faultDomain = $obj->faultDomain;
$internal_ip = $_SERVER['SERVER_ADDR'];
$external_ip = exec('curl http://ipecho.net/plain; echo');

print ("<p><b>My info</b><br>");
print ("<table border = 1>");
print ("<tr><td>My Public IP</td><td>$external_ip</td></tr>");
print ("<tr><td>My Private IP</td><td>$internal_ip</td></tr>");
print ("<tr><td><strong>Instance</strong></td><td><strong>$displayName</strong></td></tr>");
print ("<tr><td>Shape</td><td>$shape</td></tr>");
print ("<tr><td>Region</td><td>$region</td></tr>");
print ("<tr><td>Availability Domain</td><td>$ad</td></tr>");
print ("<tr><td>Fault Domain</td><td>$faultDomain</tr></tr>");
print ("</table>");

?>

<p>PHP Code from <a href="https://www.oc-blog.com/2018/12/09/where-am-i">https://www.oc-blog.com/2018/12/09/where-am-i</a></p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
</body>
</html>

This PHP code will generate a table that looks like the table below:

As I commented, this PHP code is available in the following page: OC-Blog.

Custom Image Creation

In the instance page, click on More Actions and then on Create Custom Image:

Then, you need to define a name for Custom Image. In this example I am using image-web as name. Then click on Create Custom Image:

Then, Custom Image will be created:

Termination of Instance created

Now that we have a Custom Image created, there is no need to keep the original instance running, because we created it only to customize and use it as a source to create a Custom Image.

So, in Instances page, click on “3 points” icon in the right of the screen and then click on Terminate:

Then, click on  Permanently delete the attached boot volume and then click on button Terminate Instance:

Creation of an Instance using the Custom Image

Now that we already have a Custom Image, it’s the moment to create an instance using this Custom Image.

In the Custom Images page, click on “3 points” icon, then click on Create Instance. You can use the same steps used to create an instance as we already blogged about in previous blog post: AutoScaling on Oracle Cloud Infrastructure – Post 2

Creation of an Instance Configuration

As explained in the second post for this series, AutoScaling on Oracle Cloud Infrastructure – Post 2, to create an AutoScaling Configuration we need to have an Instance Pool, and to have an Instance Pool we must create an Instance Configuration. The Instance Configuration is looks like a ‘config file’ to be used as a template when we are creating new instances.

So, in the page for instance created in previous step, click on More Actions and then click on Create Instance Configuration:

Select DEV_COMPARTMENT as compartment and define a name for Instance Configuration. In this example we are using instance-config-web:

Creation of 2 Instance Pools

With Instance Configuration Created, now we’ll create two Instance Pools:

  • One instance pool for AutoScale Configuration metric-based;
  • Another instance pool for AutoScale Configurationschedule-based.

Instance Pool Metric-Based

In Instance Configuration page for the Instance Configuration that we created previously, click on Create Instance Pool:

Select DEV_COMPARTMENT as compartment and define a name for Instance Pool, in this example we are using instance-pool-web-metric-based. Define the number of instances that your pool must have. Please note that we can create the pool with only one instance. For corporate use, you can create your pool with more instances to fit your workload. As this is an example, start the pool with only one instance is enough:

You need to specify in which AD’s, FD’s (optional), VCN and Subnet the instances will be created. Please note that some regions have only one AD, so, there is no option to add another AD’s, because of this the button will be in gray color. Please note that you can attach the instances to Load Balancer. So, choose the Load Balancer that we created in the previous post and define port 80:

Review info and click on Create:

Instance Pool Schedule-Based

Dentro da tela da Instance Configuration criada anteriormente, clique em Create Instance Pool:

Select DEV_COMPARTMENT as compartment, in this example we are using instance-pool-web-schedule-based. Define the number of instances. Define the number of instances that your pool must have. Please note that we can create the pool with only one instance. For corporate use, you can create your pool with more instances to fit your workload. As this is an example, start the pool with only one instance is enough:

You need to specify in which AD’s, FD’s (optional), VCN and Subnet the instances will be created. Please note that some regions have only one AD, so, there is no option to add another AD’s, because of this the button will be in gray color. Please note that you can attach the instances to Load Balancer. So, choose the Load Balancer that we created in the previous post and define port 80:

Review info and click on Create:

Creation of 2 AutoScaling Configurations

With the Instance Pools created, one for metric-based and another one for schedule-based, now it’s the moment to create the AutoScaling Configurations, one for metric-based using the Instance Pool metric-based and another one for schedule-based using the Instance Pool schedule-based:

AutoScaling Configuration Metric-Based

In Instance Pool instance-pool-web-metric-based page, click on More Actions and then click on Create Autoscaling Configuration:

Select DEV_COMPARTMENT as compartment and define a name for AutoScaling Configuration. In this example we are using autoscaling-config-metric-based. Click onNext:

In the below screen, select Metric-Based as AutoScaling Configuration. Then define a name for autoscaling policy, in this example, we are using autoscaling-policy-metric-based. For performance metric, select CPU utilization. In Scale-out (pool increase) rule, define that Scale-Out operator will be Greater than (>), the Threshold Percentage will be 60 and the Number of Instances to Add will be 2. This means that when average of CPU usage in pool meet the threshold of 60%, two instances will be added in the pool. In Scale-In (pool decrease) rule, define that the Scale-In operator will be Less than (<), the Threshold Percentage will be 59 and the Number of Instances to Remove será 2. In Scaling Limits define that the minimal number of instances will be 1, the maximum number of instances will be 5 and the initial number will be 1. Please note that this values can be different in a corporate environment. Click on Next:

Review the info and click on Create:

 

AutoScaling Configuration Schedule-Based

In the  Instance Pool instance-pool-web-metric-based page, click on More Actions and then click on Create Autoscaling Configuration:


Select DEV_COMPARTMENT as the compartment and define a name for the autoscaling configuration, in this example we are using autoscaling-config-schedule-based. Click on Next:

In the below screen, select that autoscaling configuration will be Schedule-based Autoscaling. So, define a name for the autoscaling policy, in this example we are using autoscaling-policy-schedule-based-policy. In Target Pool Size define the target number of instances in Pool when policy will be executed, in this case, we are increasing the pool from 1 to 2 instances, the Target Pool Size was defined as 2. In Execution Schedule, define when the policy must be executed. In this example, we are creating a custom policy that will be executed on day 15, month 08, year 2020, at hour 13, minutes 30. Click then on + Another Policy to add another policy:

In the second policy, we are using the name autoscaling-policy-schedule-based-policy-in-guob and the Target Pool Size was define as 2 (so, the Pool will be decreased from 2 to 1 instance). In Execution Schedule, define when the policy must be executed. In this example, we are creating a custom policy that will be executed on day 15, month 08, year 2020, at hour 13, minutes 50. Click on Next:

Review the info and click on Create:

 

In the AutoScaling configurations screen it’s possible to see both configurations created:

So, we have created two autoscaling configurations:autoscaling:

  • Metric-based:
    • If average of CPU usage for the pool will be greater than 60%, add two instances;
    • If average of CPU usage for the pool will be lower than 59%, remove two instances;
  • Schedule-based:
    • Change the pool size from 1 to 2 instances on August/15/2020 at 13:30 (1:30 pm);
    • Change the pool size from 2 to 1 instance on August/15/2020 at 13:50 (1:50 pm).

You can try to reproduce the metric-based autoscaling putting some CPU workload in the server with the tool named stress.

Login to the server and execute the following commands:

ssh opc@<instance_ip>
sudo su - root
stress --cpu 8 --timeout 900

This command will eat 8 CPU’s for 15 minutes. So, our CPU will be loaded up to 100%.

Please note that when you start this command, we have only two instances running in our environment, one for metric-based instance pool and another one for schedule-based instance pool:

If you go to the Instance Pool instance-pool-web-metric-based page, you can note that CPU usage is increasing:

Just reminder that the pool will increase  (autoscaling) if the average for CPU is greater than 60% for three consecutive minutes.

So, after 3 minutes, we can see that pool state will change from Running to Scaling:

We also can note that two new instances are being provisioned and have the state Provisioning:

If the average CPU usage is less than 59%, two instances will be removed.

So, we are finishing this series about autoscaling.

Hope this helps.

Peace!

 

Vinicius