Vagrant: multi-node virtual machine cluster construction

created at 07-12-2021 views: 6

I. Introduction

In software development, most of the operating environment of the program is your own computer, Windows, Mac or Linux operating system, which can run the software very well most of the time. However, there are many distributed applications that cannot be well simulated locally. For this type of application, a cluster is formed by multiple nodes in a production environment, and each node is deployed on a different machine. We generally do not buy a few hosts to deploy a private cloud in order to simulate a cluster, which is too costly. Therefore, I think that using a virtual machine is the most cost-effective choice, and the combination of Vagrant + VitualBox is the most convenient one of the virtual machines.

2. Stand-alone virtual machine construction

2.1 Install Vagrant & VirtualBox

After the download and installation are complete, run the command vagrant -v in the terminal, and a prompt similar to the following appears to indicate that the installation is successful.

Vagrant 2.2.3

2.2 Install Box

The concept of Box in Vagrant is equivalent to an operating system. Before using Vagrant to build a virtual machine, we need to add a system package to Vagrant. You can find various versions of the operating system under the official Vagrant Cloud website, with the Ubuntu 16.04 LTS version. For example, the operating system corresponds to the Box named ubuntu/xenial64.

Installation method one

We can directly add this box by running the following command. After running the command, Vagrant will download the corresponding box from the remote warehouse

~ vagrant box add ubuntu/xenial64

Installation method two

Although the above steps seem to be no problem, in the actual execution process, the download speed of Box is horribly slow, and it may take a few days to complete the download, so here you need to change the installation method.

First, run the following command first:

~ vagrant box add ubuntu/xenial64 

After running, the terminal will print out information similar to the following

~ vagrant box add ubuntu/xenial64
/opt/vagrant/embedded/gems/2.2.3/gems/vagrant-2.2.3/lib/vagrant/util/which.rb:37: warning: Insecure world writable dir /Users/meetmax/test in PATH, mode 040777
==> box: Loading metadata for box 'ubuntu/xenial64'
    box: URL: https://vagrantcloud.com/ubuntu/xenial64
==> box: Adding box 'ubuntu/xenial64' (v20190724.1.0) for provider: virtualbox
    box: Downloading: https://vagrantcloud.com/ubuntu/boxes/xenial64/versions/20190724.1.0/providers/virtualbox.box

Then, Ctrl + C interrupts the command. After Downloading is the download link, after copying the link, you can paste it directly into the browser or download it with Thunder. In the actual test, the speed will be much faster, and the download can be completed within a few minutes without accident.

After the download is complete, run the following command to add a local box

~ vagrant box add /your/path/virtualbox.box --name YourBoxName

Among them, change the path of virtualbox.box to your own local path. After --name is the name of Box, you can name it yourself. Under normal circumstances, it can be consistent with the official, that is, ubuntu/xenial64.

Verification

After the installation is complete, run the following command to verify that the installation is successful

~ vagrant box list

The terminal will print information similar to the following

laravel/homestead (virtualbox, 5.1.0)
ubuntu/xenial64   (virtualbox, 0)

Check if there is the Box you just installed.

2.3 Initialize Vagrant

After Box is installed, run the following command to initialize Vagrant

~ mkdir vagrant_test
~ cd vagrant_test
~ vagrant init ubuntu/xenial64

After the operation is completed, a Vagrantfile file will appear in the vagrant_test directory, and the terminal will print a message similar to the following.

A `Vagrantfile` has been placed in this directory. You are now
ready to `vagrant up` your first virtual environment! Please read
the comments in the Vagrantfile as well as documentation on
`vagrantup.com` for more information on using Vagrant.

The Vagrantfile file contains the configuration information related to the virtual machine. Here we will use the default configuration first and ignore it for now.

2.4 Start the virtual machine & SSH login

Build and start the virtual machine

After the initialization is complete, run the following commands in the vagrant_test directory to build and run the virtual machine

~ vagrant up

After a message similar to the following appears, the startup is successful

==> default: Machine booted and ready!
==> default: Checking for guest additions in VM...
    default: The guest additions on this VM do not match the installed version of
    default: VirtualBox! In most cases this is fine, but in rare cases it can
    default: prevent things such as shared folders from working properly. If you see
    default: shared folder errors, please make sure the guest additions within the
    default: virtual machine match the version of VirtualBox you have installed on
    default: your host and reload your VM.
    default:
    default: Guest Additions Version: 5.1.38
    default: VirtualBox Version: 6.0
==> default: Mounting shared folders...
    default: /vagrant => /Users/meetmax/vagrant/vagrant_test

4.2 SSH login
Using SSH in Vagrant is very simple, run the following command in the vagrant_test directory.

~ vagrant ssh

3 Multi-node virtual cluster construction

The above uses Vagrant's single-machine mode. Vagrant also supports multi-machine mode, that is, a single configuration starts multiple virtual machines, which can easily simulate a server cluster environment. The only difference between cluster and stand-alone mode is the Vagrantfile configuration file. In Vagrantfile, we create multiple virtual machines through loops.

3.1 Initialize Vagrant

Run the following command to initialize the cluster configuration file

~ mkdir vagrant_cluster
~ cd vagrant_cluster
~ vagrant init ubuntu/xenial64

3.2 Modify Vagrantfile

When building a virtual machine cluster, we need to modify the Vagrantfile accordingly. The configuration file is as follows.

Vagrant.configure("2") do |config|
   (1..4).each do |i|

      config.vm.define "node#{i}" do |node|

      node.vm.box = "ubuntu/xenial64"

      node.vm.hostname = "node#{i}"

      node.vm.network "private_network", ip: "192.168.60.#{10+i}"

      node.vm.synced_folder "/Users/meetmax", "/home/vagrant/code"

      node.vm.provider "virtualbox" do |v|


          v.name = "node#{i}"


          v.memory = 2048


          v.cpus = 1
      end
  end
end
end

The difference from single virtual machine creation is that cluster creation creates 4 virtual machine nodes through (1..4).each cycle.

3.3 Build and start the virtual machine cluster

The command to start the cluster is the same as the normal command. Running the following command will start all virtual machine nodes

~ vagrant up

To start a single node, you can run the following command

~ vagrant up node1

Among them, node1 is the node name. Start multiple virtual machines

~ vagrant up node1 node2

3.4 SSH login

Log in to a single node of a virtual machine

~ vagrant ssh node1

4 Commonly commands

  • Start the virtual machine: vagrant up
  • Log in to the virtual machine: vagrant ssh
  • Restart the virtual machine: vagrant reload
  • Shut down the virtual machine: vagrant halt
  • Destroy the virtual machine: vagrant destroy

summary

In the case of limited resources, a multi-node virtual machine cluster can help us simulate a distributed environment and perform various tests in virtual machines. Even if the system is damaged, it is easy to recover. Each virtual machine node is isolated. , There is no burden. Hope the above is helpful to you!

created at:07-12-2021
edited at: 07-12-2021: