Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to install a kvm virtualized environment

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces how to install the kvm virtualization environment, which has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, let the editor take you to understand it.

Kvm Virtualization Environment installation

1. Operating system installation

This paper uses Centos6.4X64 operating system, can also use RHEL/CentOS6.x.

(1) check the system version and kernel version

View system version

[root@KVM ~] # cat / etc/redhat-release

CentOS release 6.4 (Final)

Check the kernel version

[root@KVM] # uname-a

Linux KVM 2.6.32-358.el6.x86_64 # 1 SMP Fri Feb 22 00:31:26 UTC 2013 x86'64 GNU/Linux

Note: kernels below 2.6.20 need to be upgraded.

(2) shut down selinux,iptables and take effect after restart

1. Close selinux

[root@kvm ~] # vim / etc/sysconfig/selinux

SELINUX=disabled

two。 Close iptables

[root@kvm] # iptables-F

[root@kvm] # iptables-X

[root@kvm ~] # / etc/init.d/iptables save

[root@kvm ~] # / etc/init.d/iptables stop

(3) restart

[root@kvm ~] # reboot

two。 Virtualized environment configuration

(1) Centos in VMware enables virtualization

Note 1: paravirtualization cannot run and install KVM virtual machines.

(2) check whether the system supports KVM virtualization

[root@KVM ~] # egrep'(vmx | svm)'- color=always / proc/cpuinfo

If the actual environment does not exist, you need to enable Virtual Technolege (VT, virtualization technology) in the motherboard BIOS.

(3) install kvm software package

[root@KVM ~] # yum-y install kvm python-virtinst libvirt tunctl bridge-utils virt-manager qemu-kvm-tools virt-viewer virt-v2v libguestfs-tools qemu-kvm gcc*

3. View virtualized environment

(1) View the virtual machine environment

[root@KVM ~] # / etc/init.d/libvirtd start

[root@KVM ~] # virsh-c qemu:///system list

Id Name State

-

(2) check kvm module support

[root@KVM ~] # lsmod | grep kvm

Kvm_intel 55624 0

Kvm 338535 1 kvm_intel

(3) View the version of the virtual tool

[root@KVM ~] # virsh-version

0.10.2

[root@KVM ~] # virt-install-version

0.600.0

[root@KVM] # ln-s / usr/libexec/qemu-kvm / usr/bin/qemu-kvm

[root@KVM ~] # qemu-kvm-version

QEMU PC emulator version 0.12.1 (qemu-kvm-0.12.1.2-2.448.el6_6.4), Copyright (c) 2003-2008 Fabrice Bellard

4. Manually configure a virtual bridge

(1) disable networkmanager service

[root@KVM ~] # / etc/init.d/NetworkManager stop

[root@KVM ~] # chkconfig NetworkManager off

(2) create br0 net k

[root@KVM ~] # cd / etc/sysconfig/network-scripts/

[root@KVM network-scripts] # cp ifcfg-eth0 ifcfg-br0

[root@KVM network-scripts] # vim ifcfg-eth0

DEVICE=eth0

HWADDR=00:0C:29:41:DA:81

TYPE=Ethernet

UUID=21501ff5-e313-494c-a402-50f2a84f1f5c

ONBOOT=yes

NM_CONTROLLED=yes

BOOTPROTO=staticIPADDR=192.168.15.14NETMASK=255.255.255.0GATEWAY=192.168.15.2

BRIDGE=br0

[root@KVM network-scripts] # vim ifcfg-br0

DEVICE=br0

ONBOOT=yes

NM_CONTROLLED=yes

BOOTPROTO=static

IPADDR=192.168.15.14

NETMASK=255.255.255.0

GATEWAY=192.168.15.2

DNS1=114.114.114.114

TYPE=Bridge

(3) restart network service.

[root@KVM network-scripts] # / etc/init.d/network restart

(4) check the bridge br0.

(4) View the bridge

At this point, the kvm virtualization environment is installed.

Kvm Virtualization Learning Notes (2) installation of windows kvm virtual machines

Start installing windows 2003 (1) upload 2003 iso files to / data/iso folder

(2) start installation

[root@KVM ~] # cat create_win_kvm.sh

Virt-install\

-- name=win2003_1\

-- ram 1024\

-- vcpus=2\

-- disk path=/data/img/win2003_1.img,size=8\

-- accelerate\

-- cdrom / data/win_server_2003_R2x64.iso\

-- vnc\

-- vncport=5915\

-- vnclisten=0.0.0.0\

-network bridge=br0-force-autostart

(3) connect through vnc, and then proceed with the installation process, or you can continue the installation process through the console.

