KVM的热添加技术之网卡管理

简介: 文章详细介绍了如何在KVM虚拟机中进行网卡的热添加和热移除操作,包括查看环境、添加和移除网卡的命令,以及如何指定不同的虚拟网卡模型,同时提供了一些操作时的注意事项和永久配置的方法。

作者:尹正杰
版权声明:原创作品,谢绝转载!否则将追究法律责任。

一.为KVM虚拟机热添加网卡

1.添加网卡前查看环境

[root@yinzhengjie-kvm189 ~]# virsh list
 Id    Name                           State
----------------------------------------------------
 2     yinzhengjie-kvm01              running

[root@yinzhengjie-kvm189 ~]# 
[root@yinzhengjie-kvm189 ~]# virsh console yinzhengjie-kvm01 
Connected to domain yinzhengjie-kvm01
Escape character is ^]

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

yinzhengjie-kvm01 login: root
Password: 
Last login: Tue Apr  9 19:07:36 on ttyS0
[root@yinzhengjie-kvm01 ~]# 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:d1:32:de brd ff:ff:ff:ff:ff:ff
    inet 192.168.122.185/24 brd 192.168.122.255 scope global noprefixroute dynamic eth0
       valid_lft 2301sec preferred_lft 2301sec
    inet6 fe80::ae66:b680:585a:e5c2/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever
[root@yinzhengjie-kvm01 ~]#

2.为KVM虚拟机热添加网卡

    1.查看当前环境的网络类型及名称,仅需要关注interface字段即可
[root@yinzhengjie-kvm189 ~]# virsh list
 Id    Name                           State
----------------------------------------------------
 2     yinzhengjie-kvm01              running

[root@yinzhengjie-kvm189 ~]# 
[root@yinzhengjie-kvm189 ~]# virsh net-list 
 Name                 State      Autostart     Persistent
----------------------------------------------------------
 default              active     yes           yes

[root@yinzhengjie-kvm189 ~]# 
[root@yinzhengjie-kvm189 ~]# virsh edit yinzhengjie-kvm01 
      1 <domain type='kvm'>
      ...
     69     <interface type='network'>
     70       <mac address='52:54:00:d1:32:de'/>
     71       <source network='default'/>
     72       <model type='virtio'/>
     73       <address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/>
     74     </interface>
     ...


    2.添加网卡
[root@yinzhengjie-kvm189 ~]# virsh list
 Id    Name                           State
----------------------------------------------------
 2     yinzhengjie-kvm01              running

[root@yinzhengjie-kvm189 ~]# 
[root@yinzhengjie-kvm189 ~]# virsh net-list 
 Name                 State      Autostart     Persistent
----------------------------------------------------------
 default              active     yes           yes

[root@yinzhengjie-kvm189 ~]# 
[root@yinzhengjie-kvm189 ~]# virsh attach-interface yinzhengjie-kvm01 network default
Interface attached successfully

[root@yinzhengjie-kvm189 ~]#

3.添加网卡后再次查看环境

[root@yinzhengjie-kvm189 ~]# virsh list
 Id    Name                           State
----------------------------------------------------
 2     yinzhengjie-kvm01              running

[root@yinzhengjie-kvm189 ~]# 
[root@yinzhengjie-kvm189 ~]# virsh console yinzhengjie-kvm01 
Connected to domain yinzhengjie-kvm01
Escape character is ^]

[root@yinzhengjie-kvm01 ~]# 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:d1:32:de brd ff:ff:ff:ff:ff:ff
    inet 192.168.122.185/24 brd 192.168.122.255 scope global noprefixroute dynamic eth0
       valid_lft 2813sec preferred_lft 2813sec
    inet6 fe80::ae66:b680:585a:e5c2/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever
3: ens9: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 52:54:00:76:d0:88 brd ff:ff:ff:ff:ff:ff
    inet 192.168.122.225/24 brd 192.168.122.255 scope global noprefixroute dynamic ens9
       valid_lft 3500sec preferred_lft 3500sec
    inet6 fe80::542d:198:c0b6:4130/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever
