Trying CloudFoundry with Vagrant ... and some patience

This post is an updated, working and complete version of an older one I published mistakenly.

I suppose you have a functional ruby env for installing Vagrant, and that you have VirtualBox v4.1 already installed.

Let’s go!

mkdir ~/cloudfoundry
cd ~/cloudfoundry
#(optional) rvm 1.9.2@vagrant --create
gem install vagrant
vagrant box add ubuntu-lucid-64 http://s3.lds.li/vagrant/lucid64.box
vagrant init ubuntu-lucid-64
vagrant up

NB: if you’re getting a general protection fault: 0000 [#3] SMP error, it might be the sign you’re running an other virtualization system on this host (see this post about VirtualBox + kvm errors)

Then connect to your VM with vagrant ssh.

I used to install RVM manually and then install CloudFoundry step by step, manually too. But now the community provided a good install script, so let’s use it directly.

Before that, we should just fix a problem in our Vagrant image, the /home/vagrant/.gem is owned by root:root:

sudo chown vagrant:vagrant /home/vagrant/.gem -R

Then we follow the standard installation steps mentionned in the README:

sudo apt-get install curl -y
bash < <(curl -s -k -B https://raw.github.com/cloudfoundry/vcap/master/setup/install)

When it’s finished (it lasted more than 1.5 hour on my old desktop computer!), log out and vagrant ssh again (so that your groups / env get reloaded).

Before starting cloud foundry, I strongly encourage you to adapt the memory and CPU of your VM if you didn’t before (min 1G RAM and 4 cores for my setup).

Then I logged in again to the VM and prepared to start the whole thing:

rvm rvmrc trust ~/cloudfoundry/vcap
cd ~/cloudfoundry/vcap
bin/vcap start #=>BOOM :/

But I had problems at this point. I think I hit that bug. You should run this commands before starting everything, it won’t hurt if you’re not in my case, because our instance is just a test instance so we don’t need to change default configuration choices:

sed -i 's#.*db:migrate.*#Dir.chdir("\#{File.dirname(__FILE__)}/../cloud_controller") { `bundle exec rake db:migrate` }#' bin/vcap

Then you can finally start everything:

cd ~/cloudfoundry/vcap
bin/vcap start

You should see:

router              :         RUNNING
cloud_controller    :         RUNNING
dea                 :         RUNNING
health_manager      :         RUNNING
stager              :         RUNNING
redis_gateway       :         RUNNING
redis_node          :         RUNNING
mysql_gateway       :         RUNNING
mysql_node          :         RUNNING
mongodb_gateway     :         RUNNING
mongodb_node        :         RUNNING
neo4j_gateway       :         RUNNING
neo4j_node          :         RUNNING

Some components may appear as “STOPPED” if your VM is too slow (cloud_controller in my case). So maybe it’s a good idea to issue a bin/vcap status afterwards to see if everything has booted properly. If not, you can restart a specific service like that: bin/vcap start cloud_controller. It’s probably a good idea to watch the log ‘live’ too: bin/vcap tail to detect any problem. Be careful, it’s really verbose.

One other step, you need to make a local SSL tunnel so that commands sent to api.vcap.me (a conventional domain in cloud foundry ecosystem, which resolves to 127.0.0.1) are forwarded to the VM. If you find a solution to forward your local 80 port to the VM one, go ahead. But communicating to your vagrant VM directly didn’t seem to be that easy for me, so I uncommented the config.vm.forward_port (forward local 8080 to VM’s 80) in the Vagrantfile, restarted the VM (vagrant halt; vagrant up), and started CF services as described above.

When everything’s ok, log out and, from the host:

gem install vmc
vmc target api.vcap.me:8080
vmc info

You should see something like:

VMware's Cloud Application Platform
For support visit http://support.cloudfoundry.com

Target:   http://api.vcap.me:8080 (v0.999)
Client:   v0.3.14

Now we can (finally!) play with your new toy. First register and login:

vmc register --email foo@bar.com --passwd password
vmc login --email foo@bar.com --passwd password

And build our first app (adapted from the README):

mkdir env && cd env
echo -e "require 'rubygems'\nrequire 'sinatra'\nget('/'){ host = ENV['VMC_APP_HOST']; port = ENV['VMC_APP_PORT']; %(<h1>XXXXX Hello from the Cloud! via: #{host}:#{port}</h1>) }" > env.rb

Then deploy it, as easy as:

vmc push env --instances 4 --mem 64M --url env.vcap.me -n

In case it doesn’t work, and you want to consult logs manually, apps are stored in /var/vcap.local/dea/apps/ in the VM. Note that you can see an Error on startup even if the application starts correctly, don’t know why.

Anyway, the result in http://env.vcap.me:8080/ should be OK, you’ll see your four instances answer alternatively like a charm.

Run vmc help if you want to scale the app because of an excessive load!

Conclusion: the product seems great, but it’s not really well-documented and it can be hard to find informations or people with the same problems. Community seems to be super-reactive tho. In the past I tried to play with more complex app (rails+mysql) and had other problems, so I let you test and see if you’re happy with CloudFoundry.