KVM-Installation and preliminary use, KVM-Installation and Use

Source: Internet
Author: User
KVM--installation and preliminary use, KVM--installation and use

KVM is short for Kernel-based Virtual Machine and is an open source virtualization module.Today I will install KVM on the CentOS7 operating system.The following are my installation steps.

I. Environmental information

System: CentOS 7.2 IP: 10.0.0.12/24

When installing a virtual machine, you need to enable the virtualization function of the CPU

2. KVM installation steps

1. Install qemu-kvm and libvirt

[root @ kvm-node1 ~] # yum install -y qemu-kvm libvirt ### qemu-kvm is used to create virtual machine hard disk, libvirt is used to manage virtual machine
2. Install virt-install

[root @ kvm-node1 ~] # yum install -y virt-install ### Used to create a virtual machine
3. Start libvirtd and set it to boot. After startup, use ifconfig to check, and you will find an extra virbr0 network card.The default ip is 192.168.122.1/24, indicating that libvirtd started successfully. install -y net-tools installation

[root @ kvm-node1 ~] # systemctl start libvirtd && systemctl enable libvirtd
[root @ kvm-node1 ~] # ifconfig
ens33: flags = 4163 <UP, BROADCAST, RUNNING, MULTICAST> mtu 1500
        inet 10.0.0.12 netmask 255.255.255.0 broadcast 10.0.0.255
        inet6 fe80 :: 5505: 5aa: 3f96: f3f8 prefixlen 64 scopeid 0x20 <link>
        ether 00: 0c: 29: 51: 09: 70 txqueuelen 1000 (Ethernet)
        RX packets 22459 bytes 29147955 (27.7 MiB)
        RX errors 0 dropped 0 overruns 0 frame 0
        TX packets 5161 bytes 587757 (573.9 KiB)
        TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

lo: flags = 73 <UP, LOOPBACK, RUNNING> mtu 65536
        inet 127.0.0.1 netmask 255.0.0.0
        inet6 :: 1 prefixlen 128 scopeid 0x10 <host>
        loop txqueuelen 1 (Local Loopback)
        RX packets 0 bytes 0 (0.0 B)
        RX errors 0 dropped 0 overruns 0 frame 0
        TX packets 0 bytes 0 (0.0 B)
        TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

virbr0: flags = 4099 <UP, BROADCAST, MULTICAST> mtu 1500
        inet 192.168.122.1 netmask 255.255.255.0 broadcast 192.168.122.255
        ether 52: 54: 00: fb: b1: 55 txqueuelen 1000 (Ethernet)
        RX packets 0 bytes 0 (0.0 B)
        RX errors 0 dropped 0 overruns 0 frame 0
        TX packets 0 bytes 0 (0.0 B)
        TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
After the above three steps, the KVM installation is successful.Let's start using KVM to create a virtual machine.

Three. Use KVM to create a virtual machine (CentOS7)

1. Use the qemu command to create a 10G hard disk (minimum 10, G, can be more), the name of the hard disk is: CentOS-7-x86_64.raw

[root @ kvm-node1 ~] # qemu-img create -f raw /opt/CentOS-7-x86_64.raw 10G
Formatting '/opt/CentOS-7-x86_64.raw', fmt = raw size = 10737418240
[root @ kvm-node1 ~] # ll -h / opt
total 0
-rw-r--r-- 1 root root 10G Aug 22 07:59 CentOS-7-x86_64.raw
2. Use virt-install to create a virtual machine named CentOS-7-x86_64. Before creating, upload a CentOS7 ISO image. It is best to create an ISO directory folder under the root directory

[root @ kvm-node1 ~] # virt-install --virt-type kvm --name CentOS-7-x86_64 --ram 1024 --cdrom = / ISO / CentOS-7-x86_64-Everything-1611.iso- disk path = / opt / CentOS-7-x86_64.raw --network network = default --graphics vnc, listen = 0.0.0.0 --noautoconsole
 
Starting install ...
Creating domain ... | 0 B 00:03
Domain installation still in progress. You can reconnect to
the console to complete the installation process.
At this time, use the TightVNC tool to connect to the host IP 10.0.0.12, set the name of the network card where the operating system is installed to eth0,

 

The installation steps are the same as our usual method of installing an operating system.After the installation is complete, you can use virsh list --all to display all virtual machines on KVM

[root @ kvm-node1 ~] # virsh list --all
 Id Name State
