DevOps Hackathon - Session 1: Vagrant

  • 1,048 views
Uploaded on

Current session guides through Vagrant. Shows some tips and tricks and targeted to software developers. …

Current session guides through Vagrant. Shows some tips and tricks and targeted to software developers.

Practical activities can be found here: https://github.com/akranga/devops-hackathon-1

More in: Engineering
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
No Downloads

Views

Total Views
1,048
On Slideshare
0
From Embeds
0
Number of Embeds
2

Actions

Shares
Downloads
33
Comments
0
Likes
5

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. Vagrant hackaton Session Activities can be found here: https://github.com/akranga/devops-hackathon-1
  • 2. Vagrant - Vagrant will mange VM for you - Describe VM in configuration file - Can put configuration in Source Control - You can allow other to contribute - You have full control on VM
  • 3. Vagrant - Ruby powered command line interface to VM hosted in your Computer - Supports multiple VM providers (VirtualBox by default) - Allows to create reproducible and portable environments
  • 4. Vagrant - Supports run of Multi-VM environments - Allows to create/restore snapshots of VM - Allows package environment - Environment configuration stored in file (you can put it in git)
  • 5. Installing Vagrant - Download and install latest VirtualBox for your OS: https://www.virtualbox.org/wiki/Downloads - Download and install latest vagrant for your OS: http://www.vagrantup.com/downloads.html - Update PATH Environment System Variable. Add entries: - VirtualBox location - C:Program FilesOracleVirtualBox - Vagrant location - C:DevOpsvagrantbin - Might require extra directories - C:DevOpsvagrantembeddedbin - C:DevOpsvagrantembeddedmingwbin - C:DevOpsvagrantembeddedgemsbin
  • 6. Vagrant Components - Command Line Interface - Vagrantfile - Boxes - Synced Folder - Provisioning - Multi Machine - Providers
  • 7. Command Line Interface Vagrant basic operating model through CLI vagrant command Running any vagrant subcommand with --help or –h prints context help $ vagrant init $ vagrant up $ vagrant halt $ vagrant reload $ vagrant provision $ vagrant destroy $ vagrant ssh $ vagrant ssh-config Create initial Vagrant configuratoin Start or create VM if not exists Power down VM Restart VM (useful when you change config) Run Chef or Puppet scripts Destroy VM as it never existed Connect to VM if it is running Prints SSH configuration
  • 8. Command Line Interface shell $ vagrant box list $ vagrant box add $ vagrant box remove $ vagrant box repackage $ vagrant box outdated $ vagrant box update List all vagrant environments on your PC Add basebox record to vagrant registry Remove basebox (doesn’t terminate VM) Make a snapshot of your VM Deprecates the VM Checks and updates a VM if it is out-dated Additional commands to manage your vagrant environments
  • 9. Vagrantfile All VM configuration stored in Ruby DSL file. This file can be placed in git. When you run vagrant up. Vagrant will check following directories. ./Vagrantfile /home/mitchellh/projects/foo/Vagrantfile /home/mitchellh/projects/Vagrantfile /home/mitchellh/Vagrantfile /home/Vagrantfile /Vagrantfile VAGRANT_CWD to change directory where Vagrant is looking for configuration
  • 10. Boxes Vagrantfile Vagrant.configure "2" do |config| config.vm.box = “my-vm" config.vm.box_url = “box-url" end Minimal Vagrantfile looks like this Vagrant file contains API versioning.
  • 11. Vagrantfile (cont) Vagrantfile Vagrant.configure "2" do |config| config.vm.box = “my-vm" config.vm.box_url = “box-url“ (1..3).each do |i| config.vm.define "slave-#{i}" do |slave| slave.vm.provision "shell", inline: "echo hello from slave #{i}" end end end How many machines will run this file?
  • 12. Modifying scripts config.vm – to mange VM parameters Vagrantfile config.vm.box config.vm.check_update config.vm.box_url config.vm.box_version config.vm.box.hostname config.vm.provider cofnig.vm.synced_folder Name to be associated with box By default true. Can disable update checks URL of the VM to download Version of the basebox file Hostname of the VM By default VirtualBox. Can be other By default current host dir mounted as /Vagrant. Can map additional directory inside VM
  • 13. Modifying scripts config.ssh – to customize SSH behavior Vagrantfile config.ssh.username config.ssh.password config.ssh.private_key_path config.ssh.insert_key config.ssh.shell By default vagrant No pass by default Path to your SSH key. By default /home/.vagrant.d/insecure_private_key By default true. Can disable to use pass Shell to open when ssh. By default bash -l
  • 14. Boxes To create a VM Vagrant needs a basebox file which is typically Just Enough Operating System. Baseboxes can be downloaded from Community websites www.vagrantbox.es Offsite for Vagrant boxes http://opscode.github.io/bento/ Vagrant boxes optimized for Chef You can create your own box using - Packer (http://packer.io) - Packer templates for Chef optimized boxes can be found: https://github.com/opscode/bento
  • 15. Synced Folder To enable Vagrant to sync files at your Host and Guest machine By default mapped directory: /vagrant Permissions: all files in synced folder are 0777
  • 16. Vagrant Plugins Vagrant is extremely pluggable. You can add/costomize almost everything in Vagrant (such as add VMWare, AWS or Docker providers), Chef or Puppet provisioning etc. Vagrant has tons of plugins. Official hosted here: https://github.com/mitchellh/vagrant/tree/master/plugins Dog Food: API for Vagrant plugins: http://en.wikipedia.org/wiki/Eating_your_own_dog_food Most useful Plugins: - vagrant-omnibus: Chef for Vagrant VMs - vagrant-cachier: Cache for packages (can be reused across VMs) - vagrant-berkshelf: Enable Chef cookbook dependency mechanism
  • 17. Vagrant plugins command CLI to manage vagrant plugins. Will be installed in /home/.vagrant.d/gems Shell $ vagrant plugin install $ vagrant plugin list $ vagrant plugin uninstall $ vagrant plugin update Install vagrant plugin List of installed plugins Erase plugin Check plugin and update with newer version
  • 18. Activity 1 Run a vagrant box.
  • 19. Activity 1 Go to /activity1 directory 1. Create a vagrant box file: $ vagrant init vagrant-hackaton http://opscode-vm- bento.s3.amazonaws.com/vagrant/vmware/opscode_ubuntu-14.04_chef- provisionerless.box 2. Run command: This will create a Vagrant configuration which is pointed to URL. This is a good manner to specify Valid URL so vagrant config can be repeatable out of box $ vagrant up This will start download image from Internet. Please spare our bandwidth and terminate this process (CTRL+C). We will add image from our local disk
  • 20. Activity 1 3. Check if nothing installed $ vagrant box list 5. Spin up VM: Vagrant should return no images. Otherwise we might overwrite one with this activity. Or you might want to change name of VM by modifying Vagrantfile 4. Add box manually with link to local file $ vagrant box add vagrant-hackaton PATH-TO-BOX/opscode_ubuntu- 14.04_chef-provisionerless.box $ vagrant up 6. Connect via SSH $ vagrant ssh
  • 21. Activity 1 7. Type command $ vagrant destroy 8. Destroy VM in VM $ uptime $ exit
  • 22. Activity 2 Customize VM attributes
  • 23. Activity 2 Go to /activity2 directory 1. Run command $ vagrant up This will bring VM to life. Vagrant will reuse basebox from local file because activity2 VM has same name as VM from activty1 2. In the VM run command You should get some HTML text on console. Before exiting SSH sesion in VM $ sudo apt-get install –y apache2 curl $ http://localhost $ exit
  • 24. Activity 2 3. Modify Vagrantfile This will instruct Vagrant to use cache on your Host machine all software installed inside VM (vagrant requires plugin vagrant-cachier) 4. Modify Vagrantfile as per instructions Vagrantfile: TODO2 config.vm.network :forwarded_port, guest: XXXXX, host: YYYYY Vagrantfile: TODO1 config.cache.auto_detect = true Vagrantfile: TODO3 config.vm.synced_folder "webapp/", "/var/www" 5. Modify Vagrantfile as per instructions
  • 25. Activity 2 5. after Vagrantfile changes we need to raload VM $ vagrant reload 6. With your browser open: http://localhost:80801 You will get something like this (on the right) This is allright. We need to create a HTML 7. Create file webapp/index.html webapp/index.html <html> <head><title>Hello World!</title><head> <body><h1>Hello Vagrant!</h1></body </html>
  • 26. Activity 2 No needs to reload VM (directory will be synced automagically ) 8. Refresh your browser at: http://localhost:80801 You will get something like this (on the right) 7. Destroy VM $ vagrant destroy
  • 27. Activity 3 Provision DB Server
  • 28. Activity 3 Go to /activity3 directory 1. Run command $ vagrant up Vagrant will fail with Vagrant: * Unknown configuration section 'berkshelf'. * Unknown configuration section 'omnibus'. 2. You need to install additional plugins $ vagrant plugin install vagrant-berkshelf $ vagrant plugin install vagrant-omnibus $ vagrant up
  • 29. Activity 3 Go to /activity3 directory 1. Run command $ vagrant up Vagrantfile has extra configuration that says it will use Chef (Omnibus packaged) and Berkshelf to download cookbooks. But it doesn’t have plugins and it will fail: Vagrant: * Unknown configuration section 'berkshelf'. * Unknown configuration section 'omnibus'. 2. Check if plugins installed and install plugins $ vagrant plugin list $ vagrant plugin install vagrant-berkshelf $ vagrant plugin install vagrant-omnibus $ vagrant up
  • 30. Activity 3 Go to /activity3 directory 1. Run command $ vagrant up Vagrantfile has extra configuration that says it will use Chef (Omnibus packaged) and Berkshelf to download cookbooks. But it doesn’t have plugins and it will fail: Vagrant: * Unknown configuration section 'berkshelf'. * Unknown configuration section 'omnibus'. 2. Check if plugins installed and install plugins $ vagrant plugin list $ vagrant plugin install vagrant-berkshelf $ vagrant plugin install vagrant-omnibus $ vagrant up
  • 31. Retrospective
  • 32. How many VMs will this file run? Vagrantfile Vagrant.configure "2" do |config| config.vm.box = “my-vm" config.vm.box_url = “box-url“ (1..3).each do |i| config.vm.define "slave-#{i}" do |slave| slave.vm.provision "shell", inline: "echo hello from slave #{i}" end end end
  • 33. Synced folder tricks Vagrantfile require 'rbconfig' is_windows = (RbConfig::CONFIG['host_os'] =~ /mswin|mingw|cygwin/) Vagrant.configure "2" do |config| config.vm.box = “my-vm" config.vm.box_url = “box-url“ config.vm.synced_folder "webapp/", "/var/www/html", nfs: is_windows end Synced folder in windows synchronizes in one direction. To enable it to synchronize in both directions we need to say that host system is Windows by setting parameter nfs to true
  • 34. Synced folder tricks Vagrantfile require 'rbconfig' is_windows = (RbConfig::CONFIG['host_os'] =~ /mswin|mingw|cygwin/) Vagrant.configure "2" do |config| config.vm.box = “my-vm" config.vm.box_url = “box-url“ config.vm.synced_folder "webapp/", "/var/www/html", nfs: is_windows end Synced folder in windows synchronizes in one direction. To enable it to synchronize in both directions we need to say that host system is Windows by setting parameter nfs to true
  • 35. Synced folder tricks If Windows host machine you cannot create symlinks in synced directory However there is a workaround. 1. Add vm customization parameter to Vagrantfile Vagrantfile config.vm.customize ["setextradata", :id, "VBoxInternal2/SharedFoldersEnableSymlinksCreate/v-root", "1"] if is_windows 2. Run vagrant up as user Administrator
  • 36. Shell $ git clone https://github.com/opscode/bento.git $ cd bento/packer bento/packer $ packer build ubuntu-13.10-amd64.json Creating VM from scratch
  • 37. Shell bento/packer $ packer build -only=virtualbox -var-file=variables.json ubuntu-13.10-amd64.json Packer with extra params variables.json { "chef_version": "latest", "mirror": "../builds/iso/" }