Vagrant快速部署多个虚拟机


Vagrant官网:https://www.vagrantup.com/

一、安装

  1. 进入官网,点击Download 进入下载页,选择Windows 平台进行下载
  2. 点击运行安装包进行安装即可

二、添加中科大Centos7镜像box

  1. 进入项目文件夹的命令行环境,输入
    vagrant init centos7 https://mirrors.ustc.edu.cn/centos-cloud/centos/7/vagrant/x86_64/images/CentOS-7.box
  2. 此时在项目文件夹内会创建一个Vagrantfile 配置文件,提供一个模板
    # -*- mode: ruby -*-
    # vi: set ft=ruby :
    
    Vagrant.configure("2") do |config|
      config.vm.box = "centos7"
      config.vm.box_url = "https://mirrors.ustc.edu.cn/centos-cloud/centos/7/vagrant/x86_64/images/CentOS-7.box"
      config.vm.provider "virtualbox" do |vb|
        vb.memory = "2048"
      end
      (2..4).each do |i|
        config.vm.define "hadoop10#{i}" do |node|
          node.vm.network "private_network", ip: "192.168.56.10#{i}"
          node.vm.hostname = "hadoop10#{i}"
        end
      end
    end
  3. 在项目文件夹内运行vagrant up命令就会根据此配置文件创建虚拟机。

三、自定义Box以及Vagrantfile 配置文件

  1. 首先需要在VirtualBox 中创建一个虚拟机,可以直接使用默认的Vagrantfile 创建
  2. 进入创建的虚拟机中,自定义设置以及安装的软件,然后准备打包
  3. 进入到VirtualBox 安装目录,运行以下命令获取虚拟机的名字
    ./VBoxManage.exe list vms
  4. Vagrant 打包命令为
    vagrant package --help
    Usage: vagrant package [options] [name|id]
    Options:
            --base NAME                  Name of a VM in VirtualBox to package as a base box (VirtualBox Only)
            --output NAME                Name of the file to output
            --include FILE,FILE..        Comma separated additional files to package with the box
            --info FILE                  Path to a custom info.json file containing additional box information
            --vagrantfile FILE           Vagrantfile to package with the box
            --[no-]color                 Enable or disable color output
            --machine-readable           Enable machine readable output
        -v, --version                    Display Vagrant version
            --debug                      Enable debug output
            --timestamp                  Enable timestamps on log output
            --debug-timestamp            Enable debug output with timestamps
            --no-tty                     Enable non-interactive output
        -h, --help                       Print this help

文章作者: Hiper
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 Hiper !
  目录