Windows10搭建Vagrant+VirtualBox環(huán)境

一规辱、Vagrant是什么罕袋?

vagrant是一個操作虛擬機的工具.是一個基于Ruby的工具,用于創(chuàng)建和部署虛擬化開發(fā)環(huán)境溅固。 通過命令和配置文件來管理虛擬機,很快就能完成一套開發(fā)環(huán)境的部署,并可以打包傳播,統(tǒng)一了開發(fā)環(huán)境,也解決了重復配置環(huán)境的麻煩.

二侍郭、Vargant的好處

    1. Vagrant會創(chuàng)建共享文件夾掠河,用來在主機和虛擬機之間進行資源共享唠摹;
    1. Vagrant通過添加box鏡像進行快速部署,部署完畢后可以通過package進行打包分發(fā)煮甥,避免二次重建環(huán)境成肘;
    1. Vagrant可以使用puppet斧蜕、chref等管理工具進行自動化配置部署批销;
    1. Vagrant支持單機模擬多臺機器,且支持一個配置文件Vagrantfile就可以跑分布式系統(tǒng)丘逸。

三深纲、準備工作

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系統(tǒng),剛開始不清楚,裝的是最新的VirtualBox-5.2.8-121009-Win.exe+Vagrant 2.1.1啟動就報錯涛舍,后來上網(wǎng)一搜說是版本不兼容造成的唆途。我這里有兩臺電腦都是win10的有臺電腦上死活裝不上肛搬,有臺電腦卻能裝上温赔。。跟win10系統(tǒng)版本也有關系?我這系統(tǒng)版本是:


image.png

image.png

四、進行安裝

  • 1.安裝VirtualBox
    VirtualBox安裝就不多說了拜秧,一直下一步下一步就行了枉氮。

  • 2.安裝Vagrant
    Vargrant下載win版的,然后一直下一步下一步就行了楼肪。

  • 3.下載package.box
    去vagrant官網(wǎng)下載一個package.box 文件淹辞,box文件就是一個系統(tǒng)的鏡像文件 :
    http://www.vagrantbox.es/,
    https://app.vagrantup.com/boxes/search

    image.png

  • 4.把虛擬機加載到box容器中
    下載好之后象缀,在該目錄下執(zhí)行命令加載鏡像文件到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.初始化虛擬機
    在你想要創(chuàng)建虛擬機的目錄下,執(zhí)行以下命令進行初始化
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的共享文件夾,因為文件系統(tǒng)"vboxsf"無法使用.
這個文件系統(tǒng)通過VirtualBox客戶添加內(nèi)核模塊獲得,請確認是否被正確安裝.
這不是vagrant的問題,而是VirtualBox引起的問題.
請嘗試執(zhí)行,以下命令:
mount -t vboxsf -o uid=1001,gid=1001 work_ /work
命令輸出的錯誤信息為:
sbin/mount.vboxsf:掛載失敗:找不到這個文件或目錄

網(wǎng)上說的是豫尽,是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

發(fā)現(xiàn)還是不行颁井,然后又執(zhí)行以下命令更新了下系統(tǒng)

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

注意事項:
注意關機之前都要關閉下虛擬機。如果不關閉的話 明天早上起來就發(fā)現(xiàn)啟動不了

在.Vagrantfile文件中設置:
設置用戶名以及密碼:
config.ssh.username = "vagrant"
config.ssh.password = "vagrant"
設置虛擬網(wǎng)絡
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. 打包分發(fā)
vagrant package
  • 5. 幫助命令
vagrant help
  • 6. 查看vagrant版本信息
vagrant -v

Vagrant命令詳解

