Thursday, May 21, 2020

OCI Compute Instance Autoscaling configuration

6:08 AM Posted by Dilli Raj Maharjan No comments

Oracle Cloud autoscaling lets us automatically adjust the number of Compute instances in an instance pool based on performance metrics such as CPU utilization, memory utilization. This helps us provide consistent performance for end-users during periods of high demand, and helps reduce your costs during periods of low demand.

The following are a few key components for Compute autoscaling.

1. Instance pool
Instance pools let you provision and create multiple Compute instances based on the same configuration, within the same region. An instance configuration is required for instance pool configuration. An instance configuration is a template that defines the settings to use when creating instances.

2. Cooldown period
A cooldown period between autoscaling events lets the system stabilize at the updated level. The cooldown period starts when the instance pool reaches a steady state. Autoscaling continues to evaluate performance metrics during the cooldown period. When the cooldown period ends, autoscaling adjusts the instance pool's size again if needed.

3. Performance metrics
Autoscaling relies on performance metrics that are collected by the Monitoring service. These performance metrics are aggregated into one-minute time periods and then averaged across the instance pool. When three consecutive values (that is, the average metrics for three consecutive minutes) meet the threshold, an autoscaling event is triggered.

OCI Compute Instance autoscaling has the following 3 basic steps.
  1. Create Instance configuration
  2. Create Instance Pool
  3. Create Autoscaling Configuration 

Create Instance Configuration

An instance configurations let you define the settings to use when creating Compute instances. To create instance configuration. Click on the Navigation menu and then Compute and Instance.



Click on the name of an instance from which we are going to create instance configuration.



On Instance Details page, Click on More Actions and click on Create Instance Configuration.



On Create Instance Configuration page, Provide the name of Instance configuration and click on Create Instance Configuration.
 


Now once Instance Configuration has been created, we can click on Create Instance Pool to create instance pool from Instance Configuration Details page.




Create Instance Pool

 Alternatively, click on Navigation menu > Compute > Instance Pools to create an instance pool.



Click on Create Instance Pool button. 


On Create Instance Pool page, Provide the name of Instance Pool, number of Instances, instance configuration name.



Scroll down and on Availability Domain Selection 1 section provide AD name,  Name of the Virtual Cloud Network, Subnet, and Click on Additional SelectionBasically I am planning to deploy 3 instances in 3 ADs so I am adding new AD detail for autoscaling instance by clicking Additional Selection.



On Availability Domain Selection 2 section provide AD name,  Name of the Virtual Cloud Network, Subnet, and Click on Additional Selection. I am using the second AD, VCN, and my regional public subnet. I have clicked the Additional Selection button to add third AD.



On Availability Domain Selection 3 section provide AD name,  Name of the Virtual Cloud Network, Subnet. Notice +Additional Selection button is already disabled because on the region I am using to host Autoscaling configuration has 3 ADs. Click on Create Instance pool.



Instance pool is provisioning.


Once Instance Pool is fully created it looks something like below. 

Create Autoscaling Configuration

In Pool Details page click on More Actions and Click on Create Autoscaling Configuration.


Provide Autoscaling configuration name, Instance pool in Create Autoscaling Configuration page. In the case below I am using default cooldown value, 300 seconds.



Scroll down and provide Autoscaling policy name, Performance Metric, a minimum number of instances, the maximum number of instances, and the initial number of instances.




Provide Scaling Rule. Select scale-out and scale-in operator, threshold percentage, and a number of instances to add/remove. In my case, if CPU usage is greater than 80 pct for continuous 300 seconds then a new instance will be added and if CPU usage is less than 40 pct for continuous 300 seconds then 1 instance will be removed. Click on Create to create autoscaling configuration.




Once Autoscaling is configured and enabled it looks something like below.



Now on my instance page I can see three instances are created and among them 2 have status Terminating.




Connected to the instance using public IP.



Checked its uptime and CPU details.



Install Redhat package stress using yum.




Once a stress package is installed execute stress command to generate CPU load.




Notice load average has increased and the CPU usage percentage is 99.8



After 5 min of continuous load, we can notice that a new instance is provisioning.



Connect to the second instance and install the stress Redhat package on it.



