Vagrant系列(一)----win10搭建Vagrant+VirtualBox環境_


 
一、Vagrant是什么?
    vagrant是一個操作虛擬機的工具.是一個基於Ruby的工具,用於創建和部署虛擬化開發環境。    通過命令和配置文件來管理虛擬機,很快就能完成一套開發環境的部署,並可以打包傳播,統一了開發環境,也解決了重復配置環境的麻煩.

 

二、Vargant的好處
1. Vagrant會創建共享文件夾,用來在主機和虛擬機之間進行資源共享; 
2. Vagrant通過添加box鏡像進行快速部署,部署完畢后可以通過package進行打包分發,避免二次重建環境; 
3. Vagrant可以使用puppet、chref等管理工具進行自動化配置部署;
 4. Vagrant支持單機模擬多台機器,且支持一個配置文件Vagrantfile就可以跑分布式系統。
三、准備工作
win7下建議安裝版本
1.vagrant_1.8.6.msi  
2.VirtualBox-5.1.24-117012-Win.exe
win10下建議安裝版本
我這里是Win10系統,剛開始不清楚,裝的是最新的VirtualBox-5.2.8-121009-Win.exe+Vagrant 2.1.1啟動就報錯,后來上網一搜說是版本不兼容造成的。我這里有兩台電腦都是win10的有台電腦上死活裝不上,有台電腦卻能裝上。。跟win10系統版本也有關系?我這系統版本是:
四、進行安裝
1.安裝VirtualBox
VirtualBox安裝就不多說了,一直下一步下一步就行了。
2.安裝Vagrant
Vargrant下載win版的,然后一直下一步下一步就行了。
3.下載package.box
去vagrant官網下載一個package.box 文件,box文件就是一個系統的鏡像文件 : http://www.vagrantbox.es/
4.把虛擬機加載到box容器中
下載好之后,在該目錄下執行命令加載鏡像文件到Vagrant中去:
vagrant box add centos7 xxxx.box
centos7是給虛擬機起的名字 ,隨意寫。然后可以通過以下命令查看,當前vagrant下有那些可用
1
C:\Users\fendo>vagrant box list
2
centos7 (virtualbox, 0)
5.初始化虛擬機
在你想要創建虛擬機的目錄下,執行以下命令進行初始化
vagrant init centos7
會生成一個Vagrantfile文件,該文件就是Vagrant的配置文件。
6.啟動虛擬機
在該目錄下輸入:
vagrant up
啟動報錯:
1
E:\OS_WORK\Node1>vagrant up
2
Bringing machine 'default' up with 'virtualbox' provider...
3
==> default: Importing base box 'centos7'...
4
==> default: Matching MAC address for NAT networking...
5
==> default: Setting the name of the VM: Node1_default_1525964151077_64147
6
"rsync" could not be found on your PATH. Make sure that rsync
7
is properly installed on your system and available on the PATH.
修改:
C:\Users\{your_username}\.vagrant.d\boxes\centos7\0\virtualbox\Vagrantfile
原配置:
1
Vagrant.configure("2") do |config|
2
config.vm.base_mac = "525400cae48b"
3
config.vm.synced_folder ".", "/vagrant", type: "rsync"
4
end
改為:
1
Vagrant.configure("2") do |config|
2
config.vm.base_mac = "525400cae48b"
3
config.vm.synced_folder ".", "/vagrant", type: "virtualbox"
4
end
然后啟動,報如下錯誤:
1
E:\OS_WORK\Node1>vagrant up
2
Bringing machine 'default' up with 'virtualbox' provider...
3
==> default: Clearing any previously set network interfaces...
4
==> default: Preparing network interfaces based on configuration...
5
default: Adapter 1: nat
6
==> default: Forwarding ports...
7
default: 22 (guest) => 2222 (host) (adapter 1)
8
==> default: Booting VM...
9
==> default: Waiting for machine to boot. This may take a few minutes...
10
default: SSH address: 127.0.0.1:2222
11
default: SSH username: vagrant
12
default: SSH auth method: private key
13
default:
14
default: Vagrant insecure key detected. Vagrant will automatically replace
15
default: this with a newly generated keypair for better security.
16
default:
17
default: Inserting generated public key within guest...
18
default: Removing insecure key from the guest if it's present...
19
default: Key inserted! Disconnecting and reconnecting using new SSH key...
20
==> default: Machine booted and ready!
21
==> default: Checking for guest additions in VM...
22
default: No guest additions were detected on the base box for this VM! Guest
23
default: additions are required for forwarded ports, shared folders, host only
24
default: networking, and more. If SSH fails on this machine, please install
25
default: the guest additions and repackage the box to continue.
26
default:
27
default: This is not an error message; everything may continue to work properly,
28
default: in which case you may ignore this message.
29
==> default: Mounting shared folders...
30
default: /vagrant => E:/OS_WORK/Node1
31
Vagrant was unable to mount VirtualBox shared folders. This is usually
32
because the filesystem "vboxsf" is not available. This filesystem is
33
made available via the VirtualBox Guest Additions and kernel module.
34
Please verify that these guest additions are properly installed in the
35
guest. This is not a bug in Vagrant and is usually caused by a faulty
36
Vagrant box. For context, the command attempted was:
37
 
