Vagrant test and development environment I

vagrant

A great while ago I jotted a few lines down about Vagrant. It has been a year and a half and I wanted to dedicate two new posts on it because, ehh, it’s pretty awesome.

I must confess that it is since recently that I’ve started using Vagrant on a regular basis and I can’t understand why I’ve waited this long. It’s so easy to fire up an entire test and development environment, consisting of multiple servers, in a couple of minutes.

Here is how.

First of all, Vagrant runs on top of a provider and several are supported. I’m using Virtualbox because its free and does the job for me. Be sure to install it.

The Vagrant CLI interface knows a lot of commands which I’ll mention along the way. For this post I’ll be setting up a test environment for a LFCS exam I’ll be taking soon. It consists of two DNS servers, a mail server, a database server and a web server.

Create a directory where you want to setup your configuration. To take a look at an example file perform the default init command:

$ vagrant init

This creates a Vagrantfile with a lot of comments you can check out. When you’re done reading through it, delete it and create a new, minimal version of it:

$ vagrant init -m

This get’s you going with a nice clean config you can build upon:

Vagrant.configure("2") do |config|
  config.vm.box = "base"
end

Let’s create our first server, a primary DNS server, but it can be any server really. With this first server, your config should look like this:

Vagrant.configure("2") do |config|
  config.vm.define "ns1" do |ns1|
    ns1.vm.box = "ubuntu/bionic64"
    ns1.dns.tld = "cloudpro.vm"
    ns1.vm.hostname = "ns1"
    ns1.vm.network "private_network", ip: "192.168.33.10", netmask: "24"
    ns1.vm.network "forwarded_port", host: "5353", guest: "53"
    ns1.vm.provision "shell" do |shell|
      shell.inline = "apt-get update -y && apt-get upgrade -y && apt-get install bind9 -y"
    end
  end
end

I hope most if this looks somewhat familiar, but lets elaborate on it just to be sure:

  • Vagrant.configure(“2”) do |config| will be the start of every Vagrantfile. The 2 is for the version
  • config.vm.define “ns1” do |ns1| will be defining our first server, the next few indented line being its configuration
  • ns1.vm.box = “ubuntu/bionic64” specifies the box. Take a look for more boxes here
  • ns1.dns.tld = “cloudpro.vm” specifies the domainname Vagrant can use for your boxes
  • ns1.vm.hostname = “ns1” specifies the hostname for your box, as with the domainname
  • ns1.vm.network “private_network”, ip: “192.168.33.10”, netmask: “24” configures your IP and subnetmask
  • ns1.vm.network “forwarded_port”, host: “5353”, guest: “53” forwards ports from host to guest
  • config.vm.provision “shell” do |shell| this starts the configuration for a provisioner we’ll be using. In our case a few shell commands

Validate your work and correct any mistakes:

$ vagrant validate

Fire up you machine and watch the magic happen:

$ vagrant up

When your Vagrantfile is OK and your shell commands as well, your machine is running at the moment:

$ vagrant status

Connect to it via SSH

$ vagrant ssh

You can for instance check if bind9 is indeed installed. Exit the machine when you’re done.

Stop your machine:

$ vagrant halt

As a side note, when you start the Virtualbox application at this point, you can check out your machine and for instance change its specifications.

Anyway, we’ll be adding a couple of more machines, as said a secondary DNS server and a web, mail and databases server, next week!

Update: Check out this mini vagrant series here and here.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.