Setup the Vagrant Configuration - miroswan/vagrant_spec GitHub Wiki

We'll create a very basic project called mesos_test. The project will setup a vanilla Mesos cluster and verify basic functionality of the cluster. By the end of this section, you should have a basic understanding of the following:

  • Multi-Machine Vagrant configuration

Basic Vagrant Configuration

To start, open up your favorite terminal program and enter into a directory suitable for Vagrant projects and execute the following:

mkdir mesos_test
cd mesos_test
vagrant init --minimal ubuntu/trusty64
vagrant plugin install vagrant_spec
vagrant plugin install vagrant-hostmanager

The init command will create a minimal Vagrantfile with the box configured to use the 64-bit Ubuntu Trusty release from Atlas. All of the configuration for the node instantiation phase will belong in the Vagrantfile. If you view the Vagrantfile, it should look something like this:

Vagrant.configure("2") do |config|
  config.vm.box = "ubuntu/trusty64"
end

The Vagrant.configure("2") class method call creates a Vagrant::Configuration Version 2 object and yields itself with the config variable for further configuration within the file. Setting config.vm.box allows us to set the image to use for any virtual machine within the configuration file. Since we've passed this value from the command line, Vagrant conveniently configured this for us.

The plugin commands will install vagrant_spec and vagrant-hostmanager. The latter will allow the hosts and the guests to reference each other by hostname. This is merely for convenience.

Multi-Machine Vagrant Configuration

Next, open up your favorite text editor and edit the Vagrantfile to look like so:

Vagrant.configure('2') do |config|
  config.vm.box = 'ubuntu/trusty64'

  config.hostmanager.enabled      = true
  config.hostmanager.manage_host  = true
  config.hostmanager.manage_guest = true

  (1..3).each do |i|
    name = "mesos#{i}"
    config.vm.define name.to_sym do |node|
      node.vm.hostname = name
      node.vm.network(:private_network, ip: "192.168.33.#{i*10}")
    end
  end
end

Let's break this down.

The three config.hostmanager calls enable the vagrant-hostmanager plugin, enables resolution from the host to the guests, and enables resolution between the guests. For more information on vagrant-hostmanager, go here.

Next, let's take a look at the following chunk:

  (1..3).each do |i|
    name = "mesos#{i}"
    config.vm.define name.to_sym do |node|
      node.vm.hostname = name
      node.vm.network(:private_network, ip: "192.168.33.#{i*10}")
    end
  end

(1..3) creates a range, which is an interval that inherits the methods from enumerable. This allows us to call each on the range with a block parameter i. As a refresher, each is a method that allows us to iterate over each value in the enumerable on which it is called. We set name to mesos, while interpolating the value of i into the end of the name. We'll use this variable a few times later.

The config.vm.define method call accepts a symbol (or string), which is the name of the virtual machine we are defining. It yields an instance of itself, so a block parameter is expected to be used for further configuration. We use the variable node in this context. Next, we call node.vm.hostname to set the hostname to the name variable. Next, we call the node.vm.network method. This method accepts a symbol as the first parameter which describes the type of network to be configured. In our case, we want to use the private_network type so that we can guarantee a specific ip address to be assigned. The second parameter of the method is a hash that configures the private network. Here, we set the ip of the current node and interpolate values from the range to provide unique ip addresses for each instance. Each of these nodes will serve as our mesos masters.

Next, run vagrant up. This will instantiate each of the nodes configured in your Vagrantfile. If you use configuration management tools such as Chef or Puppet, you could configure each virtual machine instance to retrieve a baseline configuration that your organization expects on each node, such as SSH keys or packages. However, for the purposes of this demonstration, we'll jump right into application configuration and orchestration.

To validate the correct network configuration for your virtual machine set, run the following:

for ip in 192.168.33.10 192.168.33.20 192.168.33.30; do ping -c 1 $ip; done

Now, let's get Mesos deployed!

Next: Deploying Mesos to Vagrant