Vagrant批量生成虚拟机 windows10

当我们需要很多机器来测试初始化集群和基础服务时,如果每次都去手工安装虚拟机,显然效率是低的,这时候就可以使用vagrant来批量生产虚拟机。

一、下载

需要注意的是:最好先下载vagrant,然后在安装时,会提示你要下载的对应的virtualbox版本。
* vagrant https://www.vagrantup.com/
* virtualbox https://www.virtualbox.org/

二、下载box

下载一个box开始测试,新建一个目录E:/vagrant/,进入命令行切换到此目录,执行

vagrant init puppetlabs/centos-7.2-64-nocm

这时会下载一个centos7.2的box,并在目录下生成一个名为Vagrantfile的文件。
所谓box是一个打包好的虚拟机。

三、配置集群

参考如下:

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

servers = {
    :hadoop1 => '192.168.2.11',
    :hadoop2 => '192.168.2.12',
    :hadoop3 => '192.168.2.13',
    :hadoop4 => '192.168.2.14'
}

Vagrant.configure("2") do |config|
  config.vm.box = "puppetlabs/centos-7.2-64-nocm"
  config.vm.box_check_update = false

  servers.each do |server_name, server_ip|
      config.vm.define server_name do |server_config|
          server_config.vm.hostname = "#{server_name.to_s}"
          server_config.vm.network :private_network, ip: server_ip
          server_config.vm.provider "virtualbox" do |vb|
              vb.name = server_name.to_s
                        vb.memory = "2048"
                        vb.cpus = 1
         end
      end
  end
end

可以看到配置了4台机器,每个1核2G内存。

四、生成虚拟机并启动

1、首先可以迅雷手动去下载centos7.2版本的virtualbox(也可以下面第3步去自动下载)

https://atlas.hashicorp.com/puppetlabs/boxes/centos-7.2-64-nocm/versions/1.0.1/providers/virtualbox.box

2、用varant box 添加box,名字要和上一步init的名字保持一致(如果是自动去下载box就不需要执行这步)

vagrant box add --name puppetlabs/centos-7.2-64-nocm E:\vagrant\virtualbox.box

3、输入

vagrant up

可以看到依次生成了虚拟机并启动(==如果中途下载速度特别慢的话,可以中断然后执行完上两步之后再执行up命令==),控制台输出类似于:

E:\vagrant>vagrant up
Bringing machine 'hadoop1' up with 'virtualbox' provider...
Bringing machine 'hadoop2' up with 'virtualbox' provider...
Bringing machine 'hadoop3' up with 'virtualbox' provider...
Bringing machine 'hadoop4' up with 'virtualbox' provider...
==> hadoop1: Importing base box 'puppetlabs/centos-7.2-64-nocm'...
==> hadoop1: Matching MAC address for NAT networking...
==> hadoop1: Setting the name of the VM: caas1
==> hadoop1: Clearing any previously set network interfaces...
There was an error while executing `VBoxManage`, a CLI used by Vagrant
for controlling VirtualBox. The command and stderr is shown below.

Command: ["hostonlyif", "create"]

Stderr: 0%...
Progress state: E_FAIL
VBoxManage.exe: error: Failed to create the host-only adapter
VBoxManage.exe: error: Operation canceled by the user
VBoxManage.exe: error: Details: code E_FAIL (0x80004005), component VirtualBoxWrap, interface IVirtualBox
VBoxManage.exe: error: Context: "enum RTEXITCODE __cdecl handleCreate(struct HandlerArg *)" at line 71 of file VBoxManageHostonly.cpp

E:\vagrant>vagrant up
Bringing machine 'hadoop1' up with 'virtualbox' provider...
Bringing machine 'hadoop2' up with 'virtualbox' provider...
Bringing machine 'hadoop3' up with 'virtualbox' provider...
Bringing machine 'hadoop4' up with 'virtualbox' provider...
==> hadoop1: Clearing any previously set network interfaces...
==> hadoop1: Preparing network interfaces based on configuration...
    hadoop1: Adapter 1: nat
    hadoop1: Adapter 2: hostonly
