Vagrant for real

857 views

Published on

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 we’d like to share our experience, including but not limited to:advanced vagrantfile configurationvm configuration tips for dev environment: performance,
debug, tuning,
our wtf moments
puphet/phansilbe: hot or not?
packaging a box

Published in: Software
0 Comments
6 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
857
On SlideShare
0
From Embeds
0
Number of Embeds
63
Actions
Shares
0
Downloads
16
Comments
0
Likes
6
Embeds 0
No embeds

No notes for slide

Vagrant for real

  1. 1. Vagrant for Real
  2. 2. Michele Orselli CTO@Ideato _orso_ micheleorselli / ideatosrl mo@ideato.it Riccardo Franconi SW Engineer@Ideato ricfrank ricfrank / ideatosrl rf@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. #tips
  6. 6. vagrantfile configs vm performance app config tips provision/packaging
  7. 7. Portable configuration tips
  8. 8. Don’t use local names for your boxes
  9. 9. c.vm.box = "hashicorp/precise64"
  10. 10. c.vm.box_url = "http://your_box"
  11. 11. Avoid absolute paths
  12. 12. c.vm.synced_folder “/myProj","/var/www/myProj"
  13. 13. config.vm.synced_folder "./", "/var/www/myProj"
  14. 14. Move host specific configuration outside Vagrantfile
  15. 15. ram: 2048 cpus: 2 ipaddress: 10.10.10.10 vagrantfile-local-config.yml
  16. 16. 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
  17. 17. 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
  18. 18. Force a specific provider
  19. 19. ENV['VAGRANT_DEFAULT_PROVIDER'] = 'virtualbox'
  20. 20. Project’s folders organization
  21. 21. project_dir -- vagrant -- project -- Vagrantfile
  22. 22. project_dir -- vagrant -- Vagrantfile -- www -- project1 -- project2 -- project3
  23. 23. vagrant plugin install HostsUpdater
  24. 24. if Vagrant.has_plugin?("HostsUpdater") config.hostsupdater.aliases = ["host1.lo"] else puts "--- WARNING ---"
 puts “You should install HostsUpdater” end
  25. 25. VM Performance
  26. 26. run tests… still running…
  27. 27. mitchellh.com/comparing-filesystem-performance-in-virtual- machines Virtualbox vs VmWare
  28. 28. Virtualbox 22 min VmWare 15 min #tip: usevmware
  29. 29. Virtualbox 22 mi VmWare 15 min #tip: usevmware +30%
  30. 30. use VmWare if you can #tip: usevmware (it will cost you a few $)
  31. 31. use nfs if you can #tip: usenfs
  32. 32. vagrant plugin install vbguest #tip: vbguest keeps guest addition updated
  33. 33. On VM I/O is the bottleneck
  34. 34. loading webpages running testsuites are READ heavy
  35. 35. move I/O outside shared folders
  36. 36. 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
  37. 37. I/O: logs, cache #tip: moveiofromshared
  38. 38. 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(); } } #tip: moveiofromshared
  39. 39. 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(); } } #tip: moveiofromshared
  40. 40. class { public function { { } public function { { } } #tip: moveiofromshared +13-16%
  41. 41. I/O: increase realpath cache #tip: realpathcache
  42. 42. realpath_cache_size = 4096k realpath_cache_ttl = 7200 php.ini #tip: realpathcache
  43. 43. realpath_cache_size realpath_cache_ttl php.ini #tip: realpathcache +7%
  44. 44. I/O: move DB on RAM #tip: moveiofromshared if you use sqlite move it on /dev/shm
  45. 45. vagrant plugin install vagrantcachier #tip: vagrantcachier
  46. 46. 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 #tip: moveiofromshared Vagrantfile
  47. 47. if config.cache.synced_folder_opts type: mount_options: [ end #tip: moveiofromshared Vagrantfile +30%for reprovisioning a box with git, php apache, mysql
  48. 48. use cachefilesd for nfs #tip: cachefilesd
  49. 49. #tip: cachefilesd - 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
  50. 50. #tip: cachefilesd config.vm.synced_folder "../", "/var/www", id: “vagrant-root”, type: “nfs”, mount_options: ['rw','vers=3','tcp','fsc'] Vagrantfile
  51. 51. #tip: cachefilesd config.vm.synced_folder mount_options: [ Vagrantfile -15% +10%
  52. 52. Application Management
  53. 53. How to access mysql
  54. 54. - 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
  55. 55. - name: Install phpmyadmin apt: pkg=phpmyadmin state=present - name: Include phpmyadmin in Apache config lineinfile: dest=/etc/apache2/apache2.conf line="Include /etc/phpmyadmin apache.conf" notify: restart apache
  56. 56. - name: adminer clone git: repo=https://github.com/vrana/adminer.git dest=/var/www/adminer version=v4.2.1 accept_hostkey=true - name: adminer compile command: php compile.php chdir=/var/www/adminer creates=/var/www/adminer/adminer-4.2.1.php
  57. 57. Permissions management in shared folders
  58. 58. config.vm.synced_folder "../", "/var/www", id: "vagrant-root", owner: "vagrant", group: "vagrant", mount_options: ["dmode=777,fmode=777"]
  59. 59. Use host ssh keys
  60. 60. config.ssh.forward_agent = true
  61. 61. ForwardAgent yes check ssh config file in your host machine
  62. 62. Provisioning
  63. 63. to phansible or not to phansible? #tip: provisioningtpls
  64. 64. quick and easy way to start they’re general old platforms are not supported a lot of a good ideas you can steal from #tip: provisioningtpls
  65. 65. #tip: provisioningtpls 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
  66. 66. can you assume everyone in your team have the same version? #tip: provisioningtpls
  67. 67. #tip: provisioningtpls 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
  68. 68. the provisioning tool is a moving part: wanna update? be careful #tip: provisioningtpls
  69. 69. create Vagrant indipendent provision scripts #tip: provisioningtpls
  70. 70. #tip: provisioningtpls config.vm.provision :shell, :path => "scripts/bootstrap.sh", :args => "/var/www" config.vm.provision :shell, :path => “scripts/provision.sh", :args => "/var/www"
  71. 71. create your own template #tip: provisioningtpls https://github.com/ideatosrl/vagrant-php-template
  72. 72. you’re in control of provisioning command you can perform additional checks on host machine #tip: provisioningtpls
  73. 73. Distributing VMs
  74. 74. provisioning does not create immutable vm by default eg: package update on LTS #tip: packaging
  75. 75. live on the edge and fix provision script use stable package repositories #tip: packaging
  76. 76. https://speakerdeck.com/mitchellh/vagrant-usage-patterns Create and distribute your own VM #tip: goldenimage
  77. 77. vagrant package - -name mybox.box publish it somewhere (http, atlas) #tip: packaging c.vm.box_url = “http://../mybox.box”
  78. 78. Questions? https://joind.in/talk/view/14548

×