38
mount -t vboxsf -o uid=1000,gid=1000 vagrant /vagrant
39
 
40
The error output from the command was:
41
 
42
mount: unknown filesystem type 'vboxsf'
中文意思就是:
1
vagrant 無法裝載VirtualBox的共享文件夾,因為文件系統"vboxsf"無法使用.
2
這個文件系統通過VirtualBox客戶添加內核模塊獲得,請確認是否被正確安裝.
3
這不是vagrant的問題,而是VirtualBox引起的問題.
4
請嘗試執行,以下命令:
5
mount -t vboxsf -o uid=1001,gid=1001 work_ /work
6
命令輸出的錯誤信息為:
7
sbin/mount.vboxsf:掛載失敗:找不到這個文件或目錄
網上說的是,是Vagrant沒有安裝VBoxGuestAdditions,下面來手工安裝VitrualBox的這個增強功能:
 
●使用命令
1
E:\OS_WORK\Node1>vagrant halt
2
==> default: Attempting graceful shutdown of VM...
停止已在運行的虛擬機
●啟動VirtualBox中的虛擬機
直接在VirtualBox中啟動虛擬機,如下所示進行登錄:
輸入默認用戶名 : vagrant , 登錄密碼 : vagrant 
●掛載並安裝VBoxGuestAdditions
掛載辦法如下所示:設備->安裝增強功能
提示以下錯誤
手動安裝下,在VirtualBox安裝目錄下找到VBoxGuestAdditions.iso
加載進來,之后重啟下,使用以下命令安裝VBoxGuestAdditions
1
sudo mount /dev/cdrom /media/cdrom
2
cd /media/cdrom/
3
sudo ./VBoxLinuxAddtions.run
發現還是不行,然后又執行以下命令更新了下系統
1
sudo yum update
2
sudo yum install gcc
3
sudo yum install kernel-devel
4
 