Note: after the file is copied, windows will restart, where the virtual machine is shut down, it needs to be started manually, and VNC cannot be used.

The following appears when installing wind2003 with a vnc connection after starting win2003_1

(4) connect to the above and log in to the console.

If other environments cannot connect to the console, you can install vnc server-side tools remotely and log in to the server through vnc, which can be done securely. There's no need for us to take two steps.

# # double-click win2003_1

(5) manually mount the optical drive, as shown in the following figure, the step-by-step operation is as follows.

Return to the console and continue the installation process.

(6) enter the system to update the drivers of the network card and SCSI controller, and the new hardware will be applied after restart.

5. Resolve mouse synchronization of KVM virtual machine

Problems such as the inflexibility of the mouse in the kvm virtualized windows operating system are solved here in the following ways. Add to the configuration of the virtual machine:

one

2 # virsh edit wintest01

Test the virsh console command [root@KVM ~] # virsh console centos2

At this point, the virsh console command has been configured in the console, and the convenient console mode of the linux virtual machine will be managed in the future.

Kvm Virtualization Learning Notes (7) kvm Virtual Machine Clone

The cloning of kvm virtual machine is divided into two cases. In this paper, we clone the virtual machine as Centos 6.4X64 through the following two cases.

(1) the native virtual machine of the KVM host is cloned directly.

(2) copy the virtual machine replication clone of the configuration file and the disk file (suitable for the static migration of different machines).

1. Direct cloning of native virtual machines

(1) View the virtual machine configuration file

[root@KVM ~] # cat / etc/libvirt/qemu/centos3.xml

Virtual machine disk file: / data/img/Centos3.img

Virtual machine name: Centos3

[root@KVM ~] # virsh list-all

(2) start cloning

[root@KVM] # virt-clone-o centos3-n Linux3-f / data/img/Linux3.img

Note: clone virtual machine should be kept off, use centos3 as source, clone centos3 virtual machine, and create a virtual machine named Linux3, using disk file / data/img/Linux3.img

(3) start the virtual machine

[root@KVM ~] # virsh start Linux3

[root@KVM ~] # virsh list

(4) modify host name, IP address, etc.

[root@localhost ~] # hostname Linux3

[root@localhost ~] # bash

[root@Linux3 ~] # vi / etc/sysconfig/network

HOSTNAME=Linux3

Modify IP address

[root@Linux3 ~] # vi / etc/sysconfig/network-scripts/ifcfg-eth0

ONBOOT=yes

NM_CONTROLLED=yes

BOOTPROTO=static

IPADDR=192.168.15.135

NETMASK=255.255.255.0

GATEWAY=192.168.15.2

DNS1=114.114.114.114

[root@Linux3 ~] # / etc/init.d/network restart

two。 Copy configuration files and disk file cloning

(1) Import kvm virtual machine configuration file

Here, centos3 is used as a template for cloning.

[root@KVM ~] # virsh list-all

[root@KVM ~] # virsh dumpxml centos3 > / etc/libvirt/qemu/Linux03.xml

(2) copy kvm virtual machine disk files

This series of articles are tested with virtual disk files, not lvm volumes.

By viewing the configuration file: virtual disk location

[root@KVM ~] # cp / data/img/Centos3.img / data/img/Linux03.img

(3) directly edit and modify the configuration file

Modify name,uuid,disk location, vnc port

The profile is still registered at this time and cannot be edited through virsh edit.

[root@node1 data] # vi / etc/libvirt/qemu/Linux03.xml

Linux03

8f2bb4a7-c7ed-32aa-3676-9fb05923260d

(4) define a new virtual machine profile

[root@KVM ~] # virsh define / etc/libvirt/qemu/Linux03.xml

Start the virtual machine

[root@KVM ~] # virsh start Linux03

(5) Log in to the virtual machine to modify the hostname, IP, etc.

[root@localhost ~] # vi / etc/sysconfig/network

HOSTNAME=Linux03

[root@localhost ~] # vi / etc/sysconfig/network-scripts/ifcfg-eth0

ONBOOT=yes

NM_CONTROLLED=yes

BOOTPROTO=static

IPADDR=192.168.15.133

NETMASK=255.255.255.0

GATEWAY=192.168.15.2

DNS1=114.114.114.114

So far in this article, there are two ways to clone kvm virtual machines, each of which has its own use.

Again: the virtual machine cloned in this article is Centos6.4. If the cloned virtual machine is the RHEL/CentOS/OEL7.x operating system, the / etc/udev/rules.d/70-persistent-net.rules file needs to be modified.

