37.第31章 企业级KVM虚拟化

一.安装KVM

1.安装KVM工具包

[root@centos7 ~]# yum -y install qemu-kvm qemu-kvm-tools libvirt libvirt-client virt-manager virt-install

[root@centos7 ~]# systemctl enable --now libvirtd
[root@centos7 ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 00:0c:29:6a:7e:b8 brd ff:ff:ff:ff:ff:ff
    inet 10.0.0.7/24 brd 10.0.0.255 scope global noprefixroute eth0
       valid_lft forever preferred_lft forever
    inet6 fe80::20c:29ff:fe6a:7eb8/64 scope link 
       valid_lft forever preferred_lft forever
3: virbr0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default qlen 1000
    link/ether 52:54:00:0f:d9:91 brd ff:ff:ff:ff:ff:ff
    inet 192.168.122.1/24 brd 192.168.122.255 scope global virbr0
       valid_lft forever preferred_lft forever
4: virbr0-nic: <BROADCAST,MULTICAST> mtu 1500 qdisc pfifo_fast master virbr0 state DOWN group default qlen 1000
link/ether 52:54:00:0f:d9:91 brd ff:ff:ff:ff:ff:ff

[root@centos7 ~]# grep -R 192.168.122.1 /etc
/etc/libvirt/qemu/networks/autostart/default.xml:  <ip address='192.168.122.1' netmask='255.255.255.0'>
/etc/libvirt/qemu/networks/default.xml:  <ip address='192.168.122.1' netmask='255.255.255.0'>

[root@centos7 ~]# ll /etc/libvirt/qemu/networks/autostart/default.xml
lrwxrwxrwx 1 root root 14 Apr 11 15:05 /etc/libvirt/qemu/networks/autostart/default.xml -> ../default.xml

[root@centos7 ~]# ll /etc/libvirt/qemu/networks/default.xml
-rw------- 1 root root 576 Apr 11 15:06 /etc/libvirt/qemu/networks/default.xml


[root@centos7 ~]# vim /etc/libvirt/qemu/networks/default.xml
<!--
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 net-edit default
or other application using the libvirt API.
-->

<network>
  <name>default</name>
  <uuid>da8b956c-4336-4672-b824-b76b50a69405</uuid>
  <forward mode='nat'/>
  <bridge name='virbr0' stp='on' delay='0'/>
  <mac address='52:54:00:0f:d9:91'/>
  <ip address='10.10.10.1' netmask='255.255.255.0'>
    <dhcp>
      <range start='10.10.10.10' end='10.10.10.254'/>
    </dhcp>
  </ip>
</network>
:wq

[root@centos7 ~]# systemctl restart libvirtd

[root@centos7 ~]# reboot

2.virt-manager工具使用


设置xshell

安装Xmanager ,并打开Xmanager - Passive工具

[root@centos7 ~]# virt-manager

3.创建NAT网络虚拟机

#提前上传安装镜像
[root@centos7 ~]# ll /data/ISOs/CentOS-7-x86_64-Everything-2009.iso  
-rw-r--r-- 1 qemu qemu 10200547328 Apr 11 15:27 /data/ISOs/CentOS-7-x86_64-Everything-2009.iso

#创建raw格式磁盘
[root@centos7 ~]# qemu-img create -f raw /var/lib/libvirt/images/hf.raw 10G  
Formatting '/var/lib/libvirt/images/hf.raw', fmt=raw size=10737418240

#创建NAT网络虚拟机
[root@centos7 ~]# virt-install --virt-type kvm --name centos7_hf --memory 1024 --vcpus 2 --cdrom=/data/ISOs/CentOS-7-x86_64-Everything-2009.iso --disk path=/var/lib/libvirt/images/hf.raw --network network=default --graphics vnc,listen=0.0.0.0 --noautoconsole --autostart
WARNING  No operating system detected, VM performance may suffer. Specify an OS with --os-variant for optimal results.

centos7 安装过程这里就不说了

4,创建bridge网络虚拟机

[root@centos7 ~]# vim /etc/sysconfig/network-scripts/ifcfg-eth0 
DEVICE=eth0
NAME=eth0
BOOTPROTO=none
ONBOOT=yes
BRIDGE=br0
:wq

[root@centos7 ~]# vim /etc/sysconfig/network-scripts/ifcfg-br0 
TYPE=Bridge
BOOTPROTO=static
NAME=br0                                                                                                              
DEVICE=br0
ONBOOT=yes
IPADDR=10.0.0.7
NETMASK=255.255.255.0
GATEWAY=10.0.0.2
DNS1=10.0.0.2
:wq

[root@centos7 ~]# systemctl restart network

[root@centos7 ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast master br0 state UP group default qlen 1000
    link/ether 00:0c:29:6a:7e:b8 brd ff:ff:ff:ff:ff:ff
3: virbr0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default qlen 1000
    link/ether 52:54:00:0f:d9:91 brd ff:ff:ff:ff:ff:ff
    inet 10.10.10.1/24 brd 10.10.10.255 scope global virbr0
       valid_lft forever preferred_lft forever
4: virbr0-nic: <BROADCAST,MULTICAST> mtu 1500 qdisc pfifo_fast master virbr0 state DOWN group default qlen 1000
    link/ether 52:54:00:0f:d9:91 brd ff:ff:ff:ff:ff:ff
7: vnet0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast master virbr0 state UNKNOWN group default qlen 1000
    link/ether fe:54:00:f8:14:e5 brd ff:ff:ff:ff:ff:ff
    inet6 fe80::fc54:ff:fef8:14e5/64 scope link 
       valid_lft forever preferred_lft forever
8: br0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default qlen 1000
    link/ether 00:0c:29:6a:7e:b8 brd ff:ff:ff:ff:ff:ff
    inet 10.0.0.7/24 brd 10.0.0.255 scope global noprefixroute br0
       valid_lft forever preferred_lft forever
    inet6 fe80::e83e:4ff:feb3:5031/64 scope link 
       valid_lft forever preferred_lft forever

[root@centos7 ~]# brctl show
bridge name	bridge id		STP enabled	interfaces
br0		8000.000c296a7eb8	no		eth0
virbr0		8000.5254000fd991	yes		virbr0-nic
							vnet0

#创建qcow2格式磁盘
[root@centos7 ~]# qemu-img create -f qcow2 /var/lib/libvirt/images/hf.qcow2 10G
Formatting '/var/lib/libvirt/images/hf.qcow2', fmt=qcow2 size=10737418240 encryption=off cluster_size=65536 lazy_refcounts=off 

#创建桥接网络虚拟机
[root@centos7 ~]# virt-install --virt-type kvm --name centos7_bridge --memory 1024 --vcpus 2 --cdrom=/data/ISOs/CentOS-7-x86_64-Everything-2009.iso --disk path=/var/lib/libvirt/images/hf.qcow2 --network bridge=br0 --graphics vnc,listen=0.0.0.0 --noautoconsole --autostart
WARNING  No operating system detected, VM performance may suffer. Specify an OS with --os-variant for optimal results.

Starting install...
Domain installation still in progress. You can reconnect to 
the console to complete the installation process.


centos安装过程这里就不说了

[root@centos7 ~]# brctl show
bridge name	bridge id		STP enabled	interfaces
br0		8000.000c296a7eb8	no		eth0
							vnet0
							vnet1
virbr0		8000.5254000fd991	yes		virbr0-nic

[C:\~]$ ssh root@10.0.0.161
[root@10 ~]# systemctl disable firewalld
[root@10 ~]# vim /etc/sysconfig/selinux
SELINUX=disabled
:wq

[root@10 ~]# shutdown

通过virt-manager工具,把磁盘和网卡驱动改成virtio

5.克隆虚拟机

[root@centos7 ~]# ll /var/lib/libvirt/images/
total 5341976
-rw------- 1 qemu qemu  1727266816 Apr 11 17:40 hf-clone.qcow2
-rw-r--r-- 1 root root  1768357888 Apr 11 17:37 hf.qcow2
-rw-r--r-- 1 qemu qemu 10737418240 Apr 11 17:38 hf.raw


[C:\~]$ ssh root@10.0.0.163
[root@10 ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 52:54:00:10:9d:5a brd ff:ff:ff:ff:ff:ff
    inet 10.0.0.163/24 brd 10.0.0.255 scope global noprefixroute dynamic eth0
       valid_lft 1732sec preferred_lft 1732sec
    inet6 fe80::b64:f12f:2fb8:3662/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever [root@10 ~]# yum -y install httpd

[root@10 ~]# echo "10.0.0.160 web page" > /var/www/html/index.html

[root@10 ~]# systemctl enable --now httpd
Created symlink from /etc/systemd/system/multi-user.target.wants/httpd.service to /usr/lib/systemd/system/httpd.service.

[root@10 ~]# echo "10.0.0.163 web page" > /var/www/html/index.html

6.创建windows虚拟机

#上传镜像文件和virtio驱动
[root@centos7 ~]# ll /data/ISOs/
total 13257236
-rw-r--r-- 1 root root  3368962048 Apr 11 18:19 cn_windows_server_2008_r2_standard_enterprise_datacenter_and_web_with_sp1.iso
-rw-r--r-- 1 root root     2949120 Apr 11 18:20 virtio-win-0.1.141_amd64.vfd

#创建磁盘
[root@centos7 ~]# qemu-img create -f qcow2 /var/lib/libvirt/images/Windows-2008_r2-x86_64.qcow2 200G
Formatting '/var/lib/libvirt/images/Windows-2008_r2-x86_64.qcow2', fmt=qcow2 size=214748364800 encryption=off cluster_size=65536 lazy_refcounts=off

#创建windows虚拟机
[root@centos7 ~]# virt-install --virt-type kvm --name Win_2008_r2-x86_64 --memory 1024 --vcpus=2 --os-type=windows --cdrom=/data/ISOs/cn_windows_server_2008_r2_standard_enterprise_datacenter_and_web_with_sp1.iso --disk path=/var/lib/libvirt/images/Windows-2008_r2-x86_64.qcow2,format=qcow2,bus=virtio --disk path=/data/ISOs/virtio-win-0.1.141_amd64.vfd,device=floppy --network bridge=br0,model=virtio --graphics vnc,listen=0.0.0.0 --noautoconsole --autostart

Starting install...
Domain installation still in progress. You can reconnect to 
the console to complete the installation process.

注意在这里要加载网卡和磁盘驱动


把这两个驱动分别加载,不然无法识别磁盘和网卡


windows安装过程,这里就不说了。

posted @ 2021-04-11 22:56  网络之鹰  阅读(751)  评论(0编辑  收藏  举报