-------------------------------------------------- -
 -CentOS-7-x86_64 shut off ### You can see the name and status of the virtual machine
4. Manage KVM

1. Use virsh to start the virtual machine in KVM

[root @ kvm-node1 ~] # virsh start CentOS-7-x86_64
Domain CentOS-7-x86_64 started
After starting, use the VNC connection tool.After logging in, you can see that the name of the network card is eth0, and there is no ip address by default, because the network card is not activated, change ONBOOT to yes, restart the network card, you can see the ip address is 192.168.122.0 network segment of

When the virtual machine in KVM is started, you can see the information of the started virtual machine in the / etc / libvirt / qemu directory

[root @ kvm-node1 ~] # cd / etc / libvirt / qemu
[root @ kvm-node1 qemu] # pwd
/ etc / libvirt / qemu
[root @ kvm-node1 qemu] # ls -lh
total 4.0K
-rw ------- 1 root root 3.8K Aug 22 08:18 CentOS-7-x86_64.xml
drwx ------ 3 root root 42 Aug 22 07:51 networks
CentOS-7-x86_64.xml is the information of the started virtual machine.When we use the vim editor to view, it will prompt the following information:

<!-
WARNING: THIS IS AN AUTO-GENERATED FILE. CHANGES TO IT ARE LIKELY TO BE
OVERWRITTEN AND LOST. Changes to this xml configuration should be made using:
  virsh edit CentOS-7-x86_64
or other application using the libvirt API.
->
Seeing the prompt that you need to modify the information of the virtual machine, you need to use the command virsh edit CentOS-7-x86_64.After using this command, we can see the information of many virtual machines, including vcpu, memory, etc

