vagrant-安裝教程及常見問題

前言:
Vagrant是一個基於Ruby的工具,用於創建和部署虛擬化開發環境。
它的主要意義是讓所有開發人員都使用和線上服務器一樣的環境,本質上和你新建一個虛擬機。

那最常見的,正常我們是怎麼開發呢,大部分童鞋應該是在windows下搭建開發環境,敲代碼,運行程序,達到效果git或svn提交,發佈linux環境再看效果。

而vagrant+virtualBox實現了把代碼同步共享到linux虛擬機,而這個虛擬機你可以配成和你生產環境一樣的,
說白了,通過共享文件到虛擬機,在類生產環境下運行。【讓你在windows下體驗到在linux開發的效果。
還有一種共享方式,可以藉助IDE的develop功能通過sftp上傳到服務器,然後訪問服務器
三種方式總結:
【1】原始windows開發模式:windows開發-本地訪問調試(與生產環境畢竟不同)-發佈到linux運行
【2】vagrant + virtualBox模式: windows開發 - 文件本地共享 -- 直接訪問虛擬機(linux環境與生產一致)
【3】IDE+develop 模式: windows開發 - 文件遠程上傳 -- 訪問遠程開發機(linux環境與生產一致)

其實【1】,與【2,3】的區別就在於,程序是在哪裏運行的,windows本地?or Linux仿生產環境
(能在仿生產的環境直接開發肯定比在windows開發在放到linux更好些,開發方便避免一些環境上等的麻煩)

【2】與【3】的區別就在於文件是如何同步的:【2】是通過虛擬機文件共享實現同步;【3】直接利用sftp遠程上傳實現同步。

缺點:
【1】毫無疑問,它的弊端就是開發時不能模擬生產環境,可能會有銜接問題,環境有了問題不像虛擬機重安一臺立刻搞定。
【3】存在的問題,比如:當切換開發分支後改動了文件a和b,當前ide選中的是a文件,ok他會自動上傳更新,但是b文件不會,因爲窗口你沒在b文件下呀,沒有那麼智能不會自動觸發上傳更新,這點就坑了造成代碼不同步,需要你自己手動觸發一下相關文件的上傳,尤其是依賴一些包的時候會發生丟失,得全項目上傳一次;
【2】因爲是本地和虛擬機的文件共享嘛,沒有文件上傳遺漏一說,所以還是很推薦用

目錄:
一。安裝虛擬機
二。Vagrantfile配置文件詳解
三。連接虛擬機
四。碰到問題


一。安裝虛擬機
1.添加box
vagrant box add base your_box_addres
注意:base是默認名稱,主要用來標識一下你添加的box,後面的命令都是基於這個標識來操作的,你也可以用其他名稱【但是用了其他名字記得在第二步用此名字init】

2.初始化
vagrant init
vagrant init box_name
如果你添加的box名稱不是[base],那麼需要在初始化的時候指定名稱

3.啓動虛擬機
vagrant up
啓動過程可能比較長,耐心等待

圖一.運行過程總圖

圖二
相關指令:
vagrant up (啓動虛擬機)
vagrant halt (關閉虛擬機——對應就是關機)
vagrant suspend (暫停虛擬機——只是暫停,虛擬機內存等信息將以狀態文件的方式保存在本地,可以執行恢復操作後繼續使用)
vagrant resume (恢復虛擬機—— 與前面的暫停相對應)
vagrant destroy (刪除虛擬機,刪除後在當前虛擬機所做進行的除開Vagrantfile中的配置都不會保留)
vagrant package --base vcode_default_1517562334868_37421 --output 0329.box//打包當前版本系統爲 box,方便給其他童鞋使用(vcode_default_1517562334868_37421 換成你自己的package)

二.Vagrantfile配置文件詳解
在我們的開發目錄下有一個文件Vagrantfile,裏面包含有大量的配置信息,主要包括三個方面的配置,虛擬機的配置、SSH配置、Vagrant的一些基礎配置。(它的配置語法也是Ruby的)【修改配置文件,記得完後重啓vagrant才能生效哦】


在我們的開發目錄下有一個文件Vagrantfile,裏面包含有大量的配置信息,主要包括三個方面的配置,虛擬機的配置、SSH配置、Vagrant的一些基礎配置。Vagrant是使用Ruby開發的,所以它的配置語法也是Ruby的,但是我們沒有學過Ruby的人還是可以跟着它的註釋知道怎麼配置一些基本項的配置。

具體介紹,參考:http://blog.csdn.net/chajinglong/article/details/52805915