命令 作用
vagrant box add 添加box的操作
vagrant init 初始化box的操作弓乙,會生成vagrant的配置文件Vagrantfile
vagrant up 啟動本地環(huán)境
vagrant ssh 通過 ssh 登錄本地環(huán)境所在虛擬機
vagrant halt 關閉本地環(huán)境
vagrant suspend 暫停本地環(huán)境
vagrant resume 恢復本地環(huán)境
vagrant reload 修改了 Vagrantfile 后暇韧,使之生效(相當于先 halt,再 up)
vagrant destroy 徹底移除本地環(huán)境
vagrant box list 顯示當前已經(jīng)添加的box列表
vagrant box remove 刪除相應的box
vagrant package 打包命令巧婶,可以把當前的運行的虛擬機環(huán)境進行打包
vagrant plugin 用于安裝卸載插件
vagrant status 獲取當前虛擬機的狀態(tài)
vagrant global-status 顯示當前用戶Vagrant的所有環(huán)境狀態(tài)
最后編輯于
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末艺栈,一起剝皮案震驚了整個濱河市眼滤,隨后出現(xiàn)的幾起案子历涝,更是在濱河造成了極大的恐慌漾唉,老刑警劉巖赵刑,帶你破解...
    沈念sama閱讀 206,968評論 6 482
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件场刑,死亡現(xiàn)場離奇詭異牵现,居然都是意外死亡,警方通過查閱死者的電腦和手機科乎,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 88,601評論 2 382
  • 文/潘曉璐 我一進店門茅茂,熙熙樓的掌柜王于貴愁眉苦臉地迎上來太抓,“玉大人,你說我怎么就攤上這事碴倾〉衾觯” “怎么了机打?”我有些...
    開封第一講書人閱讀 153,220評論 0 344
  • 文/不壞的土叔 我叫張陵,是天一觀的道長皆辽。 經(jīng)常有香客問我驱闷,道長空免,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 55,416評論 1 279
  • 正文 為了忘掉前任扼菠,我火速辦了婚禮循榆,結果婚禮上,老公的妹妹穿的比我還像新娘映挂。我一直安慰自己盗尸,他們只是感情好,可當我...
    茶點故事閱讀 64,425評論 5 374
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著历恐,像睡著了一般弱贼。 火紅的嫁衣襯著肌膚如雪磷蛹。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 49,144評論 1 285
  • 那天庇勃,我揣著相機與錄音责嚷,去河邊找鬼掂铐。 笑死,一個胖子當著我的面吹牛爆班,可吹牛的內(nèi)容都是我干的辱姨。 我是一名探鬼主播雨涛,決...
    沈念sama閱讀 38,432評論 3 401
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼祟辟!你這毒婦竟也來了旧困?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 37,088評論 0 261
  • 序言:老撾萬榮一對情侶失蹤僚纷,失蹤者是張志新(化名)和其女友劉穎怖竭,沒想到半個月后陡蝇,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 43,586評論 1 300
  • 正文 獨居荒郊野嶺守林人離奇死亡广匙,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 36,028評論 2 325
  • 正文 我和宋清朗相戀三年鸦致,在試婚紗的時候發(fā)現(xiàn)自己被綠了分唾。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片狮斗。...
    茶點故事閱讀 38,137評論 1 334
  • 序言:一個原本活蹦亂跳的男人離奇死亡碳褒,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出鞍爱,到底是詐尸還是另有隱情专酗,我是刑警寧澤,帶...
    沈念sama閱讀 33,783評論 4 324
  • 正文 年R本政府宣布疗隶,位于F島的核電站翼闹,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏坚弱。R本人自食惡果不足惜关摇,卻給世界環(huán)境...
    茶點故事閱讀 39,343評論 3 307
  • 文/蒙蒙 一输虱、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧愁茁,春花似錦亭病、人聲如沸命贴。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,333評論 0 19
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至民珍,卻和暖如春盗飒,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背蝶溶。 一陣腳步聲響...
    開封第一講書人閱讀 31,559評論 1 262
  • 我被黑心中介騙來泰國打工抖所, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人暴匠。 一個月前我還...
    沈念sama閱讀 45,595評論 2 355
  • 正文 我出身青樓每窖,卻偏偏與公主長得像抹腿,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子崇败,可洞房花燭夜當晚...
    茶點故事閱讀 42,901評論 2 345

推薦閱讀更多精彩內(nèi)容