vagrant配置多机多节点


首先注意:在配置之前先把你的原来的虚拟机打包成box备份。

否则你可能面临惨痛的代价。


下一步,修改配置文件:

# All Vagrant configuration is done below. The "2" in Vagrant.configure
# configures the configuration version (we support older styles for
# backwards compatibility). Please don't change it unless you know what
# you're doing.
Vagrant.configure("2") do |config|
  # The most common configuration options are documented and commented below.
  # For a complete reference, please see the online documentation at
  # https://docs.vagrantup.com.


  # Every Vagrant development environment requires a box. You can search for
  # boxes at https://atlas.hashicorp.com/search.
  config.vm.box = "ubuntu"


  # Disable automatic box update checking. If you disable this, then
  # boxes will only be checked for updates when the user runs
  # `vagrant box outdated`. This is not recommended.
  # config.vm.box_check_update = false


  # Create a forwarded port mapping which allows access to a specific port
  # within the machine from a port on the host machine. In the example below,
  # accessing "localhost:8080" will access port 80 on the guest machine.
  # config.vm.network "forwarded_port", guest: 80, host: 8080


  # Create a private network, which allows host-only access to the machine
  # using a specific IP.
  
  config.vm.define "slave1" do |slave1|
     config.vm.network "private_network", ip: "192.168.33.10"
  end
  
  config.vm.define "slave2" do |slave2|
     config.vm.network "private_network", ip: "192.168.33.20"
  end

 

只用改上面蓝色的几处就可以了。config.vm.box不会是你把你的虚拟机打包后的自己随意命名的文件名,而是你那个box本来的名字。

如果你大部分启动只启动master,你可以把slave的box和配置文件和master的放在不同目录。


vagrant配置多机多节点_第1张图片



UPDATE:

最新方法;支持共享目录,不过要注意如果是自己的box,要配置密码。


Vagrant.configure("2") do |config|
(1..4).each do |i|
config.ssh.username = "vagrant"
config.ssh.password = "vagrant"
config.vm.define "node#{i}" do |node|
# 设置虚拟机的Box
node.vm.box = "ubuntu2"
# 设置虚拟机的主机名
node.vm.hostname="node#{i}"
# 设置虚拟机的IP
node.vm.network "private_network", ip: "192.168.33.#{i+3}"
# 设置主机与虚拟机的共享目录
node.vm.synced_folder "/share", "/home/vagrant/share"
# VirtaulBox相关配置
node.vm.provider "virtualbox" do |v|
# 设置虚拟机的名称
v.name = "node#{i}"
# 设置虚拟机的内存大小  
v.memory = 2048
# 设置虚拟机的CPU个数
v.cpus = 1
end
end
end


上面方法的最新参考:http://kiwenlau.com/2016/07/03/vagrant-vm-cluster/,感谢这位大神。

你可能感兴趣的:(运维)