Vagrant Managing Virtual Machines
“But it works on my machine….”
Vagrant – take back control! Build environments based on virtual machines Create a development system that matches production Share that system with development team
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
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
Pre-Requisites VirtualBox or similar The Virtual Machine system Vagrant Download installer from www.vagrantup.com/downloads.html A Base box (Next Slide...)
Vagrant Boxes Previously built base VM images that are ready to run Create your own Use a publicly created one https://atlas.hashicorp.com/boxes/search Various flavours of Linux, Windows or MacOS
$ 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
Hashicorp (makers of Vagrant) publish as basic Ubuntu Linux 12.04 box Official Boxes Hashicorp (makers of Vagrant) publish as basic Ubuntu Linux 12.04 box $ vagrant box add hashicorp/precise64
$ 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
$ 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
$ 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
$ vagrant ssh Logs into the VM over SSH using the public key created during initialisation
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)
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
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 16.04 VM from the open source Bento* project in a default configuration. * Bento – a single portion carry out meal in Japanese cooking
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)
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: "192.168.1.1" end
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: "10.0.0.1" end
Some Config options config.vm.provider Allows overriding options for the Virtual Machine provider. config.vm.provider "virtualbox" do |vb| vb.memory = "2048" end
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
Provisioning Allows initial configuration of the VM Shell scripts Ansible Chef Puppet etc
When provisioning happens On first vagrant up When vagrant provision is run on an active VM When vagrant reload --provision is run
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
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
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: "http://host.com/script.sh" end Vagrant.configure("2") do |config|config.vm.provision "shell", inline: <<-SHELL apt-get update mkdir /home/vagrant/src SHELL end
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
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
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: 192.168.163.152 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: http://ghastly-wombat-4051.vagrantshare.com
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>
SSH Sharing (Cont.) $ vagrant share --ssh ==> default: Detecting network information for machine... default: Local machine address: 192.168.163.152 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
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
Questions? ming@siliconbladeconsultants.com
Blatant Plug Available For: System Design Bespoke Programming Web Applications High Availability Design Weddings, Bar Mitzvahs and Baby Namings www.siliconbladeconsultants.com