在做 集群实验环境时候需要用到创建一个虚拟机集群,比如做 Redis集群、Hadoop、Spark 分布式系统运行环境。我们可以使用 Vagrant 创建多个虚拟机集群环境。
安装软件
- VirtualBox
- Vagrant
- Vagrant Manager
下载Box
vagrant box add ubuntu/trusty64
vagrant box add centos7 ./centos7.box
安装插件
vagrant plugin uninstall vagrant-vbguest
vagrant plugin install vagrant-vbguest --plugin-version 0.21
编辑Vagrantfile 文件
mkdir vagrant-cluster
cd vagrant-cluster
vi Vagrantfile
Vagrantfile 内容如下,可以通过注释理解每个自定义配置含义:
$script = <<-SCRIPT
cat>/home/vagrant/.profile<<EOF
export JAVA_HOME=/vagrant/share/java8
export GRADLE_HOME=/vagrant/share/gradle6
export REDIS_HOME=/vagrant/share/redis6
export SPRING_CLI_HOME=/vagrant/share/spring-2.4.0
export PATH=\\$PATH:\\$JAVA_HOME/bin:\\$REDIS_HOME/bin:\\$GRADLE_HOME/bin:\\$SPRING_CLI_HOME/bin
EOF
if ! grep -q "/.profile" /home/vagrant/.bashrc; then
echo "source /home/vagrant/.profile" >> /home/vagrant/.bashrc
source /home/vagrant/.bashrc
fi
SCRIPT
Vagrant.configure("2") do |config|
(1..3).each do |i|
config.vm.boot_timeout = 300
config.vm.define "node#{i}" do |node|
# 设置虚拟机的Box
node.vm.box = "centos7"
# 设置虚拟机的主机名
node.vm.hostname="node#{i}"
# 设置虚拟机的IP
node.vm.network "private_network", ip: "192.168.59.#{i}"
# 设置主机与虚拟机的共享目录
config.vm.synced_folder "/Users/lifei/Desktop/study/share", "/vagrant/share"
# VirtaulBox相关配置
node.vm.provider "virtualbox" do |v|
# 设置虚拟机的名称
v.name = "node#{i}"
# 设置虚拟机的内存大小
v.memory = 1024
# 设置虚拟机的CPU个数
v.cpus = 1
end
# 使用shell脚本进行软件安装和配置
node.vm.provision "shell", inline: $script
end
end
end
启动vagrant 集群
vagrant up
制作 vagrant 镜像
安装依赖
sudo yum -y install cpp binutils glibc glibc-kernheaders glibc-common glibc-devel gcc make
sudo yum -y install centos-release-scl devtoolset-9-gcc devtoolset-9-gcc-c++ devtoolset-9-binutils
sudo scl enable devtoolset-9 bash
设置国内镜像源码
yum repolist
cd /etc/yum.repos.d
sudo mv CentOS-Base.repo CentOS-Base.repo.bak
sudo wget -O /etc/yum.repos.d/epel.repo http://mirrors.163.com/.help/CentOS7-Base-163.repo
yum clean all
yum makecache
yum repolist
设置vagrant认证
sudo -u vagrant wget https://raw.githubusercontent.com/mitchellh/vagrant/master/keys/vagrant.pub -O /home/vagrant/.ssh/authorized_keys
chmod go-w /home/vagrant/.ssh/authorized_keys
制作镜像
vagrant halt
vboxmanage list vms
vagrant package --base node1 --output ./centos7.box
vagrant box add centos7 ./centos7.box
参考:
https://kiwenlau.com/2016/07/03/vagrant-vm-cluster/
https://blog.csdn.net/qq_27068845/article/details/80936081
https://blog.csdn.net/lianghecai52171314/article/details/110670264