Vagrant(官网github)是一款构建虚拟开发环境的工具,支持 Window,Linux,Mac OS,Vagrant 中的 Boxes 概念类似于 Docker(实质是不同的),你可以把它看作是一个箱子,里面装了一些东西,Vagrant 创建虚拟机的时候,需要用到 Box ,它里面包含了虚拟机配置、虚拟机硬盘镜像和 Vagrant 配置的压缩包,有了 Box,你不需要再重新下载 ISO 镜像文件、新建虚拟机、修改虚拟机等配置,而是直接运行你所需要的操作系统。

更多 Vagrant 概念,参考:Vagrant 是什么,不是什么。

Vagrant 支撑 VirtualBox、HyperV、VMWare 等虚拟机软件,我 Mac 电脑装的是 VMWare Fusion,但 Vagrant 支持是收费的(79 美元),好黑呀,不过 VirtualBox 是免费的,我又安装了个 VirtualBox(大概 300 M),以便做示例。

使用 Vagrant 的目的,就是方便在虚拟机中做 Consul 的集群(Mac OS、Ubuntu 安装及使用 Consul)。

安装 Vagrant(使用 homebrew

$ brew install vagrant

安装好 Vagrant 之后,就可以使用初始化 Box 了,你可以使用别人封装好的 Box,也可以自己封装 Box,比如下面命令:

$ vagrant box add ubuntu/trusty64

ubuntu/trusty64是一个公开 Boxes(更多 Boxes)。运行上面第一行命令后,Vagrant 会在工作目录下创建 Vagrantfile 配置文件。在线下载 Box 会比较慢,你可以先下载 Box 之后,再加载本地的 Box 进行初始化。

我使用的是 Ubuntu 64 Box:http://files.vagrantup.com/precise64.box,其他 Box 下载地址:http://www.vagrantbox.es/

下载好 Box 之后,你可以创建这样的工作目录:

$ tree
.
├── boxes
│   └── precise64.box
└── works 2 directories, 1 files

创建命令:

$ mkdir vagrant_projects
$ mkdir boxes
$ mkdir works

然后把下载好的 Box 放到 boxes 文件夹下,然后命令转到 boxes 目录下(cd boxes),然后执行添加 Box 命令:

$ vagrant box add ubuntu precise64.box

添加后之后,可以查看所添加的 Box 列表:

$ vagrant box list
ubuntu64 (virtualbox, 0)

命令转到 works 目录下(cd works)接着进行初始化虚拟机:

$ vagrant init ubuntu64

初始化完成后,会在当前目录下生成一个 VagrantFile 配置文件,里面是对虚拟机环境的一些配置(可以手动修改),然后启动虚拟机:

$ vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Clearing any previously set forwarded ports...
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
default: Adapter 1: nat
default: Adapter 2: bridged
==> default: Forwarding ports...
default: 22 (guest) => 2222 (host) (adapter 1)
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
default: SSH address: 127.0.0.1:2222
default: SSH username: vagrant
default: SSH auth method: private key
==> default: Machine booted and ready!
==> default: Checking for guest additions in VM...
default: The guest additions on this VM do not match the installed version of
default: VirtualBox! In most cases this is fine, but in rare cases it can
default: prevent things such as shared folders from working properly. If you see
default: shared folder errors, please make sure the guest additions within the
default: virtual machine match the version of VirtualBox you have installed on
default: your host and reload your VM.
default:
default: Guest Additions Version: 4.2.0
default: VirtualBox Version: 5.1
==> default: Configuring and enabling network interfaces...
==> default: Mounting shared folders...
default: /vagrant => /Users/xishuai/vagrant_project/works
==> default: Machine already provisioned. Run `vagrant provision` or use the `--provision`
==> default: flag to force provisioning. Provisioners marked to run always will still run.

虚拟机启动好之后(可以在 VirtualBox 中查看是否已启动),就可以登录虚拟机了:

$ vagrant ssh
Welcome to Ubuntu 12.04 LTS (GNU/Linux 3.2.0-23-generic x86_64) * Documentation: https://help.ubuntu.com/
New release '14.04.5 LTS' available.
Run 'do-release-upgrade' to upgrade to it. Welcome to your Vagrant-built virtual machine.
Last login: Tue Dec 27 10:16:06 2016 from 10.0.2.2
vagrant@precise64:~$

这个命令就表示已经进入了 Ubuntu 的系统,可以像在虚拟机中使用一样使用它了。

当然也可以把你配好的 Box 导出出来,给其他人使用,执行命令:

$ cd ~/VirtualBox\ VMs/works_default_1482820841651_93029
$ vagrant package --output works_default_1482820841651_93029 --base ubuntu64.box

Vagrant 命令列表:

  • vagrant box list:查看box列表
  • vagrant add box box 名字 box地址:添加box,自动帮你生成 Vagrantfile
  • vagrant init box 名字:初始化 Vagrantfile
  • vagrant up:启动虚拟机
  • vagrant ssh:连接虚拟机
  • vagrant halt:关闭虚拟机
  • vagrant reload:重新加载 Vagrantfile 文件
  • vagrant suspend:暂时挂起虚拟机
  • vagrant destroy:销毁虚拟机
  • vagrant status:查看虚拟机运行状态
  • vagrant package:导出 Box

在使用 Vagrant 的时候,遇到了这样一个问题:创建的 Ubuntu 虚拟机,需要访问外部网络,所以需要将虚拟机的网络模式设置为桥接模式(Bridged),于是就使用 VirtualBox 进行设置,但设置成功之后,每次 Vagrant 启动虚拟机的时候,都会进行网络模式重置,但如果用 VirtualBox 启动的话,就没有什么问题。

这个问题搞了好久,最后的解决方案是修改 Vagrantfile 配置文件,添加如下配置:

config.vm.network "public_network", bridge: "en0: Wi-Fi (AirPort)"
config.vm.boot_timeout = 20

boot_timeout是链接超时设置(20 秒),bridge后面表示桥接的网络模式(WiFi 网络),如果不进行设置的话,每次启动虚拟机的时候,会进行选择网络模式:

$ vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Clearing any previously set forwarded ports...
==> default: Clearing any previously set network interfaces...
==> default: Specific bridge 'en1: Wi-Fi (AirPort)' not found. You may be asked to specify
==> default: which network to bridge to.
==> default: Available bridged network interfaces:
1) en0: Wi-Fi (AirPort)
2) en1: Thunderbolt 1
3) en2: Thunderbolt 2
4) p2p0
5) awdl0
6) bridge0
7) vmnet1
8) vmnet8
==> default: When choosing an interface, it is usually the one that is
==> default: being used to connect to the internet.
default: Which interface should the network bridge to?

