Download presentation
Presentation is loading. Please wait.
1
Vagrant Managing Virtual Machines
2
“But it works on my machine….”
3
Vagrant – take back control!
Build environments based on virtual machines Create a development system that matches production Share that system with development team
4
What is Vagrant? A wrapper around Virtual Machines
VirtualBox, VMware, AWS Create standard environments using provisioning scripts Everyone works from the same base configuration Remote access to boxes Help test and debug remotely
5
Why use Vagrant Create new Virtual Machines (VMs) quickly
Reproducibility Provision the environments with a script Portability Only need to copy the initialisation and provisioning scripts Cross Platform Linux, MacOS, Windows
6
Pre-Requisites VirtualBox or similar The Virtual Machine system
Vagrant Download installer from A Base box (Next Slide...)
7
Vagrant Boxes Previously built base VM images that are ready to run
Create your own Use a publicly created one Various flavours of Linux, Windows or MacOS
8
$ vagrant box add USER/BOX
Vagrant Boxes (cont.) VM images are downloaded available globally to the local host system Add a box to the local repository (done automatically in the initialisation) $ vagrant box add USER/BOX
9
Hashicorp (makers of Vagrant) publish as basic Ubuntu Linux 12.04 box
Official Boxes Hashicorp (makers of Vagrant) publish as basic Ubuntu Linux box $ vagrant box add hashicorp/precise64
10
$ vagrant init hashicorp/precise64
Getting Up and Running Having found a suitable image, all it needs is to be initialised and brought up $ cd ~/wip/new_project $ vagrant init hashicorp/precise64 $ vagrant up $ vagrant ssh
11
$ vagrant init This will check the local repository for the image and download it if required Creates a copy of the image in your working directory Creates a Vagrantfile initialisation file in your working directory
12
$ vagrant up Reads the configuation from the Vagrantfile
Starts the Virtual Machine Provisions the machine according to the configuration Includes copying a SSH public key for access to the VM
13
$ vagrant ssh Logs into the VM over SSH using the public key created during initialisation
14
Vagrantfile The key configuration file
Describes the VM configuration and optional provisioning Written in Ruby (but simple enough to understand without detailed knowledge of Ruby)
15
Search Path Vagrant will climb the directory tree looking for the first Vagrantfile it can find Starts from the current directory and works up Start can be overridden with VAGRANT_CWD environment variable
16
Minimum Configuration
# -*- mode: ruby -*- # vi: set ft=ruby : Vagrant.configure("2") do |config| config.vm.box = "bento/ubuntu-16.04" end This will use the Ubuntu VM from the open source Bento* project in a default configuration. * Bento – a single portion carry out meal in Japanese cooking
17
Some Config options config.vm
config.vm.box The VM to be used config.vm.hostname Sets the hostname of the box config.vm.network Configures the network (see separate slide) config.vm.provider Configures settings specific to a provider (see separate slide) config.vm.synced_folder Configures the synced folders between the host and the guest (see separate slide)
18
Some Config options config.vm.network
Port forwarding Static IP Vagrant.configure("2") do |config| config.vm.network "forwarded_port", guest: 80, host: 8080 end Vagrant.configure("2") do |config| config.vm.network "private_network", ip: " " end
19
Private vs Public Networks
By default, networks are private (only accessible from the host machine) Use the flag "public_network" to make the guest network accessible from the LAN Vagrant.configure("2") do |config| config.vm.network "public_network", ip: " " end
20
Some Config options config.vm.provider
Allows overriding options for the Virtual Machine provider. config.vm.provider "virtualbox" do |vb| vb.memory = "2048" end
21
Some Config options config.vm.synced_folder
By default, Vagrant shares the project directory (the one with the Vagrantfile) to the guest as /vagrant Additional folders can be shared Useful option to keep project sources on the host machine Vagrant.configure("2") do |config| # other config here config.vm.synced_folder "src/", "/srv/website" end
22
Provisioning Allows initial configuration of the VM Shell scripts
Ansible Chef Puppet etc
23
When provisioning happens
On first vagrant up When vagrant provision is run on an active VM When vagrant reload --provision is run
24
Define provisioning within the Vagrant file config.vm.provision
Basic Provisioning Define provisioning within the Vagrant file config.vm.provision Vagrant.configure("2") do |config| # ... other configuration config.vm.provision "shell" do |s| s.inline = "echo hello" end
25
Either inline or path (path to an external script file)
Shell Scripts Either inline or path (path to an external script file) config.vm.provision "shell", inline: <<-SHELL apt-get update # Create the linux directory if it does not exist if [ ! -d /home/vagrant/src/linux ] then cd /home/vagrant mkdir src cd src fi SHELL end
26
Shell Scripts Vagrant.configure("2") do |config|
config.vm.provision "shell", path: "script.sh" end Vagrant.configure("2") do |config| config.vm.provision "shell", path: " end Vagrant.configure("2") do |config|config.vm.provision "shell", inline: <<-SHELL apt-get update mkdir /home/vagrant/src SHELL end
27
Files Allows uploading of files from the host to the guest VM
Used in conjunction with shell scripts Vagrant.configure("2") do |config| # ... other configuration config.vm.provision "file", source: "~/.gitconfig", destination: ".gitconfig" end
28
Sharing the VM Allows sharing of the VM to anyone (including remote sites) Three modes HTTP SSH General Any (or all) and be used at any time Requires (free) account with Hashicorp Atlas
29
Creates a URL that you can share with anyone Default mode
HTTP Sharing Creates a URL that you can share with anyone Default mode $ vagrant share ==> default: Detecting network information for machine... default: Local machine address: default: Local HTTP port: 4567 default: Local HTTPS port: disabled ==> default: Checking authentication and authorization... ==> default: Creating Vagrant Share session... default: Share will be at: ghastly-wombat-4051 ==> default: Your Vagrant Share is running! default: Name: ghastly-wombat-4051 ==> default: URL:
30
SSH Sharing Creates a hostname that can be used to provide remote SSH access Creates a public/private key pair. Private key is encrypted and uploaded to the Vagrant management server Public key copied to guest VM Access remotely with $ vagrant connect –ssh <hostname>
31
SSH Sharing (Cont.) $ vagrant share --ssh
==> default: Detecting network information for machine... default: Local machine address: default: Local HTTP port: 4567 default: Local HTTPS port: disabled default: SSH Port: 22 ==> default: Generating new SSH key... default: Please enter a password to encrypt the key: default: Repeat the password to confirm: default: Inserting generated SSH key into machine... ==> default: Checking authentication and authorization... ==> default: Creating Vagrant Share session... default: Share will be at: itty-bitty-polar-8667 ==> default: Your Vagrant Share is running! default: Name: itty-bitty-polar-8667
32
vagrant halt Stops the VM cleanly vagrant destroy Removes the VM
Stopping the VM vagrant halt Stops the VM cleanly vagrant destroy Removes the VM
33
Questions? ming@siliconbladeconsultants.com
34
Blatant Plug Available For: System Design Bespoke Programming
Web Applications High Availability Design Weddings, Bar Mitzvahs and Baby Namings
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.