Share
Explore

icon picker
ASG + Load balancer

Implementing auto scaling group on memzo's production server to ensure the high availability, reliability and optimized billing.

💡 Purpose

Implementation of Auto Scaling Group with Load Balancer can ensure the reliability of system. This implementation will target to auto scale the production server of Memzo with Staging Database on some other domain/sub-domain.

🛤 Steps


Memzo Instance AMI Creation
Test/Staging DB Setup
Launch Instance and Change DB config
Ensure if DB is functioning correctly using Dashboard
Create New AMI for Launch
Configured Launch Template for ASG (with custom user data)
Configure Auto Scaling Group
Add Network Load Balancer & Target Group
Adding ASG Policies and Ensuring execution
Testing Scaling instances using 'stress'
Setting up Elastic IP with Network Load Balancer
Creating SSL for some domain (Ex: )
Configuring SSL in Load Balancer
Accessing using Domain Name and testing ASG Activities

❓ Difficulties Identified


1. When instance is added by ASG, Apache httpd service is stopped by default so need to configured launch template again to add User Data (Startup Script) when instance is being launched. Ref : instance Config -> Advance options -> user data
2. Application load balancer doesn't support Elastic IP. So tried with Network Load Balancer but It created a loop of https request redirections. First solution is to use application load balancer - Ref :
Another solution is - to change .htaccess configurations. 3. If there are crons running on server then Auto Scaling can lead to a state of Clashing Crons that can create challenges. First solution is to Keep crons on other Server. Another solution is to Allow only first instance of ASg to run crons. Ref: https://serverfault.com/questions/727466/running-crontab-on-aws-auto-scaling-group 4. If Server is Using EBS volumes then ASG will replicate the EBS volumes for each instance and will clear all storage when the instance is terminated. To attach an EBS Volume to the instance is tricky in Auto Scaling Group. Solution - Decoupling Compute from Storage. Ref : https://portworx.com/wp-content/uploads/2017/05/asg-slides.pdf Another solution is to keep only one EBS volume and attach the same volume to instance while the instance is launching using the Advanced user-data in Launch Template configurations. Ex:

5. Practically, when the instance will be terminated the volume will stay attached/in-use until the instance terminated successfully.
Meanwhile, New instance is being launched by ASG and user-data will find volume is attached/in-use by another instance, and it’ll create a new volume.
To solve this. Analyse Auto Scaling Group lifecycle hooks.

6. EBS Attchment is possible for the instances of same availability zone.



Create AMI of Current Instance

Launch Instance using AMI -
Consider following changes in launch server:
Database configuration with Staging
Remove Crons
Change .htaccess redirections. Related to force http → https
Create New AMI

Modify Current Volumes attached with Live Memzo Instance
Memzo Instance id -
Volume id - (100 GB)
Volume id - (50 GB)
Keep only one volume with enough required storage.
Also, check availability zone of EBS volume. As per the AWS policy volume available in speific zone can be atttached to multiple instances of same zone only.

Launch Template
Use above AMI image in Launch template and keep desired instance type (t2.small in our case), security group, etc.
Dont add any EBS volume in launch template. We’ll attach it using advance user-data of instance.
User-data to Add in Launch Template Configuration
#!/bin/bash
sudo -i
service httpd start
$instanceId = Invoke-WebRequest -Uri http://169.254.169.254/latest/meta-data/instance-id
aws ec2 attach-volume --volume-id vol-0966b056db6a9e3f5 --instance-id $instanceId --device /dev/xvda

EBS Volume can be attach to instances available in same availability zone.

OR
Attach the Fix ELB Volume while configuring the launch template. Ensure that ELB allows multi-attach instances. Modify ELB to type io1/io2

To get region of instance if required -

Preventing EBS Volume from getting deleted on Instance Termination


Create Auto Scaling Group
Create New ASG using Launch Template, also new ALB can be created simultaneous (if not already available).
After ASG Launch, create Scaling Policy
image.png
Check if ALB and Target Groups are working correctly.

If Targets Group Health Check is Unhealthly
image.png
Go to Helath Checks → Health Check Settings → Edit
Advanced Health Check Settings → Success Codes → 200, 301, 302
image.png

Configuring ALB
Add Listener for SSL with SSL Certificate
image.png

image.png

Testing Auto Scaling Group
Using stress to increase cpu load, and moniter ASG activity
image.png

Connecting ALB with Route53 and Godaddy
Creating SSL in AWS Certificate Manager
image.png
Configuring/ Adding SSL in ALB
image.png
In Route53 create Hosting zones for your domain/sub-domain
image.png
Change Domain Name Server in Godaddy
image.png
Create Alias to Point Load Balancer with A Record
image.png


Want to print your doc?
This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (
CtrlP
) instead.