Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

How to Install Devstack over Virtual Box using Vagrant

DZone's Guide to

How to Install Devstack over Virtual Box using Vagrant

· Cloud Zone
Free Resource

Are you joining the containers revolution? Start leveraging container management using Platform9's ultimate guide to Kubernetes deployment.



Devstack over VirtualBox + Vagrant

[This article was originally written by Shay Naeh.]

Many of us often times need to develop and test on OpenStack, however this becomes cumbersome as OpenStack releases new versions, where each needs to be installed and configured from scratch.

I found that a good solution is to install DevStack over a VirtualBox, but the challenge here is to overcome some OpenStack configuration and networking issues. By using this combination of Vagrant, VirtualBox, and DevStack, I found simple workarounds to easily install and work with OpenStack on a local environment.

The motivation was to be able to have a local development and testing environment for our entire dev team. Below is a tutorial for this DevStack Vagrant combination.

Getting Started

Let’s start with a vagrant file - which is the easiest way to configure the VM box.
# -*- mode: ruby -*-
# vi: set ft=ruby :

# Vagrantfile API/syntax version. Don't touch unless you know what you're doing!
VAGRANTFILE_API_VERSION = "2"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  # All Vagrant configuration is done here. The most common configuration
  # options are documented and commented below. For a complete reference,
  # please see the online documentation at vagrantup.com.

  # Every Vagrant virtual environment requires a box to build off of.
  config.vm.box = "devstack5"
  config.vm.network "private_network", ip: "171.15.19.31"
  config.vm.network :forwarded_port, guest: 80, host: 8080
  config.vm.network :forwarded_port, guest: 5000, host: 5000
  config.vm.network :forwarded_port, guest: 9696, host: 9696
  config.vm.network :forwarded_port, guest: 8774, host: 8774
  config.vm.network :forwarded_port, guest: 35357, host: 35357
  config.vm.provider :virtualbox do |vb|
  #   # Don't boot with headless mode
  vb.gui = false
  #
  #   # Use VBoxManage to customize the VM. For example to change memory:
  vb.customize ["modifyvm", :id, "--memory", "4096"]
  end
  
  config.vm.provision "shell", path: "script.sh"
end

You will need to download the right Linux distro and version number, I used Ubuntu 12.04.  You will need to use the name you gave to the box (note that in the Gist I named in devstack5).  You then need to provide an IP address for a private network.  After you do so, you can allocate or create forwarding ports for all the OpenStack services’ API endpoints - Keystone, Neutron et al.

Vagrant supports also configuration of the Vagrant VM that is brought up.  You are able to configure its memory size, CPU, and other characteristics - for example look in the config VM provision section (towards the end of the file) where I created a machine 4 GB memory.

Next,  you supply your own script, which I’ll elaborate on later, that describes the initial configuration of the box, when it is brought up.

After this, run vagrant up, and the box will then load.

SSH into the box to install DevStack.  Reference Barak’s post for how to install DevStack.

Working with DevStack

DevStack comes with many configuration files, however, I’ve found that the simplest method is to change the localrc or the local.conf file to provide the information you need to configure DevStack.

In the localrc file you can provide passwords for your different services - RabbitMQ, Admin passwords, in addition to providing additional information you will need for the setup: floating IP ranges you’d like to use, the flat interface (e.g. ETH0), a location to store the log files,  where the gateway is located, and services you‘d like to enable.  For example, I enabled the Neutron service for networking purposes, (I’ll dive into networking hacks a bit later).  In the Gists below you will also see how to access Devstack from the host machine, as well as how to access the instances launched inside DevStack from the outside world.

After you create the configuration file, run stack.sh which will install MySQL and  RabbitMQ, will compile and install all of the OpenStack components, and finally will bring the system up.

After the system is up and running you will be able to access it through the Horizon dashboard.

That’s the simple stuff.  However, you will likely need to be able to have network access from the host machine to DevStack and to the launched instances. You’d be best off automating this as much as possible to simplify the process.

Kicking it Up a Notch

In the Vagrant file I defined a script that is being called to create networking routes, as well as allocate OpenStack resources and objects, and define security rules and more.  This enables you to access DevStack from the host machine. 

