Sunteți pe pagina 1din 99

Vagrant

for
Real
Michele Orselli
CTO@Ideato
_orso_
micheleorselli / ideatosrl
mo@ideato.it
http://mitchellh.com/the-tao-of-vagrant
1) clone repo
2) vagrant up
3) theres no #3
1) clone repo
2) vagrant up
#tips
vagrantfile configs vm performance

app config tips provision/packaging


Portable configuration
tips
Dont use local names
for your boxes

config.vm.box = "base"
c.vm.box = "hashicorp/precise64"
c.vm.box_url = "http://your_box"
Avoid absolute paths
c.vm.synced_folder /myProj","/var/www/myProj"
config.vm.synced_folder "./", "/var/www/myProj"
Move host specific
configuration outside
Vagrantfile
vagrantfile-local-config.yml

ram: 2048
cpus: 2
ipaddress: 10.10.10.10
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
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
Force a specific
provider
ENV['VAGRANT_DEFAULT_PROVIDER'] = 'virtualbox'
Support multiple
providers
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
Projects directories
organization
single git repository

myproject
--
-- vagrant
-- Vagrantfile

c.vm.synced_folder .,/var/www/myproject"
single git repository

myproject
-- project
--
-- vagrant
-- Vagrantfile

c.vm.synced_folder ./project,/var/www/
project
single/multiple git repositories
myproject
-- vagrant
-- www
-- project1
-- project2
-- Vagrantfile

c.vm.synced_folder ./www,/var/www
vagrant plugin install
HostsUpdater

(or HostManager)
# /etc/hosts

10.10.10.40 host1.lo #VAGRANT: e2bca7c8bf6d76cbcf6ee48998c16f


if Vagrant.has_plugin?("HostsUpdater")
config.hostsupdater.aliases = ["host1.lo"]
else
puts "--- WARNING ---"
puts You should install HostsUpdater
end
Multi-VM
Configuration
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
Dealing with shared
folders
no share
native share
- slow
- os indipendent-ish
nfs
- fast
- win support :-(
rsync
- no real share
- update
VM Performance
run tests

still running
Virtualbox vs VmWare
mitchellh.com/comparing-filesystem-performance-in-virtual-
machines
Virtualbox 22 min

VmWare 15 min
Virtualbox 22 min
+30%
VmWare 15 min
use VmWare if you can
(it will cost you a few $)
use nfs if you can*
vagrant plugin install
vbguest
keeps guest addition updated
unless Vagrant.has_plugin?(vagrant-vbguest")
raise please install vagrant-vbguest
end
On VM I/O is the
bottleneck
loading webpages
running testsuites
are READ heavy
move I/O outside
shared folders
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


I/O: logs, cache
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();
}
}
class AppKernel extends Kernel
{
public function getCacheDir()
{
if (in_array($this->environment, array('dev', 'test')))
{
return '/dev/shm/appname/cache/' . $this->environment;
}

+13-16%
return parent::getCacheDir();
}

public function getLogDir()


{
if (in_array($this->environment, array('dev', test')))
{
return '/dev/shm/appname/logs';
}

return parent::getLogDir();
}
}
I/O: move DB on RAM
if you use sqlite move it on /dev/shm
vagrant plugin install
vagrantcachier
Vagrantfile

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

if Vagrant.has_plugin?("vagrant-cachier")
config.cache.scope = :box

}
end
+30%
config.cache.synced_folder_opts = {
type: :nfs,
mount_options: ['rw','vers=3','tcp','nolock']

for reprovisioning a box with git, php apache, mysql


use cachefilesd for nfs
- 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
Vagrantfile

config.vm.synced_folder "../", "/var/www",


id: vagrant-root,
type: nfs,
mount_options: ['rw','vers=3','tcp','fsc']
Vagrantfile

-15%
config.vm.synced_folder "../", "/var/www",
id: vagrant-root,
type: nfs,
mount_options: ['rw','vers=3','tcp','fsc']

+10%
should you trust these
numbers?
Application
Management
How to access mysql
- 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
Permissions management
in shared folders
config.vm.synced_folder "../", "/var/www", id:
"vagrant-root", owner: "vagrant", group: "vagrant",
mount_options: ["dmode=777,fmode=777"]
Use host ssh keys
config.ssh.forward_agent = true
check ssh config file in your host machine

ForwardAgent yes
grunt/gulp watch

http://www.sebastien-han.fr/blog/2012/12/18/noac-performance-impact-on-web-
applications/
config.vm.synced_folder "../", "/var/www",
id: vagrant-root,
type: nfs,
mount_options: [rw,'vers=3','tcp','fsc',
'actimeo=1']
Provisioning
to phansible or not to
phansible?
quick and easy way to start

theyre general

old platforms are not supported

a lot of a good ideas you can steal from


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
can you assume everyone in your team
have the same version?
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
the provisioning tool is a moving part:
wanna update? be careful
create Vagrant
indipendent provision
scripts
config.vm.provision :shell,
:path => "scripts/bootstrap.sh",
:args => "/var/www"

config.vm.provision :shell,
:path => scripts/provision.sh",
:args => "/var/www"
create your own template

https://github.com/ideatosrl/vagrant-php-template
youre in control of provisioning command

you can perform additional checks on


host machine
Distributing VMs
provisioning does not create immutable
vm by default

eg: package update on LTS


live on the edge and fix provision script

use stable package repositories


Create and distribute your own VM

https://speakerdeck.com/mitchellh/vagrant-usage-patterns
Golden image
vagrant package - -name mybox.box

publish it somewhere (http, atlas)

c.vm.box_url = http://../mybox.box
- Dont make assumptions about the host
- Provision first, then bake your own image
- The more moving part the harder will get
Keep it simple
Thank you!
_orso_
micheleorselli / ideatosrl
mo@ideato.it

S-ar putea să vă placă și