Windows10搭建Vagrant+VirtualBox环境

一、Vagrant是什么?

vagrant是一个操作虚拟机的工具.是一个基于Ruby的工具,用于创建和部署虚拟化开发环境。 通过命令和配置文件来管理虚拟机,很快就能完成一套开发环境的部署,并可以打包传播,统一了开发环境,也解决了重复配置环境的麻烦.

二、Vargant的好处

    1. Vagrant会创建共享文件夹,用来在主机和虚拟机之间进行资源共享;
    1. Vagrant通过添加box镜像进行快速部署,部署完毕后可以通过package进行打包分发,避免二次重建环境;
    1. Vagrant可以使用puppet、chref等管理工具进行自动化配置部署;
    1. Vagrant支持单机模拟多台机器,且支持一个配置文件Vagrantfile就可以跑分布式系统。

三、准备工作

win7下建议安装版本
1.vagrant_1.8.6.msi
2.VirtualBox-5.1.24-117012-Win.exe

Win10下建议安装版本
1.VirtualBox-6.1.18-142142-Win.exe
2.vagrant_2.2.14_x86_64.msi

我这里是Win10系统,刚开始不清楚,装的是最新的VirtualBox-5.2.8-121009-Win.exe+Vagrant 2.1.1启动就报错,后来上网一搜说是版本不兼容造成的。我这里有两台电脑都是win10的有台电脑上死活装不上,有台电脑却能装上。。跟win10系统版本也有关系?我这系统版本是:


image.png

image.png

四、进行安装

  • 1.安装VirtualBox
    VirtualBox安装就不多说了,一直下一步下一步就行了。

  • 2.安装Vagrant
    Vargrant下载win版的,然后一直下一步下一步就行了。

  • 3.下载package.box
    去vagrant官网下载一个package.box 文件,box文件就是一个系统的镜像文件 :
    http://www.vagrantbox.es/,
    https://app.vagrantup.com/boxes/search

    image.png

  • 4.把虚拟机加载到box容器中
    下载好之后,在该目录下执行命令加载镜像文件到Vagrant中去:

//语法:vagrant box add [name] [xxx.box]
vagrant box add centos7 xxxx.box

centos7是给虚拟机起的名字 ,随意写。然后可以通过以下命令查看,当前vagrant下有那些可用

e:\vagrant_box>vagrant box list
centos7 (virtualbox, 0)
  • 5.初始化虚拟机
    在你想要创建虚拟机的目录下,执行以下命令进行初始化
e:\vagrant_box>vagrant init centos7
A `Vagrantfile` has been placed in this directory. You are now
ready to `vagrant up` your first virtual environment! Please read
the comments in the Vagrantfile as well as documentation on
`vagrantup.com` for more information on using Vagrant.
image.png

会生成一个Vagrantfile文件,该文件就是Vagrant的配置文件。

  • 6.启动虚拟机

在该目录下输入:

e:\vagrant_box>vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Importing base box 'centos7'...
==> default: Matching MAC address for NAT networking...
==> default: Setting the name of the VM: vagrant_box_default_1612589676611_81682
Vagrant is currently configured to create VirtualBox synced folders with
the `SharedFoldersEnableSymlinksCreate` option enabled. If the Vagrant
guest is not trusted, you may want to disable this option. For more
information on this option, please refer to the VirtualBox manual:

  https://www.virtualbox.org/manual/ch04.html#sharedfolders

This option can be disabled globally with an environment variable:

  VAGRANT_DISABLE_VBOXSYMLINKCREATE=1

or on a per folder basis within the Vagrantfile:

  config.vm.synced_folder '/host/path', '/guest/path', SharedFoldersEnableSymlinksCreate: false
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
    default: Adapter 1: nat
==> default: Forwarding ports...
    default: 22 (guest) => 2222 (host) (adapter 1)
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
    default: SSH address: 127.0.0.1:2222
    default: SSH username: vagrant
    default: SSH auth method: private key
    default: Warning: Connection reset. Retrying...
    default: Warning: Connection aborted. Retrying...
    default:
    default: Vagrant insecure key detected. Vagrant will automatically replace
    default: this with a newly generated keypair for better security.
    default:
    default: Inserting generated public key within guest...
    default: Removing insecure key from the guest if it's present...
    default: Key inserted! Disconnecting and reconnecting using new SSH key...
==> default: Machine booted and ready!
==> default: Checking for guest additions in VM...
    default: The guest additions on this VM do not match the installed version of
    default: VirtualBox! In most cases this is fine, but in rare cases it can
    default: prevent things such as shared folders from working properly. If you see
    default: shared folder errors, please make sure the guest additions within the
    default: virtual machine match the version of VirtualBox you have installed on
    default: your host and reload your VM.
    default:
    default: Guest Additions Version: 4.3.30
    default: VirtualBox Version: 6.1
==> default: Mounting shared folders...
    default: /vagrant => E:/vagrant_box

启动报错:

