1,介紹Vagrant
我們做web開發的時候經常要安裝各種本地測試環境,比如apache,php,mysql,redis等等。出于個人使用習慣,可能我們還是比較習慣用windows。雖然說在windows下搭建各種開發環境是可行的,各大開發環境都有windows版本。然而在windows下配置有時候會顯得繁瑣,并且還會導致開發環境(windows)和生產環境(lunix)不一致。
能不能在windows下也像linux那樣開發?也許你想到了,用虛擬機。用虛擬機裝個linux系統就好了。裝完linux系統就設置共享目錄,設置網絡端口映射,等等。好像也有那么點繁瑣。
還有,假如我們是一個團隊進行開發,那么每個人的電腦上都要裝個虛擬機+ linux系統+各種運行環境。手動設置麻煩不說,大家的開發環境不太一致(可能你裝了apcahe我裝了nginx等),也是頭疼。能不能把各種設置都自動化,并且保持整個團隊的開發環境一致呢?
Vagrant就是為了解決這個問題而生的。它使用開源 VirtualBox 作為虛擬化支持,可以輕松的跨平臺部署。
2,下載
下載VirtualBox:http://download.virtualbox.org/virtualbox/5.1.10/VirtualBox-5.1.10-112026-Win.exe
上面給出的是5.1.10版本的下載鏈接。要下載其他版本請訪問官網https://www.virtualbox.org/wiki/Downloads
下載Vagrant:
https://releases.hashicorp.com/vagrant/1.8.7/vagrant_1.8.7.msi
上面給出的是1.8.7版本的下載鏈接。要下載其他版本請訪問官網
http://www.vagrantup.com/downloads.html
下載虛擬鏡像:
https://github.com/CommanderK5/packer-centos-template/releases/download/0.6.7/vagrant-centos-6.7.box
上面給出的是centos-6.7鏡像下載鏈接,要下載其他鏡像請訪問官網
http://www.vagrantbox.es/
3,安裝
下載好上面的軟件包后,先安裝VirtualBox,然后安裝Vagrant。都是雙擊即可安裝的,所以沒什么好介紹。下面介紹下怎么把鏡像導入。
安裝完vagrant后 檢測是否安裝成功 打開cmd
C:\Users\Administrator>vagrant
Usage: vagrant [options] ?[]
-v, --version ? ? ? ? ? ? ? ? ? ?Print the version and exit.
-h, --help ? ? ? ? ? ? ? ? ? ? ? Print this help.
Common commands:
box ? ? ? ? ? ? manages boxes: installation, removal, etc.
connect ? ? ? ? connect to a remotely shared Vagrant environment
destroy ? ? ? ? stops and deletes all traces of the vagrant machine
global-status ? outputs status Vagrant environments for this user
halt ? ? ? ? ? ?stops the vagrant machine
help ? ? ? ? ? ?shows the help for a subcommand
init ? ? ? ? ? ?initializes a new Vagrant environment by creating a Vagrant
file
login ? ? ? ? ? log in to HashiCorp's Atlas
package ? ? ? ? packages a running vagrant environment into a box
plugin ? ? ? ? ?manages plugins: install, uninstall, update, etc.
port ? ? ? ? ? ?displays information about guest port mappings
powershell ? ? ?connects to machine via powershell remoting
provision ? ? ? provisions the vagrant machine
push ? ? ? ? ? ?deploys code in this environment to a configured destinatio
n
rdp ? ? ? ? ? ? connects to machine via RDP
reload ? ? ? ? ?restarts vagrant machine, loads new Vagrantfile configurati
on
resume ? ? ? ? ?resume a suspended vagrant machine
share ? ? ? ? ? share your Vagrant environment with anyone in the world
snapshot ? ? ? ?manages snapshots: saving, restoring, etc.
ssh ? ? ? ? ? ? connects to machine via SSH
ssh-config ? ? ?outputs OpenSSH valid configuration to connect to the machi
ne
status ? ? ? ? ?outputs status of the vagrant machine
suspend ? ? ? ? suspends the machine
up ? ? ? ? ? ? ?starts and provisions the vagrant environment
version ? ? ? ? prints current and latest Vagrant version
For help on any individual command run `vagrant COMMAND -h`
Additional subcommands are available, but are either more advanced
or not commonly used. To see all subcommands, run the command
`vagrant list-commands`.
先新建一個工作目錄
比如我新建了E:\VagrantWorkcentos-6.7-x86_64
打開cmd命令提示符,進入新目錄,以我上面的目錄為例,輸入
C:\Users\Administrator>e:
E:\>cd VagrantWorkcentos-6.7-x86_64
然后輸入命令初始化
E:\VagrantWorkcentos-6.7-x86_64>vagrant init centos6.7
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.
把下載的vagrant-centos-6.7.box復制到本目錄E:\VagrantWorkcentos-6.7-x86_64下,執行
E:\VagrantWorkcentos-6.7-x86_64>vagrant box add centos6.7 vagrant-centos-6.7.box
==> box: Box file was not detected as metadata. Adding it directly...
==> box: Adding box 'centos6.7' (v0) for provider:
box: Unpacking necessary files from: file://E:/VagrantWorkcentos-6.7-x86_64/
vagrant-centos-6.7.box
box: Progress: 100% (Rate: 177M/s, Estimated time remaining: --:--:--)
The box you're attempting to add already exists. Remove it before
adding it again or add it with the `--force` flag.
Name: centos6.7
Provider: virtualbox
Version: 0
檢查是否導入成功
E:\VagrantWorkcentos-6.7-x86_64>vagrant box list
centos6.7 (virtualbox, 0)
4,配置
用文本編輯器打開D:VagrantWorkcentos-6.6-x86_64目錄下的Vagrantfile文件便可以進行一些常用配置。
下面列舉出幾個常用的配置。要用到其他配置請訪問官網文檔或者百度谷歌一下。
1,端口映射
config.vm.network :forwarded_port, guest: 80, host: 8080
把上面這句代碼前面的#號去掉。它表示映射本機的8080端口到虛擬機的80端口
2,如果需要自己自由的訪問虛擬機,但是別人不需要訪問虛擬機,可以使用private_network,并為虛擬機設置IP。
config.vm.network :private_network, ip: 192.168.1.10
把上面這句代碼前面的#號去掉即可
3,目錄同步:
config.vm.synced_folder "C:\D\www", "/vagrant_data"
第一個是本地目錄 第二個是vagrant虛擬機目錄
5,啟動
進入目錄E:\VagrantWorkcentos-6.7-x86_64后執行命令
vagrant up
E:\VagrantWorkcentos-6.7-x86_64>vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Importing base box 'centos6.7'...
==> default: Matching MAC address for NAT networking...
==> default: Setting the name of the VM: VagrantWorkcentos-67-x86_64_default_148
0154303134_79930
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
default: Adapter 1: nat
default: Adapter 2: hostonly
==> default: Forwarding ports...
default: 80 (guest) => 8080 (host) (adapter 1)
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: Remote connection disconnect. 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 o
f
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 t
he
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: 5.1
==> default: Configuring and enabling network interfaces...
==> default: Mounting shared folders...
default: /vagrant => E:/VagrantWorkcentos-6.7-x86_64
虛擬機啟動之后則可以通過vagrant ssh 聯入虛擬機進行進一步的環境配置,或者軟件安裝相關的工作,在Windows系統下,并不能直接通過 vagrant ssh 連到虛擬機,需要使用 Putty,Xshell 等第三方工具進行連接。連接地址192.168.1.10,端口22。登錄的帳號root/vagrant 密碼為vagrant
檢測狀態
E:\VagrantWorkcentos-6.7-x86_64>vagrant status
Current machine states:
default ? ? ? ? ? ? ? ? ? running (virtualbox)
The VM is running. To stop this VM, you can run `vagrant halt` to
shut it down forcefully, or you can run `vagrant suspend` to simply
suspend the virtual machine. In either case, to restart it again,
simply run `vagrant up`.
上圖跟下面windows下面的目錄一一對應
打包分發
當你配置好開發環境后,退出并關閉虛擬機。在終端里對開發環境進行打包:
vagrant package
打包完成后會在當前目錄生成一個package.box 的文件,將這個文件傳給其他用戶,其他用戶只要添加這個 box 并用其初始化自己的開發目錄就能得到一個一模一樣的開發環境了。
導出指定的box
vagrant package --base vagrant_default_1437988069090_46714 --output php_new.box
其中 base指的是虛擬機名字, output輸出包名
6,導出
在cmd里進行工作目錄后,執行下面命令
vagrant package
完成后會在當前目錄就會生成package.box,之后新建虛擬機則可使用這個box。如果事先在你的虛擬機里建立好了各種開發環境,那么你直接把這個box給你的團隊其他成員安裝,這樣就可以省去一臺臺電腦部署的時間,還可以保持開發環境一致。很方便有木有。
7,其他命令
下面列舉出一些常用的cmd操作命令
vagrant up (啟動虛擬機)
vagrant halt (關閉虛擬機——對應就是關機)
vagrant suspend (暫停虛擬機——只是暫停,虛擬機內存等信息將以狀態文件的方式保存在本地,可以執行恢復操作后繼續使用)
vagrant resume (恢復虛擬機 —— 與前面的暫停相對應)
vagrant box remove centos6.6 (移除box,其中centos6.6是box名)
vagrant destroy (刪除虛擬機,刪除后在當前虛擬機所做進行的除開Vagrantfile中的配置都不會保留)
8,擴容
一直以來都是以vagrant+docker作為開發環境,可是久而久之,原Box自帶的8G容量就捉襟見肘了。時不時需要手動刪除一些東西。
Virtualbox 本身只支持vdi硬盤文件格式的擴容,對vmdk 格式的卻不支持。但是卻提供vmdk到vdi格式的轉化,正好可以利用這一功能進行擴容。
因為要用到VBoxManage,所以要把D:\Program Files\Oracle\VirtualBox這個加入path。
關閉虛擬機, 從Virtualbox頁面查看硬盤文件地址(選中虛擬機->右鍵->設置->存儲)(如:E:\VirtualBox VMs\VagrantWorkcentos-67-x86_64_default_1480157989392_34389)。進到文件所在目錄后執行:
$VBoxManage clonehd "centos-vm-disk1.vmdk" "centos-vm-disk1.vdi" --format VDI
給vdi格式硬盤文件擴容
$VBoxManage modifyhd centos-vm-disk1.vdi --resize 40960
從Virutalbox存儲界面刪除原硬盤文件,然后再加入新的VDI格式硬盤文件
vagrant up啟動虛擬機,然后vagrant ssh進入
利用cfdisk工具創建主分區,謹記選擇格式為Linux LVM (8e)
$ sudo cfdisk /dev/sda
cfdisk
這里的具體操作如下:
# vagrant ssh
# sudo su -
Findthe name of the logical volume mapping the file-systemison(ie./dev/mapper/VolGroupOS-lv_root).
# df
Findthe name of the physical volume(ordevice)that all the partitions are created on(ie./dev/sda).
# fdisk -l
Createanewprimary partitionforuseasaLinuxLVM
# fdisk /dev/sda
Pressp toprintthe partition table to identify the number of partitions.Bydefaultthere are two-sda1andsda2.
Pressn to create anewprimary partition.
Presspforprimary.
Press3forthe partition number,depending the output of the partition tableprint.
PressEntertwo times to accept thedefaultFirstandLastcylinder.
Presst to change the system's partition ID
Press 3 to select the newly creation partition
Type 8e to change the Hex Code of the partition for Linux LVM
Press w to write the changes to the partition table.
Reboot the machine, then ssh back in when it is up again and switch to the root user once more.
# reboot
# vagrant ssh
# sudo su -
利用pvcreate命令給新的分區創建物理卷
$sudo pvcreate /dev/sda3
Physical volume "/dev/sda3" successfully created
查看VG Name,我自己的VG Name是centos
$ sudo pvdisplay|grep"VG Name"
VGName ? ? ?VolGroup
將新分區擴展到centos這個組
$vgextend ? ?VolGroup ? /dev/sda3
Volume group "VolGroup" successfully extended
擴展邏輯分區
$?lvextend /dev/mapper/VolGroup-lv_root /dev/sda3
resize并且生效
$ resize2fs /dev/mapper/VolGroup-lv_root
resize2fs 1.41.12 (17-May-2010)
Filesystem at /dev/mapper/VolGroup-lv_root is mounted on /; on-line resizing required
old desc_blocks = 1, new_desc_blocks = 3
Performing an on-line resize of /dev/mapper/VolGroup-lv_root to 10098688 (4k) blocks.
The filesystem on /dev/mapper/VolGroup-lv_root is now 10098688 blocks long.
在centos 7下,這一步會出錯
resize2fs1.42.9(28-Dec-2013)
resize2fs:Badmagic numberinsuper-blockwhiletrying to open/dev/mapper/centos-root
Couldn't find valid filesystem superblock.
resize2fs
這時,只需要使用xfs_growfs命令替換就行了
xfs_growfs /dev/mapper/VolGroup-lv_root
xfs_growfs: /dev/mapper/VolGroup-lv_root is not a mounted XFS filesystem
至此,大分告成。vagrant reload重啟虛擬機查看效果
df -h
Filesystem? ? ? ? ? ? Size? Used Avail Use% Mounted on
/dev/mapper/VolGroup-lv_root
38G? 5.4G? 31G? 15% /
tmpfs? ? ? ? ? ? ? ? 939M? ? 0? 939M? 0% /dev/shm
/dev/sda1? ? ? ? ? ? 477M? 57M? 396M? 13% /boot
于是乎,媽媽再也不用擔心我的虛擬機磁盤沒容量了。
參考:https://www.madcoder.cn/vagrant-box-resize.html
https://gist.github.com/christopher-hopper/9755310
5.當VitrualBox啟動的時候出現? 不能為虛擬電腦打開一個任務的時候 請升級VitrualBox軟件以及Vgrant軟件后 就可以用了