Install Vgrant

Vagrant is a cross-platform virtual machine management tool. We used Deepin 20.2.3 as an example to install and use Vagrant. The Vagrant we are talking about here includes both the Vagrant tool itself and the virtual engine tool VirtualBox.

1. Install the Vagrant

When installing Vagrant, the VirtualBox version used must be supported by the corresponding version of Vagrant, and at the time of writing this document, I have installed Version 2.2.18 for Vagrant and the corresponding version of VirtualBox is 6.1.26.

  • Vagrant installation package download address: releases.hashicorp.com/vagrant/, here can see the Vagrant latest version as well as the current version of history, we can choose to the corresponding corresponding version of the operating system to download;
  • VirtualBox installation package download address: www.virtualbox.org/wiki/Downlo… Also, you can find the corresponding version of VirtualBox here.

After downloading the installation packages specified by Vagrant and VirtualBox locally, execute the installation commands as follows

#Install the VagrantSudo apt the/vagrant_2. 2.18 _x86_64. Deb#Install VirtualBoxSudo apt install. / virtualbox - 6.1 _6. 1.26 145957 _debian_buster_amd64. DebCopy the code

2. Verify the installation

Verify: Run the following command

#Create the Vagrant project path
mkdir ~/my-vagrant-project
#Example initialize the centos8 vm
vagrant init centos/8
#Starting a VM
vagrant up
#Stopping a VM
vagrant halt
#Deleting a VM
vagrant destroy
Copy the code

If the above steps can be successfully performed, the Vagrant installation is successful

2. The use of Vagrant

1. Install the box

We can think of boxes in Vgarnt as an image file of some operating system, or we can think of them as the virtual machine itself, and we can add the boxes we want, Vagrant can manage those boxes, and we can choose the boxes we want when we start the virtual machine.

We can through app.vagrantup.com/boxes/searc… The box name format is A/B, where A represents the user name that created the box, and B represents the name of the box, such as centos/7.

  • Installs the specified box
vagrant box add centos/7
Copy the code

The following selection prompt is displayed

pan@pan-PC:~$ vagrant box add centos/7 ==> box: Loading metadata for box 'centos/7' box: URL: https://vagrantcloud.com/centos/7 This box can work with multiple providers! The providers that it can work with are listed below. Please review the list and choose the provider you will be working  with. 1) hyperv 2) libvirt 3) virtualbox 4) vmware_desktop Enter your choice:Copy the code

The virtual machine engine we use is VirtualBox, so enter 3 and press Enter to download virtualBox centos/7.

  • Query the installed box
vagrant box list
Copy the code

2. Basic VM usage

  • Initialize the VM
#Create an empty project directory
mkdir ~/test
#Enter the project directory
cd ~/test
#Initialize the Vagrant project with centos/7
vagrant init centos/7
Copy the code

Vagrantfile is a configuration file that is generated in the vagrant project directory. Vagrantfile can be modified to define the virtual machine, and all subsequent operations will be performed in the Vagrant project directory

  • Starting a VM
vagrant up
Copy the code

When the virtual machine starts, the local project directory is automatically mounted to the/Vagrant directory in the virtual machine

  • Connecting a VM
#The vagrant user is used to connect to the virtual machine by default
vagrant ssh
Copy the code

Vagrant ssh-config can also be used to view SSH configuration information, including port mapping between virtual machine and local, certificate files, etc., as shown below

pan@pan-PC:~/Work/vagrant/centos$vagrant ssh-config Host default HostName 127.0.0.1 User Vagrant Port 2222 UserKnownHostsFile /dev/null StrictHostKeyChecking no PasswordAuthentication no IdentityFile /home/pan/Work/vagrant/centos/.vagrant/machines/default/virtualbox/private_key IdentitiesOnly yes LogLevel FATALCopy the code

You can also use SSH to connect to the VM

SSH [email protected] - p i. vagrant/those/default/virtualbox/private_keyCopy the code

The default password for a vagrant user is vagrant

parameter

-p: specifies the SSH port

-i: Specifies the SSH private key file

  • View the VM status
vagrant status
Copy the code
  • Vm Shutdown
vagrant halt
Copy the code
  • Suspending a VM
#Start the VM again
vagrant up
#Exiting a VM
exit
#Suspending a VM
vagrant suspend
#Continue to run the VM
vagrant resume
Copy the code

Vagrant will store the data to our local hard drive after the VIRTUAL machine is paused and will read and recover from the hard drive the next time it is started. When a VM is paused, the status of the HTTPD service is saved. For example, if the HTTPD service is running after the VM is paused, the HTTPD service also continues

  • Restart a VM.

