最近两天,在倒腾一些新东西,基本使用docker,偶然间,我发现了Vagrant,觉得也不错,可以一试。尽管Vagrant已经是很早就有的了,但是一直都没怎么用,基本上都是vmare和virtualbox居多。想试试Vagrant是因为它和docker compose有许多相似的地方。如图:图片来源:https://www.yuque.com/wukong-zorrm/xmk0v0/iaw0c8knokwhrqtw

由图可见,Vagrant和docker compose很像,在win环境据说他们是宿敌,要安装vagrant还必须卸载docker-desktop。看着很像,我开始了折腾。
折腾过程
首先是在我们的Mac(m1)中安装:
brew tap hashicorp/tap brew install hashicorp/tap/hashicorp-vagrant
接下来,定义一个vagrantfile文件,准备拉一个cento7环境
Vagrant.configure("2") do |config| config.vm.box = "centos/7" end
执行
vagrant up
提示没有插件,然后安装插件,我的Mac(m1)使用的是vmware fusion
vagrant plugin install vagrant-vmware-desktop
再次启动,提示如下:
Vagrant encountered an error while attempting to load the utility service key file. This error can occur if the Vagrant VMware Utility has not yet been installed, or if it was installed incorrectly. If this error persists after running the Vagrant VMware Utility installer again, please open a new issue at: https://github.com/hashicorp/vagrant-vmware-desktop/issues Information about the Vagrant VMware Utility, including installation instruction, can be found here: https://www.vagrantup.com/docs/vmware/vagrant-vmware-utility.html Path: /opt/vagrant-vmware-desktop/certificates/vagrant-utility.client.crt Error: No such file or directory @ rb_sysopen - /opt/vagrant-vmware-desktop/certificates/vagrant-utility.client.crt
大概意思就是我的vmvare fusion不完整(懂得都懂)。
我想Mac (m1)不行,那我试试云主机里面行不行。然后又是一顿操作:
# 配置vagrant安装源
yum install -y yum-utils
yum-config-manager --add-repo https://rpm.releases.hashicorp.com/RHEL/hashicorp.repo
yum -y install vagrant
# 其他依赖
yum -y install libGL libICE libSM libXcursor libXmu libXrender libXt libpng libvpx SDL fontconfig opus
yum -y install gcc gcc-c++ kernel-devel
# 下载、安装virtualbox
wget https://download.virtualbox.org/virtualbox/7.0.18/VirtualBox-7.0-7.0.18_162988_el7-1.x86_64.rpm
yum -y localinstall VirtualBox-7.0-7.0.18_162988_el7-1.x86_64.rpm
定义vagrantfile文件
Vagrant.configure("2") do |config| config.vm.box = "centos/7" end
执行
vagrant up Bringing machine 'default' up with 'virtualbox' provider... ==> default: Checking if box 'centos/7' version '2004.01' is up to date... ==> 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: Forwarding ports... default: 22 (guest) => 2222 (host) (adapter 1) ==> default: Booting VM... There was an error while executing `VBoxManage`, a CLI used by Vagrant for controlling VirtualBox. The command and stderr is shown below. Command: ["startvm", "0c539ee3-43ec-4af0-8e80-6f4aa76adf02", "--type", "headless"] Stderr: VBoxManage: error: VT-x is not available (VERR_VMX_NO_VMX) VBoxManage: error: Details: code NS_ERROR_FAILURE (0x80004005), component ConsoleWrap, interface IConsole
看结果是因为VT-x无效,大概是因为云主机也是从物理服务器虚拟出来的原因吧。
看着这里,我又在自己的UTM(可以在m1芯片上虚拟出x86机器)虚拟机中再试试,虚拟环境也是centos7,安装步骤同上面的云主机环境。
执行vagrant up提示如下:
[root@myserver ~]# vagrant up No usable default provider could be found for your system. Vagrant relies on interactions with 3rd party systems, known as "providers", to provide Vagrant with resources to run development environments. Examples are VirtualBox, VMware, Hyper-V. The easiest solution to this message is to install VirtualBox, which is available for free on all major platforms. If you believe you already have a provider available, make sure it is properly installed and configured. You can see more details about why a particular provider isn't working by forcing usage with `vagrant up --provider=PROVIDER`, which should give you a more specific error message for that particular provider.
按提示再次执行:
[root@myserver ~]# vagrant up --provider=virtualbox The provider 'virtualbox' that was requested to back the machine 'default' is reporting that it isn't usable on this system. The reason is shown below: VirtualBox is complaining that the kernel module is not loaded. Please run `VBoxManage --version` or open the VirtualBox GUI to see the error message which should contain instructions on how to fix this error.
跟着提示
[root@myserver ~]# VBoxManage --version WARNING: The vboxdrv kernel module is not loaded. Either there is no module available for the current kernel (3.10.0-1160.el7.x86_64) or it failed to load. Please recompile the kernel module and install it by sudo /sbin/vboxconfig You will not be able to start VMs until this problem is fixed. 7.0.18r162988
再跟
[root@myserver ~]# /sbin/vboxconfig vboxdrv.sh: Stopping VirtualBox services. vboxdrv.sh: Starting VirtualBox services. vboxdrv.sh: Building VirtualBox kernel modules. This system is currently not set up to build kernel modules. Please install the Linux kernel "header" files matching the current kernel for adding new hardware support to the system. The distribution packages containing the headers are probably: kernel-devel kernel-devel-3.10.0-1160.el7.x86_64 This system is currently not set up to build kernel modules. Please install the Linux kernel "header" files matching the current kernel for adding new hardware support to the system. The distribution packages containing the headers are probably: kernel-devel kernel-devel-3.10.0-1160.el7.x86_64 There were problems setting up VirtualBox. To re-start the set-up process, run /sbin/vboxconfig as root. If your system is using EFI Secure Boot you may need to sign the kernel modules (vboxdrv, vboxnetflt, vboxnetadp, vboxpci) before you can load them. Please see your Linux system's documentation for more information.
提示需要安装header文件构建内核模块
yum install -y kernel-devel kernel-devel-3.10.0-1160.el7.x86_64
然后重新加载内核模块
[root@myserver ~]# vboxconfig vboxdrv.sh: Stopping VirtualBox services. vboxdrv.sh: Starting VirtualBox services. vboxdrv.sh: Building VirtualBox kernel modules.
再尝试使用vagrant拉起一个centos7环境
[root@myserver ~]# vagrant up --provider=virtualbox
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Box 'centos/7' could not be found. Attempting to find and install...
default: Box Provider: virtualbox
default: Box Version: >= 0
==> default: Loading metadata for box 'centos/7'
default: URL: https://vagrantcloud.com/api/v2/vagrant/centos/7
==> default: Adding box 'centos/7' (v2004.01) for provider: virtualbox
default: Downloading: https://vagrantcloud.com/centos/boxes/7/versions/2004.01/providers/virtualbox/unknown/vagrant.box
Download redirected to host: cloud.centos.org
An error occurred while downloading the remote file. The error
message, if any, is reproduced below. Please fix this error and try
again.
# 这里遇到网络问题,重新尝试
[root@myserver ~]# vagrant up --provider=virtualbox
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Box 'centos/7' could not be found. Attempting to find and install...
default: Box Provider: virtualbox
default: Box Version: >= 0
==> default: Loading metadata for box 'centos/7'
default: URL: https://vagrantcloud.com/api/v2/vagrant/centos/7
==> default: Adding box 'centos/7' (v2004.01) for provider: virtualbox
default: Downloading: https://vagrantcloud.com/centos/boxes/7/versions/2004.01/providers/virtualbox/unknown/vagrant.box
==> default: Box download is resuming from prior download progress
Download redirected to host: cloud.centos.org
default: Calculating and comparing box checksum...
==> default: Successfully added box 'centos/7' (v2004.01) for 'virtualbox'!
"rsync" could not be found on your PATH. Make sure that rsync
is properly installed on your system and available on the PATH.
根据提示安装rsync,继续尝试
[root@myserver ~]# yum install -y rsync [root@myserver ~]# vagrant up --provider=virtualbox Bringing machine 'default' up with 'virtualbox' provider... ==> default: Importing base box 'centos/7'... ==> default: Matching MAC address for NAT networking... ==> default: Checking if box 'centos/7' version '2004.01' is up to date... ==> default: Setting the name of the VM: root_default_1720583809845_50839 ==> default: Clearing any previously set network interfaces... ==> default: Preparing network interfaces based on configuration... default: Adapter 1: nat ==> 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 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.
使用centos7的box卡住,以为是box问题,更换一个,继续尝试
[root@myserver ~]# vagrant init generic/centos7 [root@myserver ~]# vagrant up --provider=virtualbox Bringing machine 'default' up with 'virtualbox' provider... ==> default: Importing base box 'generic/centos7'... ==> default: Matching MAC address for NAT networking... ==> default: Checking if box 'generic/centos7' version '4.3.12' is up to date... ==> default: Setting the name of the VM: root_default_1720591997735_20920 ==> default: Clearing any previously set network interfaces... ==> default: Preparing network interfaces based on configuration... default: Adapter 1: nat ==> 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
到这里就卡住不动了,各种百度,最后还是没有解决,到这里已经折腾了大半天,体验感真的很差,直接放弃了,最后,我还是选择docker。
总结
对比docker的使用,我个人总结如下:
1、vagrant对底层硬件强依赖,兼容性差。
2、vagrant需要的资源较多,灵活性也没有docker好。
3、vagrant安装过程太多不确定因素。
4、vagrant拉取box时对网络不稳定,box少,基本不更新。
5、推荐在windows环境下使用vagrant,mac和linux还是老老实实用docker。
最后,个人觉得不要在这种搭建环境上面去浪费时间,真的没什么价值,有docker为什么用vagrant。