# -*- mode: ruby -*-
# vi: set ft=ruby :

# All Vagrant configuration is done below. The "2" in Vagrant.configure
# configures the configuration version (we support older styles for
# backwards compatibility). Please don't change it unless you know what
# you're doing.
Vagrant.configure(2) do |config|
  # The most common configuration options are documented and commented below.
  # For a complete reference, please see the online documentation at
  # https://docs.vagrantup.com.

  # Every Vagrant development environment requires a box. You can search for
  # boxes at https://atlas.hashicorp.com/search.
  config.vm.box = "base"

  # Disable automatic box update checking. If you disable this, then
  # boxes will only be checked for updates when the user runs
  # `vagrant box outdated`. This is not recommended.
  # config.vm.box_check_update = false

  # Create a forwarded port mapping which allows access to a specific port
  # within the machine from a port on the host machine. In the example below,
  # accessing "localhost:8080" will access port 80 on the guest machine.
  # config.vm.network "forwarded_port", guest: 80, host: 80

  # Create a private network, which allows host-only access to the machine
  # using a specific IP.
  config.vm.network "private_network", ip: "192.168.33.10"

  # Create a public network, which generally matched to bridged network.
  # Bridged networks make the machine appear as another physical device on
  # your network.
  # config.vm.network "public_network"

  # Share an additional folder to the guest VM. The first argument is
  # the path on the host to the actual folder. The second argument is
  # the path on the guest to mount the folder. And the optional third
  # argument is a set of non-required options.
  config.vm.synced_folder "D:/all_code/", "/home/www"

  # Provider-specific configuration so you can fine-tune various
  # backing providers for Vagrant. These expose provider-specific options.
  # Example for VirtualBox:
  #
  # config.vm.provider "virtualbox" do |vb|
  #   # Display the VirtualBox GUI when booting the machine
  #   vb.gui = true
  #
  #   # Customize the amount of memory on the VM:
  #   vb.memory = "1024"
  # end
  #
  # View the documentation for the provider you are using for more
  # information on available options.

  # Define a Vagrant Push strategy for pushing to Atlas. Other push strategies
  # such as FTP and Heroku are also available. See the documentation at
  # https://docs.vagrantup.com/v2/push/atlas.html for more information.
  # config.push.define "atlas" do |push|
  #   push.app = "YOUR_ATLAS_USERNAME/YOUR_APPLICATION_NAME"
  # end

  # Enable provisioning with a shell script. Additional provisioners such as
  # Puppet, Chef, Ansible, Salt, and Docker are also available. Please see the
  # documentation for more information about their specific syntax and use.
  # config.vm.provision "shell", inline: <<-SHELL
  #   sudo apt-get update
  #   sudo apt-get install -y apache2
  # SHELL
end



三.連接虛擬機
1.虛擬機相關登錄信息
vagrant ssh
這樣我們就可以像連接到一臺服務器一樣進行操作了。

圖三

2.ssh登錄
window機器不支持這樣的命令,必須使用第三方客戶端來進行連接,例如xmoba、putty、Xshell等.
ssh: 127.0.0.1
端口: 2222
用戶名: vagrant
密碼: vagrant
圖四.xshell登陸舉例

圖五.xmoba登陸舉例

3.系統信息
df -h
/vagrant這個目錄是自動映射的,被映射到你剛剛建立的文件夾,這樣就方便我們以後在開發機中進行開發,在虛擬機中進行運行效果測試了。


四。碰到問題:
問題1.vagrant up 時提示錯誤 cound not open file 問題
如果init指定了 add的名稱test,那麼init時也要說明,少了這一步
如下邊錯誤演示與正確演示。
圖六.錯誤演示
圖七.正確演示

問題2.vagrant up 時提示錯誤:
The guest machine entered an invalid state while waiting for it
to boot. Valid states are 'starting, running'. The machine is in the
'poweroff' state. Please verify everything is configured
properly and try again.

If the provider you're using has a GUI that comes with it,
it is often helpful to open that and watch the machine, since the
GUI often has more helpful error messages than Vagrant can retrieve.
For example, if you're using VirtualBox, run `vagrant up` while the
VirtualBox GUI is open.

The primary issue for this error is that the provider you're using
is not properly configured. This is very rarely a Vagrant issue.
打開虛擬機啓動,也會提示報錯:
Unable to load R3 module D:\virtualBox/VBoxDD.DLL (VBoxDD): GetLastError=1790 (VERR_UNRESOLVED_ERROR).


