安装了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中。