Vagrant批量生成虚拟机

本文介绍了如何利用Vagrant批量生成虚拟机来搭建CAAS环境。首先,需要下载VirtualBox和Vagrant的特定版本。接着,通过Vagrant初始化一个包含多个服务器配置的box。然后,配置Vagrantfile以定义集群中的虚拟机数量和参数。执行`vagrant up`即可创建和启动所有虚拟机,每个虚拟机分配2核2G内存。最后,通过SSH连接到各虚拟机进行使用。

开发CAAS需要一堆机器,有时候还需要一堆干净的机器用来测试初始化集群和基础服务,如果每次都手工安装虚拟机,很显然效率太低,于是就开始使用vagrant。

一、下载

需要下载:

  • virtualbox https://www.virtualbox.org/
  • vagrant https://www.vagrantup.com/ 比较坑的是下载最新版后不能正常使用,告诉我最新的vagrant1.8.5不能创建linux虚拟机,要退回到1.8.4。好吧,我使用1.8.4,然后一堆错,告诉我不支持最新的virtualbox,要退回到5.0.x...

二、下载box

好不容易装好了,下载一个box开始测试,测试之前最好设置一下VAGRANT_HOME系统变量,否则会将box文件什么的都放到用户目录。新建一个目录G:/caas/,进入命令行切换到此目录,执行

vagrant init puppetlabs/centos-7.2-64-nocm

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

三、配置集群

参考如下:

# -*- mode: ruby -*-# vi: set ft=ruby : servers = { :hadoop1 => '192.168.1.11', :hadoop2 => '192.168.1.12', :hadoop3 => '192.168.1.13', :hadoop4 => '192.168.1.14', :hadoop5 => '192.168.1.15', :hadoop6 => '192.168.1.16', :hadoop7 => '192.168.1.17' } 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 = 2endendendend

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

四、生成虚拟机并启动

1、首先可以迅雷手动去下载centos7.2版本的virtualbox(非必须)

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

2、用varant box 添加box,名字要和上一步init的名字保持一致(非必须)

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

3、输入

vagrant up

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

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

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

五、使用

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

vagrant destroy

然后再次在此目录下使用

vagrant up

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值