KVM创建虚拟机相关操作

KVM虚拟机知识训练!

KVM虚拟化项目

根据下面的架构图实现一个简单的虚拟化平台。

 

 

一、创建虚拟机cirrosoautoconsole

 

挂在yum源:

备份原文件,创建一个新的repo文件

[root@localhost yum.repos.d]# touch localxpl.repo

[root@localhost yum.repos.d]# ls

bak  localxpl.repo

编辑创建的yum源文件

[root@localhost yum.repos.d]# vi localxpl.repo

name=media

baseurl=file:///media

enabled=1

gpgcheck=0

挂在yum

[root@localhost ~]# mkdir media

[root@localhost media]# mount /dev/cdrom /media

mount: /dev/sr0 is write-protected, mounting read-only

[root@localhost ~]# cd /media

[root@localhost media]# ls

CentOS_BuildTag  GPL       LiveOS    RPM-GPG-KEY-CentOS-7

EFI              images    Packages  RPM-GPG-KEY-CentOS-Testing-7

EULA             isolinux  repodata  TRANS.TBL

 

[root@localhost media]# yum makecache

Loaded plugins: fastestmirror

Determining fastest mirrors

localxpl                                                 | 3.6 kB     00:00     

(1/4): localxpl/group_gz                                   | 165 kB   00:00     

(2/4): localxpl/primary_db                                 | 3.2 MB   00:00     

(3/4): localxpl/filelists_db                               | 3.3 MB   00:01     

(4/4): localxpl/other_db                                   | 1.3 MB   00:00     

Metadata Cache Created

 

[root@localhost media]# yum repolist

Loaded plugins: fastestmirror

Loading mirror speeds from cached hostfile

repo id                            repo name                            status

localxpl                             localxpl                             4,067

repolist: 4,067

查看系统cpu是否支持虚拟化

# grep -o -E 'svm|vmx' /proc/cpuinfo

[root@localhost media]#  grep -o -E 'svm|vmx' /proc/cpuinfo

vmx

vmx

1、 查看是否加载kvm模块,如果没有加载kvm模块

[root@localhost ~]# cat /proc/cpuinfo

[root@localhost ~]# lsmod | grep kvm_intel

kvm_intel             188644  0

kvm                  621480  1 kvm_intel

查看内核已加载模块 # lsmod

[root@localhost ~]# lsmod

加载模块 # modprobe kvm_intel

[root@localhost ~]# modprobe kvm_intel

2、 安装相关包

[root@localhost media]#  yum -y install qemu-kvm libvirt virt-install

qemu-kvm: kvm主程序,kvm虚拟化模块

libvirt: 虚拟化服务

virt-install:安装虚拟机的实用工具

3启动服务

# systemctl start libvirtd

# systemctl enable libvirtd

[root@localhost media]#  systemctl start libvirtd

[root@localhost media]# systemctl enable libvirtd

# ip address

[root@localhost media]# ip addr

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: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000

    link/ether 00:0c:29:8d:79:fa brd ff:ff:ff:ff:ff:ff

    inet 172.16.124.108/24 brd 172.16.124.255 scope global noprefixroute ens33

       valid_lft forever preferred_lft forever

    inet6 fe80::20c:29ff:fe8d:79fa/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:9e:d5:ef 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:9e:d5:ef brd ff:ff:ff:ff:ff:ff

[root@localhost media]#

4准备虚拟机启动镜像,上传centos7iso文件,上传cirros磁盘映像文件。上传至/opt

 

 

 

 

5关闭防火墙,以免影响网络访问

root@localhost ~]# systemctl stop firewalld

root@localhost ~]#systemctl disable firewalld

root@localhost ~]#setenforce 0

进入:

[root@localhost ~]# cat /etc/selinux/config

# This file controls the state of SELinux on the system.

# SELINUX= can take one of these three values:

#     enforcing - SELinux security policy is enforced.

#     permissive - SELinux prints warnings instead of enforcing.

#     disabled - No SELinux policy is loaded.

SELINUX=disabled

# SELINUXTYPE= can take one of three values:

#     targeted - Targeted processes are protected,

#     minimum - Modification of targeted policy. Only selected processes are protected.

#     mls - Multi Level Security protection.

SELINUXTYPE=targeted

6、默认网络NAT

(1) 创建虚拟机,系统镜像cirros,名称cirros,内存256M,网络模式NAT,创建完成后使用virsh console登陆。

# virt-install --virt-type kvm --name cirros --ram 256 –boot hd --disk path=/opt/cirros-0.3.4-x86_64-disk.img --network network=default --graphics vnc,listen=0.0.0.0 –noautoconsole我不知道为啥名称不对,没有输错,尝试了几次名字都还是oautoconsole

