vagrant是什么?
vagrant是一個操作虛擬機的工具.
通過命令和配置文件來管理虛擬機,很快就能完成一套開發(fā)環(huán)境的部署,并可以打包傳播,統(tǒng)一了開發(fā)環(huán)境,也解決了重復配置環(huán)境的麻煩.
Vargant的好處
1.統(tǒng)一開發(fā)環(huán)境。
一次配置打包,統(tǒng)一分發(fā)給團隊成員,統(tǒng)一團隊開發(fā)環(huán)境,解決諸如“編碼問題”,“缺少模塊”,“配置文件不同”帶來的問題珍逸;
2.避免重復搭建開發(fā)環(huán)境。
新員工加入聋溜,不用浪費時間搭建開發(fā)環(huán)境谆膳,快速加入開發(fā),減少時間成本的浪費撮躁;
3.多個相互隔離開發(fā)環(huán)境漱病。
可以在不用box里跑不同的語言,或者編譯安裝同一語言不同版本把曼,搭建多個相互隔離的開發(fā)環(huán)境缨称,卸載清除時也很快捷輕松。
win7/win10系統(tǒng)下使用流程
1祝迂、安裝VirtualBox睦尽、vagrant兩款軟件
直接到官網(wǎng)下載安裝即可官網(wǎng)如下:
https://www.virtualbox.org
https://www.vagrantup.com
官網(wǎng)范圍速度比較慢,而且這兩位軟件不同版本可能會造成兼容問題型雳,也就是有可能導致你的vagrant用起來出現(xiàn)一些莫名其妙的問題当凡,所以最好是制定兩款的版本安裝山害。
win10版本為:
VirtualBox-5.2.12-122591-Win.exe
vagrant_2.2.7_x86_64.msi
2、下載一個box鏡像文件
安裝后需要下載一個box鏡像文件沿量,也就是linux的鏡像(其實也可以在vagrant add安裝的時候直接指定官方的鏡像名稱就會自動下載安裝浪慌,但鑒于國內(nèi)下載官方的速度太慢了,所以強烈鑒于先用迅雷之類的工具加速下載下來再本地安裝)
下載官網(wǎng)地址如下:
https://app.vagrantup.com/boxes/search
如果是安裝centos的話直接點擊下面的鏈接直達:
https://app.vagrantup.com/centos/boxes/7
3朴则、將鏡像加載到vagrant容器中
執(zhí)行如下命令:
vagrant box add -name [虛擬機名稱] [box放置的位置]
centos/7是虛擬機名稱 最后的box放置位置直接寫你下載后返的根目錄即可权纤,如果沒指定的話,默認會自己從官網(wǎng)下載
如:vagrant box add -name 'centos/7' [box放置的位置]
下面是我自己放置的位置習慣:
$ vagrant box add xuni CentOS20200119.box
4乌妒、初始化虛擬機
$ vagrant init xuni
vagrant init命令就是初始話命令
xuni 是指box的名稱(也就是第三步[虛擬機名稱]那個位置)
5汹想、啟動虛擬機
初始話之后,就可以開始啟動虛擬機撤蚊,運行如下命令:
$ vagrant up
D:\>vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Importing base box 'xuni'...
==> default: Matching MAC address for NAT networking...
==> default: Setting the name of the VM: _default_1583637544209_19565
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: Vagrant has detected a configuration issue which exposes a
==> default: vulnerability with the installed version of VirtualBox. The
==> default: current guest is configured to use an E1000 NIC type for a
==> default: network adapter which is vulnerable in this version of VirtualBox.
==> default: Ensure the guest is trusted to use this configuration or update
==> default: the NIC type using one of the methods below:
==> default:
==> default: https://www.vagrantup.com/docs/virtualbox/configuration.html#default-nic-type
==> default: https://www.vagrantup.com/docs/virtualbox/networking.html#virtualbox-nic-type
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
default: Adapter 1: nat
default: Adapter 2: hostonly
==> default: You are trying to forward to privileged ports (ports <= 1024). Most
==> default: operating systems restrict this to only privileged process (typically
==> default: processes running as an administrative user). This is a warning in case
==> default: the port forwarding doesn't work. If any problems occur, please try a
==> default: port higher than 1024.
==> default: Forwarding ports...
default: 22 (guest) => 2222 (host) (adapter 1)
==> default: Running 'pre-boot' VM customizations...
==> 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: password
default: Warning: Connection reset. Retrying...
default: Warning: Connection aborted. Retrying...
==> default: Machine booted and ready!
==> default: Checking for guest additions in VM...
==> default: Configuring and enabling network interfaces...
==> default: Mounting shared folders...
default: /vagrant => D:/
看到這段內(nèi)容時說明你的虛擬機已經(jīng)正常安裝啟動完畢了古掏!
6、連接虛擬機
如果是使用官方基礎鏡像的話一開始是連不上的侦啸,需要先用ssh命令進入修改配置后才能連接修改配置如下
1槽唾、修改sshd_config配置
進入虛擬機中,登錄的賬號密碼均為:vagrant
進入文件夾/etc/ssh光涂,修改配置文件sshd_config
cd /etc/ssh
vi sshd_config
重啟sshd.service服務
systemctl restart sshd.service
本地使用Xshell連接虛擬機庞萍,運行命令如下:
ssh 127.0.0.1 2200
在彈出的窗口,輸入用戶名和密碼就進入了虛擬機忘闻。ssh表示連接的命令钝计,127.0.0.1 2200可以從vagrant up的時候的輸出命令中找到。
7服赎、使用xshell工具連接虛擬機
也可以直接用官網(wǎng)的ssh命令直接連接葵蒂,不過在win用ssh命令連接后還是在cmd窗口交播,以后開發(fā)不太方便重虑,最好是用xshell工具連接。
賬號密碼默認直接都是:vagrant 即可秦士,(個人改成賬號 root登錄缺厉,密碼還是vagrant)
到此就結(jié)束了,之后等鏡像調(diào)整細節(jié)完畢后直接打包成自己的鏡像隧土,以后在其他電腦用或者本電腦虛擬機出問題的話提针,可以直接一鍵安裝(相當于還原)
具體打包自己box的方法我博客中也有教程介紹 http://tiance.club/post/299409888.html
作者:xingguang
鏈接:原文鏈接:https://www.tiance.club/post/2370098709.html