e:\vagrant_box>vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Importing base box 'centos7'...
==> default: Matching MAC address for NAT networking...
==> default: Setting the name of the VM: Node1_default_1525964151077_64147
"rsync" could not be found on your PATH. Make sure that rsync
is properly installed on your system and available on the PATH.

修改:

C:\Users\{your_username}\.vagrant.d\boxes\centos7\0\virtualbox\Vagrantfile

原配置:

Vagrant.configure("2") do |config|
  config.vm.base_mac = "525400cae48b"
  config.vm.synced_folder ".", "/vagrant", type: "rsync"
end

改为:

Vagrant.configure("2") do |config|
  config.vm.base_mac = "525400cae48b"
  config.vm.synced_folder ".", "/vagrant", type: "virtualbox"
end

然后启动,报如下错误:

e:\vagrant_box>vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
    default: Adapter 1: nat
==> default: Forwarding ports...
    default: 22 (guest) => 2222 (host) (adapter 1)
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
    default: SSH address: 127.0.0.1:2222
    default: SSH username: vagrant
    default: SSH auth method: private key
    default:
    default: Vagrant insecure key detected. Vagrant will automatically replace
    default: this with a newly generated keypair for better security.
    default:
    default: Inserting generated public key within guest...
    default: Removing insecure key from the guest if it's present...
    default: Key inserted! Disconnecting and reconnecting using new SSH key...
==> default: Machine booted and ready!
==> default: Checking for guest additions in VM...
    default: No guest additions were detected on the base box for this VM! Guest
    default: additions are required for forwarded ports, shared folders, host only
    default: networking, and more. If SSH fails on this machine, please install
    default: the guest additions and repackage the box to continue.
    default:
    default: This is not an error message; everything may continue to work properly,
    default: in which case you may ignore this message.
==> default: Mounting shared folders...
    default: /vagrant => E:/OS_WORK/Node1
Vagrant was unable to mount VirtualBox shared folders. This is usually
because the filesystem "vboxsf" is not available. This filesystem is
made available via the VirtualBox Guest Additions and kernel module.
Please verify that these guest additions are properly installed in the
guest. This is not a bug in Vagrant and is usually caused by a faulty
Vagrant box. For context, the command attempted was:
 
mount -t vboxsf -o uid=1000,gid=1000 vagrant /vagrant
 
The error output from the command was:
 
mount: unknown filesystem type 'vboxsf'

中文意思就是:


vagrant 无法装载VirtualBox的共享文件夹,因为文件系统"vboxsf"无法使用.
这个文件系统通过VirtualBox客户添加内核模块获得,请确认是否被正确安装.
这不是vagrant的问题,而是VirtualBox引起的问题.
请尝试执行,以下命令:
mount -t vboxsf -o uid=1001,gid=1001 work_ /work
命令输出的错误信息为:
sbin/mount.vboxsf:挂载失败:找不到这个文件或目录

网上说的是,是Vagrant没有安装VBoxGuestAdditions,下面来手工安装VitrualBox的这个增强功能:

  • 使用命令
    e:\vagrant_box>vagrant halt
    ==> default: Attempting graceful shutdown of VM...
    停止已在运行的虚拟机

  • 启动VirtualBox中的虚拟机
    直接在VirtualBox中启动虚拟机,如下所示进行登录:


    image.png

输入默认用户名 : vagrant , 登录密码 : vagrant

  • 挂载并安装VBoxGuestAdditions
    挂载办法如下所示:设备->安装增强功能


    image.png

    提示以下错误


    image.png

手动安装下,在VirtualBox安装目录下找到VBoxGuestAdditions.iso


image.png

加载进来,之后重启下,使用以下命令安装VBoxGuestAdditions

sudo mount /dev/cdrom /media/cdrom
cd /media/cdrom/
sudo ./VBoxLinuxAddtions.run

发现还是不行,然后又执行以下命令更新了下系统

sudo yum update
sudo yum install gcc
sudo yum install kernel-devel
 
exit #退出虚拟机
vagrant halt # 关闭虚拟机

后面还是不行。然后使用vagrant命令进行安装

e:\vagrant_box>vagrant plugin install vagrant-vbguest
Installing the 'vagrant-vbguest' plugin. This can take a few minutes...
Installed the plugin 'vagrant-vbguest (0.15.1)'!

之后重启

e:\vagrant_box>vagrant reload --provision
==> default: Clearing any previously set forwarded ports...
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
    default: Adapter 1: nat
==> default: Forwarding ports...
    default: 22 (guest) => 2222 (host) (adapter 1)
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
    default: SSH address: 127.0.0.1:2222
    default: SSH username: vagrant
    default: SSH auth method: private key
==> default: Machine booted and ready!
[default] No installation found.
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.shu.edu.cn
 * extras: mirrors.shu.edu.cn
 * updates: mirrors.shu.edu.cn