已经解决:发现是"-boot"输错了。

可以先删除原本的KVM虚拟机(oautoconsole):
[root@localhost ~]# virsh destroy cirros 停止虚拟机
[root@localhost ~]# virsh undefine cirros 删除虚拟机
[root@localhost ~]# rm -f cirros.xml 删除文件。

 

然后重新创建就OK啦!

 

 virt-install --virt-type kvm --name cirros --ram 256 --boot hd --disk path=/opt/cirros-0.3.4-x86_64-disk.img --network network=default --graphics vnc,listen=0.0.0.0 --noautoconsole

 

 

[root@localhost ~]# virsh start oautoconsole

Domain oautoconsole started

[root@localhost ~]# virsh list --all

 Id    Name                           State

----------------------------------------------------

 1     oautoconsole                   running

(2) 进入虚拟机系统中

① 通过virsh控制台登陆

 root@cirros

-----END SSH HOST KEY KEYS-----

=== network info ===

if-info: lo,up,127.0.0.1,8,::1

if-info: eth0,up,192.168.122.226,24,fe80::5054:ff:fe02:774

ip-route:default via 192.168.122.1 dev eth0

ip-route:192.168.122.0/24 dev eth0  src 192.168.122.226

=== datasource: None None ===

=== cirros: current=0.3.4 uptime=248.70 ===

  ____               ____  ____

 / __/ __ ____ ____ / __ \/ __/

/ /__ / // __// __// /_/ /\ \

\___//_//_/  /_/   \____/___/

   http://cirros-cloud.net

 

 

login as 'cirros' user. default password: 'cubswin:)'. use 'sudo' for root.

cirros login: cirros

Password:

$ ls

$ ip add

1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue

    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00

    inet 127.0.0.1/8 scope host lo

    inet6 ::1/128 scope host

       valid_lft forever preferred_lft forever

2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast qlen 1000

    link/ether 52:54:00:02:07:74 brd ff:ff:ff:ff:ff:ff

    inet 192.168.122.226/24 brd 192.168.122.255 scope global eth0

    inet6 fe80::5054:ff:fe02:774/64 scope link

       valid_lft forever preferred_lft forever

② 通过VNC远程桌面软件登陆

安装vnc工具:yum -y install tigervnc-server

[root@localhost Packages]# yum -y install tigervnc-server

Loaded plugins: fastestmirror

Loading mirror speeds from cached hostfile

Resolving Dependencies

--> Running transaction check

---> Package tigervnc-server.x86_64 0:1.8.0-17.el7 will be installed

--> Processing Dependency: xorg-x11-xinit for package: tigervnc-server-1.8.0-17.el7.x86_64

--> Processing Dependency: xorg-x11-xauth for package: tigervnc-server-1.8.0-17.el7.x86_64

--> Processing Dependency: tigervnc-server-minimal for package: tigervnc-server-1.8.0-17.el7.x86_64

--> Processing Dependency: libXdamage.so.1()(64bit) for package: tigervnc-server-1.8.0-17.el7.x86_64

--> Running transaction check

---> Package libXdamage.x86_64 0:1.1.4-4.1.el7 will be installed

--> Processing Dependency: libXfixes.so.3()(64bit) for package: libXdamage-1.1.4-4.1.el7.x86_64

---> Package tigervnc-server-minimal.x86_64 0:1.8.0-17.el7 will be installed

--> Processing Dependency: xorg-x11-xkb-utils for package: tigervnc-server-minimal-1.8.0-17.el7.x86_64

..........

  mesa-libGL.x86_64 0:18.3.4-5.el7                 mesa-libglapi.x86_64 0:18.3.4-5.el7            

  tigervnc-license.noarch 0:1.8.0-17.el7           tigervnc-server-minimal.x86_64 0:1.8.0-17.el7  

  xkeyboard-config.noarch 0:2.24-1.el7             xorg-x11-server-utils.x86_64 0:7.7-20.el7      

  xorg-x11-xauth.x86_64 1:1.0.9-1.el7              xorg-x11-xinit.x86_64 0:1.3.4-2.el7            

  xorg-x11-xkb-utils.x86_64 0:7.7-14.el7          

 

Complete

在宿主机上安装:VNC Viewer

 

添加连接:

 

 

 

 

连接成功:

 

 

 

 

③ 虚拟机访问外部网络

开启宿主机路由转发功能

[root@localhost ~]# vi /etc/sysctl.conf

net.ipv4.ip_forward=1

 

[root@localhost ~]# sysctl -p

 

net.ipv4.ip_forward = 1

[root@localhost ~]# cat /proc/sys/net/ipv4/ip_forward

1

 

访问外网:ping百度成功

 

 

 