[root@yinzhengjie-kvm01 ~]#

二.虚拟网卡类型概述

1.常见的虚拟网卡模型概述

不同的虚拟机网卡模型在性能、兼容性和功能方面有一些区别。


在Proxmox Virtual Environment (PVE)中,你可以选择以下不同的虚拟网卡模型:
    Realtek RTL8139:
        性能: 
            较为老旧,性能可能不如其他模型。
        兼容性: 
            由于是一种常见的网卡模型,兼容性一般较好。
        注意事项: 
            在某些情况下可能是一种备选方案,但在性能要求较高的情况下可能不是首选。
            网卡速度可达到: 10Mb/s~100Mb/s,在市场上可以找到相对应的设备型号。

    Intel E1000:
        性能: 
            通常性能较为稳定,适用于许多场景。
        兼容性: 
            具有较好的兼容性,许多操作系统都支持这个模型。
        注意事项: 
            老牌的模型,虽然性能较为稳定,但在高负载和高性能场景下可能不如其他模型。
            网卡速度可达到: 1Gb/s,在市场上可以找到相对应的设备型号。

    VirtIO (paravirtualized):
        性能: 
            VirtIO是一种为虚拟化环境优化的半虚拟化网络驱动,性能较高,特别是在虚拟机之间有大量数据交换时。
        兼容性: 
            虽然在大多数现代操作系统中有支持,但在一些老旧或不太主流的系统中可能需要安装额外的驱动。
            比如说window7默认就不支持这种网络驱动,如果真的想要使用则需要单独下载相应的驱动进行安装哟~
        注意事项: 
            在支持VirtIO的情况下,通常是一个不错的选择,但需要注意操作系统的支持和安装虚拟化驱动。
            网卡速度可达到: 10Gb/s。尽管可以达到这个网速,前提是你的磁盘得跟得上这个速度哟。

    VMware vmxnet3:
        性能: 
            针对VMware虚拟化环境进行了优化,性能通常较高。
        兼容性: 
            主要用于VMware虚拟机,与其他虚拟化平台的兼容性可能相对较差。
        注意事项: 
            如果你的虚拟机主要在VMware环境中运行,选择vmxnet3可能是一个不错的选择。

在选择虚拟网卡模型时,你需要考虑你的使用场景、操作系统支持、性能需求以及虚拟化平台的差异。

通常来说,对于大多数一般用途的虚拟机,VirtIO或者Intel E1000是比较常见的选择。


温馨提示:
    对于KVM虚拟机环境而言,目前仅支持前面3种网络类型。
    KVM虚拟机若不指定网卡类型,则默认为"Realtek RTL8139"类型哟~添加网卡后的名称为"ens*"系列。

2.指定Intel E1000的网卡类型

[root@yinzhengjie-kvm189 ~]# virsh list
 Id    Name                           State
----------------------------------------------------
 2     yinzhengjie-kvm01              running

[root@yinzhengjie-kvm189 ~]# 
[root@yinzhengjie-kvm189 ~]# virsh attach-interface yinzhengjie-kvm01 network default --model e1000
Interface attached successfully

[root@yinzhengjie-kvm189 ~]# 
[root@yinzhengjie-kvm189 ~]# virsh console yinzhengjie-kvm01 
Connected to domain yinzhengjie-kvm01
Escape character is ^]

[root@yinzhengjie-kvm01 ~]# 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:d1:32:de brd ff:ff:ff:ff:ff:ff
    inet 192.168.122.185/24 brd 192.168.122.255 scope global noprefixroute dynamic eth0
       valid_lft 2850sec preferred_lft 2850sec
    inet6 fe80::ae66:b680:585a:e5c2/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever
