Environment to prepare
Virtualbox is free, so you don’t have to go to the trouble of cracking it. Download it and use it. Each time you install a virtual machine using VirtualBox, you need to download the ISO file, then set up the virtual machine hardware configuration, and use the ISO file to create the virtual machine. A series of manual operations, if you just create a virtual machine or two to practice, is enough. If you want to create 10 VMS, repeat 10 times. This is where vagrant comes in handy.
Vagrant creates N devices with one click, based on configuration files.
Download and install
virtualbox
Enter the Download address of VirtualBox and download different software versions based on your environment. For example, if MY machine is running Windows 10, I choose the Windows Hosts version.
After downloading, you can install it by default.
vagrant
Go to vagrant’s website and download the required version for your native operating system.
Install vagrant directly, and the installer automatically adds vagrant to the environment variable PATH. Vagrant does not have a graphical interface, so restart your computer after successful installation. If the following information is displayed, the installation is successful
The use of Vagrant was introduced later.
Vagrant use
concept
Box: indicates a VM image
Provider: indicates the virtual service provider, such as VirtualBox and VMware.
Box access
1. Obtain from the official website
Box can be retrieved from the official mirror library:
When you start the VIRTUAL machine, you will download the image from the official website image library and create the virtual machine.
2. Domestic mirror image
Vagrant images can be found in a box file in the Vagrant directory. After downloading the image, use the following command to add it to your box library.
vagrant box add centos7 D:/CentOS-7.box
Vagrant is simple to use
Initialize the
vagrant init
A VagrantFile can be initialized by executing the init command in a directory.
The VagrantFile configuration file is as follows:
Vagrant.configure("2") do |config| config.vm.box = "centos7" config.vm.define "web1" do |web| web.vm.box = "centos7" web.vm.network "private_network", ip: "192.168.14.10" end config. Vm. Define "web2" do | web | web. The vm. The box = "centos7" web.vm.net work "private_network", IP: "192.168.14.11" end to endCopy the code
This configuration file uses the image centos7 to configure two VMS with IP addresses 192.168.14.10 and 192.168.14.11.
Vagrant up # Start all configured virtual machines
or
Vagrant up web1 # Specifies the start of the Web1 virtual machine
Vagrant initialization Once the startup is complete, both root and Vagrant users are initialized with the password vagrant; By default, you are not allowed to log in using a password.
The OPERATION VM is displayed
Method 1 uses the Vagrant command
vagrant ssh web1
After logging in, the default user is vagrant. Run su root to switch to user root. The password is vagrant.
Method 2 Use the SSH client
This example uses secureCRT as an SSH client to access the Vagrant virtual machine.
First, the username must be vagrant. Logging in to root is unsuccessful. If the publicKey login mode is used, configure the VM private_key as the login key. After the configuration is complete, the login is successful.
File synchronization
Vagrant provides a way to synchronize files. We will only cover synchronization from the host machine to the virtual machine. For more information, see the official documentation.
By default, Vagrant maps the current Vagrant project root directory (where VagrantFile resides) to the/Vagrant directory of the virtual machine
The mapping is not real-time. A file synchronization is performed when the VM is started. To synchronize data during vm running, run the following command:
vagrant rsync
You can also configure the file synchronization directory through the configuration file.
Best practices
You can use the Provision to configure an initialization script for each VM to enhance VM initialization.
VagrantFile is put into the development project and submitted to Git. Other developers can also download it and start the virtual machine environment with one click. Then they can have the same virtual machine environment as you.
review
This section describes how to install software, download images, configure VMS, and start VMS. Eventually we could have a centOS7 virtual machine environment and use secureCRT to connect to it.