<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>dkume</title>
	<atom:link href="http://daniil.kulchenko.com/blog/feed/" rel="self" type="application/rss+xml" />
	<link>http://daniil.kulchenko.com/blog</link>
	<description>me, life, love, the internet, Perl, Android, cloud, cloud, cloud</description>
	<lastBuildDate>Thu, 27 Oct 2011 22:59:36 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Virtualization using LXC (Linux containers) in Amazon EC2</title>
		<link>http://daniil.kulchenko.com/blog/2011/10/virtualization-using-lxc-linux-containers-in-amazon-ec2/</link>
		<comments>http://daniil.kulchenko.com/blog/2011/10/virtualization-using-lxc-linux-containers-in-amazon-ec2/#comments</comments>
		<pubDate>Mon, 17 Oct 2011 17:58:59 +0000</pubDate>
		<dc:creator>dku</dc:creator>
				<category><![CDATA[Articles]]></category>

		<guid isPermaLink="false">http://daniil.kulchenko.com/blog/?p=55</guid>
		<description><![CDATA[(Note: this is mirrored from the Phenona blog; Phenona was acquired by ActiveState, so the original post is no longer accessible. Update 10/27: now also available at the ActiveState blog.) Virtualization EC2 is already a (para)virtualized environment, which means it&#8217;s nearly impossible to run your own virtualization (KVM/VirtualBox/qemu) from inside that environment. However, Linux, being the getting-things-done operating [...]]]></description>
			<content:encoded><![CDATA[<p><em>(Note: this is mirrored from the Phenona blog; Phenona was acquired by ActiveState, so the original post is no longer accessible.</em><strong> Update 10/27</strong>: now also available at the <a href="http://www.activestate.com/blog/2011/10/virtualization-ec2-cloud-using-lxc" target="_blank">ActiveState blog</a>.<em>)</em></p>
<p><span class="Apple-style-span" style="font-size: 15px; font-weight: bold;">Virtualization</span></p>
<p>EC2 is already a <a href="http://en.wikipedia.org/wiki/Paravirtualization" target="_blank">(para)virtualized</a> environment, which means it&#8217;s nearly impossible to run your own virtualization (KVM/VirtualBox/qemu) from inside that environment. However, Linux, being the getting-things-done operating system, recently introduced a new system into the kernel, called <code>cgroups</code>, which provides a way to isolate process groups from each other in the kernel. A project was soon formed around this new technology, which allows for very thin, fast, and secure quasi-virtualization. It&#8217;s called <strong><strong>LXC</strong></strong>, short for LinuX Containers. And it works in EC2 perfectly.</p>
<p><strong>Here&#8217;s how.</strong></p>
<p>You&#8217;ll want a recent Linux AMI (preferrably kernel 2.6.35 or higher). I use <em>Ubuntu Server 11.04</em>, and the following instructions are meant for that OS. Can&#8217;t vouch for other distros, though the instructions should be easily portable. Ubuntu&#8217;s good for EC2 and <strong>LXC</strong> because they already have <a href="http://uec-images.ubuntu.com/releases/11.04/release/" target="_blank">nice pre-made AMI images</a>, the kernel supports <strong>LXC</strong> out of the box, and they have software repositories hosted in the EC2 cloud, which makes for extremely fast system updates. Also, any instance type works, even a <code>t1.micro</code> (micro instance) will suffice (my weapon of choice for testing purposes).</p>
<h2></h2>
<p>Start by SSH-ing into your EC2 server. You&#8217;ll need to run almost all of the following instructions as root, so let&#8217;s do:</p>
<p><code>sudo -i</code></p>
<p>to become root. Otherwise, you can prepend &#8216;sudo&#8217; to the beginning of every command from now on (unless specified otherwise).</p>
<p>Now, we need to install a few packages:</p>
<p><code>apt-get update &amp;&amp; apt-get install <strong>lxc</strong> debootstrap bridge-utils dnsmasq</code></p>
<p>From the packages you installed, <strong>lxc</strong> is, well, <strong>lxc</strong>. debootstrap is a utility used to create a minimal Ubuntu install within a directory (which we will do shortly). bridge-utils is a suite of utilities used for creating <a href="http://en.wikipedia.org/wiki/Bridging_(networking)" target="_blank">network bridges</a> in Linux, which we will use to provide network access to the container. dnsmasq is a DNS/DHCP server which will allow the container(s) to identify themselves on the local network.</p>
<p>Now run <code><strong>lxc</strong>-checkconfig</code> and make sure that the tests pass (all of them should, if you&#8217;re using Ubuntu Server 10.10).</p>
<p><strong>NOTE: THIS IS IMPORTANT!</strong> Keep in mind that the effects of most of the commands from here on out (specifically iptables, sysctl, mount, brctl and any edits to /etc/resolv.conf) <strong>will not persist over a reboot</strong>, even on a EBS-backed instance. These are in-memory changes which <strong>will go away</strong> as soon as you shut down the machine. If you bring the instance back up, you&#8217;ll need to run them again, otherwise things <strong>will be broken</strong>! There are several ways to get around this: iptables rules and /etc/resolv.conf can be set by an init script, sysctl can be set in sysctl.conf, mounts can be specified in /etc/fstab, and brctl can be set in /etc/network/interfaces (add the br0 interface); however, for the purposes of this guide (I don&#8217;t use EBS-backed instances, personally), we&#8217;ll assume instance storage (config is lost on reboot).</p>
<p>We need to create a place on the system to hold cgroup information (required for <strong>LXC</strong> to work). I use /cgroup for simplicity. Let&#8217;s mount a cgroup environment there.</p>
<p><code>mkdir /cgroup<br />
mount -t cgroup none /cgroup</code></p>
<p>(If you want to keep cgroup mounted between reboots, do: <code>echo "none /cgroup cgroup defaults 0 0" &gt;&gt; /etc/fstab</code> instead.)</p>
<p>Now, let&#8217;s create a network bridge for the containers to be able to connect to the network/Internet. Simply run:</p>
<p><code>brctl addbr br0<br />
brctl setfd br0 0<br />
ifconfig br0 192.168.3.1 up</code></p>
<p>In the second command, we use 192.168.3.* as the container network for the purposes of this tutorial. If you&#8217;d like to use another subset (192.168.<strong>x</strong>.*), you&#8217;re free to do so, but be sure to change every instance of 3.* in this article to yourchoice.*, because those IPs will be references a large amount of times in configuration. We recommend you keep the IPs we suggest here for simplicity purposes.</p>
<p>Now we need to set up a few system rules for the containers to be able to reach the Internet:</p>
<p><code>iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE<br />
sysctl -w net.ipv4.ip_forward=1</code></p>
<p>Let&#8217;s set up DHCP/DNS on our new bridge. Open up <code>/etc/dnsmasq.conf</code> for editing (vim/nano/ed/cat, your choice). Uncomment the necessary lines so that the conf file looks like the following:</p>
<p><code>domain-needed<br />
bogus-priv<br />
interface = br0<br />
listen-address = 127.0.0.1<br />
listen-address = 192.168.3.1<br />
expand-hosts<br />
domain = containers<br />
dhcp-range = 192.168.3.50,192.168.3.200,1h</code></p>
<p>Now, you&#8217;ll need to edit <code>/etc/dhcp3/dhclient.conf</code> for DNS to properly resolve locally. Add the following lines to the beginning:</p>
<p><code>prepend domain-name-servers 127.0.0.1;<br />
prepend domain-search "containers.";</code></p>
<p>(Don&#8217;t forget the dot after <code>containers</code>, that&#8217;s not a typo!)</p>
<p>Now we need to renew our DHCP lease so that dhclient will regenerate /etc/resolv.conf.</p>
<p><code>dhclient3 -e IF_METRIC=100 -pf /var/run/dhclient.eth0.pid -lf /var/lib/dhcp3/dhclient.eth0.leases eth0</code></p>
<p>Now, let&#8217;s restart dnsmasq so it&#8217;ll re-read the new configuration.</p>
<p><code>service dnsmasq restart</code></p>
<h2></h2>
<p>Next, we need to create the environment inside the container. There&#8217;s a script that comes with <strong>lxc</strong> called <strong>lxc</strong>-ubuntu, which will set up the container. However, it&#8217;ll require a bit of tweaking for the environment to work. I&#8217;ve done the tweaking for you, and put the new script up, so simply run: <strong>(updated 4/30 for Ubuntu Server 11.04)</strong></p>
<p><code>wget -O <strong>lxc</strong>-ubuntu http://bit.ly/ec2ubuntulxc<br />
chmod +x <strong>lxc</strong>-ubuntu</code></p>
<p>(If you&#8217;d like to do the tweaking yourself, you&#8217;ll need to pull the script from /usr/lib/<strong>lxc</strong>/templates/<strong>lxc</strong>-ubuntu, change the hostname, change the mirror to match EC2&#8242;s Ubuntu mirror, fix the sshd runlevel script, and change the networking config of <strong>LXC</strong> for DHCP veth (check my script to see how it&#8217;s set up)).</p>
<p>Now, let&#8217;s create a new container:</p>
<p><code>./<strong>lxc</strong>-ubuntu -p /mnt/vm0 -n vm0</code></p>
<p>Wait a while for the script to finish, and your container is set up in /mnt/vm0. Let&#8217;s try it out!</p>
<p><code><strong>lxc</strong>-start -n vm0</code></p>
<p>Type in root for the username and root for the password. Try pinging Google:</p>
<p><code>ping www.google.com</code></p>
<p>If it works, your Internet is set up! Now let&#8217;s try another thing <strong>(make sure you run this from the VM, not from the host!!)</strong>:</p>
<p><code>poweroff</code> (this shuts down the VM, and puts you in the host again)<br />
<code><strong>lxc</strong>-start -n vm0 -d </code>(this runs the VM in daemon mode)</p>
<p>To check if a VM is running, type:</p>
<p><code><strong>lxc</strong>-info -n vm0</code></p>
<p>(it should say RUNNING). To test network, try pinging the VM (this might not work right away, you might have to wait up to a minute):</p>
<p><code>ping vm0<br />
ssh root@vm0</code></p>
<p>If those two work, the VM is now in your DNS and you can address it by its hostname. Cool, huh?</p>
<h2></h2>
<h3>Creating a new VM</h3>
<p>Creating another VM is as simple as:</p>
<p><code>./<strong>lxc</strong>-ubuntu -n vm1 -p /mnt/vm1</code></p>
<p>The packages won&#8217;t be redownloaded, and the command should complete quickly.</p>
<h3>Clone existing VM</h3>
<p>If you want to clone your existing VM, you&#8217;ll need to do a few things:</p>
<p><code>cp -r /mnt/vm0 /mnt/vm1</code></p>
<p>Now edit /mnt/vm1/config and replace all references of vm0 to vm1. Do the same with /mnt/vm1/fstab. Then go into /mnt/vm1/rootfs/etc/hostname and replace the hostname with vm1. Finally, run:</p>
<p><code><strong>lxc</strong>-create -n vm1 -f /mnt/vm1/config</code></p>
<p>Upon starting the VM, you should be able to ping it/ssh to it:</p>
<p><code>ping vm1<br />
ssh root@vm1</code></p>
<p>If not, <strong>lxc</strong>-console into the VM and check your connection. Keep in mind you only need one <code>br0</code> for all your instances, but you can create many, if you so desire.</p>
<h3>Running services inside the container</h3>
<p>At <strong>Phenona</strong>, we run Perl web servers and the like inside these containers. You may want them to be accessible from outside the VM (from the rest of EC2, or outside EC2). To do this, you&#8217;ll need to port forward from the host to the VM. Simply run:</p>
<p><code>iptables -t nat -A PREROUTING -p tcp --dport &lt;port on host&gt; -j DNAT --to-destination &lt;hostname of VM&gt;:&lt;port on VM&gt;</code></p>
<h3>Hibernating a container</h3>
<p>To &#8216;hibernate&#8217; a container (save the current state (running processes) of the VM for instant restoring later) do:</p>
<p><code><strong>lxc</strong>-freeze -n vm0</code></p>
<p>and later,</p>
<p><code><strong>lxc</strong>-unfreeze -n vm0</code></p>
<p>to restore.</p>
<h3>Installing additional packages into the container</h3>
<p>Your container is just like any other Ubuntu system. Therefore,</p>
<p><code>apt-get update<br />
apt-get install &lt;whatever&gt;</code></p>
<p>works great.</p>
<h3>Setting resource limits</h3>
<p>One of the benefits of <strong>LXC</strong> is that you can limit resource usage per-container. Let&#8217;s delve into the various resources you can limit:</p>
<p><strong>CPU</strong></p>
<p>There&#8217;s two ways of limiting CPU in <strong>LXC</strong>. On a multi-core system, you can assign different CPUs to different containers, as such (add this line to your container config file, /mnt/vm0/config or similar):</p>
<p><code><strong>lxc</strong>.cgroup.cpuset.cpus = 0</code> (assigns the first CPU to the container)<br />
or<br />
<code><strong>lxc</strong>.cgroup.cpuset.cpus = 0,2,3</code> (assigns the first, third, and fourth CPU to the container)</p>
<p>The alternative (this one makes more sense to me) is to use the scheduler. You can use values to say &#8216;I want this container to get 3 times the CPU of this container&#8217;. For example, add:</p>
<p><code><strong>lxc</strong>.cgroup.cpu.shares = 2048</code></p>
<p>to the config to give a container double the default (1024).</p>
<p><strong>RAM</strong></p>
<p>To limit RAM, simply set:</p>
<p><code><strong>lxc</strong>.cgroup.memory.limit_in_bytes = 256M</code></p>
<p>(replacing 256M with however much RAM you want to allow).</p>
<p>To limit swap, set:</p>
<p><code><strong>lxc</strong>.cgroup.memory.memsw.limit_in_bytes = 1G</code></p>
<p><strong>Hard Disk</strong></p>
<p>Well, there&#8217;s no official way to do this, it&#8217;s up to you. You can use LVM (in EC2? fun), or you can create a filesystem in a file (something like <code>dd if=/dev/zero of=somefile.img bs=4GB count=1 &amp;&amp; mkfs.ext3 somefile.img &amp;&amp; mount -o loop somefile.img /mnt/vm0/rootfs</code>) and mount it to /mnt/vm0/rootfs to limit space.</p>
<h3>Network Bandwidth</h3>
<p>To limit network bandwidth per container, do some reading on the <code>tc</code> utility. Keep in mind you&#8217;ll need to use separate bridges (br0, br1) for each container if you go this route. Don&#8217;t forget to edit the config of each VM to match your new bridge if you do so.<span class="Apple-style-span" style="font-size: 20px; font-weight: bold;"> </span></p>
<p>Well, this post has gotten horrendously long, but I hope I&#8217;ve covered most basic aspects of using <strong>LXC</strong> in the EC2 environment. <strong>LXC</strong> is an amazing technology, and the possibilities of what you can do with it are endless.</p>
<p>Thanks to the <a href="http://blog.foaa.de/2010/05/lxc-on-debian-squeeze" target="_blank">Foaa</a> and <a href="http://blog.mudy.info/2010/07/linux-container-on-amazon-ec2-server/" target="_blank">Mudy</a> blogs for getting me started on my way towards a running <strong>LXC</strong>.</p>
<p>Some further reading: the <a href="http://lxc.sourceforge.net/" target="_blank">main <strong>LXC</strong> site</a>, <a href="http://lxc.teegra.net/" target="_blank">the <strong>LXC</strong> HOWTO</a>, and <a href="http://www.ibm.com/developerworks/linux/library/l-lxc-containers/" target="_blank">IBM&#8217;s tutorial</a>.</p>
<p><strong>NOTE: </strong>When following other guides on <strong>LXC</strong>, be very careful with messing with the network in the EC2 environment (restarting networking services or altering /etc/network/interfaces on the <strong>host</strong>) because one wrong command, and the connection will drop between you and your instance (you&#8217;ll lose SSH), and therefore lose your instance completely. I did that many, many times while exploring <strong>LXC</strong>. The instructions I&#8217;ve provided here have been tested and will not drop your EC2 connection, can&#8217;t vouch for the other methods.</p>
]]></content:encoded>
			<wfw:commentRss>http://daniil.kulchenko.com/blog/2011/10/virtualization-using-lxc-linux-containers-in-amazon-ec2/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Brief introductions</title>
		<link>http://daniil.kulchenko.com/blog/2011/05/hello/</link>
		<comments>http://daniil.kulchenko.com/blog/2011/05/hello/#comments</comments>
		<pubDate>Mon, 23 May 2011 03:25:54 +0000</pubDate>
		<dc:creator>dku</dc:creator>
				<category><![CDATA[Myself]]></category>
		<category><![CDATA[blog]]></category>
		<category><![CDATA[me]]></category>

		<guid isPermaLink="false">http://daniil.kulchenko.com/blog/?p=12</guid>
		<description><![CDATA[Hello, I don&#8217;t think we&#8217;ve met. I&#8217;m Daniil Kulchenko, a 15 year old software developer, designer, writer, biker, driver, boater, Facebook-er, fun-have-er, hanging-out-with-friends-er, and a lot of other words that don&#8217;t actually exist. I go by dku on IRC, so this blog&#8217;s name naturally followed. I&#8217;m also the creator of Phenona, the first and only pure-Perl [...]]]></description>
			<content:encoded><![CDATA[<p>Hello, I don&#8217;t think we&#8217;ve met.</p>
<p>I&#8217;m Daniil Kulchenko, a 15 year old software developer, designer, writer, biker, driver, boater, Facebook-er, fun-have-er, hanging-out-with-friends-er, and a lot of other words that don&#8217;t actually exist. I go by dku on IRC, so this blog&#8217;s name naturally followed.</p>
<p>I&#8217;m also the creator of <a href="http://phenona.com/" target="_blank">Phenona</a>, the first and only pure-Perl PaaS online today, which allows you to easily deploy and manage Perl web applications in the cloud.</p>
<p>Welcome aboard. There&#8217;s many tech rants, new ideas, (hopefully) helpful tutorials, and whatever else happens to pop into my mind, ahead. Stay tuned.</p>
]]></content:encoded>
			<wfw:commentRss>http://daniil.kulchenko.com/blog/2011/05/hello/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
	</channel>
</rss>