3: ens9: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 52:54:00:76:d0:88 brd ff:ff:ff:ff:ff:ff
    inet 192.168.122.225/24 brd 192.168.122.255 scope global noprefixroute dynamic ens9
       valid_lft 2153sec preferred_lft 2153sec
    inet6 fe80::542d:198:c0b6:4130/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever
4: ens10: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 52:54:00:d3:f2:07 brd ff:ff:ff:ff:ff:ff
    inet 192.168.122.218/24 brd 192.168.122.255 scope global noprefixroute dynamic ens10
       valid_lft 3598sec preferred_lft 3598sec
    inet6 fe80::895:66f7:4ea5:e442/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever
[root@yinzhengjie-kvm01 ~]#

3.指定VirtIO的网卡类型

[root@yinzhengjie-kvm189 ~]# virsh list
 Id    Name                           State
----------------------------------------------------
 2     yinzhengjie-kvm01              running

[root@yinzhengjie-kvm189 ~]# 
[root@yinzhengjie-kvm189 ~]# virsh attach-interface yinzhengjie-kvm01 network default --model virtio
Interface attached successfully

[root@yinzhengjie-kvm189 ~]# 
[root@yinzhengjie-kvm189 ~]# virsh console yinzhengjie-kvm01 
Connected to domain yinzhengjie-kvm01
Escape character is ^]

[root@yinzhengjie-kvm01 ~]# 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:d1:32:de brd ff:ff:ff:ff:ff:ff
    inet 192.168.122.185/24 brd 192.168.122.255 scope global noprefixroute dynamic eth0
       valid_lft 2679sec preferred_lft 2679sec
    inet6 fe80::ae66:b680:585a:e5c2/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever
3: ens9: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 52:54:00:76:d0:88 brd ff:ff:ff:ff:ff:ff
    inet 192.168.122.225/24 brd 192.168.122.255 scope global noprefixroute dynamic ens9
       valid_lft 1983sec preferred_lft 1983sec
    inet6 fe80::542d:198:c0b6:4130/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever
4: ens10: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 52:54:00:d3:f2:07 brd ff:ff:ff:ff:ff:ff
    inet 192.168.122.218/24 brd 192.168.122.255 scope global noprefixroute dynamic ens10
       valid_lft 3427sec preferred_lft 3427sec
    inet6 fe80::895:66f7:4ea5:e442/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever
5: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 52:54:00:50:a6:dd brd ff:ff:ff:ff:ff:ff
    inet 192.168.122.206/24 brd 192.168.122.255 scope global noprefixroute dynamic eth1
       valid_lft 3600sec preferred_lft 3600sec
    inet6 fe80::87df:ffca:8adf:d3af/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever
[root@yinzhengjie-kvm01 ~]#

三.为KVM虚拟机热移除网卡

1.为虚拟机移除指定mac地址网卡

[root@yinzhengjie-kvm189 ~]# virsh console yinzhengjie-kvm01 
Connected to domain yinzhengjie-kvm01
Escape character is ^]

[root@yinzhengjie-kvm01 ~]# 
[root@yinzhengjie-kvm01 ~]# 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:d1:32:de brd ff:ff:ff:ff:ff:ff
    inet 192.168.122.185/24 brd 192.168.122.255 scope global noprefixroute dynamic eth0
       valid_lft 2484sec preferred_lft 2484sec
    inet6 fe80::ae66:b680:585a:e5c2/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever
3: ens9: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 52:54:00:76:d0:88 brd ff:ff:ff:ff:ff:ff
    inet 192.168.122.225/24 brd 192.168.122.255 scope global noprefixroute dynamic ens9
       valid_lft 3568sec preferred_lft 3568sec
    inet6 fe80::542d:198:c0b6:4130/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever
4: ens10: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 52:54:00:d3:f2:07 brd ff:ff:ff:ff:ff:ff
    inet 192.168.122.218/24 brd 192.168.122.255 scope global noprefixroute dynamic ens10
       valid_lft 3232sec preferred_lft 3232sec
    inet6 fe80::895:66f7:4ea5:e442/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever
