Brief introduction
Vagrant is an industry-proven virtualization technology that builds a configurable, renewable, and portable working environment, with a profile that completes all state definitions.
Characteristics
• Simple, a configuration file to handle
• Customize virtual machine configurations in a variety of ways, such as shell scripts, ansible, etc.
• Network configuration, support for private, shared network configuration
• Directory synchronization, keeping guest and host files synchronized, mapping the current directory to the/vagrant of the virtual machine by default
• Multi-virtual machine support to build your own cluster test environment
• Multi-medium virtual technical support, such as Virtualbox,vmware,aws and Docker
• Plug-in mechanism, flexible expansion
Concept
box is a virtual machine that has been made, poured into the ~/VAGRANG.D directory, and the standard virtual machines are joined by a number of configurations to support vagrant management, such as public key
Vagrantfile describes and configures the configuration of the virtual machines to be created
Installation
The code is as follows |
Copy Code |
sudo apt-get install vagrant |
Note that the latest version is to be downloaded from its website
Configuration
Add box
The code is as follows |
Copy Code |
Vagrant box Add Name URL |
Where Name box is named, the path to box can be remote and local
Initializing the Environment
The code is as follows |
Copy Code |
Vagrant Init |
Generate the default Vagrantfile
Modify configuration file Vagrantfile
Detailed configuration refer to official documentation
Stand-alone configuration
The code is as follows |
Copy Code |
Vagrant.configure ("2") do |config| Config.vm.box = "Base" #定义此虚拟机是从哪个box生成 Config.vm.provision:shell,:p ath => "bootstrap.sh" #通过shell配置 Config.vm.network:forwarded_port, host:8080, guest:80 #端口映射 Config.vm.network "Private_network", IP: "192.168.110.100" #私有网络配置 #awesome configuration goes here ... End |
Multi-machine configuration
The code is as follows |
Copy Code |
Vagrant.configure ("2") do |config| Config.vm.provision "Shell", inline: "Echo Hello" Config.vm.define "Web" do |web| Web.vm.box = "Apache" #awesome configuration goes here ... End Config.vm.define "DB" Do |db| Db.vm.box = "MySQL" #awesome configuration goes here ... End End |
Use
• Open/Shutdown vagrant Up/halt
• Restart Vagrant Reload
• State vagrant Status
• Login vagrant SSH
• Destruction of vagrant destroy
Example
Vagrant Boot is a complete virtual machine, not limited to what to do, we can use this virtual machine to practice configuring the server, running the application of the development environment ... Wait, it depends on what you need.
Here are two examples.
Run Nginx
Create a new test directory:
The code is as follows |
Copy Code |
$ mkdir-p ~/workspace/nginx-test $ CD ~/workspace/nginx-test/ |
New configuration:
The code is as follows |
Copy Code |
$ vagrant init precise64 Http://files.vagrantup.com/precise64.box $ vagrant Up |
Since previously downloaded precise64 this box, there is no need to download, as long as the clone came on the line. After the boot completes, login to SSH:
The code is as follows |
Copy Code |
$ vagrant SSH |
Within the vagrant virtual machine, install the Nginx:
The code is as follows |
Copy Code |
vagrant@precise64:~$ sudo apt-get update vagrant@precise64:~$ sudo apt-get install Nginx |
At this time Nginx has been installed, but found no way to the host access to the virtual machine services, we also need to set port mapping. Add one row of configuration to the native Vagrantfile configuration block:
The code is as follows |
Copy Code |
Config.vm.network:forwarded_port, guest:80, host:8080 |
This line of configuration maps the 80 ports of the virtual machine to 8080 ports on this computer, and then restarts the virtual machines.
The code is as follows |
Copy Code |
vagrant@precise64:~$ exit $ vagrant Reload |
Open the browser, Access localhost:8080, you will see Welcome to nginx! The words, the access to the virtual machine nginx success.