user2994125
user2994125

Reputation: 27

How to Duplicate an EC2 Instance for HA Purpose

Is there a way to duplicate a EC2 instance in the same VPC in different AZ for HA purpose, so that when the primary instance is bad (e.g. due to check status failure), I can shut it down and quickly switch over to the standby one.

For some reason, I need to keep the same private IP address, and public/EIP IP address. For the EIP address, I can dis-associate it from the OLD instance, and re-associate it to the NEW instance, but how about the private IP address?

Any suggestion? Thanks in advance...

Upvotes: 0

Views: 1137

Answers (2)

John Rotenstein
John Rotenstein

Reputation: 269881

What is "an instance"?

It's worth thinking about what people mean when they talk about "duplicating an EC2 instance". An EC2 instance consists of:

  • The boot disk with Operating System
  • Any data disks
  • Network settings (eg public & private IP addresses)
  • Configurations (eg Instance Type, User Data, Tags, etc)

Duplicating an EC2 instance usually means launching another instance with the same configuration, but it's not necessarily a "duplicate". For example, a different Availability Zone means it will have a different IP address (see below) and the new instance will be booted from an AMI (Amazon Machine Image) rather than being an exact duplicate of the disk from the previous instance.

Duplicates for High Availability

Next comes the issue of High Availability. A new instance can take a few minutes to launch. If the requirement is for near-instant cut-over, then the only solution is to always run more than one instance, and then re-point an IP address or DNS name.

As you mentioned, it is easy and fast to associate an Elastic IP Address to an alternate EC2 instance. This change immediately redirects traffic sent to that IP address.

However, it is not possible to reassign an Internal IP Address to another instance (but see below).

Using Auto Scaling to launch another instance

If the requirement allows for a few minutes of outage, then more possibilities arise. The simplest would be to launch the EC2 instance within an Auto Scaling group. The group can be configured to always have a certain number of instances (eg a minimum of 1 instance). Thus, when an instance fails, Auto Scaling can automatically launch a replacement instance with the same configuration (boot disk, instance type, etc).

Further, Auto Scaling can automatically launch instances in another Availability Zone if a zone fails.

However, please note that internal IP address ranges are associated with Subnets within a VPC (Virtual Private Cloud). Each subnet is associated with a single Availability Zone. The hierarchy is:

  • VPC
    • Availability Zone
      • Subnet (with CIDR range of IP addresses)
        • Instance

Thus, launching an instance in a different Availability Zone (and thus a different Subnet) will require the instance to have a different Internal IP Address.

A hack for reassigning IP addresses

While Internal IP Addresses cannot be reassigned (and especially not between Subnets), an interesting hack was described in the ARC401 session at re:Invent 2014 (see slides 33 & 34, or on YouTube).

This involved associating an IP address with a secondary Elastic Network Interface (ENI), where the IP address falls outside the VPC range. Then, use routing rules to route the traffic destined for that address to the ENI (turning off Source/Dest Check). Effectively, the traffic can be re-routed to a different instance by modifying the routing rules. A bit of a hack, but it apparently works.

Upvotes: 4

E.J. Brennan
E.J. Brennan

Reputation: 46879

You may not be able to keep the same private, primary IP address, but you cano assign a secondary private IP address to an EC2 instance in a VPC, and they are reassignable:

Private IP addresses

When you launch an instance into a VPC, a primary private IP address from the address range of the subnet is assigned to the default network interface (eth0) of the instance. If you don't specify a primary private IP address, we select an available IP address in the subnet range for you.

You can assign additional private IP addresses, known as secondary private IP addresses, to instances that are running in a VPC. Unlike a primary private IP address, you can reassign a secondary private IP address from one network interface to another.

From here: http://docs.aws.amazon.com/AmazonVPC/latest/UserGuide/vpc-ip-addressing.html

Upvotes: 1

Related Questions