vagrant 简直就是神器,当然可以相比较的就是比较火热的docker,个人还是感觉vagrant比较爽,但是要比docker重很多。
0.三个概念
provider,一个媒介,熟悉的有vmware,virtualbox之类的
vagrant,管理虚拟机的工具,针对provider的封装使用
box,一个个的虚拟机镜像
1.软件的依赖
安装很简单,假设基于virtualbox进行,那么需要安装virtualbox,vagrant,apt/yum都能找到源,很方便
2.使用,在第一步已经安装完毕的基础上进行第二步。
0.mkdir -p /data/vagrant && cd /data/vagrant
1.安装一个box,vagrant box add centos6.5 https://github.com/2creatives/vagrant-centos/releases/download/v6.5.3/centos65-x86_64-20140116.box
2.初始化,vagrant init centos6.5
3.启动,vagrant up
4.登陆,vagrant ssh
5.以上,一个centos6.5的vm就启动起来了。
4.如果我想起一个集群或者多个机器怎么办呢?
修改Vagrantfile。
➜ centos cat Vagrantfile
# -*- mode: ruby -*-
# vi: set ft=ruby :
Vagrant.configure(2) do |config|
config.vm.define :web do |web|
web.vm.provider "virtualbox" do |v|
v.customize ["modifyvm", :id, "--name", "web", "--memory", "512"]
end
web.vm.box = "centos6.5"
web.vm.hostname = "web"
web.vm.network :private_network, ip: "11.11.1.1"
end
config.vm.define :db do |db|
db.vm.provider "virtualbox" do |v|
v.customize ["modifyvm", :id, "--name", "db", "--memory", "512"]
end
db.vm.box = "centos6.5"
db.vm.hostname = "db"
db.vm.network :private_network, ip: "11.11.1.2"
end
end
➜ centos
以上抄袭自:https://github.com/astaxie/Go-in-Action/blob/master/ebook/zh/01.3.md
5.一些cmd 的cheatsheet
Common Vagrant Commands
vagrant up -- starts vagrant environment (also provisions only on the FIRST vagrant up)
vagrant status -- outputs status of the vagrant machine
vagrant halt -- stops the vagrant machine
vagrant reload -- restarts vagrant machine, loads new Vagrantfile configuration
vagrant provision -- forces reprovisioning of the vagrant machine
vagrant ssh -- connects to machine via SSH
vagrant destroy -- stops and deletes all traces of the vagrant machine
Tips
vagrant -v -- Get the vagrant version
vagrant global-status -- outputs status of all vagrant machines
vagrant suspend -- Suspends a virtual machine (remembers state)
vagrant resume -- Resume a suspended machine (vagrant up works just fine for this as well)
vagrant reload --provision -- Restart the virtual machine and force provisioning
vagrant provision --debug -- Use the debug flag to increase the verbosity of the output
vagrant push -- Yes, vagrant can be configured to deploy code!
vagrant up --provision | tee provision.log -- Runs vagrant up, forces provisioning and logs all output to a file
Notes
If you are using VVV, you can enable xdebug by running vagrant ssh and then xdebug_on from the virtual machine's CLI.
6.参考链接
部分上面的东西是抄袭的!
一些查阅过的原文:
http://notes.jerzygangi.com/vagrant-cheat-sheet/
https://gist.github.com/wpscholar/a49594e2e2b918f4d0c4
http://topmanopensource.iteye.com/blog/2002302
https://github.com/astaxie/Go-in-Action/blob/master/ebook/zh/01.3.md