Linux virtual machine management software vargrant

Article directory

  • 1 What is Linux
    • 1.1 Features of Linux
    • 1.2 Basic idea of Linux
    • 1.3 What is a Linux virtual machine
  • 2 Linux is used in those places
  • 3 How to use Linux
    • 3.1 Vargrant
      • 3.1.2 What is Vagrant
      • 3.1.3 The main working principle of vargrant
    • 3.2 Vargrant installation and configuration
      • 3.2.1 Install Virtualbox
      • 3.2.2 Installation directory and configuration
      • 3.2.3 Install vargrant
      • 3.2.4 Configuration
    • 3.3 Install centos virtual machine
      • 3.3.1 download box
      • 3.3.2 Initialization file
      • 3.3.3 Prepare box
      • 3.3.4 Install the virtual machine
      • 3.3.5 Common commands
    • 3.4 Custom box
    • 3.5 Simple custom box instance

1 What is Linux

Linux, the full name of GNU/Linux, is a set of Unix-like operating systems that are free to use and spread freely. It is a POSIX-based multi-user, multi-tasking, multi-threading and multi-CPU operating system.

1.1 Linux Features

1. Linux is not only stable in system performance, but also open source software. Its core firewall components are highly efficient and easy to configure, which ensures the security of the system. In many enterprise networks, in order to pursue speed and security, Linux is not only used by network operation and maintenance personnel as a server, but even as a network firewall. This is one of the highlights of Linux.
2. Linux has the characteristics of open source code, no copyright, and many users in the technical community. Open source code allows users to tailor it freely, with high flexibility, powerful functions, and low cost. In particular, the network protocol stack is embedded in the system, and the function of the router can be realized after proper configuration. These characteristics make Linux an ideal development platform for developing routing and switching equipment.

1.2 Basic idea of Linux

There are two basic ideas of Linux: first, everything is a file; second, each file has a definite purpose. The first one is that everything in the system boils down to a file, including commands, hardware and software devices, operating systems, processes, etc. For the operating system kernel, they are all regarded as files with their own characteristics or types. As for the fact that Linux is based on Unix, it is largely because the basic ideas of the two are very similar.

1.3 What is a Linux virtual machine

When it comes to Linux, you will use a Linux virtual machine, so what is a Linux virtual machine? Generally speaking, Linux virtual machines are installed under the Window system during development, and the virtual machine is a complete operating system independent of Window. Like Windows, it has CPU, memory, IP address and so on. In the Linux virtual machine, you can install IDEA, Eclipse and other development tools (their operating environments are all Linux version jdk and other Linux development environments). Window and Linux files can be shared and co-developed.

2 Linux is used in those places

1. Desktop application
Linux is not widely used in desktop scenarios, and windows is generally used in this scenario
2. Server (backend)
E-commerce, portal back-end services generally use linux as the operating system, java, c++, python, go, run and deploy services on linux.
3. Embedded
The smallest and most core version of linux is only a few hundred K, and smart IoT devices are convenient for embedded development.
4. Cloud Computing
Operation and maintenance and deployment scenarios, use linux.

3 How to use Linux

1 agrant is a very useful tool that can be used to program and manage multiple virtual machines (vms) on a single physical machine. It supports native VirtualBox, and also provides plug-in support for VMware Fusion and Amazon EC2 virtual machine clusters.
2 Vagrant provides an extremely easy-to-use, Ruby-based internal DSL that allows users to define one or more virtual machines using their configuration parameters. In addition, for automatic deployment, vagrant supports multiple mechanisms: you can use puppet, chef, or shell scripts for automatic installation of software programs and configurations on all virtual machines defined in the vagrant configuration file.
3 Compared with vmware, vargrant has the advantage that vargrant can create multiple virtual machines at a time, and can customize the box for multiple people to use. vmware can only create one virtual machine at a time.

3.1 Vargrant

3.1.2 What is Vagrant

1. Vagrant is a relatively popular virtual machine management software. Using Vagrant allows us to directly download the image of the virtual machine from the cloud with commands, and then create and manage it. Vagrant integrates the mainstream virtual machine management tools vmvare and Virtualbox, and uses Virtualbox by default.
2. The essence of Vagrant is in a Vagrantfile, which is functionally the same as Docker’s Dockerfile. We only need to write the steps that need to be installed and deployed in the Vagrantfile to achieve easy deployment. Vagrant also supports making the current system a mirror named with a .box suffix, similar to Docker’s image, which can easily realize the transplantation of the environment.
3. Vagrant can package the configured environment into a box and share it with others for direct use.
4. Before containers became popular, Vagrant was used to orchestrate virtual machines and automatically deploy development environments. With Docker/Kubernetes, it is indeed easier to directly use containers to orchestrate applications. But there are still some tasks, such as the installation of the container platform itself, the deployment and testing of multi-node clusters, etc., which are more convenient to solve with virtual machines.
5. One more thing, what is the Linux virtual machine and centos.box mentioned earlier, so what is the relationship between the Linux virtual machine and the box?
The simple book says that different boxes can generate different Linux virtual machines. If your box contains Java and MySQL environments, then the box will generate Linux virtual machines containing Java and MySQL. The two are the relationship between classes and objects.

3.1.3 The main working principle of vargrant


1 First of all, you need to initialize the vargrantfile file, which contains the configuration you need, such as CPU, memory, network card, etc., as well as the environment you need, such as Java, MySQL, etc.
2 This configuration file will send the configuration information to the cloud server warehouse, and the cloud server warehouse will return the information. At this time, you can download the box of the configuration information you need from the cloud server warehouse.
3 The downloaded box can be imported locally with one click.
4 This box can be used by yourself or sent to co-developers, so that other people do not need to reconfigure and download, which greatly facilitates development.