==> hadoop1: Forwarding ports...
    hadoop1: 22 (guest) => 2222 (host) (adapter 1)
==> hadoop1: Running 'pre-boot' VM customizations...
==> hadoop1: Booting VM...
==> hadoop1: Waiting for machine to boot. This may take a few minutes...
    hadoop1: SSH address: 127.0.0.1:2222
    hadoop1: SSH username: vagrant
    hadoop1: SSH auth method: private key
    hadoop1: Warning: Remote connection disconnect. Retrying...
    hadoop1: Warning: Remote connection disconnect. Retrying...
    hadoop1: Warning: Remote connection disconnect. Retrying...
    hadoop1:
    hadoop1: Vagrant insecure key detected. Vagrant will automatically replace
    hadoop1: this with a newly generated keypair for better security.
    hadoop1:
    hadoop1: Inserting generated public key within guest...
    hadoop1: Removing insecure key from the guest if it's present...
    hadoop1: Key inserted! Disconnecting and reconnecting using new SSH key...
==> hadoop1: Machine booted and ready!
==> hadoop1: Checking for guest additions in VM...
==> hadoop1: Setting hostname...
==> hadoop1: Configuring and enabling network interfaces...
==> hadoop1: Mounting shared folders...
    hadoop1: /vagrant =>  E:/vagrant
==> hadoop2: Importing base box 'puppetlabs/centos-7.2-64-nocm'...
==> hadoop2: Matching MAC address for NAT networking...
==> hadoop2: Setting the name of the VM: caas2
==> hadoop2: Fixed port collision for 22 => 2222. Now on port 2200.
==> hadoop2: Clearing any previously set network interfaces...
==> hadoop2: Preparing network interfaces based on configuration...
    hadoop2: Adapter 1: nat
    hadoop2: Adapter 2: hostonly
==> hadoop2: Forwarding ports...
    hadoop2: 22 (guest) => 2200 (host) (adapter 1)
==> hadoop2: Running 'pre-boot' VM customizations...
==> hadoop2: Booting VM...
==> hadoop2: Waiting for machine to boot. This may take a few minutes...
    hadoop2: SSH address: 127.0.0.1:2200
    hadoop2: SSH username: vagrant
    hadoop2: SSH auth method: private key
    hadoop2: Warning: Remote connection disconnect. Retrying...
    hadoop2: Warning: Remote connection disconnect. Retrying...
    hadoop2: Warning: Remote connection disconnect. Retrying...
    hadoop2:
    hadoop2: Vagrant insecure key detected. Vagrant will automatically replace
    hadoop2: this with a newly generated keypair for better security.
    hadoop2:
    hadoop2: Inserting generated public key within guest...
    hadoop2: Removing insecure key from the guest if it's present...
    hadoop2: Key inserted! Disconnecting and reconnecting using new SSH key...
==> hadoop2: Machine booted and ready!
==> hadoop2: Checking for guest additions in VM...
==> hadoop2: Setting hostname...
==> hadoop2: Configuring and enabling network interfaces...
==> hadoop2: Mounting shared folders...
    hadoop2: /vagrant => E:/vagrant
==> hadoop3: Importing base box 'puppetlabs/centos-7.2-64-nocm'...
==> hadoop3: Matching MAC address for NAT networking...
==> hadoop3: Setting the name of the VM: caas3
==> hadoop3: Fixed port collision for 22 => 2222. Now on port 2201.
==> hadoop3: Clearing any previously set network interfaces...
==> hadoop3: Preparing network interfaces based on configuration...
    hadoop3: Adapter 1: nat
    hadoop3: Adapter 2: hostonly
==> hadoop3: Forwarding ports...
    hadoop3: 22 (guest) => 2201 (host) (adapter 1)