Once stress command is executed, we can see the load average is increasing and the CPU usage is at 99.8%




On the compute Instance page, we can see the third instance is provisioning.


Within a few mins, there are 3 instances running from Autoscaling configuration.



Connected to the third instance.



Once the stress is timed out, load average and CPU usage have decreased on all 3 compute instances.
The load average is just 0.23 in the first instance.



The load average is just 0.04 in the second instance.



The load average is just 0.00 in the third instance.



Once CPU usage is below 40 for a continuous 300 seconds, the second instance state changed to Terminating. 


Again after 300 seconds, the third instance state changed to Terminating.



Finally, we have a single instance running. We have defined 1 instance as minimum instance so 1 instance will keep running forever and instance get provisioned up to 3 while CPU on the instance is more than 80% used and it gets reduced to minimum value 1 if the CPU load is below 40% used.


Wednesday, May 20, 2020

Oracle Cloud Infrastructure Change instance shape.

8:12 AM Posted by Dilli Raj Maharjan No comments
A Compute Instance shape is a template that determines the number of CPUs, amount of memory, and other resources allocated to a newly created instance. These pre-defined shapes are used to provision compute instance. Sometimes we create a compute instance  and later we required to change the instance shape. Either we need to lower down the number of resources to save cost or increase the resource to serve the higher load. 
Following are the detail steps to change instance shape from VM.Standard.2.2 to VM.Standard.2.1. A small downtime is required for Compute Instance shape change.

On my instance page, I have two Compute instance with VM.Standard.2.2 shape. Web1 and Web2 are my two instances and both are running.

Select the compute instance to change the instance size. Click on Instance name.


On instance detail page click on Change Shape.
]

Select the shape series and we can notice there is current Shape selected.



Click on required shape and click on Change Shape.



There will be confirmation window. Click on Reboot Instance to reflect the change.



We can notice the instance is STOPPING and within few min it will be back to RUNNING.


Within few moment. The instance is up with new shape.


Monday, May 18, 2020

My First Infrastructure as Code with Terraform

7:15 AM Posted by Dilli Raj Maharjan 2 comments
Terraform is the infrastructure as code offering from HashiCorp. It is a tool for building, changing, and managing infrastructure in a safe, repeatable way. Operators and Infrastructure teams can use Terraform to manage environments with a configuration language called the HashiCorp Configuration Language (HCL) for human-readable, automated deployments.

Infrastructure as Code

Infrastructure as code is the process of managing infrastructure in a file or files rather than manually configuring resources using a user interface. A resource in this instance is any piece of infrastructure in a given environment, such as a virtual machine, security group, network interface, etc.

A simple workflow for deployment will follow following steps:
  • Scope
  • Author
  • Initialize
  • Plan & apply


Terraform provides following few advantages for operators and organizations of any size.
  • Platform Agnostic
  • State Management
  • Operator Confidence
In my OCI compartment, I am planning to create full set of a web server. A vcn with the name TFDemoVCN and CIDR 192.168.100.0/23 will be created.  Internet gateway TFDEMO_IGW, default security list and defalt routing table will be created to the VCN. Inside the VCN I am planning to create two subnets: PublicSubnet and PrivateSubnet. Subnet PublicSubnet is a public subnet with 192.168.100.0/24 CIDR block and subnet PrivateSubnet is a private subnet with 192.168.101/0/24 CIDR block. All required Security list, Internet Gateway, and route tables. A VM compute instance of shape VM.Standard2.1 will be created and required package for apache web will be created using remote-exec.  Once terraform apply is completed we are ready to access web via a public IP address that is printed as output. 



Basically I have used the following files with terraform.
3. vcn.tf

Execute source and environment variable files to reflect environment variables.
source env-vars


Execute terraform init to initialize the backend and provider plugins..
terraform init


Execute terraform with validate command to validate syntax on all the terraform files
terraform validate


Execute terraform plan to display the plan. With the help of plan command with terraform we will be able to view what terraform is going to create and destroy during apply command. It is best practice to use out option to save the output to the file.
terraform plan -out terraform.plan

...........


Final step is to apply terraform with terraform apply command.
terraform apply 

....

Final output of terraform apply is below.