KVM Virtualization Foundation Management in Cent os 7

Bowen outline:

  • 1. Installing kvm virtualization;
  • Second, manually change the configuration file, clone web01 and generate a new virtual machine web02.
  • 3. What are the similarities and differences between web01 and web02?
  • Fourth, two methods of remote connection of virtual machine;
  • 5. Use the default NAT network type to enable web02 to ping out.

1. Installing kvm virtualization

[root@localhost media]# yum -y groupinstall "GNOME Desktop"          #Install GNOME Desktop Environment
[root@localhost media]# yum -y install qemu-kvm               #KVM module
[root@localhost media]# yum -y install qemu-kvm-tools             #KVM Debugging Tool, Not Installed
[root@localhost media]# yum -y install virt-install                     #Command Line Tools for Building Virtual Machines
[root@localhost media]# yum -y install qemu-img          #qemu components, creating disks, booting virtual machines, etc
[root@localhost media]# yum -y install bridge-utils               #Network Support Tools
[root@localhost media]# yum -y install libvirt                #Virtual Machine Management Tool
[root@localhost media]# yum -y install virt-manager             #Graphic Interface Management Virtual Machine
[root@localhost media]# yum -y install libguestfs-tools             #Used to manage virtual machine disk format
[root@localhost ~]# ls -sf /lib/systemd/system/graphical.target /etc/systemd/system/default.target
#Change the default target of the system to graphical.target. If you do not execute the command, you may report an error when restarting.

2. After installation, the system needs to be restarted and the following commands are executed to check the system:

[root@localhost ~]# cat /proc/cpuinfo | grep vmx                
#To see if Intel's CPU supports virtualization, if AMD's CPU, you need "cat / proc / cpuinfo | grep smv"
[root@localhost ~]# lsmod | grep kvm                   #Check whether KVM module is installed
kvm_intel             170181  0 
kvm                   554609  1 kvm_intel
irqbypass              13503  1 kvm
[root@localhost ~]# systemctl start libvirtd           #Start the libvirtd service
[root@localhost ~]# systemctl enable libvirtd       #Set this service to boot-up

3. Create virtual machines in command-line mode (refer to blog post for graphical creation of virtual machines: New Virtual Machine for Graphical Desktop)