2. CPU hot add (KVM supports CPU hot add, but the limitation is that the host and virtual machine systems are the same. Here the host is CentOS7 and the virtual machine is also CentOS7, so you can simulate CPU hot add, but the real production environment is not (It is recommended to do this)

[root @ kvm-node1 ~] # virsh edit CentOS-7-x86_64
6 <vcpu placement = 'static'> 1 </ vcpu> was modified to:
    <vcpu placement = 'auto' current = "1"> 4 </ vcpu>
## means that the current CPU is 1 core, and there can be up to 4 cores. After modifying the configuration file of the virtual machine, the virtual machine must be shut down and restarted for the configuration to take effect.
[root @ kvm-node1 ~] # virsh shutdown CentOS-7-x86_64 ## Shut down the virtual machine
Domain CentOS-7-x86_64 is being shutdown

[root @ kvm-node1 ~] # virsh list --all ## View virtual machine status
Id Name State
-------------------------------------------------- -
-CentOS-7-x86_64 shut off

[root @ kvm-node1 ~] # virsh start CentOS-7-x86_64 ## Start the virtual machine
Domain CentOS-7-x86_64 started

[root @ kvm-node1 ~] # virsh list --all
Id Name State
-------------------------------------------------- -
3 CentOS-7-x86_64 running

 
First check the CPU information of the virtual machine through the VNC tool

Add the number of cores through the command on the host machine

[root @ kvm-node1 ~] # virsh setvcpus CentOS-7-x86_64 2 --live ## Add the cpu of the virtual machine to 2core
Then use the VNC tool to view the virtual machine CPU information

3. Memory hot addition (still not recommended for production, just know the function)

Connect to the virtual machine through VNC to view

Virtual machine memory

[root @ kvm-node1 ~] # virsh qemu-monitor-command CentOS-7-x86_64 --hmp --cmd info balloon ## View the current virtual machine memory
balloon: actual = 1024


[root @ kvm-node1 ~] # virsh qemu-monitor-command CentOS-7-x86_64 --hmp --cmd balloon 512 ## Modify the current virtual machine's memory to 512


[root @ kvm-node1 ~] # virsh qemu-monitor-command CentOS-7-x86_64 --hmp --cmd info balloon ## View the modified memory information has been changed to 512
balloon: actual = 512
Use the VNC tool to connect to the virtual machine and check whether the memory has changed:

4. Network card configuration (how to change the network card in the KVM virtual machine to bridge mode so that other users on the LAN can access it normally)

[root @ kvm-node1 ~] # brctl show ## View existing bridges
bridge name bridge id STP enabled interfaces
virbr0 8000.525400fbb155 yes virbr0-nic
                                                        vnet0
Create a script to delete the IP information of the ens33 network card, create a new bridge network card, and enrich the IP information of the ens33 network card for the newly created bridge network card

[root @ kvm-node1 ~] # vim br0.sh
#! / bin / bash
brctl addbr br0 #Create a bridge network card br0
brctl addif br0 ens33 #Transfer the bridging information of ens33 network card to br0 network card
ip addr del dev ens33 10.0.0.12/24 #Remove the ip information of ens33 network card
ifconfig br0 10.0.0.12/24 up #Transfer the IP information of ens33 network card to br0 network card
route add default gw 10.0.0.2 #Add gateway
[root @ kvm-node1 ~] # chmod u + x br0.sh
[root @ kvm-node1 ~] # ./br0.sh
Use ifocnfig to view the ip information of the network card, you can see that the address of br0 has changed to 10.0.0.12/24

[root @ kvm-node1 ~] # ifconfig
br0: flags = 4163 <UP, BROADCAST, RUNNING, MULTICAST> mtu 1500
        inet 10.0.0.12 netmask 255.255.255.0 broadcast 10.0.0.255
        inet6 fe80 :: 20c: 29ff: fe51: 970 prefixlen 64 scopeid 0x20 <link>
        ether 00: 0c: 29: 51: 09: 70 txqueuelen 1000 (Ethernet)
        RX packets 553 bytes 41723 (40.7 KiB)
        RX errors 0 dropped 0 overruns 0 frame 0
        TX packets 680 bytes 63511 (62.0 KiB)
        TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

ens33: flags = 4163 <UP, BROADCAST, RUNNING, MULTICAST> mtu 1500
        inet6 fe80 :: 5505: 5aa: 3f96: f3f8 prefixlen 64 scopeid 0x20 <link>
        ether 00: 0c: 29: 51: 09: 70 txqueuelen 1000 (Ethernet)
        RX packets 6107998 bytes 8708309251 (8.1 GiB)
        RX errors 0 dropped 0 overruns 0 frame 0
        TX packets 534420 bytes 68906654 (65.7 MiB)
        TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

lo: flags = 73 <UP, LOOPBACK, RUNNING> mtu 65536
        inet 127.0.0.1 netmask 255.0.0.0
        inet6 :: 1 prefixlen 128 scopeid 0x10 <host>
        loop txqueuelen 1 (Local Loopback)
        RX packets 0 bytes 0 (0.0 B)
        RX errors 0 dropped 0 overruns 0 frame 0
        TX packets 0 bytes 0 (0.0 B)
        TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

virbr0: flags = 4163 <UP, BROADCAST, RUNNING, MULTICAST> mtu 1500
        inet 192.168.122.1 netmask 255.255.255.0 broadcast 192.168.122.255
        ether 52: 54: 00: fb: b1: 55 txqueuelen 1000 (Ethernet)
        RX packets 6685 bytes 278745 (272.2 KiB)
        RX errors 0 dropped 0 overruns 0 frame 0
        TX packets 6981 bytes 34351624 (32.7 MiB)
        TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

vnet0: flags = 4163 <UP, BROADCAST, RUNNING, MULTICAST> mtu 1500
        inet6 fe80 :: fc54: ff: fe6b: 5fee prefixlen 64 scopeid 0x20 <link>
        ether fe: 54: 00: 6b: 5f: ee txqueuelen 1000 (Ethernet)
        RX packets 16 bytes 1650 (1.6 KiB)
        RX errors 0 dropped 0 overruns 0 frame 0
        TX packets 987 bytes 52236 (51.0 KiB)
        TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
Use the command to modify the information of the KVM virtual machine

[root @ kvm-node1 ~] # virsh edit CentOS-7-x86_64
69 <interface type = 'network'> modified to:
         <interface type = 'bridge'> ## Bridge
71 <source network = 'default' /> modified to:
           <source bridge = 'br0' />
After the modification is completed, shut down the virtual machine, start the virtual machine, modify the ip to 10.0.0.14/24 through the VNC tool, try to ping the host machine to ping, and connect through ssh

[root @ kvm-node1 ~] # virsh shutdown CentOS-7-x86_64
Domain CentOS-7-x86_64 is being shutdown

[root @ kvm-node1 ~] # virsh start CentOS-7-x86_64
Domain CentOS-7-x86_64 started
Through the local ssh software, try to connect, you can connect, and you can see the IP information

The above is the installation and simple management of KVM in CentOS7.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.