5: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 52:54:00:50:a6:dd brd ff:ff:ff:ff:ff:ff
    inet 192.168.122.206/24 brd 192.168.122.255 scope global noprefixroute dynamic eth1
       valid_lft 3405sec preferred_lft 3405sec
    inet6 fe80::87df:ffca:8adf:d3af/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever
[root@yinzhengjie-kvm01 ~]# 
[root@yinzhengjie-kvm189 ~]# 
[root@yinzhengjie-kvm189 ~]# virsh detach-interface yinzhengjie-kvm01 network --mac 52:54:00:50:a6:dd
Interface detached successfully

[root@yinzhengjie-kvm189 ~]# 
[root@yinzhengjie-kvm189 ~]# virsh detach-interface yinzhengjie-kvm01 network --mac 52:54:00:d3:f2:07
Interface detached successfully

[root@yinzhengjie-kvm189 ~]#

2.移除网卡后查看虚拟机网卡信息

[root@yinzhengjie-kvm189 ~]# virsh list
 Id    Name                           State
----------------------------------------------------
 2     yinzhengjie-kvm01              running

[root@yinzhengjie-kvm189 ~]# 
[root@yinzhengjie-kvm189 ~]# virsh console yinzhengjie-kvm01 
Connected to domain yinzhengjie-kvm01
Escape character is ^]

[root@yinzhengjie-kvm01 ~]# 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:d1:32:de brd ff:ff:ff:ff:ff:ff
    inet 192.168.122.185/24 brd 192.168.122.255 scope global noprefixroute dynamic eth0
       valid_lft 3214sec preferred_lft 3214sec
    inet6 fe80::ae66:b680:585a:e5c2/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever
3: ens9: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 52:54:00:76:d0:88 brd ff:ff:ff:ff:ff:ff
    inet 192.168.122.225/24 brd 192.168.122.255 scope global noprefixroute dynamic ens9
       valid_lft 2880sec preferred_lft 2880sec
    inet6 fe80::542d:198:c0b6:4130/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever
[root@yinzhengjie-kvm01 ~]#

四.永久配置

和热添加磁盘一样,热添加网卡也有临时配置和永久配置之分,套路几乎一样。

添加网卡配置的两种方式:
    1.临时配置
virsh attach-interface yinzhengjie-kvm01 network default

    2.永久配置
virsh attach-interface yinzhengjie-kvm01 network default --config


移除网卡配置的两种方式:
    1.临时配置
virsh detach-interface yinzhengjie-kvm01 network --mac <网卡的MAC地址>

    2.永久配置
virsh detach-interface yinzhengjie-kvm01 network --mac <网卡的MAC地址> --config

五.其他注意事项

- windows 7虚拟机就不支持VirtIO网络,需要单独为其虚拟机安装相应的驱动哟;

- 对于桥接网络网卡的添加案例(前提是你配置了桥接环境):
virsh attach-interface yinzhengjie-kvm01 --type bridge --source br0 --model virtio
virsh attach-interface yinzhengjie-kvm01 --type bridge --source br0 --model virtio --config
virsh detach-interface yinzhengjie-kvm01 --type bridge --mac 52:54:00:50:a6:dd
目录
相关文章
|
3月前
|
KVM 虚拟化
KVM虚拟机的桥接网络
文章主要介绍了KVM虚拟机的NAT和桥接网络类型的工作原理、配置方法以及如何进行网络模式的切换。
47 3
KVM虚拟机的桥接网络
|
KVM 虚拟化 Windows
【KVM虚拟化】· KVM中的网络
【KVM虚拟化】· KVM中的网络
266 0
|
XML Shell API
【KVM虚拟化】· virsh管理命令
【KVM虚拟化】· virsh管理命令
383 0

相关课程

更多