美文网首页
virtual box + Vagrant

virtual box + Vagrant

作者: 运维开发_西瓜甜 | 来源:发表于2018-12-15 18:56 被阅读2次

VirtualBox

下载 https://www.virtualbox.org/wiki/Downloads

Vagrant

1. 下载安装

下载地址 https://www.vagrantup.com/downloads.html

2. 创建虚拟机

2.1 在本地创建一个 centos7 的目录

$ mkdir centos7

2.2 进入到这个目录,初始化

$ vagrant init centos/7
A `Vagrantfile` has been placed in this directory. You are now
ready to `vagrant up` your first virtual environment! Please read
the comments in the Vagrantfile as well as documentation on
`vagrantup.com` for more information on using Vagrant.
$ ls
Vagrantfile

会产生一个文件 Vagrantfile,通过这个文件来定义要创建虚拟机的具体配置项。

2.3 启动这个虚拟机

$ vagrant up

2.4 通过 SSH 连接到这台虚拟主机内

必须在初始化的目录下执行此命令

# shark @ SharkAir in ~/centos7 [16:15:09]
$ vagrant ssh

2.5 查看目前所有虚拟机的状态

# shark @ SharkAir in ~/centos7 [16:15:09]
$ 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`.

2.6 停止一个虚拟机的运行

# shark @ SharkAir in ~/centos7 [16:19:31] C:1
$ vagrant halt
==> default: Attempting graceful shutdown of VM...

# shark @ SharkAir in ~/centos7 [16:20:03]
$ vagrant status
Current machine states:

default                   poweroff (virtualbox)

The VM is powered off. To restart the VM, simply run `vagrant up`

2.7 继续运行刚才停止的虚拟机

重启虚拟机,并加载新的配置文件

$ vagrant reload

2.8 删除虚拟机

# shark @ SharkAir in ~/centos7 [16:23:45]
$ vagrant destroy
    default: Are you sure you want to destroy the 'default' VM? [y/N] y
==> default: Destroying VM and associated drives...

3. 可以从不同的 Vagrantfile 创建不同的虚拟机

https://app.vagrantup.com/boxes/search

4. Vagrantfile 配置详解

参考 官网

启动一次创建多个虚拟机并且挂载本地文件到虚拟机中

Vagrantfile

# -*- mode: ruby -*-
# vi: set ft=ruby :

Vagrant.require_version ">= 1.6.0"

boxes = [
    {
        :name => "docker-node1",
        :eth1 => "192.168.60.10",
        :mem => "1024",
        :cpu => "1"
    },
    {
        :name => "docker-node2",
        :eth1 => "192.168.60.20",
        :mem => "1024",
        :cpu => "1"
    }
]

Vagrant.configure(2) do |config|

  config.vm.box = "centos/7"

  boxes.each do |opts|
      config.vm.define opts[:name] do |config|
        config.vm.hostname = opts[:name]
        config.vm.provider "vmware_fusion" do |v|
          v.vmx["memsize"] = opts[:mem]
          v.vmx["numvcpus"] = opts[:cpu]
        end

        config.vm.provider "virtualbox" do |v|
          v.customize ["modifyvm", :id, "--memory", opts[:mem]]
          v.customize ["modifyvm", :id, "--cpus", opts[:cpu]]
        end

        config.vm.network :private_network, ip: opts[:eth1]
      end
  end

  config.vm.synced_folder "./labs", "/home/vagrant/labs", type: "nfs"
  config.vm.provision "shell", privileged: true, path: "./setup.sh"

end

VirtualBox Guest Additions未预装; 如果您需要共享文件夹,请安装vagrant-vbguest插件并将以下行添加到您的Vagrantfile:

config.vm.synced_folder ".", "/vagrant", type: "virtualbox"

推荐使用 nfs点我了解详情

Vagrant.configure("2") do |config|
   config.vm.synced_folder ".", "/vagrant", type: "nfs"
end

相关文章

网友评论

      本文标题:virtual box + Vagrant

      本文链接:https://www.haomeiwen.com/subject/mjhphqtx.html