Need a Linux box to test a Wordpress site or a Windows VM to test a web site on IE 10? Creating a virtual machine to test or deploy your software doesn’t have to be a manual process. Bring one up in seconds with Vagrant, software for creating and managing virtual machines. With Vagrant, you can bring up a new virtual machine with the software you need, share directories, copy files, and configure networking using a friendly DSL. You can even use shell scripts or more powerful provisioning tools to set up your software and install your apps. Whether you need a Windows machine for testing an app, or a full-blown production environment for your apps, Vagrant has you covered.
In this talk you’ll learn to script the creation of multiple local virtual machines. Then you’ll use the same strategy to provision production servers in the cloud.
I work with Vagrant, Terraform, Docker, and other provisioning systems daily and am excited to show others how to bring this into their own workflows.
Create Development and Production Environments with Vagrant
1. Create Development and Production
Environments with Vagrant
Brian P. Hogan
Twitter: @bphogan
2. Hi. I'm Brian.
— Programmer (http://github.com/napcs)
— Author (http://bphogan.com/publications)
— Engineering Technical Editor @ DigitalOcean
— Musician (http://soundcloud.com/bphogan)
Let's chat today! I want to hear what you're working on.
3. Roadmap
— Learn about Vagrant
— Create a headless Ubuntu
Server
— Explore provisioning
— Create a Windows 10 box
— Create boxes on
DigitalOcean
4. Disclaimers and Rules
— This is a talk for people new
to Vagrant.
— This is based on my personal
experience.
— If I go too fast, or I made a
mistake, speak up.
— Ask questions any time.
— If you want to argue, buy me
a beer later.
5. Vagrant is a tool to provision development environments in virtual
machines. It works on Windows, Linux systems, and macOS.
Vagrant
A tool to provision development environments
— Automates so!ware virtualization
— Uses VirtualBox, VMWare, or libvirt for
virtualization layer
— Runs on Mac, Windows, *nix
6. Vagrant needs a virtualization layer, so we'll use VirtualBox, from Oracle, cos it's free.
Vagrant uses SSH and if you're on Windows, you'll need PuTTY to log in, and PuTTYgen
to generate keys if you don't have public and private keys yet.
Simplest Setup - Vagrant and VirtualBox
— Vagrant: https://www.vagrantup.com
— VirtualBox: https://virtualbox.org
— On Windows
— PuTTY to log in to SSH
— PuTTYgen for SSH Keygeneration
7. Using Vagrant
$ vagrant init ubuntu/xenial64
Creates a config file that will tell Vagrant to:
— Download Ubuntu 16.04 Server base image.
— Create VirtualBox virtual machine
— Create an ubuntu user
— Start up SSH on the server
— Mounts current folder to /vagrant
9. Log in to the machine with the ubuntu user. Let's demo
it.
Using the machine
$ vagrant ssh
ubuntu@ubuntu-xenial:~$
10. While the machine is provisioning let's look at the
configuration file in detail.
Configuration with Vagrantfile
Vagrant.configure("2") do |config|
config.vm.box = "ubuntu/xenial64"
# ...
end
11. Vagrant automatically shares the current working directory.
Sharing a folder
config.vm.synced_folder "./data", "/data"
12. Forward ports from the guest to the host, for servers and
more.
Forwarding ports
config.vm.network "forwarded_port", guest: 3000, host: 3000
13. Creating a private network is easy with Vagrant. This
network is private between the guest and the host.
Private network
config.vm.network "private_network", ip: "192.168.33.10"
14. This creates a public network which lets your guest machine
interact with the rest of your network, just as if it was a real machine.
Bridged networking
config.vm.network "public_network"
15. The vm.privder block lets you pass configuration options for the provider. For
Virtualbox that means you can specify if you want a GUI, specify the amount of
memory you want to use, and you can also specify CPU and other options.
VirtualBox options
config.vm.provider "virtualbox" do |vb|
# Display the VirtualBox GUI when booting the machine
vb.gui = true
# Customize the amount of memory on the VM:
vb.memory = "1024"
end
16. We can set the name of the machine, the display name in Virtualbox, and the
hostname of the server. We just have to wrap the machine definition in a
vm.define block.
Naming things
Vagrant.configure("2") do |config|
config.vm.define "devbox" do |devbox|
devbox.vm.box = "ubuntu/xenial64"
devbox.vm.hostname = "devbox"
config.vm.network "forwarded_port", guest: 3000, host: 3000
devbox.vm.provider "virtualbox" do |vb|
vb.name = "Devbox"
vb.gui = false
vb.memory = "1024"
end
end
end
19. Demo: Apache Web Server
— Map local folder to server's /
var/www/html folder
— Forward local port 8080 to
port 80 on the server
— Install Apache on the server
20. The Vagrantfile
Vagrant.configure("2") do |config|
config.vm.define "devbox" do |devbox|
devbox.vm.box = "ubuntu/xenial64"
devbox.vm.hostname = "devbox"
devbox.vm.network "forwarded_port", guest: 80, host: 8080
devbox.vm.synced_folder ".", "/var/www/html"
devbox.vm.provision "shell", inline: <<-SHELL
apt-get update
apt-get install -y apache2
SHELL
end
end
21. Configuration Management with
Ansible
— Define idempotent tasks
— Use declarations instead of
writing scripts
— Define roles (webserver,
database, firewall)
— Use playbooks to provision
machines
22. An Ansible playbook contains all the stuff we want to do to
our box. This one just installs some things on our server.
Playbook
---
- hosts: all
become: true
tasks:
- name: update apt cache
apt: update_cache=yes
- name: install Apache2
apt: name=apache2 state=present
- name: "Add nano to vim alias"
lineinfile:
path: /home/ubuntu/.bashrc
line: 'alias nano="vim"'
23. When we run the playbook, it'll go through all of the steps and
apply them to the server. It'll skip anything that's already in place.
Playbook results
PLAY [all] *********************************************************************
TASK [Gathering Facts] *********************************************************
ok: [devbox]
TASK [update apt cache] ********************************************************
changed: [devbox]
TASK [install Apache2] *********************************************************
changed: [devbox]
TASK [Add nano to vim alias] ***************************************************
changed: [devbox]
PLAY RECAP *********************************************************************
devbox : ok=4 changed=3 unreachable=0 failed=0
24. Vagrant has support for playbooks with the ansible
provisioner.
Provisioning with Ansible
Vagrant.configure("2") do |config|
config.vm.define "devbox" do |devbox|
# ,,,
devbox.vm.provision :ansible do |ansible|
ansible.playbook = "playbook.yml"
end
end
end
26. The Vagrantfile has the same setup we had last time, but we'll use the ansible provisioner this
time. Ansible uses Python 2 but Ubuntu 16.04 doesn't include Python2. We can tell Ansible
to use Python 3 using the ansible_python_interpreter option for host_vars.
The Vagrantfile
Vagrant.configure("2") do |config|
config.vm.define "devbox" do |devbox|
devbox.vm.box = "ubuntu/xenial64"
devbox.vm.hostname = "devbox"
devbox.vm.network "forwarded_port", guest: 80, host: 8080
devbox.vm.synced_folder ".", "/var/www/html"
devbox.vm.provision :ansible do |ansible|
ansible.playbook = "playbook.yml"
ansible.host_vars = {
"devbox" => {"ansible_python_interpreter" => "/usr/bin/python3"}
}
end
end
end
27. Our playbook installs a few pieces of software including
Ruby, MySQL, Git, and Vim.
Playbook
---
- hosts: all
become: true
tasks:
- name: update apt cache
apt: update_cache=yes
- name: install required packages
apt: name={{ item }} state=present
with_items:
- apache2
- mysql-server
- ruby
- git-core
- vim
28. Vagrant Boxes
Get boxes from https://app.vagrantup.com
Useful boxes:
— scotch/box - Ubuntu 16.04 LAMP server ready to go
— Microso!/EdgeOnWindows10 - Windows 10 with
Edge browser
29. You can get more Vagrant boxes from the Vagrant Cloud web site, including boxes
that have a bunch of tooling already installed. You can even get a Windows box.
Demo: Windows Dev box
— Test out web sites on Edge
— Run so!ware that only works on Windows
— Run with a GUI
— Uses 120 day evaluation license, FREE and direct
from MS!
30. Here's how we'd configure a Windows box. We tell Vagrant we're using Windows instead
of Linux, and we specify that we're using WinRM instead of SSH. Then we provide
credentials.
The Vagrantfile
$ vagrant init
Vagrant.configure("2") do |config|
config.vm.define "windowstestbox" do |winbox|
winbox.vm.box = "Microsoft/EdgeOnWindows10"
winbox.vm.guest = :windows
winbox.vm.communicator = :winrm
winbox.winrm.username = "IEUser"
winbox.winrm.password = "Passw0rd!"
winbox.vm.provider "virtualbox" do |vb|
vb.gui = true
vb.memory = "2048"
end
end
end
31. Vagrant and the Cloud
— Provides
— AWS
— Google
— DigitalOcean
— Build and Provision
32. Creating virtual machines is great, but you can use Vagrant to
build machines in the cloud using various Vagrant providers.
First, we install the Vagrant plugin. This will let us use a new
provider. We need an API key from DigitalOcean.
Demo: Web Server on DigitalOcean
Install the plugin
$ vagrant plugin install vagrant-digitalocean
Get a DigitalOcean API token from your account and
place it in your environment:
$ export DO_API_KEY=your_api_key
We'll place that API key in an environment variable
and reference that environment variable from our
config. That way we keep it out of configurations we
33. The config has a lot more info than before. We have to specify our SSH key for
DigitalOcean. We also have to specify information about the kind of server we want to set
up. We configure these through the provider, just like we configured Virtualbox.
Demo: Ubuntu on DigitalOcean
Vagrant.configure('2') do |config|
config.ssh.private_key_path = '~/.ssh/id_rsa'
config.vm.box = 'digital_ocean'
config.vm.box_url = "https://github.com/devopsgroup-
io/vagrant-digitalocean/raw/master/box/digital_ocean.box"
config.vm.define "webserver" do |box|
box.vm.hostname = "webserver"
box.vm.provider :digital_ocean do |provider|
provider.token = ENV["DO_API_KEY"]
provider.image = 'ubuntu-16-04-x64'
provider.region = 'nyc3'
provider.size = '512mb'
provider.private_networking = true
end # provider
end # box
end # config
34. We can also use provisioning to install stuff on the server.
Provisioning works too!
Vagrant.configure('2') do |config|
# ...
config.vm.define "webserver" do |box|
#...
provider.private_networking = true
box.vm.provision "ansible" do |ansible|
ansible.playbook = "playbook.yml"
ansible.host_vars = {
"webserver" => {"ansible_python_interpreter" => "/usr/bin/python3"}
}
end # ansible
end # box
end # config
35. Immutable infrastructure
— "Treat servers like cattle, not
pets"
— Don't change an existing
server. Rebuild it
— No "Snowflake" servers
— No "configuration dri#" over
time
— Use images and tools to
rebuild servers and redeploy
apps quickly
36. Spin up an infrastructure
— Vagrantfile is Ruby code
— Vagrant supports multiple
machines in a single file
— Vagrant commands support
targeting specific machine
— Provisioning runs on all
machines
37. We define a Ruby array with a hash for each server. The
hash contains the server info we need.
The Vagrantfile
Vagrant.configure('2') do |config|
machines = [
{name: "web1", size: "1GB", image: "ubuntu-16-04-x64", region: "nyc3"},
{name: "web2", size: "1GB", image: "ubuntu-16-04-x64", region: "sfo1"}
]
config.ssh.private_key_path = '~/.ssh/id_rsa'
config.vm.box = 'digital_ocean'
config.vm.box_url = "https://github.com/devopsgroup-io/vagrant-
digitalocean/raw/master/box/digital_ocean.box"
38. We then iterate over each machine and create a vm.define block. We assign the
name, the region, the image, and the size. We could even use this to assign a
playbook for each machine.
The Vagrantfile
Vagrant.configure('2') do |config|
# ...
machines.each do |machine|
config.vm.define machine[:name] do |box|
box.vm.hostname = machine[:name]
box.vm.provider :digital_ocean do |provider|
provider.token = ENV["DO_API_KEY"]
provider.image = machine[:image]
provider.size = machine[:size]
end
box.vm.provision "ansible" do |ansible|
ansible.playbook = "playbook.yml"
ansible.host_vars = {
machine[:name] => {"ansible_python_interpreter" => "/usr/bin/python3"}
}
end # ansible
end # box
end # loop
end # config
39. Usage
— vagrant up brings all machines online and provisions
them
— vagrant ssh web1 logs into the webserver
— vagrant provision would re-run the Ansible
playbooks.
— vagrant ssh web1 logs into the db server
— vagrant rebuild rebuilds machines from scratch and
reprovisions
— vagrant destroy removes all machines.
40. Puphet is a GUI for generating Vagrant configs with provisioning. Pick your
OS, servers you need,. programming languages, and download your bundle.
Puphet
https://puphpet.com/
41. Terraform, by Hashicorp, is the production solution for building out cloud environments and provisioning them. Docker
Machine can also spin up cloud environments and then install Docker on them, which you can use to host apps
hosted in containers. And Ansible, with a little bit of code and plugins, can create and provision your infrastructure.
Alternatives to Vagrant
— Terraform
— Docker Machine
— Ansible
42. Summary
— Vagrant controls virtual
machines
— Use Vagrant's provisioner to
set up your machine
— Use existing box images to
save time
— Use Vagrant to build out an
infrastructure in the cloud