create development and production environments with vagrant
TRANSCRIPT
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.
Roadmap
— Learn about Vagrant
— Create a headless Ubuntu Server
— Explore provisioning
— Create a Windows 10 box
— Create boxes on DigitalOcean
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.
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
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
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
Log in to the machine with the ubuntu user. Let's demo it.
Using the machine
$ vagrant sshubuntu@ubuntu-xenial:~$
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
Vagrant automatically shares the current working directory.
Sharing a folder
config.vm.synced_folder "./data", "/data"
Forward ports from the guest to the host, for servers and more.
Forwarding ports
config.vm.network "forwarded_port", guest: 3000, host: 3000
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"
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"
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
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 endend
Provisioning
— Installing so!ware
— Creating files and folders
— Editing configuration files
— Adding users
Vagrant Provisioner
config.vm.provision "shell", inline: <<-SHELL
apt-get update
apt-get install -y apache2
SHELL
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
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
Configuration Management with Ansible
— Define idempotent tasks
— Use declarations instead of writing scripts
— Define roles (webserver, database, firewall)
— Use playbooks to provision machines
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"'
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
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 endend
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 endend
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
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
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!
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
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
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
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 # boxend # config
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
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
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"
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
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.
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/
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
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