[root@kvm kk]# mkdir-p/kvm/{iso, disk} # Create the corresponding directory while storing the image and virtual machine disk
 # Created / kvm directories are best mounted on LVS volumes so that volume space can be dynamically expanded
 [root@kvm iso] cp/media/CentOS-7.iso/kvm/iso/ Prepare the VM operating system files to be installed
 [root@kvm~]# qemu-img create-f raw/kvm/disk/web01.raw 10G# create virtual machine disks
 [root @ KVM ~] (virt install -- OS type = linxu -- OS variant centos7.0 -- name web01 -- ram 1024 -- vcpus 1 -- disk / KVM / disk / web01.raw, format = raw, size = 10 -- location / KVM / ISO / centos-7.iso -- Network Network = Default -- graphics VNC, listen = 0.0.0 -- noautoconsole # create virtual machine web01
 Start installing...
Search file. treeinfo... | 354 B 00:00     
Search file vmlinuz... | 5.9 MB 00:00     
Search file initrd.img... | 50 MB 00:00     
Domain installation is still in progress. You can reconnect
 Go to the console to complete the installation process.
# Open the graphical console according to the above instructions
 [root@kvm ~]# virt-manager # Open the console

After entering the command to open the console, the following window pops up:

When you click Open, the following interface will appear, just as we installed the system ourselves (you can operate on your own):

So far, the command line has finished creating the virtual machine. Next, we clone web01 and generate a new virtual machine web02 by manually changing the configuration file (there is a special command tool for cloning in the blog link in front of my blog post, just to understand the configuration file of the virtual machine).

2. Manually changing the configuration file, cloning web01 and generating a new virtual machine web02

[root@kvm iso]# virsh list --all     #Ensure that the virtual machine to be cloned is closed
 Id    Name                         state
----------------------------------------------------
 -     test01                         Close
 -     test02                         Close
 -     web01                          Close      #It's about cloning this one.
[root@kvm ~]# cd /kvm/disk/      #Switch to Virtual Disk Storage Directory of Virtual Machine
[root@kvm disk]# cp web01.raw web02.raw     #Copy a copy of the virtual machine disk to be cloned and change its name
[root@kvm disk]# cd /etc/libvirt/qemu/     #Switch to the directory of KVM virtual machine configuration files
[root@kvm qemu]# ls     #View files in the current directory
networks  test01.xml  test02.xml  web01.xml
[root@kvm qemu]# virsh dumpxml web01 > ./web02.xml  
#Backup the configuration file of web01 to web02.xml in the current directory using dumpxml tool
[root@kvm qemu]# ls         #Confirm that the backup is successful
networks  test01.xml  test02.xml  web01.xml  web02.xml
[root@kvm qemu]# vim web02.xml      #Edit backup files

<domain type='kvm'>
  <name>web02</name>     #Change the name here to web02
  <uuid>2d6f2a7c-9937-4afb-b8d6-8ef01ca61434</uuid>    #Delete the UUID line configuration to prevent conflicts
  <memory unit='KiB'>1048576</memory>
            .............#Omit part of content
  <devices>          #Locate the line label
    <emulator>/usr/libexec/qemu-kvm</emulator>
    <disk type='file' device='disk'>
      <driver name='qemu' type='raw'/>
      <source file='/kvm/disk/web02.raw'/>        #Change the path here to the web02. ray file that you just copied using the cp command
#After the change is complete, save and exit
[root@kvm qemu]# virsh define web02.xml     #Define this configuration
#(It can be understood as loading the virtual machine, at this point, you need to pay attention to the current working path)
//Definition domain Web02 (from web02.xml)

[root@kvm qemu]# virsh list --all     #See if the cloning is successful
 Id    Name                         state
----------------------------------------------------
 -     test01                         Close
 -     test02                         Close
 -     web01                          Close
 -     web02                          Close
#OK! See, you can run web02 on your own, test whether it works properly.
[root@kvm qemu]# virsh start web02     #Start web02
//Domain web02 started

[root@kvm qemu]# virsh list --all     #OK, it's running!
 Id    Name                         state
----------------------------------------------------
 3     web02                          running
          .............#Omit part of content

3. What are the similarities and differences between web01 and web02?

[root@kvm qemu]# virsh dominfo web01 # This command allows you to view information about the specified virtual machine
Id:             -
Name: web01
UUID:           2d6f2a7c-9937-4afb-b8d6-8ef01ca61434
 OS type: hvm
 Status: Close
CPU:           1
 Maximum memory: 1048576 KiB
 Memory used: 1048576 KiB
 Durable: Yes
 Auto Start: Disable # By default, this is disabled
 Management Preservation: No
 Security mode: none
 Safety DOI:0

[root@kvm qemu]# virsh autostart web01 # Set web01 as boot-up self-start
 Domain web01 is marked as automatic start
 [root@kvm qemu]# virsh dominfo web01 # Check to see if it is set to boot-up
Id:             -
Name: web01
UUID:           2d6f2a7c-9937-4afb-b8d6-8ef01ca61434
 OS type: hvm
 Status: Close
CPU:           1
 Maximum memory: 1048576 KiB
 Memory used: 1048576 KiB
 Durable: Yes
 Autostart: Enable #OK!
Management Preservation: No
 Security mode: none
 Safety DOI:0
 [root@kvm qemu]# virsh dominfo web02 # Check whether web02 is boot-up
Id:             3
 Name: web02
UUID:           f554ad15-1b67-4f01-b203-a9d0c3abe192
 OS type: hvm
 Status:running
CPU:           1
 CPU time: 21.4s
 Maximum memory: 1048576 KiB
 Memory used: 1048576 KiB
 Durable: Yes
 Autostart: Disabled # is Disabled
 Management Preservation: No
 Security mode: none
 Safety DOI:0
 # At this point, you can restart the host and view the state of the virtual machine (you need to ensure that the state of the libvirtd service is boot-up, otherwise there will be problems)

[root@kvm qemu] init 6 restart the host
 [root@kvm ~]# virsh list # to see the running virtual machine when the reboot is complete
 Id Name Status
----------------------------------------------------
 1     web01                          running
 # At this point, only web01 is running, because it sets up boot-up self-start, under the premise of libvirtd service boot-up self-start.
# It will start as the host starts up

Fourth, Two Methods of Remote Connecting Virtual Machine

(1) Using VNC software

VNC installation is relatively simple, there is no more writing.

[root@kvm ~]# virsh vncdisplay Web02 <! -- View web02's listening port - > www.virsh.vncdisplay.com
:0
    <!--
The result is ":0", which is explained as follows:
": 0" means listening on port 5900;
": 1" means listening on port 5901;
And so on......
                        -->

Open vnc to connect (pay attention to firewall configuration):

In order to facilitate the next connection using console, the kernel parameters of the virtual machine (web02) need to be adjusted at this time, as follows:

[root@localhost ~]# grubby --update-kernel=ALL --args="console=ttyS0"    #Execute this command in the virtual machine
[root@localhost ~]# init 6     #Restart this virtual machine for changes to take effect

(2) Login web02 with console

The following commands can be executed on the KVM host to successfully log in (provided that the kernel parameters of web02 have been changed when using vnc to log in)

[root@kvm ~]# virsh console web02     #Execute the command
//Connect to domain web02
//The substitution sign is ^]
#Knock the return key.

CentOS Linux 7 (Core)
Kernel 3.10.0-862.el7.x86_64 on an x86_64

web02 login: root     #Enter the username and password of web02 to login.
//Password:
Last login: Wed Oct  9 21:20:48 on tty1
[root@web02 ~]# 

5. Use the default NAT network type to enable web02 to ping out

Here is the test of Ping Baidu. Make sure that the KVM host can ping Baidu.
(1) Check the configuration

[root@kvm ~]# Virsh net-list -- all <! -- Check the current network settings - >
 //Name status automatically starts to persist
----------------------------------------------------------
 default              activity     yes           yes
[root@kvm ~]# Ifconfig <! -- Check the current network interface - >.
          .............<!--Omit part of content-->
virbr0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.122.1  netmask 255.255.255.0  broadcast 192.168.122.255
                <!--
virbr0 It is a virtual network interface generated when the host virtual machine support module is installed.
//It's also a switch and bridge that distributes content to virtual machines.
virbr0 Is a bridge that receives all of the network 192.168.122.*Content.
//Verify from the following command: -->
[root@kvm ~]# brctl show
bridge name bridge id       STP enabled interfaces
virbr0      8000.525400a8bf19   yes     virbr0-nic
                                                                     vnet0
[root@kvm ~]# route
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.1.0     0.0.0.0         255.255.255.0   U     100    0        0 ens33
192.168.122.0   0.0.0.0         255.255.255.0   U     0      0        0 virbr0

(2) Next, start configuring nat networks

[root@kvm ~]# virsh net-define /usr/share/libvirt/networks/default.xml  
#If there is no file specified above, you can use this command to define a virtual network
//You can also modify the file to create your own virtual network
[root@kvm ~]# virsh net-autostart default     #Network default is marked as auto-start
[root@kvm ~]# virsh net-start default       #Start up the network. If it is started, an error will be reported here.
[root@kvm ~]# vim /etc/sysctl.conf    #Modify this configuration file
               ............#Omit part of content
net.ipv4.ip_forward=1       #Add the line at the end of the file to allow IP forwarding
[root@kvm ~]# vim /etc/libvirt/qemu/web02.xml 
                ............#Omit part of content
   <interface type='network'>
      <mac address='52:54:00:24:9d:24'/>
      <source network='default'/>
      <model type='virtio'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/>
    </interface>
#Locate the keyword "interface" and make sure that it has the above content
                              ............#Omit part of content

(3) Connect to the virtual machine web02 and change the configuration of the network card:

[root@web02~]# vi /etc/sysconfig/network-scripts/ifcfg-eth0   #Editing Network Card Profile

After changing the network card, save and exit.

[root@web02~]# systemctl restart network         #Restart web 02 virtual machine network services

ping test:

To use static IP, the configuration file can be changed as follows (DNS configuration can be increased by restarting the network card after the change is completed):

VI. Summary of KVM Virtual Machine Management Commands

[root@kvm ~]# virsh start Virtual Machine Name   #Turn on Virtual Machine
[root@kvm ~]# virsh list    #View only the list of running virtual machines
[root@kvm ~]# virsh –list all       #View a list of all virtual machines
[root@kvm ~]# virsh shutdown Virtual Machine Name    #Normal shutdown of virtual machines
[root@kvm ~]# virsh destroy    Virtual Machine Name   #Force virtual machine shutdown (similar to unplugging)
[root@kvm ~]# virsh dumpxml Virtual Machine Name > /root/test01-dum.xml             #Virtual Machine Backup
[root@kvm ~]# virsh undefine Virtual Machine Name  #Delete virtual machines (Note: Disk files need to be deleted manually after undefined)
[root@kvm ~]# virsh define Backup.xml Virtual Machine Files    #Restore Virtual Machine
[root@kvm ~]# virsh edit Virtual Machine Name  #Edit the configuration file of the virtual machine
[root@kvm ~]# The name of virsh domrename to be changed 
//For example: ([root@kvm~] # virsh domrename test01 test03)] # Change the name of the virtual machine
[root@kvm qemu]# virsh dominfo test02           #View the details of the virtual machine
[root@kvm ~]# virsh autostart test02              #Setting Virtual Machine to Boot-Up Self-Start
[root@kvm ~]# virsh autostart --disable test02    #Cancel boot-up and start-up
[root@kvm ~]# virsh vncdisplay test02           #View the corresponding VNC connection port number; default first port number is: 5900 (denoted by ": 0")
[root@kvm ~]# virsh suspend test02         #Hang up the virtual machine
[root@kvm ~]# virsh resume test02               #Cancel suspension of virtual machine
[root@kvm ~]# virsh console test02           #console login virtual machine

———————— This is the end of the article. Thank you for reading.————————

Tags: network xml yum vnc

Posted on Wed, 09 Oct 2019 16:46:52 -0700 by snrecords