Vagrant 虚拟机管理

Vagrant 是什么

Vagrant is a tool for building and distributing development environments.

Vagrant 是构建在虚拟化技术之上的虚拟机运行环境管理工具。通过 Vagrant 可以方便的实现对虚拟机的管理,包括建立和删除虚拟机、配置虚拟机运行参数、管理虚拟机运行状态、自动化配置和安装开发环境、打包和分发虚拟机运行环境等。

Vagrant 的运行,需要依赖某项具体的虚拟化技术。由于 VirtualBox 是一项开源的虚拟化软件,因此,在 Vagrant 开发的初期,唯一支持的是 VirtualBox。随着虚拟化技术的快速发展,现在已经有了更多的虚拟化技术可供选择。像 VMware 已经可以通过 Vagrant 的管理而工作。

因此,Vagrant 是虚拟机管理工具,不是某项具体的虚拟化技术。对于各项虚拟化技术而言,Vagrant 提供了一套基于配置文件和命令行的管理工具。也正因为如此,Vagrant 完成了对虚拟化技术在一定程度上的封装。这为将虚拟化技术引入到基于桌面运行环境的开发工作流中创造了便利条件。

Vagrant 常用命令
  • vagrant box list:查看目前已有的 box
  • vagrant box add:新增加一个 box
  • vagrant box remove:删除指定 box
  • vagrant init:初始化配置 vagrantfile
  • vagrant up:启动虚拟机
  • vagrant halt:关闭虚拟机
  • vagrant reload: 重启虚拟机
  • vagrant suspend:挂起虚拟机
  • vagrant resume:恢复虚拟机
  • vagrant status:查看虚拟机运行状态
  • vagrant destroy:销毁当前虚拟机
  • vagrant ssh:ssh 登录虚拟机
Vagrantfile 配置文件
# -*- mode: ruby -*-
# vi: set ft=ruby :

# All Vagrant configuration is done below. The "2" in Vagrant.configure
# configures the configuration version (we support older styles for
# backwards compatibility). Please don't change it unless you know what
# you're doing.
Vagrant.configure("2") do |config|
  # The most common configuration options are documented and commented below.
  # For a complete reference, please see the online documentation at
  # https://docs.vagrantup.com.

  # Every Vagrant development environment requires a box. You can search for
  # boxes at https://vagrantcloud.com/search.
  config.vm.box = "ubuntu/trusty64"

  # 虚拟机主机名
  config.vm.hostname = "ubuntu"

  # Disable automatic box update checking. If you disable this, then
  # boxes will only be checked for updates when the user runs
  # `vagrant box outdated`. This is not recommended.
  # config.vm.box_check_update = false

  # Create a forwarded port mapping which allows access to a specific port
  # within the machine from a port on the host machine. In the example below,
  # accessing "localhost:8080" will access port 80 on the guest machine.
  # NOTE: This will enable public access to the opened port
  # config.vm.network "forwarded_port", guest: 80, host: 8080

  # Create a forwarded port mapping which allows access to a specific port
  # within the machine from a port on the host machine and only allow access
  # via 127.0.0.1 to disable public access
  # 端口转发设置
  # config.vm.network "forwarded_port", guest: 80, host: 8080, host_ip: "127.0.0.1"

  # Create a private network, which allows host-only access to the machine
  # using a specific IP.
  # 私有网络设置
  # config.vm.network "private_network", ip: "192.168.33.10"

  # Create a public network, which generally matched to bridged network.
  # Bridged networks make the machine appear as another physical device on
  # your network.
  # config.vm.network "public_network"

  # Share an additional folder to the guest VM. The first argument is
  # the path on the host to the actual folder. The second argument is
  # the path on the guest to mount the folder. And the optional third
  # argument is a set of non-required options.
  # 共享目录设置
  # config.vm.synced_folder "../data", "/vagrant_data"

  # Provider-specific configuration so you can fine-tune various
  # backing providers for Vagrant. These expose provider-specific options.
  # Example for VirtualBox:
  #
  # config.vm.provider "virtualbox" do |vb|
  #   # Display the VirtualBox GUI when booting the machine
  #   vb.gui = true
  #
  #   # Customize the amount of memory on the VM:
    # 虚拟机内存和CPU
    vb.memory = "1024"
    vb.cpus = 2
    # 虚拟机名称
    vb.name = "ubuntu14_64"
  # end
  #
  # View the documentation for the provider you are using for more
  # information on available options.

  # Enable provisioning with a shell script. Additional provisioners such as
  # Puppet, Chef, Ansible, Salt, and Docker are also available. Please see the
  # documentation for more information about their specific syntax and use.
  # 分发升级 box 
  # 安装新软件时打开下面的命令,之后执行 `vagrant provision` 或者 `vagrant --provision` 命令
  # config.vm.provision "shell", inline: <<-SHELL
  #   apt-get update
  #   apt-get install -y apache2
  # SHELL
end
打包命令
vagrant package --output xxx.box
多主机
config.vm.define "web" do |web|
    web.vm.box = "apache"
    web.vm.hostname = "host-web"
    web.vm.network "private_network", ip: "192.168.33.20"
    web.vm.synced_folder "web", "/vagrant"
end

config.vm.define "db" do |db|
    db.vm.box = "mysql"
    db.vm.hostname = "host-db"
    db.vm.network "private_network", ip: "192.168.33.30"
    db.vm.synced_folder "db", "/vagrant"
end
遇到的问题

共享目录设置失败:
[default] No Virtualbox Guest Additions installation found.
报错信息:

The following SSH command responded with a non-zero exit status.
Vagrant assumes that this means the command failed!

umount /mnt

Stdout from the command:

Stderr from the command:

umount: /mnt: not mounted

解决方案:

vagrant plugin uninstall vagrant-vbguest
vagrant plugin install vagrant-vbguest --plugin-version 0.21
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 1. 安装Vagrant与VirtualBox Vagrant Vagrant是一个基于Ruby的工具,用于创建和...
    翌子涵阅读 11,388评论 0 0
  • 开发需要在各种系统上进行开发任务,运维则需要在各种系统上学习工具使用。因此,虚拟机恐怕也是 IT 人员最常使用的工...
    李广慧阅读 10,879评论 3 24
  • 虚拟开发环境 在项目开发中,我们经常会遇到各种各样的问题,需要使用虚拟开发环境来完成,虚拟和正式环境一样的虚拟开发...
    sunnyaxin阅读 11,598评论 4 6
  • Vagrant是什么 Vagrant是一个软件,可以自动化虚拟机的安装和配置流程。目前市面上个人PC的主流操作系统...
    北魏企鹅阅读 30,187评论 3 40
  • 这两个月忙到没有时间进电影院,恰巧上周出差,在来回的飞机上补上了传遍大街小巷的青春剧《我的少女时代》和煽情剧《滚蛋...
    夏景暄sunny阅读 1,313评论 0 1