5
exit #退出虛擬機
6
vagrant halt # 關閉虛擬機
后面還是不行。然后使用vagrant命令進行安裝
1
E:\OS_WORK\Node1>vagrant plugin install vagrant-vbguest
2
Installing the 'vagrant-vbguest' plugin. This can take a few minutes...
3
Installed the plugin 'vagrant-vbguest (0.15.1)'!
之后重啟
1
E:\OS_WORK\Node1>vagrant reload --provision
2
==> default: Clearing any previously set forwarded ports...
3
==> default: Clearing any previously set network interfaces...
4
==> default: Preparing network interfaces based on configuration...
5
default: Adapter 1: nat
6
==> default: Forwarding ports...
7
default: 22 (guest) => 2222 (host) (adapter 1)
8
==> default: Booting VM...
9
==> default: Waiting for machine to boot. This may take a few minutes...
10
default: SSH address: 127.0.0.1:2222
11
default: SSH username: vagrant
12
default: SSH auth method: private key
13
==> default: Machine booted and ready!
14
[default] No installation found.
15
Loaded plugins: fastestmirror
16
Loading mirror speeds from cached hostfile
17
* base: mirrors.shu.edu.cn
18
* extras: mirrors.shu.edu.cn
19
* updates: mirrors.shu.edu.cn
20
Package kernel-devel-3.10.0-862.2.3.el7.x86_64 already installed and latest version
21
Package gcc-4.8.5-28.el7.x86_64 already installed and latest version
22
Package binutils-2.27-27.base.el7.x86_64 already installed and latest version
23
Package 1:make-3.82-23.el7.x86_64 already installed and latest version
24
Package 4:perl-5.16.3-292.el7.x86_64 already installed and latest version
25
Package bzip2-1.0.6-13.el7.x86_64 already installed and latest version
26
Nothing to do
27
Copy iso file C:\Program Files\Oracle\VirtualBox/VBoxGuestAdditions.iso into the box /tmp/VBoxGuestAdditions.iso
28
Mounting Virtualbox Guest Additions ISO to: /mnt
29
mount: /dev/loop0 is write-protected, mounting read-only
30
Installing Virtualbox Guest Additions 5.1.24 - guest version is unknown
31
Verifying archive integrity... All good.
32
Uncompressing VirtualBox 5.1.24 Guest Additions for Linux...........
33
VirtualBox Guest Additions installer
34
Copying additional installer modules ...
35
Installing additional modules ...
36
vboxadd.sh: Starting the VirtualBox Guest Additions.
37
 
38
Could not find the X.Org or XFree86 Window System, skipping.
39
Redirecting to /bin/systemctl start vboxadd.service
40
Redirecting to /bin/systemctl start vboxadd-service.service
41
Unmounting Virtualbox Guest Additions ISO from: /mnt
42
 
43
==> default: Checking for guest additions in VM...
44
==> default: Mounting shared folders...
45
default: /vagrant => E:/OS_WORK/Node1
復制
然后啟動:
1
E:\OS_WORK\Node1>vagrant up
2
Bringing machine 'default' up with 'virtualbox' provider...
3
==> default: Machine already provisioned. Run `vagrant provision` or use the `--provision`
4
==> default: flag to force provisioning. Provisioners marked to run always will still run.
五、Vagrant連接
1.虛擬機相關登錄信息
1
E:\OS_WORK\Node1>vagrant ssh
2
`ssh` executable not found in any directories in the %PATH% variable. Is an
3
SSH client installed? Try installing Cygwin, MinGW or Git, all of which
4
contain an SSH client. Or use your favorite SSH client with the following
5
authentication information shown below:
6
 
7
Host: 127.0.0.1
8
Port: 2222
9
Username: vagrant
10
Private key: E:/OS_WORK/Node1/.vagrant/machines/default/virtualbox/private_key
這樣我們就可以像連接到一台服務器一樣進行操作了。
2.ssh登錄
window機器不支持這樣的命令,必須使用第三方客戶端來進行連接,例如xmoba、putty、Xshell等.
1
ssh: 127.0.0.1
2
端口: 2222
3
用戶名: vagrant
4
密碼: vagrant
這里使用Xshell來登錄
找到剛剛上面生成的秘鑰: 
E:/OS_WORK/Node1/.vagrant/machines/default/virtualbox/private_key
然后點擊確定,進行連接

 

注意事項:
注意關機之前都要關閉下虛擬機。如果不關閉的話 明天早上起來就發現啟動不了
在.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/work#初始化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的所有環境狀態
 
 
 
 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM