大梦千秋

大梦千秋

Building a virtual cluster using Vagrant

What is Vagrant?#

Vagrant is a tool for managing virtual machine environments.

Taking VirtualBox as an example, VirtualBox provides an interface for creating virtual machines, and Vagrant utilizes this interface to create, manage, configure, and automatically install virtual machines.

Installing Virtual Machines#

VirtualBox#

VirtualBox
Since Vagrant does not provide virtual machine functionality itself, you need to install one. Vagrant supports multiple virtual machines (VirtualBox, Hyper-V, VMware). Here, I have chosen VirtualBox. Download link.

Since we are using Vagrant to manage virtual machines, there is no need to configure VirtualBox. The default installation is sufficient. I only modified the storage location of the virtual machine (default is on the C drive). After installation, open VirtualBox -> Manage -> Preferences -> General -> Default Machine Folder, and change it to your preferred location. After saving, close the window. You will not need to use it much afterwards.

Vagrant#

After configuring VirtualBox, you can now install Vagrant. Download link. Here, I have chosen the Windows 64-bit version. The installation process is simple. After installation, you need to restart your computer. Before restarting, you can set an environment variable by running setx.exe VAGRANT_HOME "D:/dev/vbox/boxes" in the command line (replace the path in double quotes with your preferred location). Otherwise, Vagrant will download the images on the C drive.

Running Virtual Machines#

After restarting your computer, you can start preparing the runtime environment. First, open the command line and run vagrant box add centos/7 to download the CentOS 7 image.

The download may be slow, so let it run. Then, create a folder in a location of your choice and create a Vagrantfile inside it. Open the file for editing and enter the following content:

# Define three virtual machines as a cluster, namely c1, c2, c3
Vagrant.configure("2") do |config|
  # Define a c1 virtual machine
  config.vm.define "c1" do |c1|
    # Define the hostname of c1 virtual machine
    c1.vm.hostname = "centos-1"
    # Use the centos/7 image
    c1.vm.box = "centos/7"
    # Define network configuration, fill in the bridge with the name of your network card, you can check your network configuration
    c1.vm.network "public_network", bridge: "Intel(R) Wi-Fi 6 AX200 160MHz"
    c1.vm.provider "virtualbox" do |vb|
      # Configure virtual machine parameters
      vb.memory = "2048"
      vb.cpus = 2
      vb.name = "k3s-1"
    end
  end
  config.vm.define "c2" do |c2|
    c2.vm.hostname = "centos-2"
    c2.vm.box = "centos/7"
    c2.vm.network "public_network", bridge: "Intel(R) Wi-Fi 6 AX200 160MHz"
    c2.vm.provider "virtualbox" do |vb|
      vb.memory = "2048"
      vb.cpus = 2
      vb.name = "k3s-2"
    end
  end
  config.vm.define "c3" do |c3|
    c3.vm.hostname = "centos-3"
    c3.vm.box = "centos/7"
    c3.vm.network "public_network", bridge: "Intel(R) Wi-Fi 6 AX200 160MHz"
    c3.vm.provider "virtualbox" do |vb|
      vb.memory = "2048"
      vb.cpus = 2
      vb.name = "k3s-3"
    end
  end
end

Here, I have only configured what I need. You can check the official documentation for other configuration options.

After the image download is complete, switch the command line path to the location of the Vagrantfile and run vagrant up. Vagrant will start the three virtual machines in order. If the ports are not occupied by other programs, the SSH ports for the three virtual machines should be 2222, 2200, and 2201. You can refer to the screen output log, where you will find a line saying SSH address: 127.0.0.1:2200.

In normal circumstances, all three virtual machines should start successfully. However, I encountered an abnormal situation: the log got stuck at SSH auth method: private key and timed out after a few minutes. This is not a big problem. For example, if it gets stuck when starting c2, you just need to run vagrant halt c2, vagrant destroy c2, and vagrant up c2 in order to recreate the virtual machine. If c3 has not started yet, you can run vagrant up or vagrant up c3 at the end.

After successful startup, you can use vagrant ssh c1, vagrant ssh c2, and vagrant ssh c3 to connect to the three virtual machines respectively. Here, I have configured passwordless login for the three virtual machines, so I switched to Xshell for operation.

Now we have a cluster of three servers. If the configuration is sufficient, you can add more. Virtual machines can be freely manipulated as long as the Vagrantfile is present. Just run vagrant up to rebuild.

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.