In the project directory, perform Vagrant Reload and the virtual machine will be powered off first and then powered on.

  • Destroy the current VM
vagrant destory
Copy the code

Destroying a VM will shut down the VM and delete the instance. If the VM is in the stopped state, the instance will be deleted. This command will only destroy the virtual machine we created in the current directory, not the box. At this point, we use the Vagrant status command to check the vm status and see that the VM is not created.

Vagrantfile is used to define the virtual machine

1. Add additional shared directories

In fact, vagrant automatically mounts the project directory to the/Vagrant directory in the VIRTUAL machine when the virtual machine is created. In fact, we can also mount other directories by defining vagrant files.

Open Vagrantfile and find the # config.vm.synced_folder “.. /data”, “/vagrant_data”, remove the “# “, which defines a directory mount rule. The first parameter represents the data directory in the parent directory of the current directory of the host. The second parameter represents the /vagrant_data directory mounted to the VIRTUAL machine. We can also add the following parameters, resulting in the following code

config.vm.synced_folder ".. /data"."/vagrant_data".create: true.owner: "root".group: "root"
Copy the code

Create: true: indicates that a directory will be created if no corresponding directory exists on the host.

Owner: “root” : indicates that the owner of the directory mounted to the VM is root.

Group: “root” : indicates that the directory is mounted to the VM and belongs to the root group.

After saving the Vagrantfile, perform Vagrant Reload to complete the virtual machine restart and mount the corresponding directory. Vagrant plugin install Vagrant-vbguest –plugin-version 0.21 Install the VBguest plugin and restart the VIRTUAL machine again.

2. Configure the network

We usually need to communicate with virtual machines. For example, we have installed a Web service on the VIRTUAL machine and need to open the Web service on the virtual machine through the browser of our computer. This requires us to configure the network of the virtual machine, so that the host can communicate with the virtual machine normally. Vagrant offers three network configurations.

  • Forwarded_port: For example, forward port 8080 of the host to port 80 of the VMhttp://localhost:8080Port 80 that is forwarded to the VM will be serviced. This approach is not very flexible because we need to configure all the ports that need to be forwarded;
  • Private network (private_network) : Manually set the IP address of the virtual machine, through which our host machine can communicate with the virtual machine, but we can only access the virtual machine through our host machine.
  • Public network (public_network) : A public network configures VMS as one device that can be accessed on the same LAN. For example, if our computer is connected to a router, then other devices (other computers, tablets, phones, etc.) connected to this router can also access the virtual machine in our computer.

(1) Configure a private network

Find the config.vm.network “private_network” line and uncomment it. The second argument is the specified IP address

config.vm.network "private_network".ip: "192.168.33.10"
Copy the code

Then run the ping 192.168.33.10 command to verify that the private network is set successfully.

*(2) Configure a common network

Go to the config.vm.network “public_network” line, uncomment, and comment the private network configuration. Vagrant Reload is performed to reload the Vagrantfile file, at which point the specified network card needs to be selected. The following output is displayed:

pan@pan-PC:~/Work/vagrant/ubuntu$ vagrant reload
==> default: Attempting graceful shutdown of VM...
==> default: Checking if box 'ubuntu/xenial64' version '20210804.0.0' is up to date...
==> default: Clearing any previously set forwarded ports...
==> default: Clearing any previously set network interfaces...
==> default: Available bridged network interfaces:
1) enp3s0
2) docker0
3) vethc80d1c8
4) vethca1c3e4
5) veth3992844
6) veth2014eaa
7) vethd2642b2
8) veth2b5b670
9) veth21318b5
==> default: When choosing an interface, it is usually the one that is
==> default: being used to connect to the internet.
==> default: 
    default: Which interface should the network bridge to?
Copy the code

We need to select the corresponding network adapter. At this time, I need to enter 1 and press Enter, because ENp3s0 is the physical network adapter name of my computer, while other options are virtual network adapters created by software.

3. Pack the VM

We can package an image of a running VIRTUAL machine. In this way, the new image can be used to create the VM, and the newly created VM will be the same as the VM environment during the packaging. The steps are as follows:

(1) Delete the NIC definition file on the VM

sudo rm -rf /etc/udev/rules.d/70-persistent-net.rules
Copy the code

(2) Exit the VM and pack the vm

vagrant package
Copy the code

At this point, the package.box image file is generated in the current directory

(3) Import the image file into box

#Name the imported box jkdev/ Ubuntu
vagrant box add jkdev/ubuntu package.box
Copy the code

After the import is successful, you can create a new VM using jkdev/ Ubuntu

4. Create multiple VMS

In real projects, we sometimes have web services on one or more servers and database servers on one server. So in local development, we also need to simulate the corresponding server environment. If we can create multiple virtual machines, then we can simulate a real server environment. In fact, Vagrantfile can define multiple servers at the same time, each host can have its own configuration, and we need to configure the network of each host so that the servers can communicate with each other. The creation process is as follows:

Create a directory
mkdir project
Initialize Vagrantfile
vagrant init ubuntu/xenial64
Copy the code

Add the following code to the Vagranfile in the line below config.vm.box = “Ubuntu/Xenial64”

# Define a virtual machine named Development and enable it
config.vm.define "development" do |development|
end

# Define a virtual machine named Production and enable it
config.vm.define "production" do |production|
end
Copy the code

After saving the file, use Vagrant Up to start the virtual machine, starting both virtual machines at this time. When the startup is complete, use Vagrant Status to see that two virtual machines have been started

pan@pan-PC:~/Work/vagrant/test$ vagrant status
Current machine states:

development               running (virtualbox)
production                running (virtualbox)

This environment represents multiple VMs. The VMs are all listed
above with their current state. For more information about a specific
VM, run `vagrant status NAME`.
pan@pan-PC:~/Work/vagrant/test$
Copy the code

If we want to start only one virtual machine, just specify the corresponding virtual machine name after the start command, such as Vagrant Up Production. If we need to connect to the virtual machine, use the following code:

vagrant ssh production
Copy the code

5. Configure networks for multiple VMS

In the Vagrantfile file in section 4, we define two virtual machines, both of which are based on Ubuntu/Xenial64. The reason is that this configuration belongs to the same level as the configuration of defining virtual machines. If we want to define a separate configuration for a certain virtual machine, We need to define the configuration content between config.vm.define and end. We define the private network of the two hosts as follows. Note that the IP address of the private network should not be the same as the IP address of the router connected to the host. The following configuration

config.vm.define "development" do |development|
  development.vm.network "private_network".ip: "192.168.33.11"
end

config.vm.define "production" do |production|
  production.vm.network "private_network".ip: "192.168.33.12" 
end
Copy the code

Vagrant Reload is used to complete the configuration reloading.

6. Define the host name

In 5, we must have configured the IP address of the host, we will add the configuration, define the name of the host, as follows:

config.vm.define "development" do |development|
  development.vm.network "private_network".ip: "192.168.33.11"
  development.vm.hostname = "dev"
end

config.vm.define "production" do |production|
  production.vm.network "private_network".ip: "192.168.33.12" 
  production.vm.hostname = "pro"
end
Copy the code

7. Customize the synchronization directory

By default, multiple VMS automatically share the project directory on the computer, which is mapped to the/Vagrant directory within the VM. We can also set different directories for different VMS. First, create two directories under the project, dev and pro

config.vm.define "development" do |development|
  development.vm.network "private_network".ip: "192.168.33.11"
  development.vm.hostname = "dev"
  # mount dev to /vagrant_dev
  development.vm.synced_folder "dev"."/vagrant_dev"
end

config.vm.define "production" do |production|
  production.vm.network "private_network".ip: "192.168.33.12" 
  production.vm.hostname = "pro"
  # mount pro to /vagrant_pro
  production.vm.synced_folder "pro"."/vagrant_pro"
end
Copy the code

8. Specify the CPU core and memory

We can also customize the memory and core number of the virtual machine, as shown in the following code

config.vm.define "development" do |development|
  development.vm.network "private_network".ip: "192.168.33.11"
  development.vm.hostname = "dev"
  # mount dev to /vagrant_dev
  development.vm.synced_folder "dev"."/vagrant_dev"
  # specify the number of cores and memory
  config.vm.provider "virtualbox" do |v|
    v.memory = 1024
    v.cpus = 2
  end
end

config.vm.define "production" do |production|
  production.vm.network "private_network".ip: "192.168.33.12" 
  production.vm.hostname = "pro"
  # mount pro to /vagrant_pro
  production.vm.synced_folder "pro"."/vagrant_pro"
  # specify the number of cores and memory
  config.vm.provider "virtualbox" do |v|
    v.memory = 1024
    v.cpus = 2
  end
end
Copy the code

Vagrant Reload is used to complete the virtual machine reload