由于之前的 Ubuntu 版本太低(12.04),安装 .NET Core 的时候,遇到了一些问题,后来又换了一个 Ubuntu Box(版本 14.04),但配置的时候,又遇到了下面问题:

$ vagrant init ubuntu_server1
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.
xishuaideMacBook-Pro:ubuntu_server1 xishuai$ vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Importing base box 'ubuntu_server1'...
==> default: Matching MAC address for NAT networking...
==> default: Setting the name of the VM: ubuntu_server1_default_1482924693668_66404
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
default: Adapter 1: nat
==> default: Forwarding ports...
default: 22 (guest) => 2222 (host) (adapter 1)
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
default: SSH address: 127.0.0.1:2222
default: SSH username: vagrant
default: SSH auth method: private key
default:
default: Vagrant insecure key detected. Vagrant will automatically replace
default: this with a newly generated keypair for better security.
default:
default: Inserting generated public key within guest...
default: Removing insecure key from the guest if it's present...
default: Key inserted! Disconnecting and reconnecting using new SSH key...
==> default: Machine booted and ready!
==> default: Checking for guest additions in VM...
default: No guest additions were detected on the base box for this VM! Guest
default: additions are required for forwarded ports, shared folders, host only
default: networking, and more. If SSH fails on this machine, please install
default: the guest additions and repackage the box to continue.
default:
default: This is not an error message; everything may continue to work properly,
default: in which case you may ignore this message.
==> default: Mounting shared folders...
default: /vagrant => /Users/xishuai/vagrant_project/ubuntu_server1
Vagrant was unable to mount VirtualBox shared folders. This is usually
because the filesystem "vboxsf" is not available. This filesystem is
made available via the VirtualBox Guest Additions and kernel module.
Please verify that these guest additions are properly installed in the
guest. This is not a bug in Vagrant and is usually caused by a faulty
Vagrant box. For context, the command attempted was: mount -t vboxsf -o uid=1000,gid=1000 vagrant /vagrant The error output from the command was: mount: unknown filesystem type 'vboxsf'

解决方案(参考 Vagrant error : Failed to mount folders in Linux guest):

$ vagrant plugin install vagrant-vbguest

另外,附一些 Linux 常用命令:

  • command &:将进程放在后台执行
  • ctrl + z:暂停当前进程 并放入后台
  • jobs:查看当前后台任务
  • bg( %id):将任务转为后台执行
  • fg( %id):将任务调回前台
  • kill( %id):杀掉任务

参考资料:

