环境准备
- 下载vagrant和virtualbox,并安装
-
安装步骤
vagrant安装插件
vagrant plugin install vagrant-hostmanager
创建vagrantfile所在文件夹,并创建两个文件Vagrantfile和init.sh
VagrantFile
init.sh
##其中VagrantFile是vagrant的启动配置文件, init.sh是初始环境的安装脚本
编辑VagrantFile文件
Vagrant.configure("2") do |config|
config.vm.define :master1, primary: true do |master|
master.vm.provider "virtualbox" do |v|
v.customize ["modifyvm", :id, "--name", "node", "--memory", "512"]
end
master.vm.box = "centos/7"
master.vm.hostname = "node"
master.vm.network :private_network, ip: "192.168.10.10"
end
(1..4).each do |i|
config.vm.define "node#{i}" do |node|
node.vm.box = "centos/7"
node.vm.hostname = "node#{i}"
node.vm.network :private_network, ip: "192.168.10.1#{i}"
node.vm.provider "virtualbox" do |vb|
vb.name="node#{i}"
vb.memory = "1024"
end
end
end
#manage hosts file
config.hostmanager.enabled = true
config.hostmanager.manage_host = true
config.hostmanager.manage_guest = true
#provision
config.vm.provision "shell", path: "init.sh", privileged: false
end
以上创建了5台虚拟机 node node1 node2 node3 node4
启动虚拟机, 验证配置是否正确
vagrant up
正常启动后,我们就可以使用以下命令登录到虚拟机了
vagrant ssh node
编写provision文件 provision的作用是帮助我们进行主机环境的初始化工作,编写init.sh ```bash sudo yum install -y epel-release
sudo yum install -y lrzsz.x86_64 sudo yum install -y nmap-ncat.x86_64 sudo yum install -y net-tools sudo yum install -y vim-enhanced.x86_64 sudo yum install -y sshpass suod yum install ntp -y
编写完后,运行命令进行生效
```bash
vagrant provision
- 设置ssh互信 vim /etc/ssh/sshd_config, 修改如下配置为yes
PubkeyAuthentication yes
PasswordAuthentication yes