{"id":39,"date":"2008-02-02T23:29:00","date_gmt":"2008-02-02T23:29:00","guid":{"rendered":"http:\/\/www.mohanjith.net\/wordpress\/?p=39"},"modified":"2008-02-02T23:29:00","modified_gmt":"2008-02-02T23:29:00","slug":"amazon-ec2-with-rock-solid-persistent","status":"publish","type":"post","link":"https:\/\/mohanjith.net\/blog\/2008\/02\/amazon-ec2-with-rock-solid-persistent.html","title":{"rendered":"Amazon EC2 with rock solid persistent storage"},"content":{"rendered":"<p>With the power of duplicity and chroot we can make a Amazon EC2 image that is as good as a harware node, i.e. with persistent storage. Let me explain how to do it your self as well. However I&#8217;ll be leaving out the minute details.<\/p>\n<p>Step 1: Start an instance of a public AMI<\/p>\n<p>I would recommend ami-76cb2e1f because i you are able to use the same image for x-large and large instance powering up. Also it has the ec2 ami tools installed and patched. Login to the instance as root using the certificate you provided when starting the instance. Also do not forget to give the following as User Data.<\/p>\n<pre>chroot_bucket=[your_bucket_name]<\/pre>\n<p>Step 2: Download and install duplicity and boto<\/p>\n<p>You need to install <a href=\"http:\/\/duplicity.nongnu.org\/\">duplicity<\/a> 0.4.9 or later and <a href=\"http:\/\/code.google.com\/p\/boto\/\">boto<\/a> 1.0 or later.<\/p>\n<p>Step 3: Create a PGP key<\/p>\n<p>Run the following and follow the instructions that will appear. <\/p>\n<pre> # gpg --gen-key<\/pre>\n<p>Please note down the key id because we need it later on. It should look something like  860BCFF6.<\/p>\n<pre>gpg: key <b>860BCFF6<\/b> marked as ultimately trusted<\/pre>\n<p>Step 4: Install libpam-chroot<\/p>\n<p>You have to install libpam-chroot for it to be possible to push the user inside the chroot when the user logs in via ssh.<\/p>\n<p>Step 5: Create the chroot<\/p>\n<p>Create the chroot and install all the applications you need inside the chroot. Read about how to create a chroot in a debian system <a href=\"http:\/\/www.debian.org\/doc\/manuals\/reference\/ch-tips.en.html#s-chroot\">here<\/a>. Create your users inside the chroot. It is important that you understand how chroot works as well.<\/p>\n<p>Step 6: Push the users to chroot<\/p>\n<p>You need to change \/etc\/security\/chroot.cnf and add a line similar to bellow.<\/p>\n<pre>[username] \/mnt\/chroot<\/pre>\n<p>Step 6: Download the scripts<\/p>\n<p>You need to download the scripts archive that contains the scripts necessary to do all the magic to ensure that data actually persist. Download it from <a href=\"http:\/\/www.mohanjith.net\/downloads\/amazon\/ec2\/ec2-chroot-persistence-1.0.tar.gz\">http:\/\/www.mohanjith.net\/downloads\/amazon\/ec2\/ec2-chroot-persistence-1.0.tar.gz<\/a><\/p>\n<p>Step 7: Extract and edit the scripts<br \/>Extract the scripts from out side the chroot, preferably in \/.<\/p>\n<pre> # cd \/<br \/> # tar -xzf [path_to_archive]\/ec2-chroot-persistence-1.0.tar.gz<\/pre>\n<p>You need to edit \/etc\/init.d\/ec2 and \/etc\/ec2\/cron and change the lines that look like bellow.<\/p>\n<pre>export AWS_ACCESS_KEY_ID=[your_aws_access_key_id]<br \/>export AWS_SECRET_ACCESS_KEY=[your_aws_secret_access_key]<br \/>export PASSPHRASE=[your_gpg_passphrase]<br \/>export gpg_key=[your_gpg_key_id]<\/pre>\n<p>Step 8: Set up the scripts<\/p>\n<p>You will also have to setup a cron job outside the chroot to backup the data to S3. The script to invoke is \/etc\/ec2\/cron. I would recommend hourly backups, but anything more frequently will be bad because the time it takes to backup will increase drastically.<\/p>\n<p>You will also have to make sure ec2 service (\/etc\/init.d\/ec2) is run on power on, power off and restart. To do that you will have to create sym links to \/etc\/init.d\/ec2 from \/etc\/rc0.d\/K10ec2, \/etc\/rc3.d\/S90ec2, \/etc\/rc4.d\/S90ec2, and \/etc\/rc6.d\/K10ec2.<\/p>\n<p>Step 9: Where to persist data.<\/p>\n<p>Run the bellow as root outside the chroot.<\/p>\n<pre>curl http:\/\/169.254.169.254\/2007-08-29\/user-data > \/tmp\/my-user-data<\/pre>\n<p>Step 8: Remaster the AMI<\/p>\n<p>Step 10: Create your machine image<br \/>Read more about creating an machine image at Amazon EC2 Getting started guide <a href=\"http:\/\/docs.amazonwebservices.com\/AWSEC2\/2007-08-29\/GettingStartedGuide\/\">here<\/a>.<\/p>\n<p>Step 11: Back up your chroot<br \/>Run \/etc\/ec2\/cron to back up the chroot.<\/p>\n<p>Step 12: Power off and power on<\/p>\n<p>Power off the instance you are running with the public image and when it has properly shutdown, start the image we just created in step 10 with the chroot_bucket with the same bucket you provided when you power up the public image.<\/p>\n<p>All the data in \/mnt\/chroot is backed up to S3 by \/etc\/ec2\/cron and when the instance is started after a shutdown \/mnt\/chroot is restored from S3. The script is configured to backup on power down but it is always recommended to run \/etc\/ec2\/cron just before a power down.<\/p>\n<p>You might also want to set up dynamic DNS for your instance such that you don&#8217;t have to always try hard to remember the ugly public DNS provided by Amazon. You can use <code>ddclient<\/code> to update the dynamic DNS service with your new IP. You can install ddclient inside the chroot.<\/p>\n<p>This method was tested for more than 1 month and everything worked smoothly for me, but depending on your configuration your experience may defer. It is always good to test before you use in production environment.<\/p>\n<div id=\"fb-like\" style=\"\"><iframe src=\"http:\/\/www.facebook.com\/plugins\/like.php?href=https:\/\/mohanjith.net\/blog\/2008\/02\/amazon-ec2-with-rock-solid-persistent.html&amp;layout=standard&amp;show_faces=true&amp;width=300&amp;action=like&amp;font=&amp;colorscheme=light&amp;locale=en_US\" scrolling=\"no\" frameborder=\"0\" allowTransparency=\"true\" style=\"border:none; overflow:hidden; width:300px; height:30px\"><\/iframe><\/div>","protected":false},"excerpt":{"rendered":"<p>With the power of duplicity and chroot we can make a Amazon EC2 image that is as good as a harware node, i.e. with persistent storage. Let me explain how to do it your self as well. However I&#8217;ll be leaving out the minute details. Step 1: Start an instance of a public AMI I &#8230; <a title=\"Amazon EC2 with rock solid persistent storage\" class=\"read-more\" href=\"https:\/\/mohanjith.net\/blog\/2008\/02\/amazon-ec2-with-rock-solid-persistent.html\" aria-label=\"More on Amazon EC2 with rock solid persistent storage\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"enabled":false},"version":2}},"categories":[95,98,99,97,96,94],"tags":[407,410,411,409,408,406],"class_list":["post-39","post","type-post","status-publish","format-standard","hentry","category-amazon","category-boto","category-chroot","category-duplicity","category-ec2","category-s3","tag-amazon","tag-boto","tag-chroot","tag-duplicity","tag-ec2","tag-s3"],"aioseo_notices":[],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p5lUHm-D","jetpack_likes_enabled":false,"_links":{"self":[{"href":"https:\/\/mohanjith.net\/blog\/wp-json\/wp\/v2\/posts\/39","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mohanjith.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mohanjith.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mohanjith.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mohanjith.net\/blog\/wp-json\/wp\/v2\/comments?post=39"}],"version-history":[{"count":0,"href":"https:\/\/mohanjith.net\/blog\/wp-json\/wp\/v2\/posts\/39\/revisions"}],"wp:attachment":[{"href":"https:\/\/mohanjith.net\/blog\/wp-json\/wp\/v2\/media?parent=39"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mohanjith.net\/blog\/wp-json\/wp\/v2\/categories?post=39"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mohanjith.net\/blog\/wp-json\/wp\/v2\/tags?post=39"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}