Vagrant简介

本文环境基于macOS

Virtualbox

brew cask install virtualbox
# brew cask reinstall virtualbox

Vagrant

brew cask install vagrant
# brew cask reinstall vagrant

vagrant -v
# 2.2.5

Box

vagrant box add ubuntu/xenial64

vagrant box list
# ubuntu/xenial64 (virtualbox, 20191024.0.0)

Vagrantfile

mkdir playbook && cd playbook

vagrant init ubuntu/xenial64

vim Vagrantfile
Vagrant.configure("2") do |config|
    config.vm.box = "ubuntu/xenial64"
    config.vm.host_name = "node1"
    config.vm.network "private_network", ip: "192.168.56.100"
    config.vm.provider "virtualbox" do |vb|
        vb.name = "node1"
        vb.memory = "3000"
        vb.cpus = "2" 
    end
    config.vm.provision "file", source: "~/.ssh/id_rsa.pub", destination: "~/.ssh/id_rsa.pub"
    config.vm.provision "shell", inline: <<-SHELL
        cat /home/vagrant/.ssh/id_rsa.pub >> /home/vagrant/.ssh/authorized_keys
    SHELL
end
vagrant up

vagrant ssh
# ssh [email protected]

vagrant destroy

Cluster

vim Vagrantfile
VAGRANTFILE_API_VERSION = "2"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
    (1..2).each do |i|
        config.vm.define "node#{i}" do |node|
            node.vm.box = "ubuntu/xenial64"
            node.vm.host_name = "node#{i}"
            node.vm.network "private_network", ip: "192.168.56.#{100+i}"

            node.vm.provider "virtualbox" do |vb|
                vb.name = "node#{i}"
                vb.memory = "1500"
                vb.cpus = "1"
            end

            node.vm.provision "file", source: "~/.ssh/id_rsa.pub", destination: "~/.ssh/id_rsa.pub"
            node.vm.provision "shell", inline: <<-SHELL
                cat /home/vagrant/.ssh/id_rsa.pub >> /home/vagrant/.ssh/authorized_keys
            SHELL
        end
    end
end
vagrant up

vagrant ssh node1
# ssh [email protected]

vagrant ssh node2
# ssh [email protected]

vagrant destroy

参考

  • Vagrant Documentation

  • Vagrant—多节点虚拟机集群搭建

  • How do I add my own public key to Vagrant VM?

你可能感兴趣的:(Vagrant简介)