课题:负载均衡

一、个人分工

我主要负责拥塞状况和负载均衡结果的查看,以及视频的录制。

拓扑图

查看s2,s3流表

h1向h2发送数据

查看s2,s3流表

h1向h2发送数据包,h1经s3转发,当s3流量达到极限值,h1部分数据经s2转发,从而实现负载均衡。

二、实现代码

1、建立拓扑

s1 = self.addSwitch('s1')
        s2 = self.addSwitch('s2')
        s3 = self.addSwitch('s3')
        s4 = self.addSwitch('s4')

        h1 = self.addHost('h1')
        h2 = self.addHost('h2')
        h3 = self.addHost('h3')
        
        self.addLink(h1,s1)
        self.addLink(s1,s2)
        self.addLink(s1,s3)
        self.addLink(s2,s4)
        self.addLink(s3,s4)
        self.addLink(h2,s4)
        self.addLink(h3,s4)

topos = { 'mytopo': ( lambda: MyTopo() ) }

2、下发流表

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))
       h1h2body2 ='{"flow": [{"id": "3","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": "699","cookie": "3","table_id": "0"}]}'

       mh1h2body2 ='{"flow": [{"id": "3","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": "702","cookie": "3","table_id": "0"}]}'
       h1h2body3 ='{"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": "700","cookie": "5","table_id": "0"}]}'

       mh1h2body3 ='{"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": "702","cookie": "5","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/5', body=h1h2body3, 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/3', 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/3', 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/5', body=mh1h2body3, method='PUT',headers=headers)
		
                time.sleep(0.1)	
   
       print(content.decode())

odl = OdlUtil('127.0.0.1','8181')
odl.install_flow()

四、学习总结

首先,在这次课程学习中最大的收获是学会运用虚拟机的部分终端指令,Mininet,Floodlight,ODL,Wireshark等软件的基本用法和用途,以及负载均衡的原理及实现。
在一开始学习的过程中,对sdn课程的学习十分迷茫,上机课也总是跟不上进度,有时作业的完成也是跟着ppt依葫芦画瓢地照做。后来要求逐渐提高,许多都是以前面课程的一些学习为基础进行的。通过反复翻看以往的步骤和上网查阅相关资料后,渐渐对实验的内容有所理解和掌握,大体了解了每次上机的要求是什么。因此,在计算机的学习中,自学是非常重要的,且要多试多摸索。

 posted on 2018-01-25 02:06  Zhamao  阅读(248)  评论(0编辑  收藏  举报