ovn經過宿主機出網方案

當前網絡架構

物理環境: master01(172.31.133.26), node01(172.31.133.27), node02(172.31.133.28)node

邏輯網絡: 一個邏輯路由器router0,鏈接三個邏輯交換機bridge0,bridge1,bridge2。每一個交換機上有一個端口,鏈接上pod或者vm。以下:網絡

ovn-nbctl show架構

switch 679c8a34-5f60-42a8-91a3-5f727e41e2af (bridge1)
    port bridge1-vm1
        addresses: ["dynamic"]
    port bridge1-router0
        type: router
        router-port: router0-bridge1
switch 7124d3c4-950a-4f4a-9733-db7f204a96c4 (bridge2)
    port bridge2-router0
        type: router
        router-port: router0-bridge2
    port bridge2-vm2
        addresses: ["dynamic"]
switch 05b72423-5db2-4279-8b5f-5f4b5e8c1408 (bridge0)
    port bridge0-router0
        type: router
        router-port: router0-bridge0
    port bridge0-vm0
        addresses: ["dynamic"]
router a96ea502-72df-41a2-ac27-0271e60661d2 (router0)
    port router0-bridge1
        mac: "04:ac:10:ff:34:01"
        networks: ["172.66.2.10/24"]
    port router0-bridge0
        mac: "04:ac:10:ff:34:00"
        networks: ["172.66.1.10/24"]
    port router0-bridge2
        mac: "04:ac:10:ff:34:02"
        networks: ["172.66.3.10/24"]

ovs-vsctl show (node02上)分佈式

1bd24d64-5b67-4497-b972-5789ba8d4fa7
    Bridge br-int
        fail_mode: secure
        Port "qtest3vm0mqm5g"
            Interface "qtest3vm0mqm5g"
        Port "ovn-aa8648-0"
            Interface "ovn-aa8648-0"
                type: geneve
                options: {csum="true", key=flow, remote_ip="172.31.133.27"}
        Port br-int
            Interface br-int
                type: internal
        Port "ovn-1800fb-0"
            Interface "ovn-1800fb-0"
                type: geneve
                options: {csum="true", key=flow, remote_ip="172.31.133.26"}
    ovs_version: "2.11.2"

ovn 出網

圖片描述
相對物理網絡
在集羣每一個節點的ovs網橋br-int上添加一個端口ovn0,經過路由規則,將ovn0做爲外部流量出入口。測試

邏輯網絡
添加邏輯交換機join,將各個節點的ovn0流量關聯到交換機join的邏輯端口。而後將交換機連到邏輯路由器router0上,組成連通的ovn邏輯網絡spa

組網

建立邏輯交換機jion,並連上邏輯路由器router0。code

# 建立邏輯交換機jion
ovn-nbctl ls-add join

# 關聯邏輯路由器router0
ovn-nbctl lrp-add router0 router0-join 04:ac:10:ff:35:02 172.66.4.10/24
ovn-nbctl lsp-add join join-router0 
ovn-nbctl lsp-set-type  join-router0  router
ovn-nbctl lsp-set-addresses  join-router0  04:ac:10:ff:35:02
ovn-nbctl lsp-set-options  join-router0  router-port=router0-join

# 刪除端口
# ovn-nbctl lrp-del router0-jion
# ovn-nbctl lsp-del join-router0

爲邏輯交換機jion添加端口router

添加port
ovn-nbctl lsp-add join master01
ovn-nbctl lsp-set-addresses  master01    "04:ac:10:ff:35:04 172.66.4.1"

ovn-nbctl lsp-add join node01
ovn-nbctl lsp-set-addresses  node01    "04:ac:10:ff:35:05 172.66.4.2"

ovn-nbctl lsp-add join node02
ovn-nbctl lsp-set-addresses  node02    "04:ac:10:ff:35:06 172.66.4.3"

# 刪除port
# ovn-nbctl lsp-del master01
# ovn-nbctl lsp-del node01
# ovn-nbctl lsp-del node02

關聯邏輯端口(在每一個節點上執行),並設置mac和ipblog

# 在master01上
ovs-vsctl add-port br-int ovn0 -- set Interface ovn0 type=internal
ovs-vsctl set Interface ovn0  external_ids:iface-id=master01
ifconfig ovn0 172.66.4.1  netmask 255.255.255.0
ifconfig ovn0 hw ether 04:ac:10:ff:35:04

# 在node01上
ovs-vsctl add-port br-int ovn0 -- set Interface ovn0 type=internal
ovs-vsctl set Interface ovn0  external_ids:iface-id=node01
ifconfig ovn0 172.66.4.2  netmask 255.255.255.0
ifconfig ovn0 hw ether 04:ac:10:ff:35:05

# 在node02上
ovs-vsctl add-port br-int ovn0 -- set Interface ovn0 type=internal
ovs-vsctl set Interface ovn0  external_ids:iface-id=node02
ifconfig ovn0 172.66.4.3  netmask 255.255.255.0
ifconfig ovn0 hw ether 04:ac:10:ff:35:06

添加route

如今有三個用戶交換機bridge0(172.66.1.10/24),bridge1(172.66.2.10/24),bridge2(172.66.3.10/24),也就是三個網段須要出網圖片

添加宿主機路由

# 須要在每一個節點上執行
# 在node01上,(node02 master01上一樣操做)
route add -net 172.66.1.0/24 gw 172.66.4.10
route add -net 172.66.2.0/24 gw 172.66.4.10
route add -net 172.66.3.0/24 gw 172.66.4.10

添加ovn虛擬路由器router0路由

# 添加後容器內能夠ping通宿主機
ovn-nbctl   lr-route-add  router0   172.31.133.27  172.66.4.2
ovn-nbctl   lr-route-add  router0   172.31.133.28  172.66.4.3
ovn-nbctl   lr-route-add  router0   172.31.133.26  172.66.4.1

展現

[root@master01 /]# ovn-nbctl   lr-route-list router0
IPv4 Routes
            172.31.133.26                172.66.4.1 dst-ip
            172.31.133.27                172.66.4.2 dst-ip
            172.31.133.28                172.66.4.3 dst-ip

上面的操做執行後,容器和宿主機能夠互通。若是想要出公網,還須要添加以下路由:

添加ovn虛擬路由器router0路由

ovn-nbctl  --policy=src-ip lr-route-add  router0   172.66.1.12 172.66.4.3
ovn-nbctl  --policy=src-ip lr-route-add  router0   172.66.3.12 172.66.4.2
ovn-nbctl  --policy=src-ip lr-route-add  router0   172.66.2.12 172.66.4.3

如上配置的是分佈式網關,也能夠經過路由規則實現集中式網關如:ovn-nbctl --policy=src-ip lr-route-add router0 172.66.1.12/24 172.66.4.3,讓全部172.66.1.12/24網段的流量都走172.66.4.3出網

展現

[root@master01 /]# ovn-nbctl   lr-route-list router0
IPv4 Routes
            172.31.133.26                172.66.4.1 dst-ip
            172.31.133.27                172.66.4.2 dst-ip
            172.31.133.28                172.66.4.3 dst-ip
              172.66.1.12                172.66.4.3 src-ip
              172.66.2.12                172.66.4.3 src-ip
              172.66.3.12                172.66.4.2 src-ip

添加snat

在每須要出網的節點上執行(node01,node02)
> iptables -t nat -A POSTROUTING -s 172.66.3.0/24 -o bond0.1331  -j MASQUERADE
> iptables -t nat -A POSTROUTING -s 172.66.1.0/24 -o bond0.1331  -j MASQUERADE
> iptables -t nat -A POSTROUTING -s 172.66.2.0/24 -o bond0.1331  -j MASQUERADE

測試
我這邊邏輯交換機上連的是容器化的虛機

[root@master01 ovn]# virtctl console vm1
[root@vm1 ~]# ping 14.215.177.39
PING 14.215.177.39 (14.215.177.39) 56(84) bytes of data.
64 bytes from 14.215.177.39: icmp_seq=1 ttl=49 time=28.0 ms
64 bytes from 14.215.177.39: icmp_seq=2 ttl=49 time=25.9 ms
 
--- 14.215.177.39 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1001ms
rtt min/avg/max/mdev = 25.943/26.989/28.036/1.059 ms

完美收工

相關文章
相關標籤/搜索