Successfully reported this slideshow.
Your SlideShare is downloading. ×

Vagrant for real (codemotion rome 2016)

Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
Loading in …3
×

Check these out next

1 of 99 Ad

Vagrant for real (codemotion rome 2016)

Download to read offline

Vagrant is a well-known tool for creating development environments in a simple and consistent way. Since we adopted in our organization we experienced several benefits: lower project setup times, better shared knowledge among team members, less wtf moments ;-)
In this session I'd like to share our experience, including but not limited to:
- advanced vagrantfile configuration
- vm configuration tips for dev environment: performance, debug, tuning
- our wtf moments
- puphet/phansilbe: hot or not?
- tips for sharing a box

Vagrant is a well-known tool for creating development environments in a simple and consistent way. Since we adopted in our organization we experienced several benefits: lower project setup times, better shared knowledge among team members, less wtf moments ;-)
In this session I'd like to share our experience, including but not limited to:
- advanced vagrantfile configuration
- vm configuration tips for dev environment: performance, debug, tuning
- our wtf moments
- puphet/phansilbe: hot or not?
- tips for sharing a box

Advertisement
Advertisement

More Related Content

Similar to Vagrant for real (codemotion rome 2016) (20)

Advertisement

More from Michele Orselli (20)

Advertisement

Vagrant for real (codemotion rome 2016)

  1. 1. Vagrant for Real
  2. 2. Michele Orselli CTO@Ideato _orso_ micheleorselli / ideatosrl mo@ideato.it
  3. 3. http://mitchellh.com/the-tao-of-vagrant
  4. 4. 1) clone repo 2) vagrant up 3) there’s no #3
  5. 5. 1) clone repo 2) vagrant up
  6. 6. #tips
  7. 7. vagrantfile configs vm performance app config tips provision/packaging
  8. 8. Portable configuration tips
  9. 9. Don’t use local names for your boxes config.vm.box = "base"
  10. 10. c.vm.box = "hashicorp/precise64"
  11. 11. c.vm.box_url = "http://your_box"
  12. 12. Avoid absolute paths
  13. 13. c.vm.synced_folder “/myProj","/var/www/myProj"
  14. 14. config.vm.synced_folder "./", "/var/www/myProj"
  15. 15. Move host specific configuration outside Vagrantfile
  16. 16. ram: 2048 cpus: 2 ipaddress: 10.10.10.10 vagrantfile-local-config.yml
  17. 17. require ‘yaml' _config = YAML.load(File.open(File.join(File.dirname(__FILE__), “vagrantfile-local-config.yml”), File::RDONLY).read) CONF = _config config.vm.provider "virtualbox" do |vb| vb.customize["modifyvm",:id,“--memory", CONF["ram"]] vb.customize ["modifyvm",:id,"--cpus", CONF[“cpus"]] … end
  18. 18. As rule of thumb you could assign the half cpus and a quarter of the memory based on your host machine https://stefanwrobel.com/how-to-make-vagrant-performance-not-suck
  19. 19. Force a specific provider
  20. 20. ENV['VAGRANT_DEFAULT_PROVIDER'] = 'virtualbox'
  21. 21. Support multiple providers
  22. 22. config.vm.provider "virtualbox" do |vb| vb.customize ["modifyvm",:id, "--memory", CONF["ram"]] vb.customize ["modifyvm",:id, "--cpus", CONF["cpus"]] end config.vm.provider "vmware_fusion" do |v| v.vmx["memsize"] = CONF["ram"] v.vmx["numvcpus"] = CONF["cpus"] end
  23. 23. Project’s directories organization
  24. 24. myproject -- … -- vagrant -- Vagrantfile single git repository c.vm.synced_folder “.”,”/var/www/myproject"
  25. 25. myproject -- project --… -- vagrant -- Vagrantfile single git repository c.vm.synced_folder “./project”,”/var/www/ project”
  26. 26. myproject -- vagrant -- www -- project1 -- project2 -- Vagrantfile single/multiple git repositories c.vm.synced_folder “./www”,”/var/www”
  27. 27. vagrant plugin install HostsUpdater (or HostManager)
  28. 28. # /etc/hosts 10.10.10.40 host1.lo #VAGRANT: e2bca7c8bf6d76cbcf6ee48998c16f
  29. 29. if Vagrant.has_plugin?("HostsUpdater") config.hostsupdater.aliases = ["host1.lo"] else puts "--- WARNING ---"
 puts “You should install HostsUpdater” end
  30. 30. Multi-VM Configuration
  31. 31. config.vm.define "web" do |web| web.vm.network :private_network, ip: "10.0.0.2" web.vm.provider :virtualbox do |v| v.customize ["modifyvm", :id, "--memory", 2048] end web.vm.synced_folder "../", “/var/www" end config.vm.define "db" do |db| db.vm.network :private_network, ip: "10.0.0.3" db.vm.provision :shell, :path => 'vagrant/db.sh' end
  32. 32. Dealing with shared folders
  33. 33. no share
  34. 34. native share - slow - os indipendent-ish
  35. 35. nfs - fast - win support :-(
  36. 36. rsync - no real share - update
  37. 37. VM Performance
  38. 38. run tests… still running…
  39. 39. mitchellh.com/comparing-filesystem-performance-in-virtual- machines Virtualbox vs VmWare
  40. 40. Virtualbox 22 min VmWare 15 min
  41. 41. Virtualbox 22 min VmWare 15 min +30%
  42. 42. use VmWare if you can (it will cost you a few $)
  43. 43. use nfs if you can*
  44. 44. vagrant plugin install vbguest keeps guest addition updated
  45. 45. unless Vagrant.has_plugin?(“vagrant-vbguest") raise ”please install vagrant-vbguest” end
  46. 46. On VM I/O is the bottleneck
  47. 47. loading webpages running testsuites are READ heavy
  48. 48. move I/O outside shared folders
  49. 49. Default provider: virtualbox 3 PHP test suites with unit, functional, integration mix small (sf1): build runs in ~25 sec medium (zf2): build runs in ~2 mins large (sf2): build runs ~ 20 mins
  50. 50. I/O: logs, cache
  51. 51. class AppKernel extends Kernel { public function getCacheDir() { if (in_array($this->environment, array('dev', 'test'))) { return '/dev/shm/appname/cache/' . $this->environment; } return parent::getCacheDir(); } public function getLogDir() { if (in_array($this->environment, array('dev', ‘test'))) { return '/dev/shm/appname/logs'; } return parent::getLogDir(); } }
  52. 52. class AppKernel extends Kernel { public function getCacheDir() { if (in_array($this->environment, array('dev', 'test'))) { return '/dev/shm/appname/cache/' . $this->environment; } return parent::getCacheDir(); } public function getLogDir() { if (in_array($this->environment, array('dev', ‘test'))) { return '/dev/shm/appname/logs'; } return parent::getLogDir(); } } +13-16%
  53. 53. I/O: move DB on RAM if you use sqlite move it on /dev/shm
  54. 54. vagrant plugin install vagrantcachier
  55. 55. if Vagrant.has_plugin?("vagrant-cachier") config.cache.scope = :box config.cache.synced_folder_opts = { type: :nfs, mount_options: ['rw','vers=3','tcp','nolock'] } end Vagrantfile
  56. 56. if Vagrant.has_plugin?("vagrant-cachier") config.cache.scope = :box config.cache.synced_folder_opts = { type: :nfs, mount_options: ['rw','vers=3','tcp','nolock'] } end Vagrantfile +30%for reprovisioning a box with git, php apache, mysql
  57. 57. use cachefilesd for nfs
  58. 58. - name: Install cachefilesd apt: pkg=cachefilesd state=present - name: Enable cachefilesd lineinfile: dest=/etc/default/cachefilesd line=“RUN=yes” - name: Start cachefilesd service service: name=cachefilesd state=restarted
  59. 59. config.vm.synced_folder "../", "/var/www", id: “vagrant-root”, type: “nfs”, mount_options: ['rw','vers=3','tcp','fsc'] Vagrantfile
  60. 60. config.vm.synced_folder "../", "/var/www", id: “vagrant-root”, type: “nfs”, mount_options: ['rw','vers=3','tcp','fsc'] Vagrantfile -15% +10%
  61. 61. should you trust these numbers?
  62. 62. Application Management
  63. 63. How to access mysql
  64. 64. - name: add mysql user mysql_user: name=ideato host='%' password=ideato priv=*.*:ALL,GRANT login_user=root login_password= - name: config bind address to allow remote remote connections lineinfile: dest=/etc/mysql/my.cnf state=present regexp='bind-address = 127.0.0.1' line='bind-address = 0.0.0.0' backup=yes - name: restart mysql service: name=mysql state=restarted
  65. 65. Permissions management in shared folders
  66. 66. config.vm.synced_folder "../", "/var/www", id: "vagrant-root", owner: "vagrant", group: "vagrant", mount_options: ["dmode=777,fmode=777"]
  67. 67. Use host ssh keys
  68. 68. config.ssh.forward_agent = true
  69. 69. ForwardAgent yes check ssh config file in your host machine
  70. 70. grunt/gulp watch http://www.sebastien-han.fr/blog/2012/12/18/noac-performance-impact-on-web- applications/
  71. 71. config.vm.synced_folder "../", "/var/www", id: “vagrant-root”, type: “nfs”, mount_options: [‘rw’,'vers=3','tcp','fsc', 'actimeo=1']
  72. 72. Provisioning
  73. 73. to phansible or not to phansible?
  74. 74. quick and easy way to start they’re general old platforms are not supported a lot of a good ideas you can steal from
  75. 75. if which('ansible-playbook') config.vm.provision "ansible" do |ansible| ansible.playbook = "ansible/playbook.yml" ansible.inventory_path = "ansible/inventories/dev" ansible.limit = 'all' ansible.extra_vars = { private_interface: "192.168.33.99", hostname: "default" } end else config.vm.provision :shell, path: "ansible/windows.sh", args: ["default"] end
  76. 76. can you assume everyone in your team have the same version?
  77. 77. if which('ansible-playbook') config.vm.provision "ansible" do |ansible| ansible.playbook = "ansible/playbook.yml" ansible.inventory_path = "ansible/inventories/dev" ansible.limit = 'all' ansible.extra_vars = { private_interface: "192.168.33.99", hostname: "default" } end else config.vm.provision :shell, path: "ansible/windows.sh", args: ["default"] end
  78. 78. the provisioning tool is a moving part: wanna update? be careful
  79. 79. create Vagrant indipendent provision scripts
  80. 80. config.vm.provision :shell, :path => "scripts/bootstrap.sh", :args => "/var/www" config.vm.provision :shell, :path => “scripts/provision.sh", :args => "/var/www"
  81. 81. create your own template https://github.com/ideatosrl/vagrant-php-template
  82. 82. you’re in control of provisioning command you can perform additional checks on host machine
  83. 83. Distributing VMs
  84. 84. provisioning does not create immutable vm by default eg: package update on LTS
  85. 85. live on the edge and fix provision script use stable package repositories
  86. 86. https://speakerdeck.com/mitchellh/vagrant-usage-patterns Create and distribute your own VM
  87. 87. Golden image
  88. 88. vagrant package - -name mybox.box publish it somewhere (http, atlas) c.vm.box_url = “http://../mybox.box”
  89. 89. - Don’t make assumptions about the host - Provision first, then bake your own image - The more moving part the harder will get
  90. 90. Keep it simple
  91. 91. Thank you! _orso_ micheleorselli / ideatosrl mo@ideato.it

×