二、创建centos7-01

 

创建虚拟机,系统centos7.01,名称guest01,内存1G ,磁盘5G,网络模式NAT,创建完成后使用VNC登陆虚拟机,完成安装过程。

添加镜像文件centos7-01.raw,模拟VM磁盘。

[root@localhost ~]# qemu-img create -f raw /opt/centos7-01.raw 5G

创建虚拟机--virt-type kvm --name centos

[root@localhost ~]# virt-install 7-01 --ram 1024 --cdrom=/opt/CentOS-7-x86_64-DVD-1908.iso --disk path=/opt/centos7-01.raw --network network=default --graphics vnc,listen=0.0.0.0 --noautoconsole

查看虚拟机配置文件

[root@localhost ~]#  ll /etc/libvirt/qemu

total 12

-rw-------  1 root root 4225 Apr  8 13:42 centos7-01.xml

drwx------. 3 root root   42 Apr  1 19:56 networks

-rw-------. 1 root root 3432 Apr  1 22:36 oautoconsole.xml

 

使用VNC登录工具查看:

 

7、创建虚拟机,系统centos7-2,名称guest01,内存1G ,磁盘5G,网络使用桥接模式,创建完成后使用VNC登陆虚拟机,完成安装过程。

[root@localhost ~]# qemu-img create -f raw /opt/centos7-2.raw 5G

  [root@localhost ~]#virt-install --virt-type kvm --name centos7-2 --ram 1024 --cdrom=/opt/CentOS-7-x86_64-DVD-1908.iso --disk path=/opt/centos7--2.raw --network network=default --graphics vnc,listen=0.0.0.0 --noautoconsole

 

A、网络配置

1)命令方式(临时生效)

 

[root@localhost ~]# brctl addbr br0

[root@localhost ~]# brctl show

bridge name     bridge id               STP enabled     interfaces

br0             8000.000c298d79fa       no              ens33

virbr0          8000.5254009ed5ef       yes             virbr0-nic

                                                        vnet0

 

宿主机桥设备virbr0

 

# ip add del dev ens33 IP

# ip add add IP dev br0

# route add default gw GATEWAY

2)修改配置文件(永久生效)

添加桥接模式下的虚拟网桥

[root@localhost ~]# brctl addbr br0

配置以太网卡

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

TYPE=Ethernet

PROXY_METHOD=none

BROWSER_ONLY=no

BOOTPROTO=static

DEFROUTE=yes

NAME=ens33

DEVICE=ens33

ONBOOT=yes

BRIDGE=br0

配置虚拟网桥

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

TYPE=Bridge

PROXY_METHOD=none

BROWSER_ONLY=no

BOOTPROTO=none

DEFROUTE=yes

NAME=br0

DEVICE=br0

ONBOOT=yes

IPADDR=172.16.124.108

NETMASK=255.255.255.0

GATEWAY=172.16.124.2

DNS1=8.8.8.8

重启网络服务

[root@localhost ~]# systemctl restart network

[root@localhost ~]# ip add

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: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast master br0 state UP group default qlen 1000

    link/ether 00:0c:29:46:f1:aa brd ff:ff:ff:ff:ff:ff

3: br0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default qlen 1000

    link/ether 00:0c:29:46:f1:aa brd ff:ff:ff:ff:ff:ff

    inet 172.16.124.108/24 brd 172.16.124.255 scope global noprefixroute br0

       valid_lft forever preferred_lft forever

    inet6 fe80::2c4b:17ff:fe67:175c/64 scope link

       valid_lft forever preferred_lft forever

4: virbr0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default qlen 1000

    link/ether 52:54:00:9e:d5:ef 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

5: virbr0-nic: <BROADCAST,MULTICAST> mtu 1500 qdisc pfifo_fast master virbr0 state DOWN group default qlen 1000

    link/ether 52:54:00:9e:d5:ef brd ff:ff:ff:ff:ff:ff

6: vnet0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast master br0 state UNKNOWN group default qlen 1000

    link/ether fe:54:00:14:71:96 brd ff:ff:ff:ff:ff:ff

    inet6 fe80::fc54:ff:fe14:7196/64 scope link

       valid_lft forever preferred_lft forever

7: vnet1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast master virbr0 state UNKNOWN group default qlen 1000

    link/ether fe:54:00:6a:5d:4c brd ff:ff:ff:ff:ff:ff

    inet6 fe80::fc54:ff:fe6a:5d4c/64 scope link

       valid_lft forever preferred_lft forever

8: vnet2: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast master virbr0 state UNKNOWN group default qlen 1000

    link/ether fe:54:00:02:07:74 brd ff:ff:ff:ff:ff:ff

    inet6 fe80::fc54:ff:fe02:774/64 scope link

       valid_lft forever preferred_lft forever

 

