Wednesday, April 18, 2012

replicating an amazon instance to different zones or regions


We have to replicate an amazon ec2 instance running in one zone or  region to other zones for load balancing and high availability. Or you may want to migrate your instance to other region for low latency. We can do this by ec2-migrate-bundle command. First we have to create an image. See this post for creating image of amazon linux instance. Then we have to we create a s3 bucket in the destination zone and migrate the image to the destination bucket. Then we can register ami based on that and create instances.

1) Determining the zone to create the new instance.
2) Creating Amazon s3 bucket in the new zone.
3) Migrating the bundle
4) Registering the AMI
5) Creating new instance based on the AMI
6) Testing the AMI

We need the following files and keys for during the replication.
Private Key File: pk-PRIVATEKEY.pem
X.509 Certificate File: cert-X509CERT.pem
Existing instance stored AMI
Security Group and key pair in destination zone.

Determining the zone :
First we have to determine Amazon zone we are going to use. There are a few zones. We can get the zone details using the command ec2-describe-regions.

[root@hostname ~]# ec2-describe-regions -K pk-PRIVATEKEY.pem -C cert-X509CERT.pem
REGION eu-west-1
REGION sa-east-1
REGION us-east-1
REGION ap-northeast-1
REGION us-west-2
REGION us-west-1
REGION ap-southeast-1
[root@hostname ~]#

Creating Amazon s3 bucket in the required zone :
Once the zone is decided we have to get the AMI of the instance available in the zone. For that we have to,
1) Create an amazon s3 bucket in the region
2) Migrate the bundle to the newly created bucket
3) Register the AMI

Migrating the bundle :
While migrating the bundle we need to understand a few things. Same kernel and ramdisk may not be available in the destination zone. You may have to either specify them explicitly or let the Amazon decide which is best.

ec2-migrate-bundle -k pk-key.pem -c cert-cert.pem -a Access-Key-Id -s Secret-Access-Key --bucket source_bucket --destination-bucket destination_bucket --manifest NAME.ami.manifest.xml --region ZONECode
ZONE-Code – is important because based on this ec2-migrate tool will decide the best kernel and ramdisk in the destination zone.

Registering the AMI :
We have to register the AMI with Amazon. Then only we can create instances based on that AMI.
ec2-register -K pk-PRIVATEKEY.pem -C cert-X509CERT.pem -n name_of_the_AMI Full_path_of_manifest.xml_in_s3_bucket --region zone-id
[root@hostname_of_instace image]# ec2-register -K pk-ABCDEFGHIJKLMN.pem -C cert-ABCDEFGHIJKLMN.pem -n nagios bucket_name/images/nagios.ami.manifest.xml --region us-west-2
IMAGE ami-wwwwww
[root@hostname_of_instace image]#

It will give the AMI ID as the output. In this case,
IMAGE ami-wwwwww
--region zone-id is important. Else it will create the instance in default zone (virginia)

Creating new instance based on the AMI
ec2-run-instances -K pk-ABCDEFGHIJKLMN.pem -C cert-ABCDEFGHIJKLMN.pem -g Basics -k cdnkey ami-ID --region us-west-2
-g is for the Security group. We have to specify which security group we are using.
-k is for key type. We have to specify which key type we are using.
We have to have the key pair and security group prior to instance launch.
Last field is the AMI-ID based on the instance will be created.

Testing the AMI:
After launching the new instance we must check a few things:
Check the following things are same for original and new instances:
Disk usage
Services running
Accessibility of services such as ssh, http, tomcat and mysql
Ensuring mysql database is up-to-date.
All the important data are present.

Recommended Reading

1. Host Your Web Site In The Cloud: Amazon Web Services Made Easy: Amazon EC2 Made Easy
2. Programming Amazon Web Services: S3, EC2, SQS, FPS, and SimpleDB
3. Middleware and Cloud Computing: Oracle on Amazon Web Services (AWS), Rackspace Cloud and RightScale (Volume 1)

No comments:

Post a Comment

Be nice. That's all.