环境准备

  1. 下载vagrant和virtualbox,并安装
  2. 虚拟机配置

    安装步骤

  3. vagrant安装插件

    1. vagrant plugin install vagrant-hostmanager
  4. 创建vagrantfile所在文件夹,并创建两个文件Vagrantfile和init.sh

    1. VagrantFile
    2. init.sh
    3. ##其中VagrantFile是vagrant的启动配置文件, init.sh是初始环境的安装脚本
  5. 编辑VagrantFile文件

    1. Vagrant.configure("2") do |config|
    2. config.vm.define :master1, primary: true do |master|
    3. master.vm.provider "virtualbox" do |v|
    4. v.customize ["modifyvm", :id, "--name", "node", "--memory", "512"]
    5. end
    6. master.vm.box = "centos/7"
    7. master.vm.hostname = "node"
    8. master.vm.network :private_network, ip: "192.168.10.10"
    9. end
    10. (1..4).each do |i|
    11. config.vm.define "node#{i}" do |node|
    12. node.vm.box = "centos/7"
    13. node.vm.hostname = "node#{i}"
    14. node.vm.network :private_network, ip: "192.168.10.1#{i}"
    15. node.vm.provider "virtualbox" do |vb|
    16. vb.name="node#{i}"
    17. vb.memory = "1024"
    18. end
    19. end
    20. end
    21. #manage hosts file
    22. config.hostmanager.enabled = true
    23. config.hostmanager.manage_host = true
    24. config.hostmanager.manage_guest = true
    25. #provision
    26. config.vm.provision "shell", path: "init.sh", privileged: false
    27. end

    以上创建了5台虚拟机 node node1 node2 node3 node4

  6. 启动虚拟机, 验证配置是否正确

    1. vagrant up

    正常启动后,我们就可以使用以下命令登录到虚拟机了

    1. vagrant ssh node
  7. 编写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

  1. 编写完后,运行命令进行生效
  2. ```bash
  3. vagrant provision
  1. 设置ssh互信 vim /etc/ssh/sshd_config, 修改如下配置为yes
    1. PubkeyAuthentication yes
    2. PasswordAuthentication yes