[root@localhost ~]#brctl show

bridge name     bridge id               STP enabled     interfaces

br0             8000.000c29e74d35       no              ens33

virbr0          8000.525400914fb7       yes             virbr0-nic

                                                        vnet0

B、创建虚拟机

(1) 创建新的虚拟机

[root@localhost ~]#  qemu-img create -f raw /opt/centos7.raw 5G

Formatting '/opt/centos7.raw', fmt=raw size=5368709120

[root@localhost ~]#  virt-install --virt-type kvm --name centos7 --ram 1024 --cdrom=/opt/CentOS-7-x86_64-DVD-1908.iso --disk path=/opt/centos7.raw --network bridge=br0 --graphics vnc,listen=0.0.0.0  --noautoconsolew --network bridge=br0 --graphics vnc,listen=0.0.0..0  --noautoconsole

Starting install...

Domain installation still in progress. You can reconnect to

the console to complete the installation process.

打开所有的虚拟机:

 

[root@localhost ~]# virsh start centos7

Domain centos7 started

[root@localhost ~]# virsh start  centos7-01

Domain centos7-01 started

[root@localhost ~]# virsh start  oautoconsole

Domain oautoconsole started

 

[root@localhost ~]# virsh list --all

 Id    Name                           State

----------------------------------------------------

 1     centos7                        running

 2     centos7-01                     running

 3     oautoconsole                   running

[root@localhost ~]# netstat -ntpl

Active Internet connections (only servers)

Proto Recv-Q Send-Q Local Address           Foreign Address         State        PID/Program name    

tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN       1049/master         

tcp        0      0 0.0.0.0:5900            0.0.0.0:*               LISTEN       1510/qemu-kvm       

tcp        0      0 0.0.0.0:5901            0.0.0.0:*               LISTEN       1569/qemu-kvm       

tcp        0      0 0.0.0.0:5902            0.0.0.0:*               LISTEN       1601/qemu-kvm       

tcp        0      0 0.0.0.0:111             0.0.0.0:*               LISTEN       1/systemd           

tcp        0      0 192.168.122.1:53        0.0.0.0:*               LISTEN       1312/dnsmasq        

tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN       942/sshd            

tcp6       0      0 ::1:25                  :::*                    LISTEN       1049/master         

tcp6       0      0 :::111                  :::*                    LISTEN       1/systemd           

tcp6       0      0 :::22                   :::*                    LISTEN       942/sshd

然后使用VN工具打开centos7

 

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

ONBOOT=yes这样开机就能自动启动网卡

 

在这里我发现网络桥接成功了。

 

 

 

三、添加硬盘并分区

要求:

1、假设用户在使用VM2的过程当中磁盘空间不够,需要添加一块新的硬盘,

请为其添加一块5G的硬盘vdb

第一种:

[root@localhost ~]# qemu-img create -f raw /opt/centos72.raw 5G

[root@localhost ~]# virsh attach-disk centos7 /opt/centos72.raw vdb

Disk attached successfully

第二种:

[root@localhost ~]# virsh edit centos7

在里面添加如下:

<disk type='file' device='disk'>

        <driver name='qemu' type='raw' cache='none'/>

        <source file='/opt/centos72.raw'/>

        <target dev='vdb' bus='virtio'/>

</disk>

 

 

 

Domain centos7 XML configuration edited.

然后再重启虚拟机。

 

 

 

 

 

 

 

 

 

并进行分区,分区要求:

 

采用默认的msdos分区模式;
b.1个分区 /dev/vdb1 的大小200M

 

 


c.2个分区 /dev/vdb的大小200M

 


d.3个分区 /dev/vdb的分区大小为100M

 

 

 

查看分区:

 

 

使用ll /dev

 

 

 

e.分区文件系统类型为xfs

f.三个分区分别挂载在/mnt/vfs1/mnt/vfs2/mnt/vfs3下,并实现开机自动挂载

 

先在/mnt下创建这三个目录。

 

 

 

然后进行格式化。

 

再进行挂载。

 

 

 

为了能开机自动挂载,需要在/etc/fstab下面添加自动挂载项。

 

2、系统架构搭建完成后,分别使用VNCSSH的方式登陆虚拟机中,并测试其通信情况(虚拟机访问www.baidu.com,以及VM1VM2之间的通信情况)

 

 

 

 

 

 

Cirros可以pingcentos7,反过来centos7ping不通cirros,但是可以ping5cirros的网关,两台虚拟机都可以访问www.baidu.com

后续还需改进,谢谢支持!

 

posted @ 2020-04-22 17:02  小耿想努力呀  阅读(655)  评论(0编辑  收藏  举报