Vagrant虛擬機之生命週期(up、down、suspend、halt、destroy)

通常會把專案或資料放在一個目錄下,例如:projects

mkir projects
cd projects
mkdir vagrant-quickstart
cd vagrant-quickstart
git init .
// 記得將 .vagrant 加入到 .gitignore

初始化Vagrant

  • 建立 Vagrantfile
vagrant init hashicorp/precise64
  • Control CPU and Memory
 config.vm.provider "virtualbox" do |vb| 
   vb.memory = "1024"
   vb.cpus = "2"
 end
  • 將Vagrant加入到git中
git add .
git commit -m "Adding initial Vagrantfile"

執行vagrant專案(啟動虛擬機器)

  • Vagrant up
vagrant up
vagrant status

Access the VM

vagrant ssh
cd /vagrant/      // 會進入Guest /vagrant目錄及本機目錄
exit   //離開VM

關閉虛擬機

vagrant suspend   // 暫停VM
vagrant halt  // 關閉VM
vagrant destory  // 刪除VM

重啟虛擬機 Reloading in Vagrant

vagrant up
vagrant reload

#

Pattern for multiple machines

Vagrant.configure("2") do |config| 
 config.vm.define "chefserver" do |chefserver| 
  chefserver.vm.box = "centos/7" 
  chefserver.vm.hostname = "chefserver.vagrant.test" 
  chefserver.vm.provider :virtualbox do |v| 
   v.customize ["modifyvm", :id, "--memory", "1024"] 
  end 
 end
 config.vm.define "chefworkstation1" do |chefworkstation1| 
  chefworkstation1.vm.box = "ubuntu/xenial" 
  chefworkstation1.vm.hostname = "chefworkstation1.vagrant.test" 
  chefworkstation1.vm.provider :virtualbox do |v| 
   v.customize ["modifyvm", :id, "--memory", "512"] 
  end 
 end
end