Wednesday, April 18, 2012

replicating an amazon instance to different zones or regions

Advertisements

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.



Steps:
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

Requirements:
We need the following files and keys for during the replication.
Private Key File: pk-PRIVATEKEY.pem
X.509 Certificate File: cert-X509CERT.pem
ACCESS-KEY-ID (S3)
SECRET-ACCESS-KEY (S3)
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.

Example:
[root@hostname ~]# ec2-describe-regions -K pk-PRIVATEKEY.pem -C cert-X509CERT.pem
REGION eu-west-1 ec2.eu-west-1.amazonaws.com
REGION sa-east-1 ec2.sa-east-1.amazonaws.com
REGION us-east-1 ec2.us-east-1.amazonaws.com
REGION ap-northeast-1 ec2.ap-northeast-1.amazonaws.com
REGION us-west-2 ec2.us-west-2.amazonaws.com
REGION us-west-1 ec2.us-west-1.amazonaws.com
REGION ap-southeast-1 ec2.ap-southeast-1.amazonaws.com
[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.

Syntax:
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.
Syntax:
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
Example:
[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
Syntax:
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.