虚拟化-ovn入门到精通(五)-1

OVN-L3 网关连接外部网络

1. 网关联通

参考网上,TOPO是这么个意思~

正如上面所看到的,我们添加了以下新组件:

  • 逻辑交换机(outside),用于将edge1连接到实验室网络

我们要做的是让VPC内的虚拟机能联通underlay物理网络,打通三层, NAT出去~

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

| eth1 | Physical Network
----------
|
____|_____
| bridge | br-ex 192.168.0.100
----------
| mapping
outside(sw)
| port: tenant1-outside <192.168.0.101/24>
router-tenant1(dr) port: tenant1-localnet <172.16.250.1/24,172.16.251.1/24>
/ \
ls01(sw) ls02(sw)
/ | / \
vm1 vm2 vm11 vm12

创建逻辑边界路由器,gateway router位于一个特定的chassis中。为了完成绑定,我们需要确定 central节点的chassis id(我理解这里是VTEP网关的意思,需要固定到哪一台上,相当于是集中式的网络节点吧)

1
#ovn-sbctl show

1
#ovn-nbctl create Logical_Router name=edge1 options:classis=32209610-88c9-495e-ab88-134923afea59

创建outside交换机用于连接外网和tenant1 , 作用: 连接两个路由器,因为网关路由器仅可以经由逻辑交换机连接到其他路由器

  1. 联通路由器tenant1到逻辑交换机outside上
1
2
#ovn-sbctl show

1
2
3
4
5
6
7
8
9
10
11
12
13

# create new port on router 'tenant1'
ovn-nbctl lrp-add router-tenant1 tenant1-outside 02:0a:7f:18:01:02 192.168.0.101/24
# set gateway chassis(通过ovn-sbctl show 选取central节点作为vtep网关节点)
ovn-nbctl lrp-set-gateway-chassis tenant1-outside fda80787-897d-40af-811c-2c9f7caedb3b

# create new logical switch and connect it to 'router0'
ovn-nbctl ls-add outside
ovn-nbctl lsp-add outside outside-tenant1
ovn-nbctl lsp-set-type outside-tenant1 router
ovn-nbctl lsp-set-addresses outside-tenant1 02:0a:7f:18:01:02
ovn-nbctl lsp-set-options outside-tenant1 router-port=tenant1-outside
# ovn-nbctl lsp-set-options outside-tenant1 nat-addresses=router router-port=tenant1-outside
  1. 创建ovs网桥br-ex,并关联逻辑交换机outside
1
2
3
4
5
# create localnet port on 'outside'. set the network name to "phynet"
ovn-nbctl lsp-add outside outside-localnet
ovn-nbctl lsp-set-addresses outside-localnet unknown
ovn-nbctl lsp-set-type outside-localnet localnet
ovn-nbctl lsp-set-options outside-localnet network_name=phynet1
  1. 在central节点上创建ovs网桥br-ex,然后将eth1挂到ovs网桥上
    1
    2
    3
    4
    5
    6
    # create a bridge , then mapping outside port
    ovs-vsctl add-br br-ex
    ovs-vsctl set Open_vSwitch . external-ids:ovn-bridge-mappings=phynet1:br-ex

    # add nic eth1
    ovs-vsctl add-port br-ex eth1
    给 central节点br-ex 配置ip测试联通
    1
    2
    #ifconfig br-ex 192.168.0.100/24
    #ip link set br-ex up
1
2
3
#ip netns exec ns1 ping -c 2 192.168.0.101
#ip netns exec ns1 ping -c 2 192.168.0.100
结果发现 虚机能联通 192.168.0.101,确不能联通192.168.0.100
  1. 通过snat实现访问外网。通过dnat_and_snat实现fip
    1
    2
    3
    4
    # snat 连外网
    ovn-nbctl -- --id=@nat create nat type="snat" logical_ip=172.16.250.0/24 external_ip=192.168.0.101 -- add logical_router router-tenant1 nat @nat
    # fip
    ovn-nbctl -- --id=@nat create nat type="dnat_and_snat" logical_ip=172.16.250.11 external_ip=192.168.0.102 -- add logical_router router-tenant1 nat @nat