原因:
本目錄中的以下三個文件是原始的未被破解的WIN7 64位系統主題文件:
themeservice.dll , themeui.dll ,uxtheme.dll

爲了使用工具UniversalThemePatcher-x64.exe恢復主題,特地將以上3個文件,
各拷貝了一份,並重新命名如下:
themeservice.dll.backup ,themeui.dll.backup ,uxtheme.dll.backup

解決方法:
我們只須把重命名後的3個文件:
themeservice.dll.backup ,themeui.dll.backup ,uxtheme.dll.backup
拷貝到C:\Windows\System32目錄下面,
然後運行工具UniversalThemePatcher-x64.exe進行恢復即可。


下載地址:http://download.csdn.net/download/ty_hf/10013443【裏邊有使用說明】

問題3.虛擬機一切正常,文件代碼也映射到虛擬機,但就是不能正常訪問。
a).配置文件需要刪除
sudo rm -f /etc/udev/rules.d/70-persistent-net.rules
問題就處在在持久網絡設備udev規則(persistent network device udev rules)是被原VM設置好的,再用box生成新VM時,這些rules需要被更新。而這和Vagrantfile裏對新VM設置private network的指令發生衝突。刪除就好了。

b).如果設置了host,虛擬機裏也要設置一次
vi /etc/hosts

問題3.問題 default: Warning: Authentication failure. Retrying...
Bringing machine 'default' up with 'virtualbox' provider...
==> 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: Adapter 2: hostonly
==> default: Forwarding ports...
    default: 22 (guest) => 2222 (host) (adapter 1)
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minute
    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: Warning: Authentication failure. Retrying...
    default: Warning: Authentication failure. Retrying...
    default: Warning: Authentication failure. Retrying...
 
Timed out while waiting for the machine to boot. This means that
Vagrant was unable to communicate with the guest machine within
the configured ("config.vm.boot_timeout" value) time period.

If you look above, you should be able to see the error(s) that
Vagrant had when attempting to connect to the machine. These errors
are usually good hints as to what may be wrong.

If you're using a custom box, make sure that networking is properly
working and you're able to connect to the machine. It is a common
problem that networking isn't setup properly in these boxes.
Verify that authentication configurations are also setup properly,
as well.

If the box appears to be booting properly, you may want to increase
the timeout ("config.vm.boot_timeout") value.
解決:
編輯Vagrantfile ,添加如下文件
config.ssh.username = "vagrant" 
config.ssh.password = "vagrant"

然後重啓vagrant 重新加載配置文件
vagrant halt
vagrant up
注意登陸的時候看下 vagrant ssh 看下你的登錄信息,端口號

問題4.報錯問題:
Bringing machine 'default' up with 'virtualbox' provider...
Your VM has become "inaccessible." Unfortunately, this is a critical error
with VirtualBox that Vagrant can not cleanly recover from. Please open VirtualBox
and clear out your inaccessible virtual machines or find a way to fix
 解決:
 http://doodlebobbers.com/vagrant-error-your-vm-has-become-inaccessible/
其實就是刪除.vagrant文件夾,然後重啓vagrant

問題5.vagrant 啓動的時候報錯
A Vagrant environment or target machine is required to run this
command. Run vagrant init to create a new Vagrant environment. Or,
get an ID of a target machine from vagrant global-status to run
this command on. A final option is to change to a directory with a
Vagrantfile and to try again.
少前邊步驟了,比如vagrant init;或者沒有進入對應vagrant init的文件夾

問題6.vagrant dns解析異常,訪問接口特別慢,在Vagrantfile 配置文件中添加下邊代碼:
config.vm.provider :virtualbox do |vb|
vb.customize ["modifyvm", :id, "--natdnshostresolver1", "on"]
vb.customize ["modifyvm", :id, "--natdnsproxy1", "on"]
問題7.vagrant up 又提示新報錯!
D:\vcode>vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
Your VM has become "inaccessible." Unfortunately, this is a critical error
with VirtualBox that Vagrant can not cleanly recover from. Please open VirtualBo
x
and clear out your inaccessible virtual machines or find a way to fix
them.

問題8:C盤空間緊張
https://jingyan.baidu.com/article/ab0b563088c0adc15afa7db2.html


問題9:Centos 配置eth0 提示Device does not seem to be present

https://blog.csdn.net/xiaobei4929/article/details/40515247

相關資源:
虛擬機等相關安裝包: 鏈接:http://pan.baidu.com/s/1dEWSnEL 密碼:ajaq


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章