Mirage is emerging as a wondrous approach for producing specialised applications for the cloud.
The following provide fine introduction:
There are a number of fine articles describing the production of Mirage unikernels for serving static sites:
Rather than simply cloning one of the above approaches, I wanted to go with Test Kitchen (with Vagrant) for unikernel production with Packer for composing AMIs suitable for launching on EC2.
(For the moment, Packer is not used within the workflow. This will be revisited shortly. Fabric and boto will be used for the AMI production.)
We will be using the wonderful poole for our exemplar site, choosing the Lanyon theme.
The following was performed on Mac OS X 10.9.2.
$ git clone https://github.com/sstephenson/rbenv.git ~/.rbenv
$ echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bash_profile
$ echo 'eval "$(rbenv init -)"' >> ~/.bash_profile
$ curl -O http://www.openssl.org/source/openssl-1.0.1g.tar.gz
$ tar xvf openssl-1.0.1g.tar.gz
$ cd openssl-1.0.1g
$ ./Configure darwin64-x86_64-cc --prefix=$HOME/local/openssl-1.0.1g
$ make
$ make install
$ curl -O http://cache.ruby-lang.org/pub/ruby/2.1/ruby-2.1.1.tar.gz
$ tar xvf ruby-2.1.1.tar.gz
$ cd ruby-2.1.1
$ ./configure --with-openssl-dir=$HOME/local/openssl-1.0.1g --with-openssl-include-dir=$HOME/local/openssl-1.0.1g/include/openssl --with-openssl-lib-dir=$HOME/local/openssl-1.0.1g/lib --prefix=$HOME/.rbenv/versions/2.1.1
$ make
$ make install
$ rbenv rehash
$ ruby -v
ruby 2.0.0p247 (2013-06-27 revision 41674) [universal.x86_64-darwin13]
$ rbenv versions
* system (set by /<home_dir>/.rbenv/version)
2.1.1
$ ruby -v
ruby 2.1.1p76 (2014-02-24 revision 45161) [x86_64-darwin13.0]
$ rbenv global 2.1.1
$ rbenv rehash
Reading the Getting Started Guide for installing Test Kitchen and Berkshelf. These feature in this project's Gemfile
.
$ which gem
/<home_dir>/.rbenv/shims/gem
$ gem install bundler
$ rbenv rehash
Notice the rbenv rehash
above: this follows the direction "Run this command after you install a new version of Ruby, or install a gem that provides commands" from https://github.com/sstephenson/rbenv#rbenv-rehash
.
We install Vagrant 1.5.2.
We install VirtualBox 4.3.10.
$ git clone https://github.com/iw/mirage-lanyon.git
$ git clone https://github.com/iw/mirage-jekyll.git
And install Test Kitchen and Berkshelf:
$ cd mirage-jekyll
$ bundle install
$ bundle show test-kitchen
/<home_dir>/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/test-kitchen-1.2.1
$ kitchen version
Test Kitchen version 1.2.1
$ bundle show berkshelf
/<home_dir>/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/berkshelf-2.0.14
$ rbenv rehash
$ cd ..
Having installed Jekyll through Bundler
we can build our site:
$ cd mirage-lanyon
$ jekyll build
$ cd ..
We chose to use pyenv for managing our python environments.
$ cd mirage-jekyll
$ pyenv system
$ python --version
Python 2.7.5
$ mkvirtualenv mirage-jekyll
$ export CFLAGS=-Qunused-arguments
$ export CPPFLAGS=-Qunused-arguments
$ pip install -r requirements.txt
$ cd ..
Notice: .kitchen.yml
shares the static site folder (mirage-lanyon
) with the Vagrant VM. This will need to be changed when choosing your own static site folder.
$ cd mirage-jekyll
$ kitchen create
$ kitchen converge
$ fab install_mirage
You can test the Mirage UNIX build with:
$ fab build_unix_unikernel:build_dir=/home/vagrant/mirage-lanyon/_mirage
To build the Mirage Xen unikernel:
$ fab build_xen_unikernel:build_dir=/home/vagrant/mirage-lanyon/_mirage
The following assumes we have defined:
mirage-www
mirage-unikernel-builder
(allowing SSH access)mirage-www
(allowing HTTP access on port 80)AWS_ACCESS_KEY
and AWS_SECRET_KEY
containing the respective AWS access keysBuilding the unikernel:
$ fab -i ~/.ssh/mirage-www.pem build_mirage_ami:access_key="$AWS_ACCESS_KEY",secret_key="$AWS_SECRET_KEY",xen_unikernel=`pwd`/../mirage-lanyon/_mirage/mir-www.xen
Note the "Registered unikernel AMI" that is displayed from performing the above.
Launching the unikernel:
Using the unikernel AMI from the previous step, we can now launch an instance serving our static site:
$ fab -i ~/.ssh/mirage-www.pem launch_mirage:access_key="$AWS_ACCESS_KEY",secret_key="$AWS_SECRET_KEY",image_id=<unikernel_ami_id>
If you have the AMI Tools installed you can monitor the progress of the launching of the unikernel with:
$ ec2-get-console-output --region eu-west-1 <unikernel_instance_id>