Kvm Virtualization Learning Notes (8) kvm virtual machine vnc configuration

The purpose of this paper is to access the KVM virtual machine on the virtual host through vnc.

Accessing the virtual machine through vnc here is not the installation and configuration of the vnc server in the kvm virtual machine, but through the IP address and port of the virtual host. Kvm virtualization supports vnc much better than xen. Configuring VNC to access the virtual machine on the virtual host is also to provide one more way to access the virtual machine.

1. Modify qemu.conf

[root@KVM ~] # vim / etc/libvirt/qemu.conf

Vnc_listen = "0.0.0.0"

Note that 1:vnclisten default binding 127.0.0.1 specifies the VNC binding 0.0.0.0IP in the configuration file, so there is no need to specify the vnclisten parameter when installing the kvm virtual machine.

Note 2: when there are many virtual machines on the virtual host, you need to specify the port of each virtual machine, otherwise it will be very messy.

two。 Modify virtual machine configuration fil

[root@KVM ~] # virsh edit Linux03

Start the virtual machine and test the VNC

Vnc tool login

Change to character interface startup, vnc can also appear interface, which is better than xen virtualized vnc. Log in to vnc as follows. So far, kvm virtual machine management has been introduced in two ways, one is to configure VNC on the KVM virtual host, and the other is to configure the virsh console console on the kvm virtual host, not to mention the virtualization manager.

Kvm Virtualization Learning Notes (9) kvm Virtual Machine time configuration

Note: in a virtualized environment, the time of a virtual machine slows down during a long run, and it is common practice to configure ntpdate timing to synchronize time with the time server.

The KVM virtual machine uses utc time by default, which requires special modification, as well as considering kvm time synchronization issues.

1. Kvm virtual machine modification time profile

Kvm virtual machine uses utc time, so you need to modify the configuration file to synchronize the time of using KVM virtual machine with the virtual host.

[root@KVM ~] # virsh edit Linux03

On the kvm virtual machine time problem solving ideas (1) slow virtual machine time is a common problem of all virtualization platforms. (2) the final solution to the time problem is to configure the time server in the production environment. Both the kvm virtualized server and the KVM machine have to configure the scheduled task of time synchronization, which is the final solution to this problem. Example: if you can access the Internet, the easiest thing is to synchronize the kvm virtual machine with the host time, but it will be slow to run for a period of time, so be sure to do time synchronization, especially in time-sensitive environments.

Crontab-e

01 * / 3 * / usr/sbin/ntpdate ntp.fudan.edu.cn > > / dev/null 2 > & 1

The time configuration of virtual machine is discussed in this article.

Kvm Virtualization Learning Notes (10) kvm virtual machine snapshot backup

Kvm virtual machine uses raw image format by default, which has the best performance and the fastest speed. Its disadvantage is that it does not support some new functions, such as supporting mirroring, zlib disk compression, AES encryption and so on.

To use the mirroring feature, the disk format must be qcow2. Let's start the process of kvm virtual machine snapshot backup.

Further study reference: kvm+libvirt Virtual Machine Snapshot Analysis of http://itxx.sinaapp.com/blog/content/130

1. View existing disk image format and conversion

(1) View disk format

[root@KVM ~] # qemu-img info / data/img/kvm_centos2.img

Snapshot management of virtual machines (1) create a snapshot of an oeltest01 virtual machine or you can create an alias for the last snapshot by virsh snapshot-create-as centos2 centos2_snapshot. [root@KVM ~] # virsh snapshot-create-as centos2 centos2_snaphshot

(2) View the version of the virtual machine image snapshot

[root@KVM ~] # virsh snapshot-list centos2

(3) View the version of the current virtual machine image snapshot

You can see that this is the latest snapshot version.

[root@KVM ~] # virsh snapshot-create-as centos2 centos2_snaphshot

[root@KVM ~] # virsh snapshot-current centos2

Start centos2, install ftp, and take a snapshot.

[root@KVM ~] # virsh start centos2

[root@centos2 ~] # yum-y install vsftpd

[root@KVM ~] # virsh snapshot-create-as centos2 centos_installftp

[root@KVM ~] # qemu-img info / data/img/kvm_centos2.qcow2

Snapshot profile is under / var/lib/libvirt/qemu/snapshot/ virtual machine name /

4. Restore virtual machine snapshot

(1) to restore a virtual machine snapshot, you must shut down the virtual machine.

[root@KVM ~] # virsh shutdown centos2

Confirm that the virtual machine is powered off

(2) confirm the snapshot time that needs to be restored. Restore to centos2_installftp here.