#!/bin/bash
echo `whoami`
sudo chmod +x /vagrant/start.sh
sudo -u stack /vagrant/start.sh
#!/bin/bash
cd /home/stack/devstack
source openrc admin
wget -q http://uec-images.ubuntu.com/precise/current/precise-server-cloudimg-amd64-disk1.img 
/home/stack/devstack/stack.sh
glance image-create --name="Ubuntu12.04" --disk-format=qcow2 --container-format=bare --is-public=true < precise-server-cloudimg-amd64-disk1.img
neutron subnet-update public-subnet --dns_nameservers list=true 8.8.8.8
neutron subnet-update private-subnet --dns_nameservers list=true 8.8.8.8
nova flavor-create m1.tiny2 auto 512 10 1
nova secgroup-add-rule default icmp -1 -1 0.0.0.0/0
nova secgroup-add-rule default tcp 22 22 0.0.0.0/0
nova secgroup-add-rule default tcp 80 80 0.0.0.0/0

When I did this, I had a problem running it in one script, since I discovered that it has to run under a specific user.  Since I installed DevStack under the “stack” user, I had to switch automatically from the vagrant user  to the stack user before the  script runs, as well as  change the execution permissions to the file.  To do so, I created a script called script.sh for the permission manipulation.

Through the start.sh file you can actually access external images, which you will need to do, since DevStack comes with a very small number of pre-loaded images that are not necessarily suitable, for example Ubuntu is not one of them.  To do so, you can provide a URL or HTTP, and by using w-get you can download additional images.  To bring DevStack up automatically, you’re also able to run the stack.sh from the script as well.

Note, that to add images it is not enough to download them and just put them in the directory, you will also need to tell Glance (the OpenStack image repository) to create a new image ID from the downloaded image.

Do this by calling glance image-create with the format, the container, and image reference.

Networking Hacks

Regarding networking, if you want to have DNS and name resolution the easiest way is to call the Neutron subnet update, and add a DNS name server. I added the Google public name server for name resolution -  it’s IP address is 8.8.8.8. 

Another networking configuration is to create security groups. I did this to be able to ping the new launched instances, and be able to SSH into them, as well as be able to access the web apps installed on port 80.

In order to be able to access the DevStack machine from the host machine, you have to use the route add command, as you can see in the example below. You will need to create a route using a gateway which tells the host machine if it receives a packet from a certain subnet to which interface it should be forwarded. 

#!/bin/bash
sudo /sbin/route add -net 10.0.2.0 netmask 255.255.255.0 gw 171.15.19.31
sudo /sbin/route add -net 172.24.4.0 netmask 255.255.255.0 gw 171.15.19.31
vagrant up
For this purpose I added two different routes.  I did this before I ran the vagrant up command. In order to access the launch instances or ping them, you can use the name spaces.

I used the netns command to access the internal launched instances.  OpenStack uses network namespaces to isolate networks where each virtual network gets its own space.  A space means network interfaces, routing tables, IP tables separately for each tenant.

You can run IP netns list to receive the list of namespaces, and then choose the one you need.

Run exec with the namespace you defined, and define additional parameters such as IP tables, and routing information, for example:

sudo ip netns exec qrouter-1520casp2-f23c-497a-8578-3b2dcb7c2457 ping 10.0.0.7

sudo ip netns exec qrouter-152mnwd2-f54c-481a-8895-3b2dcb7c2624 ssh –i keyname ubuntu@10.0.0.7

I ran into a few issues when attempting to debug, so I chose to use the screens method explained in the DevStack + Heat post by Yoram. With the screen command you can see all of the DevStack terminals/consoles (about 15), e.g. Neutron, Nova, Horizon, APIs and others, and dive into their error messages.

To check that everything worked as it should, I launched a few instances, and was able to access them from the network.

Once I saw that it was up and running as needed, I packaged everything into a Vagrant box, this enables:

  • Anyone to have their own DevStack local test and development environment.

  • Anyone can have an IaaS in a box (VBox) locally - where you can define instances, configure, etc.

That’s it.  With a few simple commands you can have your own local OpenStack and IaaS environments up and running.

If you want to upgrade to the newest OpenStack release follow the setup above, repackage it, and you’re good to go.

Using Containers? Read our Kubernetes Comparison eBook to learn the positives and negatives of Kubernetes, Mesos, Docker Swarm and EC2 Container Services.

Topics:

Published at DZone with permission of Cloudify Community, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}