- Ansible 2 Cloud Automation Cookbook
- Aditya Patawari Vikas Aggarwal
- 657字
- 2021-06-24 18:43:39
How to do it...
We will be using the ec2_lc module for creating launch configurations, the ec2_asg module for creating auto scaling groups, the ec2_scaling_policy module to define scaling policies and the ec2_metric_alarm module for creating a metric alarm to take actions like scaling up EC2 instances.
- In this task we will be creating an auto scaling group with a scale-up policy piped with a CloudWatch alarm configured with CPU utilization metrics; and if it goes above a defined threshold, it will scale up EC2 instances in auto scaling groups:
- name: Create Launch Configuration
ec2_lc:
region: ""{{ aws_region }}""
aws_access_key: ""{{ access_key }}""
aws_secret_key: ""{{ secret_key }}""
name: my_first_lc
image_id: ""{{ ami_id }}""
key_name: my_first_key
instance_type: ""{{ instance_type }}""
security_groups: ""{{ my_first_sg.group_id }}""
instance_monitoring: yes
In the preceding task, we created a launch configuration which would be used to create new instances in case of a scale-up event. We used the same parameters that we used to create an EC2 instance; that is an AMI ID, a key pair name, a security group and instance ID, and a region. We used one additional parameter (instance_monitoring) which will enable CloudWatch metric monitoring such as CPU utilization metric.
- Let us create the auto scaling group next:
- name: Create Auto Scaling group
ec2_asg:
name: my_first_asg
region: "{{ aws_region }}"
aws_access_key: "{{ access_key }}"
aws_secret_key: "{{ secret_key }}"
load_balancers:
- first-elb
launch_config_name: my_first_lc
min_size: 1
max_size: 5
desired_capacity: 3
vpc_zone_identifier:
- "{{ my_private_subnet.subnet.id }}"
tags:
- environment: test
In this task, we are creating an auto scaling group for our EC2 instances. This task will need the launch configuration name, the min/max count of instances we want to keep in our auto scaling group, the desired capacity we want to keep in our auto scaling group, the VPC subnet where we want to add more EC2 instances, and the tags as input parameters. We should note here that we are using the subnet ID registered while creating the VPC subnet.
- Next, we will create a scaling policy:
- name: Configure Scaling Policies (scale-up)
ec2_scaling_policy:
region: "{{ aws_region }}"
aws_access_key: "{{ access_key }}"
aws_secret_key: "{{ secret_key }}"
name: scale-up policy
asg_name: my_first_asg
state: present
adjustment_type: ChangeInCapacity
min_adjustment_step: 1
scaling_adjustment: +1
register: scale_up_policy
Here we have defined an auto scaling policy for scaling up EC2 instances. This task requires the name of the auto scaling group, the amount by which the auto scaling group is adjusted (scaling_adjustment), the minimum amount type by which the auto scaling group is adjusted by the policy (min_adjustment), and the type of change in capacity (adjustment_type), which can be set as ChangeInCapacity, ExactCapacity, or PercentageChangeInCapacity.
- Now, we will configure CloudWatch alarm:
- name: Configure CloudWatch Metric for Scaling Up Policy
ec2_metric_alarm:
state: present
region: "{{ aws_region }}"
aws_access_key: "{{ access_key }}"
aws_secret_key: "{{ secret_key }}"
name: "scale-up-metric"
description: "This alarm notify auto scaling policy to step up instance"
metric: "CPUUtilization"
namespace: "AWS/EC2"
statistic: "Average"
comparison: ">="
threshold: 60.0
unit: "Percent"
period: 300
evaluation_periods: 3
dimensions:
AutoScalingGroupName: "my_first_asg"
alarm_actions:
- "{{ scale_up_policy.arn }}"
In the preceding task, we are creating a CloudWatch metric alert which will trigger the scaling policy we defined in the preceding task. This task will need the following parameters:
- Metric to monitor, which we have selected as CPU utilization.
- Namespace of that metric, which, in our case, is AWS/EC2 as we are working with our EC2 instances.
- Aggregation (statistics) we want to perform on the metric. We will be doing the average over data points.
- Unit of metric. We are using percentage here.
- Will look at data points for a period 300 seconds.
- Threshold to trigger the alert, which is 60.0 % of CPU utilization.
- Evaluation period of 3, which means, over a period of 300 seconds, if the average CPU utilization is greater than 60% for 3 consecutive runs, it will trigger an alarm.
- Action required if the alarm is triggered. We are notifying our auto scaling policy defined in the previous task as an alarm_actions.
- 數據展現的藝術
- 高效能辦公必修課:Word圖文處理
- PowerShell 3.0 Advanced Administration Handbook
- Hands-On Artificial Intelligence on Amazon Web Services
- 機器人智能運動規劃技術
- 觸控顯示技術
- Implementing Splunk 7(Third Edition)
- Linux服務與安全管理
- Red Hat Linux 9實務自學手冊
- 網絡脆弱性掃描產品原理及應用
- Mastering Ansible(Second Edition)
- 傳感器原理與工程應用
- Red Hat Enterprise Linux 5.0服務器構建與故障排除
- Hands-On Geospatial Analysis with R and QGIS
- Java Deep Learning Projects