(3) perform the restore and confirm the restore version

[root@KVM ~] # virsh snapshot-revert centos2 centos2_installftp

(4) Log in to centos2 to verify the ftp command

Expand the existing virtual machine disk size directly through the dd command. After the expansion, the original disk size increases. After entering the virtual machine system, the newly added space is partitioned directly through the fdisk partition, and then the partition is incorporated into the lvm logical volume to expand the disk space.

1. Turn off capacity expansion kvm (centos1)

[root@kvm ~] # virsh shutdown centos1

[root@kvm ~] # virsh list-all

two。 Directly expand existing disk space (centos1_new.img)

[root@kvm ~] # du-sh / data/img/centos1_new.img

[root@kvm ~] # dd if=/dev/zero bs=1024MB count=2 > > / data/img/centos1_new.img

3. Start kvm (centos1)

[root@kvm ~] # virsh start centos1

[root@kvm ~] # virsh list

4. Check the list of partitions. (note: it is easy to create a partition)

[root@centos1] # df-h

[root@centos1 ~] # fdisk-l / dev/sdb

2. The idea of expanding qcow2 disk format is as follows.

(1) the expansion of raw disk format disk can be done in a consistent way.

(2) qcow2 format disk, expand qcow2 disk directly through qemu-img, add a new disk of raw format to KVM virtual machine, and then manage it through virtual machine system lvm logical volume management to expand disk space.

Next you will begin to add disks in qcow2 format.

Method 1: add a qcow2 disk to join the virtual machine

1. View current virtual machine disk information

1. View kvm virtual machine (centos1) disk format

[root@kvm ~] # virsh edit centos1

two。 View disk format information

[root@kvm ~] # qemu-img info / data/img/kvm_centos1.qcow2

3. Enter the virtual machine and view the disk capacity

[root@centos1] # df-h

4. Add a qcow2 disk

[root@kvm] # qemu-img create-f qcow2 / data/img/kvm_centosA1.qcow2 2G

5. Close kvm (centos1) to add a qcow2 disk information to join the configuration file

[root@kvm ~] # virsh shutdown centos1

[root@kvm ~] # virsh edit centos1

6. Start the virtual machine and configure the disk

Root@kvm ~] # virsh start centos1

[root@centos1 ~] # fdisk-l

two。 Configure LVM expansion disk

1. Disk partition

[root@centos1 ~] # fdisk / dev/sdb

two。 Expand the lvm logical volume.

[root@centos1 ~] # pvcreate / dev/sdb1

[root@centos1 ~] # pvs

PV VG Fmt Attr PSize PFree

/ dev/sda2 VolGroup lvm2 a-9.51g 0

/ dev/sdb1 lvm2 a-2.00g 1020.00m

[root@centos1 ~] # vgextend VolGroup / dev/sdb1

[root@centos1] # lvextend-L + 1024MB / dev/mapper/VolGroup-lv_root

[root@centos1 ~] # resize2fs / dev/mapper/VolGroup-lv_root

[root@centos1] # df-h

As you can see, the root "/" partition has increased by 1G capacity.

Method 2: directly expand the qcow2 disk (use kvm_Centos4 as a demonstration)

(1) shut down the virtual machine.

(2) Delete the second disk information in the configuration file

1. Directly extend qcow2 disk

(1) View the disk

[root@kvm ~] # virsh edit centos40

(2) directly expand the qcow2 disk

[root@kvm ~] # qemu-img resize / data/img/kvm_centos4.qcow2 + 4G after comparison, the disk size has been expanded from 14G to 18G, and the qcow2 disk format must be expanded in this way. Do not use dd, dd is suitable for raw format.

two。 Enter the system and start expanding the disk

(1) start kvm (centos4) and check the partition size

[root@kvm ~] # virsh start centos4

[root@centos4 ~] # fdisk-l

(2) start zoning

[root@centos4 ~] # fdisk / dev/vda View current disk space

[root@centos4 ~] # df-h (3) format the partition and mount it.

[root@centos4 ~] # mkfs.ext4 / dev/vda3

[root@centos4 ~] # mkdir / database

[root@centos4 ~] # mount / dev/vda3 / data

[root@centos4] # df-h successfully expands the disk space of this kvm virtual machine. When the disk format of the virtual machine is changed to qcow2, there are indeed many points to pay attention to. Qcow2 format supports dynamic expansion and snapshot functions, although it is slightly worse than raw, but it is indeed very practical.

Thank you for reading this article carefully. I hope the article "how to install kvm Virtualization Environment" shared by the editor will be helpful to you. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report