背景
应用迁移到AWS公有云,通常我们使用ELB实现应用高可用与负载均衡,实际存在部分负载不大,通过EIP发布的应用,如果应用所在EC2出现故障或者可用区出现故障时,应用不可用。 本文介绍了使用AWS服务实现上述场景下应用高可用的方法。
原理
通过EC2自身状态检查功能实现EC2的终止操作。
通过AutoScaling功能实现EC2节点数监控,启动新的EC2实例。
步骤
- 创建一个IAM角色,可执行ec2 CLI的命令;
- 创建EC2,具有步骤1中IAM角色,部署应用后,绑定EIP;
- EC2上创建一个脚本,增加执行脚本到rc.local文件中,实现开机自动绑定EIP,内容如下:
#! /bin/bash instanceid=`curl 52.83.57.155//169.254.169.254/latest/meta-data/instance-id` aws configure set region cn-northwest-1 --profile user1 export AWS_DEFAULT_PROFILE=user1 aws ec2 associate-address --instance-id $instanceid --allocation-id eipalloc-090bf95cf8d916786 替换加粗字体部分为实际EIP ID |
- 测试无问题后,用上述EC2制作AMI镜像文件(每次应用升级制作新的AMI镜像);
- 配置EC2 系统状态检查 VS 实例状态检查,当状态异常时,执行终止实例操作;
- 基于步骤4制作的AMI创建启动配置,具备步骤1中的角色;
- 使用步骤6的启动配置增加Auto Scaling 组,子网指定多可用区多子网,组大小为1;
- 将当前EC2增加到步骤7中的Auto Scaling 组,配置完成。
上述配置完成后,当EC2状态异常或者可用区状态异常时,实现应用分钟级的自动化故障切换,增加应用的高可用性。
Background
When migrating applications to AWS public cloud, we typically use ELB to achieve application high availability and load balancing. In reality, there are some applications with low load that are published through EIP. If the EC2 where the application is located fails or the availability zone fails, the application becomes unavailable. This article introduces a method to achieve application high availability in the above scenario using AWS services.
Principle
Use EC2's own status check function to implement EC2 termination operation.
Use AutoScaling function to monitor EC2 node count and start new EC2 instances.
Steps
- Create an IAM role that can execute EC2 CLI commands;
- Create an EC2 with the IAM role from step 1, deploy the application, and bind an EIP;
- Create a script on the EC2, add the script execution to the rc.local file to automatically bind EIP on boot, with the following content:
#! /bin/bash instanceid=`curl http://169.254.169.254/latest/meta-data/instance-id` aws configure set region cn-northwest-1 --profile user1 export AWS_DEFAULT_PROFILE=user1 aws ec2 associate-address --instance-id $instanceid --allocation-id eipalloc-090bf95cf8d916786 Replace the bold part with the actual EIP ID |
- After testing without issues, create an AMI image file from the above EC2 (create a new AMI image for each application upgrade);
- Configure EC2 system status check vs instance status check, when status is abnormal, execute instance termination operation;
- Create a launch configuration based on the AMI from step 4, with the role from step 1;
- Use the launch configuration from step 6 to add an Auto Scaling group, specify multiple availability zones and subnets for the subnet, group size is 1;
- Add the current EC2 to the Auto Scaling group from step 7, configuration complete.
After the above configuration is complete, when EC2 status is abnormal or availability zone status is abnormal, automatic failover at the minute level is achieved for the application, increasing application high availability.