Create Development and Production Environments with Vagrant

Brian Hogan
Brian HoganProgrammer and writer.

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
Brian P. Hogan
Twitter: @bphogan
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
Downloads image, brings up machine.
Firing it up
$ vagrant up
Log in to the machine with the ubuntu user. Let's demo
it.
Using the machine
$ vagrant ssh
ubuntu@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
end
end
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
end
end
Demo: Dev box
— Ubuntu 16.04 LTS
— Apache/MySQL/PHP
— Git/vim
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
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
Vagrant and the Cloud
— Provides
— AWS
— Google
— DigitalOcean
— Build and Provision
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 # box
end # 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
Questions
— Slides: https://bphogan.com/presentations/
vagrant2017
— Twitter: @bphogan
— Say hi!
```

Recommended

What is objectives of software testing by
What is objectives of software testingWhat is objectives of software testing
What is objectives of software testingSoftware Testing Books
1.3K views1 slide
Software Testing by
Software TestingSoftware Testing
Software TestingDhanasekaran Nagarajan
1.5K views29 slides
Cucumber BDD by
Cucumber BDDCucumber BDD
Cucumber BDDPravin Dsilva
2.6K views30 slides
Manual testing by
Manual testingManual testing
Manual testingvigneshasromio
8.1K views20 slides

More Related Content

What's hot

WCAG 2.1 Mobile Accessibility by
WCAG 2.1 Mobile AccessibilityWCAG 2.1 Mobile Accessibility
WCAG 2.1 Mobile AccessibilityInteractive Accessibility
5K views31 slides
Devops | CICD Pipeline by
Devops | CICD PipelineDevops | CICD Pipeline
Devops | CICD PipelineBinish Siddiqui
364 views10 slides
Jenkins presentation by
Jenkins presentationJenkins presentation
Jenkins presentationValentin Buryakov
1.4K views19 slides
Industrialisation Du Logiciel - Introduction Et Bonnes Pratiques by
Industrialisation Du Logiciel  - Introduction Et Bonnes PratiquesIndustrialisation Du Logiciel  - Introduction Et Bonnes Pratiques
Industrialisation Du Logiciel - Introduction Et Bonnes PratiquesEmmanuel Hugonnet
8K views50 slides
Automated Test Framework with Cucumber by
Automated Test Framework with CucumberAutomated Test Framework with Cucumber
Automated Test Framework with CucumberRamesh Krishnan Ganesan
6.1K views22 slides
Yale Jenkins Show and Tell by
Yale Jenkins Show and TellYale Jenkins Show and Tell
Yale Jenkins Show and TellE. Camden Fisher
19.4K views24 slides

What's hot(20)

Industrialisation Du Logiciel - Introduction Et Bonnes Pratiques by Emmanuel Hugonnet
Industrialisation Du Logiciel  - Introduction Et Bonnes PratiquesIndustrialisation Du Logiciel  - Introduction Et Bonnes Pratiques
Industrialisation Du Logiciel - Introduction Et Bonnes Pratiques
Ii 1500-publishing your android application by Adrian Mikeliunas
Ii 1500-publishing your android applicationIi 1500-publishing your android application
Ii 1500-publishing your android application
Adrian Mikeliunas1.4K views
Significance of metrics by David Karlsen
Significance of metricsSignificance of metrics
Significance of metrics
David Karlsen6.7K views
An Introduction To Jenkins by Knoldus Inc.
An Introduction To JenkinsAn Introduction To Jenkins
An Introduction To Jenkins
Knoldus Inc.23.2K views
Mobile application testing by Softheme
Mobile application testingMobile application testing
Mobile application testing
Softheme10.9K views
Bdd – with cucumber and gherkin by Arati Joshi
Bdd – with cucumber and gherkinBdd – with cucumber and gherkin
Bdd – with cucumber and gherkin
Arati Joshi2.8K views
Introduction to BDD with Cucumber for Java by Seb Rose
Introduction to BDD with Cucumber for JavaIntroduction to BDD with Cucumber for Java
Introduction to BDD with Cucumber for Java
Seb Rose9K views
Se (techniques for black box testing ppt) by Mani Kanth
Se (techniques for black box testing ppt)Se (techniques for black box testing ppt)
Se (techniques for black box testing ppt)
Mani Kanth2.2K views
What is-requirement-traceability-matrix-and-why-is-it-needed- by pooja deshmukh
What is-requirement-traceability-matrix-and-why-is-it-needed-What is-requirement-traceability-matrix-and-why-is-it-needed-
What is-requirement-traceability-matrix-and-why-is-it-needed-
pooja deshmukh964 views

Similar to Create Development and Production Environments with Vagrant

Vagrant - Version control your dev environment by
Vagrant - Version control your dev environmentVagrant - Version control your dev environment
Vagrant - Version control your dev environmentbocribbz
3.7K views42 slides
Intro to vagrant by
Intro to vagrantIntro to vagrant
Intro to vagrantMantas Klasavicius
1.2K views45 slides
Vagrant - Team Development made easy by
Vagrant - Team Development made easyVagrant - Team Development made easy
Vagrant - Team Development made easyMarco Silva
2.2K views23 slides
Create your very own Development Environment with Vagrant and Packer by
Create your very own Development Environment with Vagrant and PackerCreate your very own Development Environment with Vagrant and Packer
Create your very own Development Environment with Vagrant and Packerfrastel
3.6K views67 slides
Making Developers Productive with Vagrant, VirtualBox, and Docker by
Making Developers Productive with Vagrant, VirtualBox, and DockerMaking Developers Productive with Vagrant, VirtualBox, and Docker
Making Developers Productive with Vagrant, VirtualBox, and DockerJohn Rofrano
1.1K views109 slides
Vagrant For DevOps by
Vagrant For DevOpsVagrant For DevOps
Vagrant For DevOpsLalatendu Mohanty
10.5K views18 slides

Similar to Create Development and Production Environments with Vagrant(20)

Vagrant - Version control your dev environment by bocribbz
Vagrant - Version control your dev environmentVagrant - Version control your dev environment
Vagrant - Version control your dev environment
bocribbz3.7K views
Vagrant - Team Development made easy by Marco Silva
Vagrant - Team Development made easyVagrant - Team Development made easy
Vagrant - Team Development made easy
Marco Silva2.2K views
Create your very own Development Environment with Vagrant and Packer by frastel
Create your very own Development Environment with Vagrant and PackerCreate your very own Development Environment with Vagrant and Packer
Create your very own Development Environment with Vagrant and Packer
frastel3.6K views
Making Developers Productive with Vagrant, VirtualBox, and Docker by John Rofrano
Making Developers Productive with Vagrant, VirtualBox, and DockerMaking Developers Productive with Vagrant, VirtualBox, and Docker
Making Developers Productive with Vagrant, VirtualBox, and Docker
John Rofrano1.1K views
Quick & Easy Dev Environments with Vagrant by Joe Ferguson
Quick & Easy Dev Environments with VagrantQuick & Easy Dev Environments with Vagrant
Quick & Easy Dev Environments with Vagrant
Joe Ferguson1.6K views
Vagrant introduction for Developers by Antons Kranga
Vagrant introduction for DevelopersVagrant introduction for Developers
Vagrant introduction for Developers
Antons Kranga2.1K views
Puppet and Vagrant in development by Adam Culp
Puppet and Vagrant in developmentPuppet and Vagrant in development
Puppet and Vagrant in development
Adam Culp6.8K views
Vagrant-Overview by Crifkin
Vagrant-OverviewVagrant-Overview
Vagrant-Overview
Crifkin3.3K views
NetDevOps Developer Environments with Vagrant @ SCALE16x by Hank Preston
NetDevOps Developer Environments with Vagrant @ SCALE16xNetDevOps Developer Environments with Vagrant @ SCALE16x
NetDevOps Developer Environments with Vagrant @ SCALE16x
Hank Preston240 views
Cooking Perl with Chef: Hello World Tutorial by David Golden
Cooking Perl with Chef: Hello World TutorialCooking Perl with Chef: Hello World Tutorial
Cooking Perl with Chef: Hello World Tutorial
David Golden6.8K views
Continuous Delivery: The Next Frontier by Carlos Sanchez
Continuous Delivery: The Next FrontierContinuous Delivery: The Next Frontier
Continuous Delivery: The Next Frontier
Carlos Sanchez1.6K views
Continuous Delivery with Maven, Puppet and Tomcat - ApacheCon NA 2013 by Carlos Sanchez
Continuous Delivery with Maven, Puppet and Tomcat - ApacheCon NA 2013Continuous Delivery with Maven, Puppet and Tomcat - ApacheCon NA 2013
Continuous Delivery with Maven, Puppet and Tomcat - ApacheCon NA 2013
Carlos Sanchez16.9K views
Preparation study of_docker - (MOSG) by Soshi Nemoto
Preparation study of_docker  - (MOSG)Preparation study of_docker  - (MOSG)
Preparation study of_docker - (MOSG)
Soshi Nemoto597 views

More from Brian Hogan

Creating and Deploying Static Sites with Hugo by
Creating and Deploying Static Sites with HugoCreating and Deploying Static Sites with Hugo
Creating and Deploying Static Sites with HugoBrian Hogan
1.2K views65 slides
Automating the Cloud with Terraform, and Ansible by
Automating the Cloud with Terraform, and AnsibleAutomating the Cloud with Terraform, and Ansible
Automating the Cloud with Terraform, and AnsibleBrian Hogan
1.8K views80 slides
Docker by
DockerDocker
DockerBrian Hogan
1.2K views86 slides
Getting Started Contributing To Open Source by
Getting Started Contributing To Open SourceGetting Started Contributing To Open Source
Getting Started Contributing To Open SourceBrian Hogan
867 views34 slides
Rethink Frontend Development With Elm by
Rethink Frontend Development With ElmRethink Frontend Development With Elm
Rethink Frontend Development With ElmBrian Hogan
4.3K views64 slides
Testing Client-side Code with Jasmine and CoffeeScript by
Testing Client-side Code with Jasmine and CoffeeScriptTesting Client-side Code with Jasmine and CoffeeScript
Testing Client-side Code with Jasmine and CoffeeScriptBrian Hogan
4.9K views59 slides

More from Brian Hogan(20)

Creating and Deploying Static Sites with Hugo by Brian Hogan
Creating and Deploying Static Sites with HugoCreating and Deploying Static Sites with Hugo
Creating and Deploying Static Sites with Hugo
Brian Hogan1.2K views
Automating the Cloud with Terraform, and Ansible by Brian Hogan
Automating the Cloud with Terraform, and AnsibleAutomating the Cloud with Terraform, and Ansible
Automating the Cloud with Terraform, and Ansible
Brian Hogan1.8K views
Getting Started Contributing To Open Source by Brian Hogan
Getting Started Contributing To Open SourceGetting Started Contributing To Open Source
Getting Started Contributing To Open Source
Brian Hogan867 views
Rethink Frontend Development With Elm by Brian Hogan
Rethink Frontend Development With ElmRethink Frontend Development With Elm
Rethink Frontend Development With Elm
Brian Hogan4.3K views
Testing Client-side Code with Jasmine and CoffeeScript by Brian Hogan
Testing Client-side Code with Jasmine and CoffeeScriptTesting Client-side Code with Jasmine and CoffeeScript
Testing Client-side Code with Jasmine and CoffeeScript
Brian Hogan4.9K views
FUD-Free Accessibility for Web Developers - Also, Cake. by Brian Hogan
FUD-Free Accessibility for Web Developers - Also, Cake.FUD-Free Accessibility for Web Developers - Also, Cake.
FUD-Free Accessibility for Web Developers - Also, Cake.
Brian Hogan3.2K views
Responsive Web Design by Brian Hogan
Responsive Web DesignResponsive Web Design
Responsive Web Design
Brian Hogan1.3K views
Web Development with CoffeeScript and Sass by Brian Hogan
Web Development with CoffeeScript and SassWeb Development with CoffeeScript and Sass
Web Development with CoffeeScript and Sass
Brian Hogan9.3K views
Building A Gem From Scratch by Brian Hogan
Building A Gem From ScratchBuilding A Gem From Scratch
Building A Gem From Scratch
Brian Hogan1.4K views
Intro To Advanced Ruby by Brian Hogan
Intro To Advanced RubyIntro To Advanced Ruby
Intro To Advanced Ruby
Brian Hogan2.5K views
Turning Passion Into Words by Brian Hogan
Turning Passion Into WordsTurning Passion Into Words
Turning Passion Into Words
Brian Hogan1.1K views
HTML5 and CSS3 Today by Brian Hogan
HTML5 and CSS3 TodayHTML5 and CSS3 Today
HTML5 and CSS3 Today
Brian Hogan2.2K views
Web Development With Ruby - From Simple To Complex by Brian Hogan
Web Development With Ruby - From Simple To ComplexWeb Development With Ruby - From Simple To Complex
Web Development With Ruby - From Simple To Complex
Brian Hogan4.8K views
Stop Reinventing The Wheel - The Ruby Standard Library by Brian Hogan
Stop Reinventing The Wheel - The Ruby Standard LibraryStop Reinventing The Wheel - The Ruby Standard Library
Stop Reinventing The Wheel - The Ruby Standard Library
Brian Hogan9.8K views
Intro to Ruby by Brian Hogan
Intro to RubyIntro to Ruby
Intro to Ruby
Brian Hogan1.7K views
Intro to Ruby - Twin Cities Code Camp 7 by Brian Hogan
Intro to Ruby - Twin Cities Code Camp 7Intro to Ruby - Twin Cities Code Camp 7
Intro to Ruby - Twin Cities Code Camp 7
Brian Hogan2.2K views
Make GUI Apps with Shoes by Brian Hogan
Make GUI Apps with ShoesMake GUI Apps with Shoes
Make GUI Apps with Shoes
Brian Hogan10.4K views
Story-driven Testing by Brian Hogan
Story-driven TestingStory-driven Testing
Story-driven Testing
Brian Hogan1.2K views

Recently uploaded

Microsoft Power Platform.pptx by
Microsoft Power Platform.pptxMicrosoft Power Platform.pptx
Microsoft Power Platform.pptxUni Systems S.M.S.A.
53 views38 slides
Serverless computing with Google Cloud (2023-24) by
Serverless computing with Google Cloud (2023-24)Serverless computing with Google Cloud (2023-24)
Serverless computing with Google Cloud (2023-24)wesley chun
11 views33 slides
Five Things You SHOULD Know About Postman by
Five Things You SHOULD Know About PostmanFive Things You SHOULD Know About Postman
Five Things You SHOULD Know About PostmanPostman
33 views43 slides
Special_edition_innovator_2023.pdf by
Special_edition_innovator_2023.pdfSpecial_edition_innovator_2023.pdf
Special_edition_innovator_2023.pdfWillDavies22
17 views6 slides
Piloting & Scaling Successfully With Microsoft Viva by
Piloting & Scaling Successfully With Microsoft VivaPiloting & Scaling Successfully With Microsoft Viva
Piloting & Scaling Successfully With Microsoft VivaRichard Harbridge
12 views160 slides
The Research Portal of Catalonia: Growing more (information) & more (services) by
The Research Portal of Catalonia: Growing more (information) & more (services)The Research Portal of Catalonia: Growing more (information) & more (services)
The Research Portal of Catalonia: Growing more (information) & more (services)CSUC - Consorci de Serveis Universitaris de Catalunya
80 views25 slides

Recently uploaded(20)

Serverless computing with Google Cloud (2023-24) by wesley chun
Serverless computing with Google Cloud (2023-24)Serverless computing with Google Cloud (2023-24)
Serverless computing with Google Cloud (2023-24)
wesley chun11 views
Five Things You SHOULD Know About Postman by Postman
Five Things You SHOULD Know About PostmanFive Things You SHOULD Know About Postman
Five Things You SHOULD Know About Postman
Postman33 views
Special_edition_innovator_2023.pdf by WillDavies22
Special_edition_innovator_2023.pdfSpecial_edition_innovator_2023.pdf
Special_edition_innovator_2023.pdf
WillDavies2217 views
Piloting & Scaling Successfully With Microsoft Viva by Richard Harbridge
Piloting & Scaling Successfully With Microsoft VivaPiloting & Scaling Successfully With Microsoft Viva
Piloting & Scaling Successfully With Microsoft Viva
Case Study Copenhagen Energy and Business Central.pdf by Aitana
Case Study Copenhagen Energy and Business Central.pdfCase Study Copenhagen Energy and Business Central.pdf
Case Study Copenhagen Energy and Business Central.pdf
Aitana16 views
Attacking IoT Devices from a Web Perspective - Linux Day by Simone Onofri
Attacking IoT Devices from a Web Perspective - Linux Day Attacking IoT Devices from a Web Perspective - Linux Day
Attacking IoT Devices from a Web Perspective - Linux Day
Simone Onofri16 views
Voice Logger - Telephony Integration Solution at Aegis by Nirmal Sharma
Voice Logger - Telephony Integration Solution at AegisVoice Logger - Telephony Integration Solution at Aegis
Voice Logger - Telephony Integration Solution at Aegis
Nirmal Sharma39 views
The details of description: Techniques, tips, and tangents on alternative tex... by BookNet Canada
The details of description: Techniques, tips, and tangents on alternative tex...The details of description: Techniques, tips, and tangents on alternative tex...
The details of description: Techniques, tips, and tangents on alternative tex...
BookNet Canada127 views
SAP Automation Using Bar Code and FIORI.pdf by Virendra Rai, PMP
SAP Automation Using Bar Code and FIORI.pdfSAP Automation Using Bar Code and FIORI.pdf
SAP Automation Using Bar Code and FIORI.pdf
PharoJS - Zürich Smalltalk Group Meetup November 2023 by Noury Bouraqadi
PharoJS - Zürich Smalltalk Group Meetup November 2023PharoJS - Zürich Smalltalk Group Meetup November 2023
PharoJS - Zürich Smalltalk Group Meetup November 2023
Noury Bouraqadi127 views
Business Analyst Series 2023 - Week 3 Session 5 by DianaGray10
Business Analyst Series 2023 -  Week 3 Session 5Business Analyst Series 2023 -  Week 3 Session 5
Business Analyst Series 2023 - Week 3 Session 5
DianaGray10248 views
【USB韌體設計課程】精選講義節錄-USB的列舉過程_艾鍗學院 by IttrainingIttraining
【USB韌體設計課程】精選講義節錄-USB的列舉過程_艾鍗學院【USB韌體設計課程】精選講義節錄-USB的列舉過程_艾鍗學院
【USB韌體設計課程】精選講義節錄-USB的列舉過程_艾鍗學院

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
  • 8. Downloads image, brings up machine. Firing it up $ vagrant up
  • 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
  • 17. Provisioning — Installing so!ware — Creating files and folders — Editing configuration files — Adding users
  • 18. Vagrant Provisioner config.vm.provision "shell", inline: <<-SHELL apt-get update apt-get install -y apache2 SHELL
  • 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
  • 25. Demo: Dev box — Ubuntu 16.04 LTS — Apache/MySQL/PHP — Git/vim
  • 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