分类目录归档:vagrant

vagrant创建多个虚拟机

建立多台VM,並且让他们之间能够相通信,假设一台是应用服务器、一台是DB服务器,那么这个结构在Vagrant中非常简单,其实和单台的配置差不多,你只需要通过config.vm.define来定义不同的角色就可以了,这里的设置和前面我们单机设置配置类似,只是我们使用了:web以及:db分別做了两个VM的设置,并且给每个VM设置了不同的hostname和IP,设置好之后再使用vagrant up将虚拟机跑起来

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 = "base"
    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 = "base"
    db.vm.hostname = "db"
    db.vm.network :private_network, ip: "11.11.1.2"
  end
end

vagrant使用

前面讲了Vagrant的几个命令:

  • vagrant box add 添加box的操作
  • vagrant init 初始化box的操作
  • vagrant up 启动虚拟机的操作
  • vagrant ssh 登录虚拟机的操作

Vagrant还包括如下一些操作:

  • vagrant box list显示当前已经添加的box列表
    $ vagrant box list
    base (virtualbox)
  • vagrant box remove删除相应的box
    $ vagrant box remove base virtualbox
    Removing box 'base' with provider 'virtualbox'...
  • vagrant destroy停止当前正在运行的虚拟机并销毁所有创建的资源
    $ vagrant destroy
    Are you sure you want to destroy the 'default' VM? [y/N] y
    [default] Destroying VM and associated drives...
  • vagrant halt关机
    $ vagrant halt
    [default] Attempting graceful shutdown of VM...
  • vagrant package打包命令,可以把当前的运行的虚拟机环境进行打包
    $ vagrant package
    [default] Attempting graceful shutdown of VM...
    [default] Clearing any previously set forwarded ports...
    [default] Creating temporary directory for export...
    [default] Exporting VM...
    [default] Compressing package to: /Users/astaxie/vagrant/package.box
  • vagrant plugin用于安装卸载插件
  • vagrant provision通常情况下Box只做最基本的设置,而不是设置好所有的环境,因此Vagrant通常使用Chef或者Puppet来做进一步的环境搭建。那么Chef或者Puppet称为provisioning,而该命令就是指定开启相应的provisioning。按照Vagrant作者的说法,所谓的provisioning就是"The problem of installing software on a booted system"的意思。除了Chef和Puppet这些主流的配置管理工具之外,我们还可以使用Shell来编写安装脚本。

    例如: vagrant provision --provision-with chef

  • vagrant reload重新启动虚拟机,主要用于重新载入配置文件
    $ vagrant reload
    [default] Attempting graceful shutdown of VM...
    [default] Setting the name of the VM...
    [default] Clearing any previously set forwarded ports...
    [default] Creating shared folders metadata...
    [default] Clearing any previously set network interfaces...
    [default] Preparing network interfaces based on configuration...
    [default] Forwarding ports...
    [default] -- 22 => 2222 (adapter 1)
    [default] Booting VM...
    [default] Waiting for VM to boot. This can take a few minutes.
    [default] VM booted and ready for use!
    [default] Setting hostname...
    [default] Mounting shared folders...
    [default] -- /vagrant
  • vagrant resume恢复前面被挂起的状态
    $vagrant resume
    [default] Resuming suspended VM...
    [default] Booting VM...
    [default] Waiting for VM to boot. This can take a few minutes.
    [default] VM booted and ready for use!
  • vagrant ssh-config输出用于ssh连接的一些信息
    $vagrant ssh-config
    Host default
      HostName 127.0.0.1
      User vagrant
      Port 2222
      UserKnownHostsFile /dev/null
      StrictHostKeyChecking no
      PasswordAuthentication no
      IdentityFile "/Users/astaxie/.vagrant.d/insecure_private_key"
      IdentitiesOnly yes
      LogLevel FATAL
  • vagrant status获取当前虚拟机的状态
    $vagrant status
    Current machine states:
    
    default                   running (virtualbox)
    
    The VM is running. To stop this VM, you can run `vagrant halt` to
    shut it down forcefully, or you can run `vagrant suspend` to simply
    suspend the virtual machine. In either case, to restart it again,
    simply run `vagrant up`.
  • vagrant suspend挂起当前的虚拟机
    $ vagrant suspend
    [default] Saving VM state and suspending execution...