I read the introduction of Vagrant a few days ago, and I have time today to try it out. Due to their use of Windows development environment, so in the Window to try to build the next. Make a note of any problems you encounter.

About Vagrant and why vagrant is used. You can read the article why Vagrant is Used.

Next, tell me about my entire installation process.

First, preparation

In addition to VirtualBox, Vagrant also supports virtual software such as HyperV and VMWare. Virtualbox and Vagrant are the most commonly used and documented pairing, so I chose them as well.

In addition to using the downloaded local file, you can also use the online box image. When adding the image, you will still download it.

Two, installation and application

2.1 Install VirtualBox and Vagrant

As with any Windows software, “Next” is enough.

2.2 applications

1. Add images to Vagrant.

D:\vagrant\>vagrant box add base centos-6.6-x86_64.box
Copy the code

Description:

  • vagrant boxVagrant command
  • baseAdd the name of box
  • Centos - 6.6 - x86_64. Box -The filename of the local box
  • usevagrant box listView the list of box images added.

Online boxes can also be added, just like local boxes. By using the command Vagrant box add Ubuntu /trusty64. Ubuntu/TruSTY64 is the name of the online image.

Online image address vagrantCloud. (Domestic network access may be difficult, it is recommended to add after downloading)

This is a mirror of my centos: centos-6.6-x86_64.box

2. Initialize the VM.

D:\ vagrant\\>mkdir worker # Create work environment D:\\vagrant\ > CD worker D:\\vagarnt\ worker\ > Vagrant init [boxName] # when the alias of the box added is not When base, you need to add boxName hereCopy the code

3. Start the VM

D:\\vagrant\\worker\\>vagrant up
Copy the code

The VM is successfully started if the following information is displayed:

D:\vagrant\worker>vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Importing base box 'base'...
==> default: Matching MAC address for NAT networking...
==> default: Setting the name of the VM: worker_default_1448939529781_46842
==> default: Clearing any previously set forwarded ports...
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
    default: Adapter 1: nat
    default: Adapter 2: hostonly
==> default: Forwarding ports...
    default: 22 => 2222 (adapter 1)
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
    default: SSH address: 127.0.0.1:2222
    default: SSH username: vagrant
    default: SSH auth method: private key
    default: Warning: Connection timeout. Retrying...
    default: Warning: Connection timeout. Retrying...
    default: Warning: Remote connection disconnect. Retrying...
    default:
    default: Vagrant insecure key detected. Vagrant will automatically replace
    default: this with a newly generated keypair for better security.
    default:
    default: Inserting generated public key within guest...
    default: Removing insecure key from the guest if it's present...
    default: Key inserted! Disconnecting and reconnecting using new SSH key...
==> 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: 4.3.28
    default: VirtualBox Version: 5.0
==> default: Configuring and enabling network interfaces...
==> default: Mounting shared folders...
    default: /vagrant => D:/vagrant/worker
Copy the code

Note: There is an error here, stuck here:

Bringing machine 'default' up with 'hyperv' provider...
==> default: Verifying Hyper-V is enabled...
Copy the code

A number of Internet users pointed to Vagrant’s use of the HyperV virtual machine that came with Windows as the cause of her virtual boot. But most of their problems are on Windows 8, and MINE is on Windows 7.

Add the virtualBox installation directory to the virtualBox environment variable path.

20151217 update:

The network configuration is in the Vagrantfile file under the Worker folder. Vagrant created a virtual machine with three configuration network modes:

  • Port mapping is more commonly used, which is to map ports in a VM to ports corresponding to the host. Vagrantfile is configured as follows:

    Config.vm.net work Forwarded_port, guest: 80, host: 8080

Simply remove the # comment from the configuration. Guest: 80 indicates port 80 on the VM. Host: 8080 indicates port 8080 mapped to the host.

  • Vagrantfile if you need to access the VM freely but no one else needs to access the VM, use private_network and set the IP address for the VM.

    Config.vm.net work “private_network”, IP: “192.168.56.2”

192.168.56.2 specifies the VM IP address. If multiple VMS need to communicate with each other, set the IP address to the same network segment. This IP address is enabled for eth1 by default.

  • If you need to use the virtual machine as a computer in the current LAN for DHCP, configure it in Vagrantfile:

    config.vm.network :public_network

In this case, the VM is connected to the network as the host and has the same network rights as the host. In this case, if the IP address is freely allocated, you can use 127.0.0.1.

Port Use the port prompted in the command window during startup. Log in to the machine and check the assigned IP address. Then log in using the assigned IP address. If the IP address is fixed, you can write it in the configuration. As follows:

Network bridge :"public_network", IP :" 192.168.56.2" config.vm.network "public_network", :bridge => 'en1: Wi-Fi (AirPort)'Copy the code

4. Use of links

Since Windows does not support SSH, we need to use another SSH client to link. Take the Xshell I use for example.

Enter: Vagrant SSH in a CMD window. You’ll see your host address, port, and the location of your key.

D:\vagrant\worker>vagrant ssh
`ssh` executable not found in any directories in the %PATH% variable. Is an
SSH client installed? Try installing Cygwin, MinGW or Git, all of which
contain an SSH client. Or use your favorite SSH client with the following
authentication information shown below:

Host: 127.0.0.1
Port: 2222
Username: vagrant
Private key: D:/vagrant/worker/.vagrant/machines/default/virtualbox/private_key
Copy the code

Add the SSH key to xshell. You can refer to it here.

Since our key has already been generated, we just need to import it.

