SlideShare une entreprise Scribd logo
1  sur  43
Télécharger pour lire hors ligne
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!
```

Contenu connexe

Tendances

CyberAgentのプライベートクラウド Cycloudの運用及びモニタリングについて #CODT2020 / Administration and M...
CyberAgentのプライベートクラウド Cycloudの運用及びモニタリングについて #CODT2020 / Administration and M...CyberAgentのプライベートクラウド Cycloudの運用及びモニタリングについて #CODT2020 / Administration and M...
CyberAgentのプライベートクラウド Cycloudの運用及びモニタリングについて #CODT2020 / Administration and M...
whywaita
 

Tendances (20)

Linux KVM環境におけるGPGPU活用最新動向
Linux KVM環境におけるGPGPU活用最新動向Linux KVM環境におけるGPGPU活用最新動向
Linux KVM環境におけるGPGPU活用最新動向
 
世界一わかりやすいClean Architecture
世界一わかりやすいClean Architecture世界一わかりやすいClean Architecture
世界一わかりやすいClean Architecture
 
開発者のためのActive Directory講座
開発者のためのActive Directory講座開発者のためのActive Directory講座
開発者のためのActive Directory講座
 
Java開発の強力な相棒として今すぐ使えるGroovy
Java開発の強力な相棒として今すぐ使えるGroovyJava開発の強力な相棒として今すぐ使えるGroovy
Java開発の強力な相棒として今すぐ使えるGroovy
 
[Golang] 以 Mobile App 工程師視角,帶你進入 Golang 的世界 (Introduction of GoLang)
[Golang] 以 Mobile App 工程師視角,帶你進入 Golang 的世界 (Introduction of GoLang) [Golang] 以 Mobile App 工程師視角,帶你進入 Golang 的世界 (Introduction of GoLang)
[Golang] 以 Mobile App 工程師視角,帶你進入 Golang 的世界 (Introduction of GoLang)
 
MongoDB Oplog入門
MongoDB Oplog入門MongoDB Oplog入門
MongoDB Oplog入門
 
Git超入門_座学編.pdf
Git超入門_座学編.pdfGit超入門_座学編.pdf
Git超入門_座学編.pdf
 
インフラエンジニアのためのRancherを使ったDocker運用入門
インフラエンジニアのためのRancherを使ったDocker運用入門インフラエンジニアのためのRancherを使ったDocker運用入門
インフラエンジニアのためのRancherを使ったDocker運用入門
 
OSvの概要と実装
OSvの概要と実装OSvの概要と実装
OSvの概要と実装
 
Flutterアプリ開発におけるモジュール分割戦略
Flutterアプリ開発におけるモジュール分割戦略Flutterアプリ開発におけるモジュール分割戦略
Flutterアプリ開発におけるモジュール分割戦略
 
鯨物語~Dockerコンテナとオーケストレーションの理解
鯨物語~Dockerコンテナとオーケストレーションの理解鯨物語~Dockerコンテナとオーケストレーションの理解
鯨物語~Dockerコンテナとオーケストレーションの理解
 
JDK 16 で導入された JEP 396 にご注意!! (JJUG CCC 2021 Spring)
JDK 16 で導入された JEP 396 にご注意!! (JJUG CCC 2021 Spring)JDK 16 で導入された JEP 396 にご注意!! (JJUG CCC 2021 Spring)
JDK 16 で導入された JEP 396 にご注意!! (JJUG CCC 2021 Spring)
 
Dockerを使ったローカルでの開発から本番環境へのデプロイまで
Dockerを使ったローカルでの開発から本番環境へのデプロイまでDockerを使ったローカルでの開発から本番環境へのデプロイまで
Dockerを使ったローカルでの開発から本番環境へのデプロイまで
 
MongoDB: システム可用性を拡張するインデクス戦略
MongoDB: システム可用性を拡張するインデクス戦略MongoDB: システム可用性を拡張するインデクス戦略
MongoDB: システム可用性を拡張するインデクス戦略
 
Docker入門-基礎編 いまから始めるDocker管理【2nd Edition】
Docker入門-基礎編 いまから始めるDocker管理【2nd Edition】Docker入門-基礎編 いまから始めるDocker管理【2nd Edition】
Docker入門-基礎編 いまから始めるDocker管理【2nd Edition】
 
CyberAgentのプライベートクラウド Cycloudの運用及びモニタリングについて #CODT2020 / Administration and M...
CyberAgentのプライベートクラウド Cycloudの運用及びモニタリングについて #CODT2020 / Administration and M...CyberAgentのプライベートクラウド Cycloudの運用及びモニタリングについて #CODT2020 / Administration and M...
CyberAgentのプライベートクラウド Cycloudの運用及びモニタリングについて #CODT2020 / Administration and M...
 
HashiCorpのNomadを使ったコンテナのスケジューリング手法
HashiCorpのNomadを使ったコンテナのスケジューリング手法HashiCorpのNomadを使ったコンテナのスケジューリング手法
HashiCorpのNomadを使ったコンテナのスケジューリング手法
 
.NET Core時代のCI/CD
.NET Core時代のCI/CD.NET Core時代のCI/CD
.NET Core時代のCI/CD
 
Kotlinアンチパターン
KotlinアンチパターンKotlinアンチパターン
Kotlinアンチパターン
 
これからのJDK 何を選ぶ?どう選ぶ? (v1.2) in 熊本
これからのJDK 何を選ぶ?どう選ぶ? (v1.2) in 熊本これからのJDK 何を選ぶ?どう選ぶ? (v1.2) in 熊本
これからのJDK 何を選ぶ?どう選ぶ? (v1.2) in 熊本
 

Similaire à Create Development and Production Environments with Vagrant

Continuous Delivery with Maven, Puppet and Tomcat - ApacheCon NA 2013
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 Sanchez
 
Node.js, Vagrant, Chef, and Mathoid @ Benetech
Node.js, Vagrant, Chef, and Mathoid @ BenetechNode.js, Vagrant, Chef, and Mathoid @ Benetech
Node.js, Vagrant, Chef, and Mathoid @ Benetech
Christopher Bumgardner
 

Similaire à Create Development and Production Environments with Vagrant (20)

Vagrant - Version control your dev environment
Vagrant - Version control your dev environmentVagrant - Version control your dev environment
Vagrant - Version control your dev environment
 
Intro to vagrant
Intro to vagrantIntro to vagrant
Intro to vagrant
 
Vagrant - Team Development made easy
Vagrant - Team Development made easyVagrant - Team Development made easy
Vagrant - Team Development made easy
 
Create your very own Development Environment with Vagrant and Packer
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
 
Making Developers Productive with Vagrant, VirtualBox, and Docker
Making Developers Productive with Vagrant, VirtualBox, and DockerMaking Developers Productive with Vagrant, VirtualBox, and Docker
Making Developers Productive with Vagrant, VirtualBox, and Docker
 
Vagrant For DevOps
Vagrant For DevOpsVagrant For DevOps
Vagrant For DevOps
 
Quick & Easy Dev Environments with Vagrant
Quick & Easy Dev Environments with VagrantQuick & Easy Dev Environments with Vagrant
Quick & Easy Dev Environments with Vagrant
 
Vagrant introduction for Developers
Vagrant introduction for DevelopersVagrant introduction for Developers
Vagrant introduction for Developers
 
Puppet and Vagrant in development
Puppet and Vagrant in developmentPuppet and Vagrant in development
Puppet and Vagrant in development
 
Vagrant-Overview
Vagrant-OverviewVagrant-Overview
Vagrant-Overview
 
Vagrant
VagrantVagrant
Vagrant
 
NetDevOps Developer Environments with Vagrant @ SCALE16x
NetDevOps Developer Environments with Vagrant @ SCALE16xNetDevOps Developer Environments with Vagrant @ SCALE16x
NetDevOps Developer Environments with Vagrant @ SCALE16x
 
Cooking Perl with Chef: Hello World Tutorial
Cooking Perl with Chef: Hello World TutorialCooking Perl with Chef: Hello World Tutorial
Cooking Perl with Chef: Hello World Tutorial
 
Vagrant
VagrantVagrant
Vagrant
 
Vagrant Up in 5 Easy Steps
Vagrant Up in 5 Easy StepsVagrant Up in 5 Easy Steps
Vagrant Up in 5 Easy Steps
 
Minicurso de Vagrant
Minicurso de VagrantMinicurso de Vagrant
Minicurso de Vagrant
 
Continuous Delivery: The Next Frontier
Continuous Delivery: The Next FrontierContinuous Delivery: The Next Frontier
Continuous Delivery: The Next Frontier
 
Continuous Delivery with Maven, Puppet and Tomcat - ApacheCon NA 2013
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
 
Node.js, Vagrant, Chef, and Mathoid @ Benetech
Node.js, Vagrant, Chef, and Mathoid @ BenetechNode.js, Vagrant, Chef, and Mathoid @ Benetech
Node.js, Vagrant, Chef, and Mathoid @ Benetech
 
Preparation study of_docker - (MOSG)
Preparation study of_docker  - (MOSG)Preparation study of_docker  - (MOSG)
Preparation study of_docker - (MOSG)
 

Plus de 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.
FUD-Free Accessibility for Web Developers - Also, Cake.
Brian Hogan
 
Responsive Web Design
Responsive Web DesignResponsive Web Design
Responsive Web Design
Brian Hogan
 
Turning Passion Into Words
Turning Passion Into WordsTurning Passion Into Words
Turning Passion Into Words
Brian Hogan
 

Plus de Brian Hogan (20)

Creating and Deploying Static Sites with Hugo
Creating and Deploying Static Sites with HugoCreating and Deploying Static Sites with Hugo
Creating and Deploying Static Sites with Hugo
 
Automating the Cloud with Terraform, and Ansible
Automating the Cloud with Terraform, and AnsibleAutomating the Cloud with Terraform, and Ansible
Automating the Cloud with Terraform, and Ansible
 
Docker
DockerDocker
Docker
 
Getting Started Contributing To Open Source
Getting Started Contributing To Open SourceGetting Started Contributing To Open Source
Getting Started Contributing To Open Source
 
Rethink Frontend Development With Elm
Rethink Frontend Development With ElmRethink Frontend Development With Elm
Rethink Frontend Development With Elm
 
Testing Client-side Code with Jasmine and CoffeeScript
Testing Client-side Code with Jasmine and CoffeeScriptTesting Client-side Code with Jasmine and CoffeeScript
Testing Client-side Code with Jasmine and CoffeeScript
 
FUD-Free Accessibility for Web Developers - Also, Cake.
FUD-Free Accessibility for Web Developers - Also, Cake.FUD-Free Accessibility for Web Developers - Also, Cake.
FUD-Free Accessibility for Web Developers - Also, Cake.
 
Responsive Web Design
Responsive Web DesignResponsive Web Design
Responsive Web Design
 
Web Development with CoffeeScript and Sass
Web Development with CoffeeScript and SassWeb Development with CoffeeScript and Sass
Web Development with CoffeeScript and Sass
 
Building A Gem From Scratch
Building A Gem From ScratchBuilding A Gem From Scratch
Building A Gem From Scratch
 
Intro To Advanced Ruby
Intro To Advanced RubyIntro To Advanced Ruby
Intro To Advanced Ruby
 
Turning Passion Into Words
Turning Passion Into WordsTurning Passion Into Words
Turning Passion Into Words
 
HTML5 and CSS3 Today
HTML5 and CSS3 TodayHTML5 and CSS3 Today
HTML5 and CSS3 Today
 
Web Development With Ruby - From Simple To Complex
Web Development With Ruby - From Simple To ComplexWeb Development With Ruby - From Simple To Complex
Web Development With Ruby - From Simple To Complex
 
Stop Reinventing The Wheel - The Ruby Standard Library
Stop Reinventing The Wheel - The Ruby Standard LibraryStop Reinventing The Wheel - The Ruby Standard Library
Stop Reinventing The Wheel - The Ruby Standard Library
 
Intro to Ruby
Intro to RubyIntro to Ruby
Intro to Ruby
 
Intro to Ruby - Twin Cities Code Camp 7
Intro to Ruby - Twin Cities Code Camp 7Intro to Ruby - Twin Cities Code Camp 7
Intro to Ruby - Twin Cities Code Camp 7
 
Make GUI Apps with Shoes
Make GUI Apps with ShoesMake GUI Apps with Shoes
Make GUI Apps with Shoes
 
The Why Of Ruby
The Why Of RubyThe Why Of Ruby
The Why Of Ruby
 
Story-driven Testing
Story-driven TestingStory-driven Testing
Story-driven Testing
 

Dernier

Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Victor Rentea
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Victor Rentea
 

Dernier (20)

ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistan
 
Spring Boot vs Quarkus the ultimate battle - DevoxxUK
Spring Boot vs Quarkus the ultimate battle - DevoxxUKSpring Boot vs Quarkus the ultimate battle - DevoxxUK
Spring Boot vs Quarkus the ultimate battle - DevoxxUK
 

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