安裝了vagrant之后,當然需要將它投入使用。由於計划中的生產環境是 CentOS 7,因此想搭建一個 基於CentOS 7(x86_64)的虛擬開發環境。
最開始的一系列工作,比如在vbox中安裝CentOS 7、打包box文件、將box導入vagrant等都不在此一一細述,后面將有專文總結這些過程。
然后坑來了:修改Vagrantfile配置文件欲實現 public_network:
config.vm.network "public_network", ip: "192.168.0.6"
然后 vagrant up 啟動虛擬機:
$ vagrant up
==> default: Attempting graceful shutdown of VM...
==> 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: bridged
==> default: Forwarding ports...
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: Connection reset. Retrying...
C:/HashiCorp/Vagrant/embedded/gems/gems/net-ssh-4.1.0/lib/net/ssh/transport/server_version.rb:54:in `readpartial': An established connection was aborted by the software in your host machine. (Errno::ECONNABORTED)
from C:/HashiCorp/Vagrant/embedded/gems/gems/net-ssh-4.1.0/lib/net/ssh/transport/server_version.rb:54:in `block (2 levels) in negotiate!'
from C:/HashiCorp/Vagrant/embedded/gems/gems/net-ssh-4.1.0/lib/net/ssh/transport/server_version.rb:52:in `loop'
from C:/HashiCorp/Vagrant/embedded/gems/gems/net-ssh-4.1.0/lib/net/ssh/transport/server_version.rb:52:in `block in negotiate!'
from C:/HashiCorp/Vagrant/embedded/gems/gems/net-ssh-4.1.0/lib/net/ssh/transport/server_version.rb:50:in `loop'
from C:/HashiCorp/Vagrant/embedded/gems/gems/net-ssh-4.1.0/lib/net/ssh/transport/server_version.rb:50:in `negotiate!'
from C:/HashiCorp/Vagrant/embedded/gems/gems/net-ssh-4.1.0/lib/net/ssh/transport/server_version.rb:32:in `initialize'
from C:/HashiCorp/Vagrant/embedded/gems/gems/net-ssh-4.1.0/lib/net/ssh/transport/session.rb:84:in `new'
from C:/HashiCorp/Vagrant/embedded/gems/gems/net-ssh-4.1.0/lib/net/ssh/transport/session.rb:84:in `initialize'
from C:/HashiCorp/Vagrant/embedded/gems/gems/net-ssh-4.1.0/lib/net/ssh.rb:233:in `new'
from C:/HashiCorp/Vagrant/embedded/gems/gems/net-ssh-4.1.0/lib/net/ssh.rb:233:in `start'
from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.9.4/plugins/communicators/ssh/communicator.rb:397:in `block (2 levels) in connect'
from C:/HashiCorp/Vagrant/embedded/lib/ruby/2.2.0/timeout.rb:88:in `block in timeout'
from C:/HashiCorp/Vagrant/embedded/lib/ruby/2.2.0/timeout.rb:32:in `block in catch'
from C:/HashiCorp/Vagrant/embedded/lib/ruby/2.2.0/timeout.rb:32:in `catch'
from C:/HashiCorp/Vagrant/embedded/lib/ruby/2.2.0/timeout.rb:32:in `catch'
from C:/HashiCorp/Vagrant/embedded/lib/ruby/2.2.0/timeout.rb:103:in `timeout'
from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.9.4/plugins/communicators/ssh/communicator.rb:371:in `block in connect'
from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.9.4/lib/vagrant/util/retryable.rb:17:in `retryable'
from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.9.4/plugins/communicators/ssh/communicator.rb:370:in `connect'
from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.9.4/plugins/communicators/ssh/communicator.rb:68:in `block in wait_for_ready'
from C:/HashiCorp/Vagrant/embedded/lib/ruby/2.2.0/timeout.rb:88:in `block in timeout'
from C:/HashiCorp/Vagrant/embedded/lib/ruby/2.2.0/timeout.rb:32:in `block in catch'
from C:/HashiCorp/Vagrant/embedded/lib/ruby/2.2.0/timeout.rb:32:in `catch'
from C:/HashiCorp/Vagrant/embedded/lib/ruby/2.2.0/timeout.rb:32:in `catch'
from C:/HashiCorp/Vagrant/embedded/lib/ruby/2.2.0/timeout.rb:103:in `timeout'
from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.9.4/plugins/communicators/ssh/communicator.rb:46:in `wait_for_ready'
from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.9.4/lib/vagrant/action/builtin/wait_for_communicator.rb:16:in `block in call'
就這么出錯了。Guest已經啟動,可以通過 vagrant ssh 連接,但錯誤的結果導致不能在已啟動的 vagrant box 中共享主機文件,這可是個大問題。先后許多天,一直陷在這個巨坑里,從 vagrant 配置、插件,到 CentOS 7 Guest 的配置,想盡了各種辦法,始終無法解決這個問題。從網上搜集到的辦法主要是針對 Ubuntu Guest 的,依照做了之后都不起作用。
絕望之余,又想起了 vagrant plugin,有沒有一個插件能解決這個問題呢? 到 Ruby Gems去查找 Vagrant Centos,果然找到一個: vagrant-centos7_fix (version 0.0.1),描述為:Uses Fedora network scripts on CentOS 7.看來這是用來解決CentOS7網絡問題的啊。帶着希望安裝了這個插件:
$ vagrant plugin install vagrant-centos7_fix
但是並沒有能解決問題。再一細看,這個插件已經是2014年1月11日發布的,距今已三年多。期間 vagrant 的版本不斷升級,如果有什么問題的話應該早已經解決了。
但是這個插件的出現給我一個提示:是否CentOS 7原生地存在着在 vagrant 中的這些問題呢?帶着這個疑問,其后試驗性安裝 Ubuntu、Fedora等Linux發行版,都沒有任何問題。看來只有放棄了,換一種吧。由於CentOS與Fedora同出一門,因此就選用Fedora作為開發平台吧,畢竟是兄弟,使用上與CentOS差別不大。其后直接換用 Fedora 25 Server,順利編譯安裝配置了 MySQL 5.7.18、Apache 2.4.25、PHP 7.1.4,虛擬開發環境很快地搭建起來。
當然,如果堅持要使用 CentOS 7 作為 Vagrant 的Guest端的話,你必須從兩者中選擇一個:要么不使用 bridged network,要么不映射主機共享文件夾到Guest中。