3.2 vargrant installation and configuration

3.2.1 Install Virtualbox

VirtualBox is a free and open source virtual machine, which is smaller than VMware, and I like it personally. Although Vagrant also supports VMware, VMware is charged, and the corresponding Vagrant version is also charged. Download address https://www.virtualbox.org/wiki/Downloads

3.2.2 Installation directory and configuration

1 It is recommended to install on the D drive
2 configuration

3.2.3 Install vargrant

1 https://www.vagrantup.com/downloads Download the latest version of Vagrant. Note that Vagrant has no graphical interface, so there is no desktop shortcut after installation.
2 It is recommended to install it on the D drive.

3.2.4 Configuration

3.3 Install centos virtual machine

3.3.1 Download box

You can download more different systems or even boxes that can be used directly in the configured environment at http://www.vagrantbox.es/. Although you can directly use the URL in Vagrant, and Vagrant will automatically download and install it, considering the network situation, it is recommended to download it yourself first.

3.3.2 Initialization file

1 Create a new folder, and there cannot be Chinese in the directory.
2 Use cmd to dismiss the DOS window.
3 In the DOS window, use the cd command to enter the directory in the newly created file.
4 Execute the command vagrant init centos in the DOS window to generate a configuration file.
5 Modify the configuration file and change it to the configuration information such as memory, network card, etc. that you need.
6 Examples are as follows:
Some inline code snippets are shown below.

// A code block
var foo = 'bar';
VAGRANTFILE_API_VERSION = "2"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|

  (1..3).each do |i|
    config.vm.define vm_name = "s#{i}" do |config|
        config.vm.provider "virtualbox" do |v|
            v.customize ["modifyvm", :id, "--name", vm_name]
v.customize ["modifyvm", :id, "--memory", "8192"]
            v.customize ["modifyvm", :id, "--cpus", "2"]
        end
        config.vm.box = "centos_v1"
        config.vm.hostname=vm_name
        config.ssh.username = "root"
        config.ssh.password = "vagrant"
# config.ssh.shell = "powershell"
#config.ssh.shell = "bash -l"
        config.vm.network : private_network, ip: "192.168.56.7#{i}"
config.vm.provision :shell, :path => "bootstrap.sh"
    end
  end
end

Screenshot below:

7 configure bootstrap.sh

#!/usr/bin/env bash

# The output of all these installation steps is noisy. With this utility
# the progress report is nice and concise.

echo "Update /etc/hosts"
cat > /etc/hosts <<EOF
127.0.0.1 localhost

192.168.56.71 s1
192.168.56.72 s2
192.168.56.73 s2
EOF

echo "Disable iptables"
setenforce 0 >/dev/null 2> & amp; 1 & amp; & amp; iptables -F

### Set env ###
echo "export LC_ALL=en_US.UTF-8" >> /etc/profile
cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

Screenshot below:

3.3.3 Prepare box

1 Use the vargrant boxl ist command to check the local box.
2 Add the local box to vargrant

F:\java\virtual\s13>vagrant box add centos_v1 ./centos_v1.box
==> box: Box file was not detected as metadata. Adding it directly... ?
==> box: Adding box 'centos_v1' (v0) for provider:
    box: Unpacking necessary files from: file://F:/java/virtual/s13/vagrant-centos-7.2.box
    box:
==> box: Successfully added box 'centos_v1' (v0) for 'virtualbox'!

3.3.4 Install a virtual machine

1 Install the virtual machine with vargrant up.
2 Use MobaXterm software to connect to the virtual machine just installed.

3.3.5 Common commands

#Start Vagrantfile (s1, s2, s3) to configure all virtual
vagrant up

#Start a specific virtual machine
vagrant up s1

vagrant halt #close all

vagrant halt s1

3.4 Custom box

Installed java development environment
● JDK
● Mysql
● redis
● Elasticsearch
● RocketMQ
● mions
●…
Can you pack the installed virtual machine into a custom box and share and manage our virtual machine with us?

3.5 Simple custom box instance

1 The standard virtual machine box does not have vim, use the command yum install vim -y to install the box with the vim command in the virtual machine.
2 Package the current virtual machine and generate a custom box

#Close the virtual machine
vagrant halt s1
#Package the current virtual machine
vagrant package --base s1 --output centos_v2.box


3 Delete the existing virtual machine

vagrant halt
vagrant destroy # delete all virtual machines
vagrant destroy s1 #delete s1

4 Add centos_v2 to vagrant

#list
F:\java\virtual\s13>vagrant box list
centos_v1 (virtualbox, 0)

#Add to
F:\java\virtual\s13>vagrant box add centos_v2 ./centos_v2.box
==> box: Box file was not detected as metadata. Adding it directly... ?
==> box: Adding box 'centos_v2' (v0) for provider:
    box: Unpacking necessary files from: file://F:/java/virtual/s13/centos_v2.box
    box:
==> box: Successfully added box 'centos_v2' (v0) for 'virtualbox'!

# list
F:\java\virtual\s13>vagrant box list
centos_v1 (virtualbox, 0)
centos_v2 (virtualbox, 0)

5 Modify Vagrantfile, set box to use centos_v2


6 Use the vargrannt up command to install a new virtual machine on the vim box.
7 Enter the virtual machine to view vim, which has been installed.