Vagrant! Another super product from Hashicorp.
Vagrant makes it really easy to provision virtual servers, which they refer as “boxes”, that enables developers to run their jobs/tasks/applications in a really easy and fast way. Vagrant utilizes a declarative configuration model, so you can describe which OS you want, bootstrap them with installation instructions as soon as it boots, etc.
What are we doing today?
When completing this tutorial, you should be able to launch a Ubuntu Virtual Server locally with Vagrant and using the Virtualbox Provider which will be responsible for running our VM’s.
I am running this on a Ubuntu 19 Desktop, but you can run this on Mac/Windows/Linux. First we will install Virtualbox, then Vagrant, then we will provision a Ubuntu box and I will also show how to inject shell commands into your Vagrantfile so that you can provision software to your VM, and also forward traffic to a web server through your host to the guest.
Virtualbox
Install some pre-requirements:
1
|
|
Head over to Virtualbox’s download page and grab the latest version of virtualbox and install it.
After the installation run vboxconfig
to build the kernel modules. If you get the error that I received as seen below:
1 2 3 4 5 6 |
|
This resource on askubuntu.com helped me out. In short, theres a requirement that all the kernel modules must be signed by a key trusted by the UEFI system.
To resolve:
1 2 3 4 5 6 7 |
|
Remember the password, as you will require it when you reboot. You will get the option to “Enroll MOK”, select that, enter the initial password and reboot.
1
|
|
You should be able to get a response from the binary:
1 2 |
|
Install Vagrant
Head over to Vagrant’s installation page, get the latest version for your operating system and install it.
After installing it you should get the following response:
1 2 |
|
Provision a Box with Vagrant
When you head over to app.vagrantup.com/boxes/search you can select the pre-packed operating system of your choice. As for this demonstration, I went with: ubuntu/trusty64
First we will need to initialize a new Vagrant environment by creating a Vagrantfile, as we will be passing the name of our operating system, it will be populated in our Vagrantfile:
1 2 3 4 5 6 |
|
Now since the Vagrantfile has been placed in our current working directory, let’s have a look at it:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
|
As you can see the Vagrantfile has a set of instructions of how we want our VM to be. At this moment you will only see that the image is defined as ubuntu/trusty64
.
Let’s start our VM:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
|
Now that our VM has been booted, we can ssh to our server by simply running:
1 2 |
|
Making changes to your config
So let’s say we want to edit our Vagrantfile to provide shell commands to install nginx and forward our host port 8080 to our guest port 80, so that we can access our VM’s webserver on localhost using port 8080.
Edit your Vagrantfile so that it looks like this:
1 2 3 4 5 6 7 8 |
|
In order to call the shell activity we need to call the provision argument:
1
|
|
That will install nginx to our VM, then call reload to change to port configuration:
1
|
|
Now that everything is in order, we can access our nginx web server:
1 2 3 4 |
|
Tear down
Delete the server by running:
1
|
|