==> hadoop3: Running 'pre-boot' VM customizations...
==> hadoop3: Booting VM...
==> hadoop3: Waiting for machine to boot. This may take a few minutes...
    hadoop3: SSH address: 127.0.0.1:2201
    hadoop3: SSH username: vagrant
    hadoop3: SSH auth method: private key
    hadoop3: Warning: Remote connection disconnect. Retrying...
    hadoop3: Warning: Remote connection disconnect. Retrying...
    hadoop3: Warning: Remote connection disconnect. Retrying...
    hadoop3:
    hadoop3: Vagrant insecure key detected. Vagrant will automatically replace
    hadoop3: this with a newly generated keypair for better security.
    hadoop3:
    hadoop3: Inserting generated public key within guest...
    hadoop3: Removing insecure key from the guest if it's present...
    hadoop3: Key inserted! Disconnecting and reconnecting using new SSH key...
==> hadoop3: Machine booted and ready!
==> hadoop3: Checking for guest additions in VM...
==> hadoop3: Setting hostname...
==> hadoop3: Configuring and enabling network interfaces...
==> hadoop3: Mounting shared folders...
    hadoop3: /vagrant =>  E:/vagrant
==> hadoop4: Importing base box 'puppetlabs/centos-7.2-64-nocm'...
==> hadoop4: Matching MAC address for NAT networking...
==> hadoop4: Setting the name of the VM: caas4
==> hadoop4: Fixed port collision for 22 => 2222. Now on port 2202.
==> hadoop4: Clearing any previously set network interfaces...
==> hadoop4: Preparing network interfaces based on configuration...
    hadoop4: Adapter 1: nat
    hadoop4: Adapter 2: hostonly
==> hadoop4: Forwarding ports...
    hadoop4: 22 (guest) => 2202 (host) (adapter 1)
==> hadoop4: Running 'pre-boot' VM customizations...
==> hadoop4: Booting VM...
==> hadoop4: Waiting for machine to boot. This may take a few minutes...
    hadoop4: SSH address: 127.0.0.1:2202
    hadoop4: SSH username: vagrant
    hadoop4: SSH auth method: private key
    hadoop4: Warning: Remote connection disconnect. Retrying...
    hadoop4: Warning: Remote connection disconnect. Retrying...
    hadoop4: Warning: Remote connection disconnect. Retrying...
    hadoop4:
    hadoop4: Vagrant insecure key detected. Vagrant will automatically replace
    hadoop4: this with a newly generated keypair for better security.
    hadoop4:
    hadoop4: Inserting generated public key within guest...
    hadoop4: Removing insecure key from the guest if it's present...
    hadoop4: Key inserted! Disconnecting and reconnecting using new SSH key...
==> hadoop4: Machine booted and ready!
==> hadoop4: Checking for guest additions in VM...
==> hadoop4: Setting hostname...
==> hadoop4: Configuring and enabling network interfaces...
==> hadoop4: Mounting shared folders...
    hadoop4: /vagrant =>  E:/vagrant

本质上vagrant就是将box导入到virtualbox(也可以是其他虚拟化软件,但不同虚拟化软件的box格式不同,需要分别下载),并参照Vagrantfile的设置修改虚拟机配置,包括硬件和网络配置。

五、使用

启动后使用SSH登录到各个虚拟机的22端口,即可开始使用。
==root密码为puppet==。
如果需要清除己有虚拟机,可以使用

vagrant destroy

然后再次在此目录下使用

vagrant up

即可重新生成干净的虚拟机。

正常生成虚拟机后,打开你的virtualbox ,这时可以看到生成的虚拟机都是正在运行的状态。
但如果你是windows10系统,那么你很有可能怀疑自己是不是眼花,看到的是却是“已关闭”,如下图:

这里写图片描述

状态依然是已关闭,此时,你可以Xshell连接127.0.0.1:2200等虚拟机端口,但这不是我们要达到的效果

我们在vagrant命令界面,运行vagrant halt停掉服务,然后到virtualbox这边全选无界面启动,就可以了,这样麻烦的地方就在于每次启动要到virtualbox里面来启动
这里写图片描述

ok完了
这里写图片描述

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值