SDN负载均衡
负载均衡程序
程序流程图
代码
from mininet.topo import Topo
class MyTopo( Topo ):
"Simple topology example."
def __init__( self ):
# Initialize topology
Topo.__init__( self )
sw1 = self.addSwitch('s1')
sw2 = self.addSwitch('s2')
sw3 = self.addSwitch('s3')
sw4 = self.addSwitch('s4')
h1 = self.addHost('h1')
h2 = self.addHost('h2')
h3 = self.addHost('h3')
h4 = self.addHost('h4')
self.addLink(h1,sw1,1)
self.addLink(sw1,sw2,2,1)
self.addLink(sw1,sw4,3,2)
self.addLink(sw1,sw3,4,1)
self.addLink(sw2,sw4,2,1)
self.addLink(sw3,sw4,2,3)
self.addLink(sw4,h2,4)
self.addLink(sw4,h3,5)
self.addLink(sw4,h4,6)
topos = { 'mytopo': ( lambda: MyTopo() ) }
//拓扑
import httplib2
import time
class OdlUtil:
url = ''
def __init__(self, host, port):
self.url = 'http://' + host + ':' + str(port)
def install_flow(self, container_name='default',username="admin", password="admin"):
http = httplib2.Http()
http.add_credentials(username, password)
headers = {'Accept': 'application/json'}
flow_name = 'flow_' + str(int(time.time()*1000))
h1h4body1 ='{"flow": [{"id": "1","match": {"ethernet-match":'
'{"ethernet-type": {"type": "2048"}},'
'"ipv4-source":"10.0.0.1/32","ipv4-destination": "10.0.0.4/32"},'
'"instructions": {"instruction": [{"order": "0",'
'"apply-actions": {"action": [{"output-action": {'
'"output-node-connector": "4"},"order": "0"}]}}]},'
'"priority": "101","cookie": "1","table_id": "0"}]}'
mh1h4body1 ='{"flow": [{"id": "1","match": {"ethernet-match":'
'{"ethernet-type": {"type": "2048"}},'
'"ipv4-source":"10.0.0.1/32","ipv4-destination": "10.0.0.4/32"},'
'"instructions": {"instruction": [{"order": "0",'
'"apply-actions": {"action": [{"output-action": {'
'"output-node-connector": "4"},"order": "0"}]}}]},'
'"priority": "102","cookie": "1","table_id": "0"}]}'
h1h4body2 ='{"flow": [{"id": "5","match": {"ethernet-match":'
'{"ethernet-type": {"type": "2048"}},'
'"ipv4-source":"10.0.0.1/32","ipv4-destination": "10.0.0.4/32"},'
'"instructions": {"instruction": [{"order": "0",'
'"apply-actions": {"action": [{"output-action": {'
'"output-node-connector": "2"},"order": "0"}]}}]},'
'"priority": "100","cookie": "5","table_id": "0"}]}'
mh1h4body2 ='{"flow": [{"id": "5","match": {"ethernet-match":'
'{"ethernet-type": {"type": "2048"}},'
'"ipv4-source":"10.0.0.1/32","ipv4-destination": "10.0.0.4/32"},'
'"instructions": {"instruction": [{"order": "0",'
'"apply-actions": {"action": [{"output-action": {'
'"output-node-connector": "2"},"order": "0"}]}}]},'
'"priority": "102","cookie": "5","table_id": "0"}]}'
h1h4body3 ='{"flow": [{"id": "6","match": {"ethernet-match":'
'{"ethernet-type": {"type": "2048"}},'
'"ipv4-source":"10.0.0.1/32","ipv4-destination": "10.0.0.4/32"},'
'"instructions": {"instruction": [{"order": "0",'
'"apply-actions": {"action": [{"output-action": {'
'"output-node-connector": "3"},"order": "0"}]}}]},'
'"priority": "99","cookie": "6","table_id": "0"}]}'
mh1h4body3 ='{"flow": [{"id": "6","match": {"ethernet-match":'
'{"ethernet-type": {"type": "2048"}},'
'"ipv4-source":"10.0.0.1/32","ipv4-destination": "10.0.0.4/32"},'
'"instructions": {"instruction": [{"order": "0",'
'"apply-actions": {"action": [{"output-action": {'
'"output-node-connector": "3"},"order": "0"}]}}]},'
'"priority": "102","cookie": "6","table_id": "0"}]}'
h1h2body1 ='{"flow": [{"id": "1","match": {"ethernet-match":'
'{"ethernet-type": {"type": "2048"}},'
'"ipv4-source":"10.0.0.1/32","ipv4-destination": "10.0.0.2/32"},'
'"instructions": {"instruction": [{"order": "0",'
'"apply-actions": {"action": [{"output-action": {'
'"output-node-connector": "4"},"order": "0"}]}}]},'
'"priority": "101","cookie": "1","table_id": "0"}]}'
mh1h2body1 ='{"flow": [{"id": "1","match": {"ethernet-match":'
'{"ethernet-type": {"type": "2048"}},'
'"ipv4-source":"10.0.0.1/32","ipv4-destination": "10.0.0.2/32"},'
'"instructions": {"instruction": [{"order": "0",'
'"apply-actions": {"action": [{"output-action": {'
'"output-node-connector": "4"},"order": "0"}]}}]},'
'"priority": "102","cookie": "1","table_id": "0"}]}'
h1h2body2 ='{"flow": [{"id": "5","match": {"ethernet-match":'
'{"ethernet-type": {"type": "2048"}},'
'"ipv4-source":"10.0.0.1/32","ipv4-destination": "10.0.0.2/32"},'
'"instructions": {"instruction": [{"order": "0",'
'"apply-actions": {"action": [{"output-action": {'
'"output-node-connector": "2"},"order": "0"}]}}]},'
'"priority": "100","cookie": "5","table_id": "0"}]}'
mh1h2body2 ='{"flow": [{"id": "5","match": {"ethernet-match":'
'{"ethernet-type": {"type": "2048"}},'
'"ipv4-source":"10.0.0.1/32","ipv4-destination": "10.0.0.2/32"},'
'"instructions": {"instruction": [{"order": "0",'
'"apply-actions": {"action": [{"output-action": {'
'"output-node-connector": "2"},"order": "0"}]}}]},'
'"priority": "102","cookie": "5","table_id": "0"}]}'
h1h2body3 ='{"flow": [{"id": "6","match": {"ethernet-match":'
'{"ethernet-type": {"type": "2048"}},'
'"ipv4-source":"10.0.0.1/32","ipv4-destination": "10.0.0.2/32"},'
'"instructions": {"instruction": [{"order": "0",'
'"apply-actions": {"action": [{"output-action": {'
'"output-node-connector": "3"},"order": "0"}]}}]},'
'"priority": "99","cookie": "6","table_id": "0"}]}'
mh1h2body3 ='{"flow": [{"id": "6","match": {"ethernet-match":'
'{"ethernet-type": {"type": "2048"}},'
'"ipv4-source":"10.0.0.1/32","ipv4-destination": "10.0.0.2/32"},'
'"instructions": {"instruction": [{"order": "0",'
'"apply-actions": {"action": [{"output-action": {'
'"output-node-connector": "3"},"order": "0"}]}}]},'
'"priority": "102","cookie": "6","table_id": "0"}]}'
h1h3body1 ='{"flow": [{"id": "1","match": {"ethernet-match":'
'{"ethernet-type": {"type": "2048"}},'
'"ipv4-source":"10.0.0.1/32","ipv4-destination": "10.0.0.3/32"},'
'"instructions": {"instruction": [{"order": "0",'
'"apply-actions": {"action": [{"output-action": {'
'"output-node-connector": "4"},"order": "0"}]}}]},'
'"priority": "101","cookie": "1","table_id": "0"}]}'
mh1h3body1 ='{"flow": [{"id": "1","match": {"ethernet-match":'
'{"ethernet-type": {"type": "2048"}},'
'"ipv4-source":"10.0.0.1/32","ipv4-destination": "10.0.0.3/32"},'
'"instructions": {"instruction": [{"order": "0",'
'"apply-actions": {"action": [{"output-action": {'
'"output-node-connector": "4"},"order": "0"}]}}]},'
'"priority": "102","cookie": "1","table_id": "0"}]}'
h1h3body2 ='{"flow": [{"id": "5","match": {"ethernet-match":'
'{"ethernet-type": {"type": "2048"}},'
'"ipv4-source":"10.0.0.1/32","ipv4-destination": "10.0.0.3/32"},'
'"instructions": {"instruction": [{"order": "0",'
'"apply-actions": {"action": [{"output-action": {'
'"output-node-connector": "2"},"order": "0"}]}}]},'
'"priority": "100","cookie": "5","table_id": "0"}]}'
mh1h3body2 ='{"flow": [{"id": "5","match": {"ethernet-match":'
'{"ethernet-type": {"type": "2048"}},'
'"ipv4-source":"10.0.0.1/32","ipv4-destination": "10.0.0.3/32"},'
'"instructions": {"instruction": [{"order": "0",'
'"apply-actions": {"action": [{"output-action": {'
'"output-node-connector": "2"},"order": "0"}]}}]},'
'"priority": "102","cookie": "5","table_id": "0"}]}'
h1h3body3 ='{"flow": [{"id": "6","match": {"ethernet-match":'
'{"ethernet-type": {"type": "2048"}},'
'"ipv4-source":"10.0.0.1/32","ipv4-destination": "10.0.0.3/32"},'
'"instructions": {"instruction": [{"order": "0",'
'"apply-actions": {"action": [{"output-action": {'
'"output-node-connector": "3"},"order": "0"}]}}]},'
'"priority": "99","cookie": "6","table_id": "0"}]}'
mh1h3body3 ='{"flow": [{"id": "6","match": {"ethernet-match":'
'{"ethernet-type": {"type": "2048"}},'
'"ipv4-source":"10.0.0.1/32","ipv4-destination": "10.0.0.3/32"},'
'"instructions": {"instruction": [{"order": "0",'
'"apply-actions": {"action": [{"output-action": {'
'"output-node-connector": "3"},"order": "0"}]}}]},'
'"priority": "102","cookie": "6","table_id": "0"}]}'
headers = {'Content-type': 'application/json'}
num=0
while num < 4 :
response, content = http.request(uri='http://127.0.0.1:8181/restconf/config/opendaylight-inventory:nodes/node/openflow:1/flow-node-inventory:table/0/flow/1', body=mh1h4body1, method='PUT',headers=headers)
response, content = http.request(uri='http://127.0.0.1:8181/restconf/config/opendaylight-inventory:nodes/node/openflow:1/flow-node-inventory:table/0/flow/1', body=mh1h3body1, method='PUT',headers=headers)
response, content = http.request(uri='http://127.0.0.1:8181/restconf/config/opendaylight-inventory:nodes/node/openflow:1/flow-node-inventory:table/0/flow/1', body=mh1h2body1, method='PUT',headers=headers)
time.sleep(0.1)
response, content = http.request(uri='http://127.0.01:8181/restconf/config/opendaylight-inventory:nodes/node/openflow:1/flow-node-inventory:table/0/flow/1', body=h1h4body1, method='PUT',headers=headers)
response, content = http.request(uri='http://127.0.01:8181/restconf/config/opendaylight-inventory:nodes/node/openflow:1/flow-node-inventory:table/0/flow/5', body=mh1h4body2, method='PUT',headers=headers)
response, content = http.request(uri='http://127.0.01:8181/restconf/config/opendaylight-inventory:nodes/node/openflow:1/flow-node-inventory:table/0/flow/1', body=h1h3body1, method='PUT',headers=headers)
response, content = http.request(uri='http://127.0.01:8181/restconf/config/opendaylight-inventory:nodes/node/openflow:1/flow-node-inventory:table/0/flow/5', body=mh1h3body2, method='PUT',headers=headers)
response, content = http.request(uri='http://127.0.01:8181/restconf/config/opendaylight-inventory:nodes/node/openflow:1/flow-node-inventory:table/0/flow/1', body=h1h2body1, method='PUT',headers=headers)
response, content = http.request(uri='http://127.0.01:8181/restconf/config/opendaylight-inventory:nodes/node/openflow:1/flow-node-inventory:table/0/flow/5', body=mh1h2body2, method='PUT',headers=headers)
time.sleep(0.1)
response, content = http.request(uri='http://127.0.0.1:8181/restconf/config/opendaylight-inventory:nodes/node/openflow:1/flow-node-inventory:table/0/flow/5', body=h1h4body2, method='PUT',headers=headers)
response, content = http.request(uri='http://127.0.0.1:8181/restconf/config/opendaylight-inventory:nodes/node/openflow:1/flow-node-inventory:table/0/flow/6', body=mh1h4body3, method='PUT',headers=headers)
response, content = http.request(uri='http://127.0.0.1:8181/restconf/config/opendaylight-inventory:nodes/node/openflow:1/flow-node-inventory:table/0/flow/5', body=h1h3body2, method='PUT',headers=headers)
response, content = http.request(uri='http://127.0.0.1:8181/restconf/config/opendaylight-inventory:nodes/node/openflow:1/flow-node-inventory:table/0/flow/6', body=mh1h3body3, method='PUT',headers=headers)
response, content = http.request(uri='http://127.0.0.1:8181/restconf/config/opendaylight-inventory:nodes/node/openflow:1/flow-node-inventory:table/0/flow/5', body=h1h2body2, method='PUT',headers=headers)
response, content = http.request(uri='http://127.0.0.1:8181/restconf/config/opendaylight-inventory:nodes/node/openflow:1/flow-node-inventory:table/0/flow/6', body=mh1h2body3, method='PUT',headers=headers)
time.sleep(0.1)
response, content = http.request(uri='http://127.0.0.1:8181/restconf/config/opendaylight-inventory:nodes/node/openflow:1/flow-node-inventory:table/0/flow/6', body=h1h4body3, method='PUT',headers=headers)
response, content = http.request(uri='http://127.0.0.1:8181/restconf/config/opendaylight-inventory:nodes/node/openflow:1/flow-node-inventory:table/0/flow/6', body=h1h3body3, method='PUT',headers=headers)
response, content = http.request(uri='http://127.0.0.1:8181/restconf/config/opendaylight-inventory:nodes/node/openflow:1/flow-node-inventory:table/0/flow/6', body=h1h2body3, method='PUT',headers=headers)
print(content.decode())
odl = OdlUtil('127.0.0.1', '8181')
odl.install_flow()
代码思路
拓扑结构
代码分析
因为对该拓扑结构,只需要在s1处进行负载均衡,s2、s3、s4到任何目的主机或交换机的链路都是唯一的,所以不需要考虑负载均衡。
我们原本打算尝试更复杂一些的可以实现在节点上进行阻塞判断的负载均衡,但因为ODL方面编程的资料我们能找到的不是很充足,而且关于北向接口可以参考的实例程序几乎没有,再加之Python入门水平实在十分有限,因此也一度决定改用Ryu尝试开发,因为我们找到了北邮李呈所写的一个简单的负载均衡,可以进行参考。然而最终也是因为许多接口的用法、代码不能理解而无法成功开发。因为时间有限,又在期末时期学业十分紧张,我们最终选择了在群内学长分析的简单的以Python连接ODL然后下发流表的方式,实现一个简单的分时机制进行负载均衡。具体方法是为每一条链路分配相等时长的时间片,这里设置为0.1s。具体算法的原理是:首先将端口2出发的链路优先级设为最高(102),每一次分配时间片时,将下一条链路的优先级设为最高(102),上一次最高优先级的链路设为最低(当前最低值-1),反复循环,即可实现在2,3,4链路上按分时机制进行负载均衡。
应用到我们选用的具体拓扑上,就是:
2端口,优先级102(最高) 0.1s后 3端口,优先级102 2端口,优先级101(102-1) 0.1s后 4端口,优先级102 3端口,优先级100(101-1) 0.1s后 4端口,优先级99(100-1)