Package kernel-devel-3.10.0-862.2.3.el7.x86_64 already installed and latest version
Package gcc-4.8.5-28.el7.x86_64 already installed and latest version
Package binutils-2.27-27.base.el7.x86_64 already installed and latest version
Package 1:make-3.82-23.el7.x86_64 already installed and latest version
Package 4:perl-5.16.3-292.el7.x86_64 already installed and latest version
Package bzip2-1.0.6-13.el7.x86_64 already installed and latest ve  rsion
Nothing to do
Copy iso file C:\Program Files\Oracle\VirtualBox/VBoxGuestAdditions.iso into the box /tmp/VBoxGuestAdditions.iso
Mounting Virtualbox Guest Additions ISO to: /mnt
mount: /dev/loop0 is write-protected, mounting read-only
Installing Virtualbox Guest Additions 5.1.24 - guest version is unknown
Verifying archive integrity... All good.
Uncompressing VirtualBox 5.1.24 Guest Additions for Linux...........
VirtualBox Guest Additions installer
Copying additional installer modules ...
Installing additional modules ...
vboxadd.sh: Starting the VirtualBox Guest Additions.
 
Could not find the X.Org or XFree86 Window System, skipping.
Redirecting to /bin/systemctl start vboxadd.service
Redirecting to /bin/systemctl start vboxadd-service.service
Unmounting Virtualbox Guest Additions ISO from: /mnt
 
==> default: Checking for guest additions in VM...
==> default: Mounting shared folders...
    default: /vagrant => e:\vagrant_box

然后启动:

e:\vagrant_box>vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Machine already provisioned. Run `vagrant provision` or use the `--provision`
==> default: flag to force provisioning. Provisioners marked to run always will still run.

五、Vagrant连接

  • 1.虚拟机相关登录信息##


e:\vagrant_box>vagrant ssh
`ssh` executable not found in any directories in the %PATH% variable. Is an
SSH client installed? Try installing Cygwin, MinGW or Git, all of which
contain an SSH client. Or use your favorite SSH client with the following
authentication information shown below:
 
Host: 127.0.0.1
Port: 2222
Username: vagrant
Private key: e:\vagrant_box/.vagrant/machines/default/virtualbox/private_key

这样我们就可以像连接到一台服务器一样进行操作了。

  • 2.ssh登录##

window机器不支持这样的命令,必须使用第三方客户端来进行连接,例如xmoba、putty、Xshell等.

ssh: 127.0.0.1  
端口: 2222  
用户名: vagrant  
密码: vagrant 

这里使用Xshell来登录


image.png
image.png

找到刚刚上面生成的秘钥:

E:\vagrant_box\.vagrant\machines\default\virtualbox\private_key
image.png

然后点击确定,进行连接


image.png

注意事项:
注意关机之前都要关闭下虚拟机。如果不关闭的话 明天早上起来就发现启动不了

在.Vagrantfile文件中设置:
设置用户名以及密码:
config.ssh.username = "vagrant"
config.ssh.password = "vagrant"
设置虚拟网络
config.vm.network "private_network", ip: "192.168.33.10"

六、Vagrant常用命令

  • 1. 添加box
提前下载好的box文件,~/box/centos.box,我们给这个box命名为centos vagrant box add centos ~/box/centos.box#box文件也可以是远程地址 base 为默认名称#vagrant box add base http://files.vagrantup.com/lucid64.box
  • 2. 初始化
打开目录#cd ~/vagrant#初始化vagrant init#如果你添加的box名称不是base,那么需要在初始化的时候指定名称,例如vagrant init centos
  • 3. 启动虚拟机
vagrant up
  • 4. 打包分发
vagrant package
  • 5. 帮助命令
vagrant help
  • 6. 查看vagrant版本信息
vagrant -v

Vagrant命令详解

命令 作用
vagrant box add 添加box的操作
vagrant init 初始化box的操作,会生成vagrant的配置文件Vagrantfile
vagrant up 启动本地环境
vagrant ssh 通过 ssh 登录本地环境所在虚拟机
vagrant halt 关闭本地环境
vagrant suspend 暂停本地环境
vagrant resume 恢复本地环境
vagrant reload 修改了 Vagrantfile 后,使之生效(相当于先 halt,再 up)
vagrant destroy 彻底移除本地环境
vagrant box list 显示当前已经添加的box列表
vagrant box remove 删除相应的box
vagrant package 打包命令,可以把当前的运行的虚拟机环境进行打包
vagrant plugin 用于安装卸载插件
vagrant status 获取当前虚拟机的状态
vagrant global-status 显示当前用户Vagrant的所有环境状态
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 207,113评论 6 481
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 88,644评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 153,340评论 0 344
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 55,449评论 1 279
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 64,445评论 5 374
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 49,166评论 1 284
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,442评论 3 401
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 37,105评论 0 261
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 43,601评论 1 300
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,066评论 2 325
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,161评论 1 334
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,792评论 4 323
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,351评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,352评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,584评论 1 261
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 45,618评论 2 355
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,916评论 2 344

推荐阅读更多精彩内容