美文网首页server程序员
Vagrant 虚拟机管理

Vagrant 虚拟机管理

作者: 与蟒唯舞 | 来源:发表于2017-06-12 11:11 被阅读38次
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

相关文章

网友评论

    本文标题:Vagrant 虚拟机管理

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