Hjälpcentral

Auto Scaling Groups Setup

Auto Scaling is a powerful feature in Amazon Web Services (AWS) that automatically adjusts the number of Amazon EC2 instances in response to the demand for your application. The core concept is to ensure that you always have the right amount of compute capacity at any time. Auto Scaling enables cost-efficient use of resources by automatically increasing the number of instances during high demand and reducing them during low demand, keeping the application highly available.

This guide will walk through the comprehensive process of setting up Auto Scaling Groups (ASGs) on AWS, with detailed explanations of the key components, setup process, configuration, scaling policies, monitoring, and best practices.

What is Auto Scaling?

Auto Scaling is a feature that automatically adjusts the number of EC2 instances to meet the current demand of your application. AWS Auto Scaling works in conjunction with Auto Scaling Groups (ASGs), which are logical groups of EC2 instances. Auto Scaling can:

  • Ensure high availability by replacing unhealthy instances.
  • Scale your application automatically to accommodate increases in traffic.
  • Optimize resource utilization and reduce costs by terminating unused instances.

Auto Scaling Groups define the minimum, maximum, and desired number of EC2 instances that can run at any given time.

Key Components of Auto Scaling Groups

Auto Scaling relies on several key components:

  • Launch Template or Launch Configuration: Specifies the configuration of EC2 instances, including AMI (Amazon Machine Image), instance type, key pair, security groups, and block storage options. A Launch Template is more flexible and supports newer features compared to the older Launch Configuration.

  • Auto Scaling Group (ASG): A logical grouping of EC2 instances that share the same configuration and scaling policies. An ASG ensures that the desired number of instances is running and distributes traffic across multiple instances to balance the load.

  • Scaling Policies: Define the rules and conditions that trigger the scaling actions. AWS supports different types of scaling policies:

    • Target Tracking Scaling: Automatically adjusts capacity to keep a specific metric, such as CPU utilization, at a target value.
    • Step Scaling: Adds or removes instances based on predefined thresholds for metrics.
    • Scheduled Scaling: Adjusts capacity at specific times based on a defined schedule.
  • Health Checks: Monitor the health of instances within the ASG. AWS automatically replaces unhealthy instances with new ones to maintain availability.

Prerequisites for Setting Up Auto Scaling Groups

Before setting up an Auto Scaling Group, you need to complete the following tasks:

  • Create an Amazon EC2 Instance: This will serve as the template for your ASG. Configure it with the necessary software, security groups, and networking configurations.

  • Set up a Launch Template or Launch Configuration: A Launch Template allows you to save configuration details about the instance, such as the instance type, AMI ID, security groups, key pairs, and user data scripts.

  • IAM Role and Policies: Assign the appropriate IAM role to your Auto Scaling Group to ensure it can interact with other AWS services. For example, it may need permissions to modify EC2 instances or update CloudWatch metrics.

  • VPC and Subnets: Ensure you have a properly configured VPC with at least one subnet where your EC2 instances will be deployed. It's advisable to use multiple Availability Zones for better availability.

Step by Step Guide to Setting Up Auto Scaling Groups

Let’s go through the process of setting up an Auto Scaling Group using the AWS Management Console.

Create a Launch Template

  1. Navigate to EC2: Log in to the AWS Management Console and navigate to the EC2 dashboard.
  2. Create Launch Template:
    • Under the Instances section, select Launch Templates.
    • Click Create Launch Template and fill out the details:
      • Template Name: Choose a descriptive name.
      • AMI ID: Select the AMI for the instances (e.g., Amazon Linux 2, Ubuntu, etc.).
      • Instance Type: Choose the instance type (e.g., t2.micro, m5.large).
      • Key Pair: Select an existing key pair or create a new one for SSH access.
      • Security Groups: Assign a security group that allows the necessary traffic (e.g., HTTP, HTTPS, SSH).
      • Storage (EBS volumes): Configure storage based on your application’s needs.
    • Advanced Options: Add any additional settings such as user data scripts for instance initialization.

