版本
CentOS 7.6 5.1.2-1.el7.elrepo.x86_64
virtualbox 5.2.30
vagrant 2.1.0
安装VirtualBox
# cd /etc/yum.repos.d/
# wget http://download.virtualbox.org/virtualbox/rpm/rhel/virtualbox.repo
# yum install VirtualBox-5.2
# /sbin/rcvboxdrv setup
安装Vrgrant
# curl https://releases.hashicorp.com/vagrant/2.1.0/vagrant_2.1.0_x86_64.rpm -o
# vagrant_2.1.0_x86_64.rpm --progress
# rpm -ivh vagrant_2.1.0_x86_64.rpm
启动虚拟机配置文件
# Vagrant文件
Vagrant.configure("2") do |config|
(100..105).each do |i|
config.vm.define "node#{i}" do |node|
# 设置虚拟机的Box
node.vm.box = "centos7"
# 设置虚拟机的主机名
node.vm.hostname="node#{i}"
# 设置虚拟机的IP
node.vm.network "private_network", ip: "10.0.0.#{i}"
# 设置主机与虚拟机的共享目录
#node.vm.synced_folder "~/Desktop/share", "/home/vagrant/share"
# 配置端口映射
#node.vm.network "forwarded_port", guest: 88, host: 1234
# VirtaulBox相关配置
node.vm.provider "virtualbox" do |v|
# 设置虚拟机的名称
v.name = "node#{i}"
# 设置虚拟机的内存大小
v.memory = 4096
# 设置虚拟机的CPU个数
v.cpus = 2
end
end
end
end
# 在Vagrantfile所在目录运行下面命令,启动虚拟机
# vagrant up
Vagrant基本操作
# 启动虚拟机
# vagrant up
# 登录虚拟机
# vagrant ssh node1
# 停止虚拟机,不指定虚拟机,则停止所有虚拟机
# vagrant halt node1
# 销毁虚拟机,则销毁所有虚拟机
# vagrant destroy node1
注意
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-123.el7.x86_64
系统当前不能构建VirtualBox的内核模块,因为没有安装kernel-devel-3.10.0-123.el7.x86_64.rpm这个包,而安装的时候找不到该包,只能升级Linux,然后
安装相关依赖:
# yum install -y gcc make perl
安装相关库:
# yum install kernel-devel-$(uname -r)
再次运行命令:
# /sbin/vboxconfig
就可以构建VirtualBox内核模块,VirtualBox可以正常使用
- curl: (35) Peer reports incompatible or unsupported protocol version.
yum update curl
- 避免虚拟机占用根目录的磁盘,修改virtualbox相关配置
主要是修改配置文件,
/root/.config/VirtualBox/VirtualBox.xml
# 如迁移vm的保存位置
修改defaultMachineFolder为defaultMachineFolder="/home/shared/vm"
这个文件是vagrant up后产生的,不要自己先去创建,可以vagrant up,当创建虚拟机的时候,Ctrl+C停止,然后去修改此文件即可
- 避免Vagrant box占用根目录的磁盘,迁移vagrant家目录
# cp -r /root/.vagrant.d/* /home/shared/vagrant/
# vim /etc/profile
添加如下内容:
export VAGRANT_HOME='/home/shared/vagrant'
# source /etc/profile
完成vagrant家目录迁移
``
#### 参考
- Vagrant配置 https://github.com/astaxie/go-best-practice/blob/master/ebook/zh/01.2.md
- Vagrant官网 https://www.vagrantup.com/
- VirtualBox官网 https://www.virtualbox.org/wiki/Downloads
- Vagrant CentOS7 box下载地址 http://cloud.centos.org/centos/7/vagrant/x86_64/images/
网友评论