Vagrantup.com 是虚拟机管理利器。
安装 virtualbox:
brew cask install virtualbox
brew cask virtualbox-extension-pack
brew cask install vagrant
如果用 brew 安装 virtualbox 失败,到 Download VirtualBox 下载。
到 vagrant boxes search 中查找操作系统镜像,然后用 vagrant box 下载,例如:
vagrant box add centos/7
创建一个目录,创建虚拟机:
mkdir centos & cd centos
vagrant init centos/7
启动:
vagrant up
进入虚拟机:
vagrant ssh
vagrant 支持在一个 vagrantfile 中配置多台虚拟机。先生成初始的 Vagrantfile,然后修改其内容,在 Vagrant.configure(“2”) do | config | 中用 config.vm.define 定义多台虚拟机。 |
vagrant init ubuntu/focal64
如下创举一个三台虚拟机的环境:
Vagrant.configure("2") do |config|
config.vm.define "node1" do |node1|
node1.vm.box="ubuntu/focal64"
node1.vm.network "private_network", ip: "192.168.33.11"
end
config.vm.define "node2" do |node1|
node1.vm.box="ubuntu/focal64"
node1.vm.network "private_network", ip: "192.168.33.12"
end
config.vm.define "node3" do |node1|
node1.vm.box="ubuntu/focal64"
node1.vm.network "private_network", ip: "192.168.33.13"
end
config.vm.define "node4" do |node1|
node1.vm.box="ubuntu/focal64"
node1.vm.network "private_network", ip: "192.168.33.14"
end
end
然后就可以一次启动 4 台虚拟机:
$ vagrant up
Bringing machine 'node1' up with 'virtualbox' provider...
Bringing machine 'node2' up with 'virtualbox' provider...
Bringing machine 'node3' up with 'virtualbox' provider...
Bringing machine 'node4' up with 'virtualbox' provider...