Create an Auto Scaling Group

  1. Navigate to Auto Scaling Groups: In the EC2 dashboard, find the Auto Scaling Groups section under the Auto Scaling category.
  2. Create Auto Scaling Group:
    • Click Create Auto Scaling Group and follow the prompts.
    • Auto Scaling Group Name: Provide a name for your ASG.
    • Launch Template: Select the Launch Template created in Step 1.
    • VPC & Subnets: Choose the VPC and subnets where your instances will be launched. For high availability, select multiple subnets across different Availability Zones.

Define Group Size and Scaling Policies

  1. Set Minimum, Maximum, and Desired Capacity:

    • Minimum Capacity: The lowest number of instances that should always be running.
    • Maximum Capacity: The highest number of instances that can be created.
    • Desired Capacity: The number of instances you want running at any given time (Auto Scaling will maintain this number unless scaling events occur).
  2. Health Check Type:

    • Use EC2 health checks by default. For deeper checks, you can configure Elastic Load Balancer (ELB) health checks.
  3. Scaling Policies:

    • Set up scaling policies that trigger changes in the desired capacity based on CloudWatch metrics (more on this later).

Attach Load Balancer (Optional)

If your application requires traffic distribution, attach an Elastic Load Balancer (ELB) to your Auto Scaling Group.

  1. Choose Load Balancer: You can attach either an Application Load Balancer (ALB) or a Network Load Balancer (NLB) to the ASG.
  2. Configure Listener and Target Groups: Ensure the load balancer can route traffic to the instances in the ASG.

Review and Create

Review your Auto Scaling Group settings and click Create Auto Scaling Group. Your group will automatically start managing the EC2 instances according to your configuration.

Configuring Scaling Policies for Auto Scaling

Scaling policies define the conditions that trigger instance scaling. AWS supports several types of scaling policies:

Target Tracking Scaling

Target Tracking adjusts the capacity of the ASG to maintain a specific metric at a target value. For example, if you want to maintain the CPU utilization at 50%, Auto Scaling will add or remove instances to achieve this target.

  • Go to the Auto Scaling Group in the AWS Console.
  • Click on the Scaling Policies tab and choose Add Policy.
  • Select Target Tracking Scaling.
  • Choose the metric you want to track (e.g., CPU utilization).
  • Set the Target Value (e.g., 50%).

Step Scaling

Step Scaling adjusts capacity in steps when a metric crosses a threshold. For example, if CPU utilization exceeds 70%, you can increase capacity by two instances, and if it exceeds 90%, you can increase capacity by three instances.

  • Select Step Scaling.
  • Define the CloudWatch metric and thresholds.
  • Specify scaling actions (e.g., add 1 instance when CPU exceeds 70%).

Scheduled Scaling

Scheduled Scaling allows you to adjust capacity based on a pre-defined schedule. This is useful for predictable workloads, such as increased demand during business hours.

  • Go to Scheduled Actions in the Auto Scaling console.
  • Specify the start time, recurrence (e.g., daily), and the desired capacity.

Scaling Based on Scheduled Actions

Scheduled scaling ensures that your application has the required capacity at specific times. This can help manage predictable traffic patterns and avoid unexpected scaling events.

  • Go to the Auto Scaling Group dashboard.
  • Click Scheduled Actions.
  • Click Create Scheduled Action.
    • Action Name: Give it a name (e.g., Scale for Traffic Peak).
    • Recurrence: Define the frequency (daily, weekly, or monthly).
    • Start Time: Set the time to begin the scaling action.
    • Desired Capacity: Specify the number of instances to run during this period.

Monitoring Auto Scaling Groups

Monitoring is crucial to ensure that your Auto Scaling Group is working efficiently. AWS provides several tools for monitoring ASGs:

Amazon CloudWatch Metrics

CloudWatch collects performance data from your Auto Scaling Group and instances. You can monitor metrics like:

  • GroupTotalInstances: The total number of instances in the group.
  • GroupInServiceInstances: The number of instances that are running and healthy.
  • 0 användare blev hjälpta av detta svar
Hjälpte svaret dig?