Virtual deployment under linux

One, kvm

Define the
commonly used virtual machine under linux system is kvm (kernel-based virtual machine), which is an open source system virtualization module.

Installation conditions
inter cpu-------->vmx
amd cpu ------------>svm

#Executable cat /proc/cpuinfo to view the cpu type, when linux is inter cpu, and there are vmx instruction-level parameters, kvm can be installed and used with this; if it is amd cpu, svm must be an instruction parameter.
Insert picture description here

Two, kvm virtualization installation

Preparations: add hard disks to the virtual machine:
shut down the virtual machine and edit the virtual machine settings.
Insert picture description here
Allocate 6144Mb of memory to the virtual machine.
Insert picture description here

Assign a processor to it, each processor is assigned 4 cores, choose the virtualization Intel VT-x/EPT or AMD-V/RVI(V) below.
Insert picture description here

Choose to add a hard disk, pay attention to select the SATA mode, the next step is to select 60 G
Note: Do not add to the c drive
Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here

Turn on the virtual machine and configure the software installation environment
in the virtual machine. Build a software warehouse in the vm virtual machine:
select the CD icon in the lower right corner, right-click to select settings, and change the device status to connected
Insert picture description here
Insert picture description here

df command to view, edit westos.repo in /etc/yum.repos. d/ and
Insert picture description here
Insert picture description here
use dnf command to list hidden groups
dnf group list --hidden
Insert picture description here
There is a Virtualization Client, Virtualization Hypervisor in the available groups in the hidden (Virtualization core suite), Virtualization Tools (virtualization tools)
Insert picture description here

Install the above three groups
dnf group install "Virtualization Client" "Virtualization Hypervisor" "Virtualization Tools" -y
Insert picture description here
virtual machine management tool installation is complete!
Insert picture description here
Format the hard disk in the virtual machine and mount it to the
fdisk /dev/sdb # partition in the virtual machine storage directory . Except for the first input n and the last input wq, the rest are all Enter
Insert picture description here

mkfs.xfs /dev/ sdb1 -K #Format the device
Insert picture description here
mount /dev/sdb1 /var/lib/libvirt/images/ #Directly mount it
Insert picture description here
to permanently mount the software warehouse, mount /dev/sdb1 /var/ Write lib/libvirt/images/ to /etc/rc.d/rc.local, then give /etc/rc.d/rc.local a permission, chmod a+x /etc/rc.d/rc.local
Insert picture description here
Insert picture description here

Three, sakvm virtualization related information

Service name: libvirtd
Open this service: systemctl enable --now libvirtd
Insert picture description here

Virtualization core: qemu/kvm

Virtualization storage directory (virtual machine hard disk)
/var/lib/libvirt/images/xxx.qcow2
Insert picture description here
Insert picture description here
Virtualization hardware information
/etc/libvirt/qemu/xxx.xml
Insert picture description here
This xxx.xml will record the virtual machine's network card location, Boot partition and other related hardware information.
Insert picture description here

Four, manually install the virtual machine

Proceed as follows:
Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here

Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here

Five, virtual machine management commands

virt-viewer test #Display test virtual machine
Insert picture description here
Insert picture description here

virt-manager #Open the virtual controller
virsh list #List the running virtual machines
virsh list --all
Insert picture description here
#List all the virtual machines vish start test #Open the virtual machine
Insert picture description here
Insert picture description here

virsh shutdown test #Shut down the virtual machine normally (may be blocked)
Insert picture description here
virsh destory test #Power off the virtual machine test (will not be blocked)

Insert picture description here

Six, the virtual machine is transmitted in the linux system

Move the hardware information and hard disk of the virtual machine from their respective locations to another unrelated place (simulate two hosts, move the hardware information and hard disk of the virtual machine of host 1 to another host without virtual machine 2 Above)
Insert picture description here
Then move the hardware information and hard disk to /var/lib/libvirt/images (simulate in host 2, move the hardware information and hard disk to this directory), you
Insert picture description here
Insert picture description here
can open the hardware information through virsh create xxxx.xml Virtual machine, but after destruction, the virtual machine disappears
Insert picture description here
Insert picture description here
Insert picture description here