Tools – > User Key Management – > Import.

After creating the reply, select Public Key when you click Login and you’ll see the private_key we imported.

There is no need to fill in the password. Click login directly to enter the system command line.

At this point, it’s just like operating a normal Linux system.

To log in as root, see section 3.3 below.

5. Pack and distribute

When you have configured the development environment, exit and shut down the virtual machine. Package the development environment in the terminal:

vagrant package
Copy the code

When the package is complete, a package.box file is generated in the current directory and passed to other users, who simply add the box

Use it to initialize your own development directory to get an identical development environment.

Third, other

3.1 Common commands

Virtual machine: D:\\ Vagrant \\worker\ > Vagrant halt

Virtual machine suspends: D:\\vagrant\\worker\ > Vagrant suspend

Virtual machine Resume: D:\\ Vagrant \\worker\ > Vagrant Resume

Delete virtual machine: D:\\vagrant\\worker\ > Vagrant destory

Check the vm running status: D:\\vagrant\\worker\ > Vagrant Status

Restart the virtual machine: D:\\vagrant\\worker\ > Vagrant Reload

3.2 Vagrant and VirtualBox configuration modifications

After Vagrant and VirtualBox are installed, the default location for storing vm image files is on the system disk. For most people with limited system disk capacity,

You will soon receive an “Insufficient disk capacity” alarm. Perform necessary Settings to remove the mirror data from the system disk.

3.2.1 Changing the Storage Location of the VirtualBox Image File

The specific steps are as follows:

1. Open VirtualBox and select Global Settings from the menu (CtrL-G)

2, Select the default virtual machine location in general (M)

3. Set it to the position of a non-system disk.

4. Move the VM image from the original location to the new location.

If the VM has been installed before the configuration, select the Vbox file in the corresponding directory in Windows Explorer to add the VM image in the new directory to VirtualBox.

3.2.2 Changing the location of Vagrant’s image storage

Vagrant manages virtual machines in two parts: Box and Machine. Box is the initial undeployed VM image file. This file is essentially a template for the virtual machine,

Unlimited number of copies can be made. Machine refers to a VM that is running. When box is added to Vagrant, Vagrant saves these vm template images by default

Put it in C: User\.vagrant.d.

Therefore, when there are many virtual machine template images managed using Vagrant, the directory is also large. You can move to another disk partition. The method is:

1. Move the c:\User\.vagrant.d directory to its new location

2. Set the VAGRANT_HOME environment variable to the new location.

3.3 Log in to the VM as user root

It is uncomfortable to have no root permission on Linux, so I checked the root login configuration and added the configuration to vagrantfile:

config.ssh.username = 'root'
config.ssh.password = 'vagrant'
config.ssh.insert_key = 'true'
Copy the code

See log after startup:

D:\vagrant\worker>vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Clearing any previously set forwarded ports...
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
    default: Adapter 1: nat
    default: Adapter 2: hostonly
==> default: Forwarding ports...
    default: 22 => 2222 (adapter 1)
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
    default: SSH address: 127.0.0.1:2222
    default: SSH username: root
    default: SSH auth method: password
    default: Warning: Connection timeout. Retrying...
    default: Warning: Connection timeout. Retrying...
==> 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: 4.3.28
    default: VirtualBox Version: 5.0
==> default: Configuring and enabling network interfaces...
==> default: Mounting shared folders...
    default: /vagrant => D:/vagrant/dev
==> default: Machine already provisioned. Run `vagrant provision` or use the `--provision`
==> default: flag to force provisioning. Provisioners marked to run always will still run.
Copy the code

3.4 A VagrantFile file manages multiple virtual machines

Vagrant Init 1. Initialize with box: Vagrant init

2. Modify the configuration file as follows:

# -*- mode: ruby -*- # vi: set ft=ruby : # All Vagrant configuration is done below. The "2" in Vagrant.configure # configures the configuration version (we support older styles for # backwards compatibility). Please don't change it unless you know what # you're doing. Vagrant.configure("2") do |config| config.vm.provision "shell", inline: "echo Hello" config.vm.define "master" do |saltmaster| saltmaster.vm.box = "base" saltmaster.vm.host_name = 'saltmaster.local' saltmaster.vm.network "private_network", ip: Username = 'root' saltmaster.ssh.password = 'vagrant' saltmaster.ssh.insert_key = 'true' end config.vm.define "minion" do |saltminion| saltminion.vm.box = "base" saltminion.vm.host_name = 'saltminion.local' saltminion.vm.network "private_network", ip: "192.168.33.14" saltminion.ssh.username = 'root' saltminion.ssh.password = 'vagrant' saltminion.ssh.insert_key = 'true' end endCopy the code

3, normal start can be: Vagrant up

4, login

  • Log in as Vagrant SSH +

    pylixm@pylixm-pc /d/vagrant/dev331314 $Vagrant SSH Master [email protected]'s password: Welcome to your Vagrant-built virtual machine. [root@saltmaster ~]#Copy the code
  • Use the IP address to log in directly

Add the existing VirtualBox virtual machine to the Vagrant administration

Instead of adding it directly, there is a way to change the direction: first convert the VirtualBox virtual machine into a Box image, and then add the image to the Vagrant management.

Details — > here

1. Pack the existing virtual machine (the virtual machine must have been opened by VirtualBox, so that virtualBox can find the virtual machine)

vagrant package --base mybox --output /path/to/mybox.box
Copy the code

2. Add a Box image

vagrant box add mybox /path/to/mybox.box
Copy the code

Reference: