如何使用 CentOS 7 为 KVM 来宾定义多个路由

如何使用 CentOS 7 为 KVM 来宾定义多个路由

下面的代码在 CentOS 7 HOST 上生成两个 CentOS 7 GUEST 虚拟机。其中一台虚拟机通过公共 IP 地址连接到互联网。另一个虚拟机获得一个私有 IP 地址,但无法访问ping网关或被ping其他虚拟机访问。

服务器需要两条路由。每个虚拟机使用专用 IP 地址相互通信的一条路由。第一个虚拟机使用公共 IP 地址与外界通信的另一条路线。为了实现这一目标,需要对以下内容进行哪些具体更改?


创建公共虚拟机

以下命令在安装过程中自动成功设置 IP 地址和网关、dns、网络掩码等,以便生成的虚拟机立即能够作为所需的 PUBLIC IP 在互联网上进行交互:

virt-install --name=public-centos7 --disk path=/var/lib/libvirt/images/guest.img,size=50 \
--graphics none --vcpus=1 --memory=2048 --location /tmp/CentOS-7-x86_64-Minimal-1611.iso \
--network=default --os-type=linux --os-variant=rhel7.0 --initrd-inject=/tmp/vm.ks \
--extra-args "ks=file:/vm.ks console=ttyS0 ip=PUBLIC.IP.OF.VM::PUBLIC.IP.OF.GATEWAY:255.255.255.248:localhost2:eth0:none"


创建私有虚拟机

以下命令创建一个无法ping PUBLIC.IP.OF.GATEWAY或任何其他地址的虚拟机。此外,ping 192.168.122.2运行以下命令后, 上面创建的其他虚拟机也无法运行:

virt-install --name=private-centos7 --disk path=/var/lib/libvirt/images/guest2.img,size=50 \
--graphics none --vcpus=1 --memory=2048 --location /tmp/CentOS-7-x86_64-Minimal-1611.iso \
--network=default --os-type=linux --os-variant=rhel7.0 --initrd-inject=/tmp/vm.ks \
--extra-args "ks=file:/vm.ks console=ttyS0 ip=192.168.122.2::PUBLIC.IP.OF.GATEWAY:255.255.255.0:localhost2:eth0:none"

virt-install但是,在运行上述命令时,dmesg日志中添加了以下几行:

[94333.242521] systemd[1]: sys-subsystem-net-devices-vnet1.device changed plugged -> dead
[94333.242528] systemd[1]: sys-devices-virtual-net-vnet1.device changed plugged -> dead
[94333.242533] systemd[1]: Collecting sys-devices-virtual-net-vnet1.device
[94333.242534] systemd[1]: Collecting sys-subsystem-net-devices-vnet1.device

dmesg运行期间还会写入其他日志virt-install,但上面的 4 行与网络最相关。 我是否应该寻找某种方法来处理已失效的报告vnet1?如果是这样,怎么办?


default主机上的网络:

以下是HOST上网络 xml的定义:default

[root@localhost ~]# virsh net-edit default
<network>
  <name>default</name>
  <uuid>some-long-complex-string</uuid>
  <forward mode='nat'/>
  <bridge name='br0' stp='on' delay='0'/>
  <mac address='11:22:33:44:55:66'/>
  <ip address='192.168.122.1' netmask='255.255.255.0'>
    <dhcp>
      <range start='192.168.122.2' end='192.168.122.254'/>
    </dhcp>
  </ip>
</network>
~
~
"/tmp/virshycDOpm.xml" 12L, 347C

以下是列出主机上可用网桥的输出:

[root@localhost ~]# brctl show
bridge name     bridge id               STP enabled     interfaces
br0             8000.1stStringID        no              eno1
                                                        vnet0
                                                        vnet1
virbr0          8000.2ndStringID        yes             virbr0-nic
[root@localhost ~]#  

请注意,无论网络定义bridge name中的被设置为或 ,同样的问题仍然存在。 defaultxmlvirbr0br0


私人访客配置:

[root@localhost ~]# virsh console public2-centos7
Connected to domain public2-centos7
Escape character is ^]
ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1
    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 qlen 1000
    link/ether 11:22:33:44:55:66 brd aa:bb:cc:dd:ee:ff
    inet 192.168.122.2/29 brd 192.168.122.7 scope global eth0
       valid_lft forever preferred_lft forever
    inet6 ipv6:addr:does:NOT::matter/128 scope global dynamic
       valid_lft 603985sec preferred_lft 603985sec
    inet6 ipv6:addr:does:NOT::matter/64 scope global noprefixroute dynamic
       valid_lft 2371267sec preferred_lft 297667sec
    inet6 ipv6:addr:does:NOT::matter/64 scope link
       valid_lft forever preferred_lft forever
[root@localhost ~]# ping 192.168.122.7
Do you want to ping broadcast? Then -b
[root@localhost ~]#


@Christopher的建议:

根据 @Christopher 的建议,我在主机上运行了以下命令:

[root@remote-host ~]# netstat -anr
Kernel IP routing table
Destination           Gateway                Genmask         Flags   MSS Window  irtt Iface
0.0.0.0               PUBLIC.IP.OF.GATEWAY   0.0.0.0         UG        0 0          0 br0
PUBLIC.IP.OF.HOST-1   0.0.0.0                255.255.255.248 U         0 0          0 br0
192.168.122.0         0.0.0.0                255.255.255.0   U         0 0          0 virbr0

[root@remote-host ~]# vi /etc/iproute2/rt_tables
#
# reserved values
#
255     local
254     main
253     default
0       unspec
#
# local
#
#1      inr.ruhep
~
"/etc/iproute2/rt_tables" 11L, 87C

请注意,指定的目的地br0比 少一个PUBLIC.IP.OF.HOST,这意味着它是一个公共 IP,但它不是我们的互联网帐户有任何声明的公共 IP 之一。

根据@Christopher的建议,我听说我应该改变/etc/iproute2/rt_tables成:

#
# reserved values
#
255     local
254     main
253     default
0       unspec
#
# local
#
#1      inr.ruhep
200 mgmt

CentOS 7 服务器上包含文件的目录内容ifcfg如下:

[root@remote-host ~]# ls -al /etc/sysconfig/network-scripts
total 256
drwxr-xr-x. 2 root root  4096 Mar 28 08:18 .
drwxr-xr-x. 6 root root  4096 Mar 28 08:18 ..
-rw-r--r--. 1 root root   148 Mar 24 19:39 ifcfg-br0
-rw-r--r--. 1 root root   129 Mar 24 19:36 ifcfg-eno1
-rw-r--r--. 1 root root 12288 Mar 26 13:18 .ifcfg-eno1.swp
-rw-r--r--. 1 root root   254 Sep 12  2016 ifcfg-lo
lrwxrwxrwx. 1 root root    24 Mar 24 19:16 ifdown -> ../../../usr/sbin/ifdown
-rwxr-xr-x. 1 root root   627 Sep 12  2016 ifdown-bnep
-rwxr-xr-x. 1 root root  5817 Sep 12  2016 ifdown-eth
-rwxr-xr-x. 1 root root  6196 Nov  5 18:47 ifdown-ib
-rwxr-xr-x. 1 root root   781 Sep 12  2016 ifdown-ippp
-rwxr-xr-x. 1 root root  4201 Sep 12  2016 ifdown-ipv6
lrwxrwxrwx. 1 root root    11 Mar 24 19:16 ifdown-isdn -> ifdown-ippp
-rwxr-xr-x. 1 root root  1778 Sep 12  2016 ifdown-post
-rwxr-xr-x. 1 root root  1068 Sep 12  2016 ifdown-ppp
-rwxr-xr-x. 1 root root   837 Sep 12  2016 ifdown-routes
-rwxr-xr-x. 1 root root  1444 Sep 12  2016 ifdown-sit
-rwxr-xr-x. 1 root root  1621 Nov  5 18:36 ifdown-Team
-rwxr-xr-x. 1 root root  1556 Apr 15  2016 ifdown-TeamPort
-rwxr-xr-x. 1 root root  1462 Sep 12  2016 ifdown-tunnel
lrwxrwxrwx. 1 root root    22 Mar 24 19:16 ifup -> ../../../usr/sbin/ifup
-rwxr-xr-x. 1 root root 12688 Sep 12  2016 ifup-aliases
-rwxr-xr-x. 1 root root   859 Sep 12  2016 ifup-bnep
-rwxr-xr-x. 1 root root 11880 Sep 12  2016 ifup-eth
-rwxr-xr-x. 1 root root 10145 Nov  5 18:47 ifup-ib
-rwxr-xr-x. 1 root root 12039 Sep 12  2016 ifup-ippp
-rwxr-xr-x. 1 root root 10525 Sep 12  2016 ifup-ipv6
lrwxrwxrwx. 1 root root     9 Mar 24 19:16 ifup-isdn -> ifup-ippp
-rwxr-xr-x. 1 root root   642 Sep 12  2016 ifup-plip
-rwxr-xr-x. 1 root root  1043 Sep 12  2016 ifup-plusb
-rwxr-xr-x. 1 root root  2772 Sep 12  2016 ifup-post
-rwxr-xr-x. 1 root root  4154 Sep 12  2016 ifup-ppp
-rwxr-xr-x. 1 root root  1925 Sep 12  2016 ifup-routes
-rwxr-xr-x. 1 root root  3263 Sep 12  2016 ifup-sit
-rwxr-xr-x. 1 root root  1755 Apr 15  2016 ifup-Team
-rwxr-xr-x. 1 root root  1876 Apr 15  2016 ifup-TeamPort
-rwxr-xr-x. 1 root root  2682 Sep 12  2016 ifup-tunnel
-rwxr-xr-x. 1 root root  1740 Sep 12  2016 ifup-wireless
-rwxr-xr-x. 1 root root  4623 Sep 12  2016 init.ipv6-global
-rw-r--r--. 1 root root 15383 Sep 12  2016 network-functions
-rw-r--r--. 1 root root 26829 Sep 12  2016 network-functions-ipv6

其内容ifcfg-br0如下:

[root@remote-host ~]# vi /etc/sysconfig/network-scripts/ifcfg-br0
TYPE=Bridge
BOOTPROTO=static
NAME=br0
DEVICE=br0
ONBOOT=yes
DNS1=11.11.11.11
DNS2=11.11.22.22
IPADDR=PUBLIC.IP.OF.HOST
PREFIX=29
GATEWAY=PUBLIC.IP.OF.GATEWAY
~
~
"/etc/sysconfig/network-scripts/ifcfg-br0" 11L, 148C  

其内容ifcfg-eno1为:

[root@remote-host ~]# vi /etc/sysconfig/network-scripts/ifcfg-eno1
TYPE=Ethernet
BOOTPROTO=static
NAME=eno1
UUID=some-long-complicated-string-here
DEVICE=eno1
ONBOOT=yes
BRIDGE=br0

我们需要添加额外的文件并修改其他文件吗?如果是这样,怎么办?

相关内容