使用Vagrant和VirtualBox一步步地創建一個Base Box


box集合  http://www.vagrantbox.es/

Vagrant和VirtualBox軟件的安裝步驟省略,去官網下載最新的版本然后下一步下一步地安裝就行了,和正常的安裝軟件沒有什么區別。下面看看利用vagrant和virtualbox如果一步步打造出自己的可復用性強的開發環境。

開始前確保您的計算機支持虛擬化技術,在BIOS中可以開啟。

一、在virtualbox中新建一個虛擬機

我這里使用的是CentOS-6.5-x86_64-minimal.iso這個鏡像文件來安裝的,1G內存,8G虛擬硬盤的配置(后面可以修改)。虛擬機名為centos。

安裝好后該虛擬機已經自帶了ssh服務,如果沒有,需要自己先進行安裝。

開啟虛擬機,並登陸root用戶。

二、配置虛擬機

創建admin用戶組

[root@localhost ~]# groupadd admin

創建vagrant用戶並添加到admin用戶組

[root@localhost ~]# useradd -g admin vagrant

為vagrant用戶設置密碼,密碼為vagrant(會提示密碼太弱,可以不用管它)

[root@localhost ~]# passwd vagrant

修改sudoers文件

[root@localhost ~]# vi /etc/sudoers

在該文件添加下面兩行代碼,並注釋掉requiretty行

Defaults env_keep="SSH_AUTH_SOCK"
%admin ALL=NOPASSWD: ALL

配置網卡:將ONBOOT=no 改為 ONBOOT=yes

[root@localhost ~]# vi /etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE=eth0
HWADDR=08:00:27:25:4A:9A
TYPE=Ethernet
UUID=751ae3bb-eb75-4f60-b5c3-7f1afa1dfce1
ONBOOT=yes
NM_CONTROLLED=yes
BOOTPROTO=dhcp

配置好網卡后需要重啟網絡服務:

[root@localhost ~]# service network restart

三、設置端口轉發

配置virtualbox端口轉發(NAT模式)

主機ip127.0.0.1

主機端口22(或者其他未被占用端口,最好還是設置成22)

子系統ip可以在虛擬通過ifconfg命令查看到,即eth0網卡的ip地址

子系統端口為22

設置好端口轉發規則之后,就能在本地的命令行工具(Xshell,putty..)里通過命令 ssh root@127.0.0.1 連接到該虛擬機。

四、添加vagrant公鑰,使vagrant用戶可以免密碼ssh連接該虛擬機

在虛擬機里切換到vagrant用戶

[root@localhost ~]# su vagrant
[vagrant@localhost root]$ cd ~
[vagrant@localhost ~]$

然后執行:

[vagrant@localhost ~]$ mkdir ~/.ssh/
[vagrant@localhost ~]$ cd ~/.ssh
[vagrant@localhost .ssh]$ sudo yum -y install wget
[vagrant@localhost .ssh]$ wget http://github.com/mitchellh/vagrant/raw/master/keys/vagrant
[vagrant@localhost .ssh]$ wget http://github.com/mitchellh/vagrant/raw/master/keys/vagrant
[vagrant@localhost .ssh]$ mv vagrant.pub authorized_keys

清理緩存

[vagrant@localhost .ssh]$ sudo yum clean all

然后關閉此虛擬機,通過virtualbox將其電源關閉

五、打包生成box文件

在本地命令行進入該虛擬機目錄

cd Users\Admin\VirtualBox VMs\centos\

進行打包

vagrant package --output mycentos.box --base centos

完成之后你將會在C:\Users\Admin\VirtualBox VMs\centos目錄下看到一個mycentos.box文件,我的這個文件有544M。

VirtualBox VMs這個目錄可以在virtualbox全局設定-常規里進行修改

到此一個Base Box就制作完成了。

這個box是很干凈的,然后可以在這個box的基礎上去搭建自己的開發環境,比如lnmp等等。再次進行打包,最后一個帶開發環境的box就做好了,可以自己用也可以分發給其他小伙伴使用,不用以后每次都要去搭建一遍環境。

六、附上vagrant常用操作命令

vagrant box list 查看在vagrant里已經安裝了哪些box

vagrant box add name boxname.box 添加一個本地的box鏡像到vagrant 這個box鏡像可以自己制作也可以使用網上的(vagrant box add ubuntu ubuntu/trusty64)

vagrant init name 初始化一個虛擬機,這個name是通過vagrant box list查看

vagrant up 啟動虛擬機 在virtualbox中可以看到啟動后的虛擬機,第一次啟動時時間會長一點

vagrant ssh 連接虛擬機

vagrant reload 重啟虛擬機

vagrant status 查看虛擬機當前狀態

七、常見問題處理

問題1:

default: in which case you may ignore this message.
==> default: Mounting shared folders...
default: /vagrant => E:/vagrantbox/centos67
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=500,gid=500 vagrant /vagrant

The error output from the command was:

mount: unknown filesystem type 'vboxsf'

上面這樣的問題可以執行下命令 vagrant plugin install vagrant-vbguest ,然后重啟vagrant reload來解決。

問題2:

vagrant up的時候出現 “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\admin.vagrant.d\boxes\centos7\0\virtualbox類似這樣的目錄下面的Vagrantfile文件

修改type配置項: config.vm.synced_folder “.”, “/vagrant”, type: “virtualbox”


免責聲明!

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



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