Mac OS 使用 Vagrant 管理虚拟机(VirtualBox)的更多相关文章

  1. VMware Workstation11安装Mac OS X 10.10虚拟机

    原文:http://jingyan.baidu.com/article/3f16e003eac66e2591c103e0.html 优化:http://www.cnblogs.com/yipu/p/4 ...

  2. VMware 12安装虚拟机Mac OS X 10.10使用小技巧(虚拟机Mac OS X 10.10时间设置,虚拟机Mac OS X 10.10通过代理上网,Mac OS X 10.10虚拟机优化,VMware虚拟机相互复制)

    1:修改Mac OS 系统时间 2:Mac OS系统 通过代理上网 VMware 12安装Mac OS X 10.10虚拟机优化心得 虚拟显卡硬伤,所以必须要优化下才能用,优化的原则就是能精简的精简, ...

  3. 宿主机mac os无法连接到虚拟机centos

    宿主机: Mac OS 10.9.2 虚拟机: [root@localhost ~]# cat /etc/redhat-release CentOS release 6.4 (Final) [root ...

  4. MAC OS 10.11.1虚拟机免费下载已安装Xcode7图片后有下载地址

    MAC OS 10.11.1虚拟机免费下载已安装Xcode7图片后有下载地址 注意:已经下载过MAC OS 10.10.5虚拟机免费下载(可安装Xcode7)链接:http://www.cnblogs ...

  5. MAC OS 10.10.5虚拟机免费下载(可安装Xcode7)

    MAC OS 10.10.5虚拟机免费下载(可安装Xcode7)   MAC OS 10.10.5虚拟机免费(可安装Xcode7)下载地址:链接: http://pan.baidu.com/s/1dD ...

  6. Windows 8.1下安装Mac OS X 10.8虚拟机

    转载自http://blog.csdn.net/jordanxinwang/article/details/43637799 1.准备 宿主操作系统:Windows 8.1 64位.特别地,需要CPU ...

  7. Mac OS X系统下利用VirtualBox安装和配置Windows XP虚拟机

    准备工作 下载并安装VirtualBox for Mac到https://www.virtualbox.org/wiki/Downloads下载VirtualBox <版本> for OS ...

  8. Vagrant 管理部署 VirtualBox (推荐使用)

    学习一段时间的大数据和容器技术,使用虚拟机搭建实验环境还是挺耗时耗力的. 一旦虚拟机坏掉了,还要重新开始. 最近发现了Vagrant, 简直好用上天,方便快捷,易用. 下面介绍如何在Windows中安 ...

  9. Mac OS X安装之虚拟机环境下的总结

    最近一直忙着公司iOS Touch的新版发布,终于忙过了.现在,又开始了新的阶段,不过算是轻松了很多.回来一看,自己的博客空空如也,实在受不了了.于是,开始更一下吧,哈哈. 这个文档是我几个月前,开始 ...

随机推荐

  1. Android请求网络共通类——Hi_博客 Android App 开发笔记

    今天 ,来分享一下 ,一个博客App的开发过程,以前也没开发过这种类型App 的经验,求大神们轻点喷. 首先我们要创建一个Andriod 项目 因为要从网络请求数据所以我们先来一个请求网络的共通类. ...

  2. AndroidTips-052:.aar文件依赖

    aar aar 文件是android 类库项目的输出文件,其中可以包含普通的.class,清单,以及android项目特有的资源文件. 使用方式 将.aar文件放在在自己项目的libs目录下 在gra ...

  3. 菜鸟学Struts2——Interceptors

    昨天学习Struts2的Convention plugin,今天利用Convention plugin进行Interceptor学习,虽然是使用Convention plugin进行零配置开发,这只是 ...

  4. Android raw to bmp

    Android raw 格式转 bmp 图像 raw 保存的为裸数据,转换时都需要把它转成RGBA 的方式来显示.其中: 8位RAW: 四位RGBA 来表示一位灰度; 24位RAW: 三位RGB相同, ...

  5. UWP开发之Mvvmlight实践九:基于MVVM的项目架构分享

    在前几章介绍了不少MVVM以及Mvvmlight实例,那实际企业开发中将以那种架构开发比较好?怎样分层开发才能节省成本? 本文特别分享实际企业项目开发中使用过的项目架构,欢迎参照使用!有不好的地方欢迎 ...

  6. Kooboo CMS技术文档之四:Kooboo CMS的站点组成部分

    Kooboo CMS本着功能独立分离的原则,将站点分为三部分组成:用户管理,站点管理和内容数据库管理.各个功能之间既可独立使用,也可以容易组成在一起形成一个完整的系统. 用户管理 管理整个系统内的用户 ...

  7. H3 BPM初次安装常见错误详解1-4

    错误1: 首次安装完成无法访问,效果如下. 错误原因:没有配置IIS. 解决方法: 控制面板-程序-打开或关闭Windows功能,选择internet信息服务. 因为安装的时候没有没有iis,所以程序 ...

  8. iOS之UILabel的自动换行

    思路: 获取UILabel的frame大小 获取UILabel的字体大小 获取UILabel的文本内容 根据上面的3部分数据,计算文本显示区域大小 根据4计算的大小,实时改变UILabel的frame ...

  9. 开源 iOS 项目分类索引大全 - 待整理

    开源 iOS 项目分类索引大全 GitHub 上大概600个开源 iOS 项目的分类和介绍,对于你挑选和使用开源项目应该有帮助 系统基础库 Category/Util sstoolkit 一套Cate ...

  10. 项目自动化建构工具gradle 入门1——输出helloWorld

    先来一个简单的例子,4个步骤: 1.进入D:\work\gradle\java 目录  ,您电脑没这目录? 那辛苦自己一级一级建立起来吧 新建文件build.gradle,文件内容是: apply p ...