基於openvswitch+Docker構建SDN網路測試環境 (使用ovs-docker進行構建)

NinWoo發表於2018-09-14

這是一篇之前寫的筆記,主要記錄了使用openvswitch + Docker 等進行一個小型的SDN網路搭建的操作步驟。由於
之前臨時有其他任務,耽擱了一下,最近開始重新整理,並計劃開發一個簡單的Python指令碼,簡化該網路的建立過程。
同時希望該指令碼可以和控制器的程式進行融合,方便未來可以和docker更容易的結合在一起。專案地址:DockerSDN

以下內容或許存在問題,如果在實現過程中發現問題,可與我聯絡,謝謝、

Date: 2018.7.9
Auther: joliu ljo0412@live.com

  • OpenFlow
    • basic knowlege
  • OVS command
    • how to add flow table to open vSwitch
    • how to bind dockers to vSwitch

Install ovs and mininet

learn details about this

Install Docker

Use DaoCloud accelerator

curl -sSL https://get.daocloud.io/docker | sh

Change Docker registry to Aliyun

sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-`EOF`
{
  "registry-mirrors": ["https://xxxxxxx.mirror.aliyuncs.com"]
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker

請替換https://xxxxxxx.mirror.aliyuncs.com,詳細配置內容參見我的另外一篇隨筆CentOS7使用阿里源安裝最新版Docker

Get Basic Docker Image

docker pull alpine

Exercise 1

Create Three Container

docker run -d -i --name RouterA --net=none --privileged alpine sh
docker run -d -i --name RouterB --net=none --privileged alpine sh
docker run -d -i --name RouterC --net=none --privileged alpine sh

Check Containers` Status

[typh@localhost ~]$ docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED              STATUS              PORTS               NAMES
37b9ca888f03        alpine              "sh"                About a minute ago   Up About a minute                       RouterC
f1604beb380f        alpine              "sh"                About a minute ago   Up About a minute                       RouterB
3b3c52c5a582        alpine              "sh"                3 minutes ago        Up 3 minutes 

Create Virtual Network Bridge

sudo ovs-vsctl add-br vnbr

Set Protocols

ovs-vsctl set bridge br0 protocols=OpenFlow13

Check the brige is added

[typh@localhost ~]$ sudo ovs-vsctl show
d9d42750-7b9b-4688-adf9-d07f619e9bac
    Bridge vnbr
        Port vnbr
            Interface vnbr
                type: internal
    ovs_version: "2.5.0"

Add Network Port

sudo ovs-docker add-port vnbr eth0 RouterA --ipaddress="10.0.1.1/24"
sudo ovs-docker add-port vnbr eth0 RouterB --ipaddress="10.0.1.2/24"
sudo ovs-docker add-port vnbr eth0 RouterC --ipaddress="10.0.3.1"

Check The Status of vnbr

[typh@localhost ~]$ sudo ovs-vsctl show
d9d42750-7b9b-4688-adf9-d07f619e9bac
    Bridge vnbr
        Port vnbr
            Interface vnbr
                type: internal
        Port "facad1a218c64_l"
            Interface "facad1a218c64_l"
        Port "d48d429fe1894_l"
            Interface "d48d429fe1894_l"
        Port "12437ccb4eab4_l"
            Interface "12437ccb4eab4_l"
    ovs_version: "2.5.0"

Set OpenFlow Port ID

sudo ovs-vsctl set interface facad1a218c64_l ofport_request=100
sudo ovs-vsctl set interface d48d429fe1894_l ofport_request=200
sudo ovs-vsctl set interface 12437ccb4eab4_l ofport_request=300

Connect to ODL Controller

sudo ovs-vsctl set-controller vnbr tcp:127.0.0.1:6633

Check the Connections with Controller

d9d42750-7b9b-4688-adf9-d07f619e9bac
    Bridge vnbr
        Controller "tcp:127.0.0.1:6633"
            is_connected: true
        Port vnbr
            Interface vnbr
                type: internal
        Port "facad1a218c64_l"
            Interface "facad1a218c64_l"
        Port "d48d429fe1894_l"
            Interface "d48d429fe1894_l"
        Port "12437ccb4eab4_l"
            Interface "12437ccb4eab4_l"
    ovs_version: "2.5.0"

Test the connection between the two host By PING

docker exec -it RouterA ping -c 4 10.0.1.2

Generally, they cannot connect with the other.

Add Flows

sudo ovs-ofctl add-flow vnbr "priority=1 idle_timeout=0,in_port=1,actions=output:2"
sudo ovs-ofctl add-flow vnbr "priority=2 idle_timeout=0,in_port=1,actions=output:1"

Get the Flows from vSwitch

ovs-ofctl -O OpenFlow13 dump-flows vnbr

Test Again.

docker exec -it RouterA ping -c 4 10.0.1.2

Example of the successful result.

[master@MiWiFi-R3-srv ~]$ sudo docker exec -it RouterA ping -c 4 10.0.1.2
PING 10.0.1.2 (10.0.1.2): 56 data bytes
64 bytes from 10.0.1.2: seq=0 ttl=64 time=0.062 ms
64 bytes from 10.0.1.2: seq=1 ttl=64 time=0.061 ms
64 bytes from 10.0.1.2: seq=2 ttl=64 time=0.086 ms
64 bytes from 10.0.1.2: seq=3 ttl=64 time=0.078 ms

--- 10.0.1.2 ping statistics ---
4 packets transmitted, 4 packets received, 0% packet loss
round-trip min/avg/max = 0.061/0.071/0.086 ms

Start karaf

feature:install odl-restconf odl-l2switch-switch odl-mdsal-apidocs odl-dlux-all

相關文章