You can use virsh define xxx.xml to open the virtual machine by opening the hardware information. After the destruction, the virtual machine will not disappear
Insert picture description here
Insert picture description here

Insert picture description here

If you want to delete a virtual machine, enter virsh underfine xxxxx to delete only the hardware information of the virtual machine. You should also delete the hard disk with rm -fr /var/lib/libvirt/qemu/xxxx.qcow2, so that the virtual machine can be completely deleted.

Insert picture description here

Seven, virtual machine snapshot

qemu-img create #take a snapshot

Give test a snapshot of node1
Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here

Insert picture description here
Insert picture description here

8. Build a network source to install a virtual machine

dnf install httpd -y
Insert picture description here
Turn off the firewall and turn on the httpd service
systemctl disable --now firewalld
systemctl enable --now httpd

Insert picture description here
mkdir /var/www/html/westos
umount /dev/sr0 #
mount /dev/sr0 /var/www/html/westos/ #Mount to the specified shared directory
Insert picture description here
At this time, you can see the mounted warehouse on the web page:
Insert picture description here

Edit the network configuration file
nm-connection-editor
nmcli connection reload
nmcli connection down xxxx
nmcli connection up xxxx
ifconfig
Insert picture description here
Insert picture description here

cd /var/lib/libvirt/images/
chown qemu.qemu /var/lib/libvirt/images/ #Change all groups to enable the creation of virtual machines (this is the path to the virtual machine hard disk)
Insert picture description here

virt-manager
is installed via network:
Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here

But when using virtualization in Linux, there is no bridged network by default, and the default is NAT mode.
Insert picture description here
If there is a bridge:
Insert picture description here

How to build a network bridge to install a virtual machine:
On the real host:
cd /etc/sysconfig/network-scripts/
ls
ifcfg-ens160
cp ifcfg-ens160 ifcfg-br0
Insert picture description here
vim ifcfg-br0
vim ifcfg -ens160
Insert picture description here
Insert picture description here

Insert picture description here
Insert picture description here

nmcli connection reload
nmcli connection down xxxx
nmcli connection up xxxx
Insert picture description here

Check the bridge:
bridge link
Insert picture description here

At this time, the virtual machine is established and there is a bridging option
Insert picture description here
Insert picture description here

But after the bridge is established, the newly created virtual machine cannot get the IP,
so you need to build dhcp on the real host (for details, please review the chapter on network configuration under linux)
dnf install dhcp-server -y
Insert picture description here
Insert picture description here
There is no problem during installation at this time

Nine, use the command line to install the virtual machine

Virtual machine installation command line
virt-insatll

Edit the execution document
vim westos.sh
Insert picture description here
#!/bin/bash #Script specifies the interpreter
virt-install \
#Virtual machine installation command --location http://192.168.1.216/westos \ #Specify installation resources
--os-variant rhel8.2 \
#Specify the installation system version --vcpus 1 \ #cpu core number --memory
2048 \
#Memory size --disk /var/lib/libvirt/images/$1.qcow2,size=8,bus=virtio \ #hard disk location, hard disk size , Hard disk working stack
-name $1 \
#virtual machine name -network bridge=br0,model=virtio & #Virtual network card is a bridge, the working mode of the network card is virtual input and output

Insert picture description here

$1 #The first string of characters followed by the script
\ #Newline character, there must be a space before each newline character, representing division, do not add anything after the newline character, including spaces
& # can be followed by & to indicate that it will run in the background

Run sh.westos.sh lee
Insert picture description here
to create a virtual machine named lee

Guess you like

Origin blog.csdn.net/shanshuyue/article/details/113136935
Recommended