This is the 13th day of my participation in the August More Text Challenge. For details, see:August is more challenging

The article directories

      • Vagrant manages VM VMS
        • Install VirtualBox
        • Install vagrant
          • 1. Create a VM
          • 2. Copy the VM
          • 3. Create VMS in batches
          • 4. Configure the network

Vagrant manages VM VMS

Install VirtualBox

[virtualbox virtualbox virtual technology is introduced: please click -- baidu encyclopedia] (https://baike.baidu.com/item/VirtualBox/5842786?fr=aladdin)Copy the code
  • Attached to the download address: www.virtualbox.org/wiki/Downlo…

Install vagrant

Vagrant is a tool for operating virtual machines. Is a Ruby based tool for creating and deploying virtualized development environments. Using commands and configuration files to manage virtual machines, you can quickly complete a set of development environment deployment, and can be packaged and spread, unified development environment, but also solve the problem of repeated configuration environment.Copy the code
  • The vagrant download address: www.vagrantup.com/downloads.h…
    • Releases.hashicorp.com/vagrant/2.2…
  • Mirror box address: cloud.centos.org/centos/7/va…
    • Cloud.centos.org/centos/7/va…
    • Cloud.centos.org/centos/7/va…
The command explain
vagrant box list View the existing boxes
vagrant box add [local-box-name] [local-box-file / remote-box-file] A local Box system image is added
vagrant box remove [local-box-name] Delete specified box
vagrant init [local-box-name] Initialize the vagrantFile
vagrant up Starting a VM
vagrant ssh Logging in to a VM
vagrant suspend Suspend the VM
vagrant reload Restart the VM. < Use after modifying Vagrantfile >
vagrant halt Stop the VM.
vagrant resume Restore the local VM
vagrant status View the VM status
vagrant destroy Deleting a VM
1. Create a VM
Install virtualBox first, then install Vagrant. Open virtualBox Management-Global Settings: Specify the location of the virtual machine, do not use the default configuration to drive C, specify another path to saveCopy the code
  • Step 1: Vagrant box add [add local image name, such as centos7] *. Box system image file [but download to local, also can use download address]
  • Step 2: Vagrant Box List view loaded into your local image box system
  • Step 3: Vagrant init [the name of the system image just added to the box] will generate a Vagrantfile in the current directory
Create a Vagrantfile and go to a directory under another drive letter, because the Vagrantfile will generate the vm image data file in the startup vm. Select Vagrantfile, Vagrantfile, Vagrantfile, Vagrantfile, Vagrantfile, Vagrantfile, Vagrantfile, Vagrantfile, Vagrantfile, Vagrantfile, Vagrantfile, Vagrantfile, Vagrantfile, Vagrantfile, Vagrantfile, Vagrantfile, Vagrantfile, Vagrantfile, Vagrantfile, Vagrantfile. Config.vm.net work" public_network" Memory, CPU and name config. The vm. The provider "virtualbox" do | | vb vb. The memory = "4096" vb. The name = "centos7 - test" # that is the name of the virtual machine, Do not care if vagrant init is initialized, as this is the name of the box image vB.cpus = 2 endCopy the code
  • Start the virtual machine in the current directory. Open VirtualBox to see a running virtual machine
  • Vagrant SSH [current directory to execute]
The default user is vagrant and the password is also vagrant. Vagrant \machines\default\virtualbox\private_key [vagrant@localhost ~]# sudo -i /etc/ssh/sshd_config: allow user root to log in using the password. -passwordauthentication no change to PasswordAuthentication yes - sed Sed -i 's/PasswordAuthentication no /PasswordAuthentication yes/g' /etc/ssh/sshd_config - Restart the SSH service. systemctl restart sshd.serviceCopy the code
  • Step 6: install vim with yum install vim and passwd to change the password of the current user
  • To delete the VIRTUAL machine, do not need to stop the virtual machine, directly go to the virtual machine directory: Vagrant deStory destroy
2. Copy the VM
Stop the VM to be replicated: Stop the VM first and then replicate the VMCopy the code
  • Go to the vm directory: vagrant package –output centos7-test.box
  • Vagrant box add box-name centos7-test.box
  • Create a new directory and initialize the VM: vagrant init box-name
  • Finally, you can start the virtual machine: vagrant up, and pay attention to modifying the network
3. Create VMS in batches
  • Step 1: To prepare the environment, add the box system image to the local, vagrant Box List to view
  • Vagrantfile: type nul>Vagrantfile
  • Step 3: Modify the Vagrantfile with the following information:
boxes = [
	{
		:name => "master",
		:eth1 => "192.168.2.113",
		:mem => "2048",
		:cpu => "2",
		:sshport => 22230
	},
	{
		:name => "slave1",
		:eth1 => "192.168.2.114",
		:mem => "2048",
		:cpu => "2",
		:sshport => 22231
	},
	{
		:name => "slave2",
		:eth1 => "192.168.2.115",
		:mem => "2048",
		:cpu => "2",
		:sshport => 22232
	}
]
Vagrant.configure(2) do |config|
	config.vm.box = "centos7"
	boxes.each do |opts|
		config.vm.define opts[:name] do |config|
			config.vm.hostname = opts[:name]
			config.vm.network :public_network, ip: opts[:eth1]
			config.vm.network "forwarded_port", guest: 22, host: 2222, id: "ssh", disabled: "true"
		config.vm.network "forwarded_port", guest: 22, host: opts[:sshport]
			config.vm.provider "vmware_fusion" do |v|
				v.vmx["memsize"] = opts[:mem]
				v.vmx["numvcpus"] = opts[:cpu]
			end
			config.vm.provider "virtualbox" do |v|
				v.customize ["modifyvm", :id, "--memory", opts[:mem]]
			v.customize ["modifyvm", :id, "--cpus", opts[:cpu]]
				v.customize ["modifyvm", :id, "--name", opts[:name]] end end end end ---------- tip: Set auto_config to network if boxes are not configured with network IP:trueAutomatic configuration. The default value isfalse
Copy the code
  • Step 4: Start the virtual machine safely: Vagrant up, because there are multiple, so need to keep up with the name of the virtual machine to start
4. Configure the network
The company number segment is 192.168.2. XXX LAN; So want to configure different IP end to use locally; If the development test environment is all local IP, then use the domain name, app test needs local hosts mapping;Copy the code
  • Create a VM. Edit Vagrantfile Select a network. Public_network Is an unused IP address in the local IP address segment
  • If you select private_network and do not share the same network segment with the local IP address, you can only use the private_network on the local PC
  • Run the vim /etc/sysconfig/network-scripts/ifcfg-eth1 command to change the network on the VM
Run the following command to restart the NIC: sed -i 's/old_ip/new_ip/g' /etc/sysconfig/network-scripts/ifcfg-eth1 Command to restart the NIC: service network restartCopy the code