环境
集群
节点 | ip 1 | ip 2 | 组件 |
---|---|---|---|
master01 | 172.31.133.26/26 | 172.31.133.90/26 | ovn-central(nb,sb,northd) |
node01 | 172.31.133.27/26 | 172.31.133.91/26 | controller,ovs(vswtichd,db) |
node02 | 172.31.133.28/26 | 172.31.133.92/26 | controller,ovs(vswtichd,ovsdb) |
ovn初始环境
一个逻辑交换机bridge0 和一个逻辑路由器router0,交换机上挂了容器化的虚机vm0 172.66.1.12
网络配置
# add the router
ovn-nbctl lr-add router0
# create router port for the connection to 'bridge0'
ovn-nbctl lrp-add router0 router1-bridge0 04:ac:10:ff:34:00 172.66.1.10/24
# create the 'bridge0' switch port for connection to 'router0'
ovn-nbctl lsp-add bridge0 bridge0-router0
ovn-nbctl lsp-set-type bridge0-router0 router
ovn-nbctl lsp-set-addresses bridge0-router0 04:ac:10:ff:34:00
ovn-nbctl lsp-set-options bridge0-router0 router-port=router0-bridge0
ovn-nbctl show
router dfecb747-b655-42d8-a63b-54aad5123ab6 (router0)
port router0-bridge0
mac: "04:ac:10:ff:34:00"
networks: ["172.66.1.10/24"]
switch b60a46af-de3a-44c2-ac88-4426fa004140 (bridge0)
port bridge0-vm0
addresses: ["dynamic"]
port bridge0-router0
type: router
router-port: router0-bridge0
ovs-sbctl show
Chassis "aa8648e9-e367-4992-9d87-e96b99993ccc"
hostname: "node01"
Encap geneve
ip: "172.31.133.27"
options: {csum="true"}
Chassis "76dc7a18-0b4e-4a25-84c8-5fce4578cf78"
hostname: "node02"
Encap geneve
ip: "172.31.133.28"
options: {csum="true"}
Port_Binding "bridge0-vm0"
gateway
网络拓扑图
文本流程图
__________
| enp6s0f1 | Physical Network
----------
|
____|_____
| bridge | br-ex 172.31.133.92/26
----------
____|____
| switch | outside
---------
|
____|____
| router | gw1 port 'gw1-outside': 172.31.133.95/26 nat
--------- port 'gw1-join': 192.168.255.1/24
____|____
| switch | join 192.168.255.0/24
---------
____|____
| router | router0 port 'router0-join': 192.168.255.2/24
--------- port 'router0-bridge0': 172.66.1.10/24
|
____|____
| switch | bridge0 172.66.1.0/24
---------
/ \
_______/_ _\_______
| vm0 | | vm1 |
--------- ---------
172.66.1.12 172.66.1.13
网络配置
STEP1
创建并连接交换机join和路由器gw1。
# gw1 join, chassis is master02's id
ovn-nbctl create Logical_Router name=gw1 options:chassis=76dc7a18-0b4e-4a25-84c8-5fce4578cf78
ovn-nbctl ls-add join
# link gw1 and join
ovn-nbctl lrp-add gw1 gw1-join 02:ac:10:ff:ff:01 192.168.255.1/24
ovn-nbctl lsp-add join join-gw1
ovn-nbctl lsp-set-type join-gw1 router
ovn-nbctl lsp-set-addresses join-gw1 02:ac:10:ff:ff:01
ovn-nbctl lsp-set-options join-gw1 router-port=gw1-join
连接交换机join和路由器router0
#link join and router0
ovn-nbctl lrp-add router0 router0-join 02:ac:10:ff:ff:02 192.168.255.2/24
ovn-nbctl lsp-add join join-router0
ovn-nbctl lsp-set-type join-router0 router
ovn-nbctl lsp-set-addresses join-router0 02:ac:10:ff:ff:02
ovn-nbctl lsp-set-options join-router0 router-port=router0-join
添加路由
# add route for gw1 and router0
ovn-nbctl lr-route-add gw1 "172.66.1.0/24" 192.168.255.2
ovn-nbctl lr-route-add router0 "0.0.0.0/0" 192.168.255.1
STEP2
创建交换机outside并连接路由器gw1
# create new port on router 'gw1'
ovn-nbctl lrp-add gw1 gw1-outside 02:0a:7f:18:01:02 172.31.133.95/26
# create new logical switch and connect it to 'gw1'
ovn-nbctl ls-add outside
ovn-nbctl lsp-add outside outside-gw1
ovn-nbctl lsp-set-type outside-gw1 router
ovn-nbctl lsp-set-addresses outside-gw1 02:0a:7f:18:01:02
ovn-nbctl lsp-set-options outside-gw1 router-port=gw1-outside
STEP3
创建ovs网桥br-ex,并关联逻辑交换机outside
# 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=phyNet
# on node02
# create a bridge , then mapping outside port
ovs-vsctl add-br br-ex
ip link set br-ex up
ovs-vsctl set Open_vSwitch . external-ids:ovn-bridge-mappings=phyNet:br-ex
将enp6s0f1挂到ovs网桥上,并将网卡的ip给网桥
# on node02
# add nic for birdge, then set ip
ovs-vsctl add-port br-ex enp6s0f1
ip addr del 172.31.133.92/26 dev enp6s0f1
ip addr add 172.31.133.92/26 dev br-ex
# add route on node02
ip route add 172.66.1.0/24 via 172.31.133.95 dev br-ex
STEP4
通过snat实现访问外网。通过dnat_and_snat实现fip
# snat 连外网
ovn-nbctl -- --id=@nat create nat type="snat" logical_ip=172.66.1.0/24 external_ip=172.31.133.95 -- add logical_router gw1 nat @nat
# fip
ovn-nbctl -- --id=@nat create nat type="dnat_and_snat" logical_ip=172.66.1.12 external_ip=172.31.133.96 -- add logical_router gw1 nat @nat
查看 snat 和 dnat_and_snat
[root@master01 /]# ovn-nbctl lr-nat-list gw1
TYPE EXTERNAL_IP LOGICAL_IP EXTERNAL_MAC LOGICAL_PORT
dnat_and_snat 172.31.133.96 172.66.1.12
snat 172.31.133.95 172.66.1.0/24
验证
在vm0里ping node01
注:此环境中enp6s0f1无法连通外网,不然可以在vm0中ping通外网
[root@master01 ovn]# virtctl console vm0
[root@vm0 ~]#
[root@vm0 ~]# ping 172.31.133.91
PING 172.31.133.91 (172.31.133.91) 56(84) bytes of data.
64 bytes from 172.31.133.91: icmp_seq=1 ttl=62 time=1.38 ms
64 bytes from 172.31.133.91: icmp_seq=2 ttl=62 time=0.396 ms
--- 172.31.133.91 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1002ms
rtt min/avg/max/mdev = 0.396/0.889/1.383/0.494 ms
[root@vm0 ~]#
在master01上ping fip 172.31.133.96
[root@master01 home]# ping 172.31.133.96
PING 172.31.133.96 (172.31.133.96) 56(84) bytes of data.
64 bytes from 172.31.133.96: icmp_seq=1 ttl=62 time=2.04 ms
64 bytes from 172.31.133.96: icmp_seq=2 ttl=62 time=0.530 ms
^C
--- 172.31.133.96 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1001ms
rtt min/avg/max/mdev = 0.530/1.285/2.041/0.756 ms
[root@master01 home]#