sdn网络搭建以及负载均衡
SDN网络搭建
1.准备工作
1)虚拟机vmware(注意版本,高版本的系统克隆之后,在低版本上不兼容)
2)Ubuntu18.04 64位
3)配置尽量好一点
2.安装mininet(这里介绍的是下载源码的方法)
1)更新软件,注意前面需要加上sudo(super user do,获取超级管理员权限)
apt-get update
apt-get upgrade
2)打开Ubuntu终端,首先安装git命令
apt-get install git
3)从github上获取mininet源码
git clone git://github.com/mininet/mininet
4)查看版本
cd mininet
git tag
5)选择版本
git checkout -b tag_name
- 安装mininet
cat INSTALL
切换到mininet文件下
cd mininet
这里有多个安装选项:mininet/util/install.sh[options]
“-a”:完整安装包括Mininet VM,还包括如Open vSwitch等依赖关系软件,以及像的OpenFlow Wireshark和POX。默认情况下,这些工具将被安装在你的home目录中。完整安装命令:
./util/install.sh -a
“-nfv”:安装Mininet、基于OpenFlow的交换机和Open vSwitch。命令:
./util/install.sh -nfv
“-s mydir” :使用此选项可将源代码建立在一个指定的目录中,而不是在home目录中。另外,你只想安装OpenFlow1.3和Open vSwitch2.3.0,可以使用安装命令:
./util/install.sh -s mydir
./util/install.sh -n3V 2.3.0
6)简单测试mininet的基本基本功能
sudo mn --test pingall
最好换一下源,换源参考:
https://blog.csdn.net/toopoo/article/details/104513365/?utm_medium=distribute.pc_relevant.none-task-blog-baidujs-4
3.安装控制器(这里介绍的是opendaylight,还有floodlight,ryu等控制器)
1)安装odl依赖包
sudo apt-get update
a) 基础包
sudo apt-get install unzip lrzsz
b) jdk
sudo apt-get install openjdk-8-jdk
c) 设置java环境变量
gedit /etc/environment
文件最末尾增加一行:
JAVA_HOME="/usr/lib/jvm/java-8-openjdk-amd64"
当前终端需要退出ubuntu然后重新登入,才能加载该环境变量
2)下载ODL包
从官网下载:https://www.opendaylight.org/downloads
(我用的是碳版本)
3)解压配置
tar -zxvf distribution-karaf-0.6.0-Carbon.tar.gz
解压之后进入distribution-karaf-0.6.1-Carbon目录, 修改etc/org.apache.karaf.management.cfg文件的以下两行内容:
rmiRegistryHost = 127.0.0.1
rmiServerHost = 127.0.0.1
4)运行odl
进入distribution-karaf-0.6.1-Carbon目录
./bin/karaf
5)安装功能组件
opendaylight-user@root>feature:install odl-restconf
opendaylight-user@root>feature:install odl-l2switch-switch-ui
opendaylight-user@root>feature:install odl-openflowplugin-flow-services-ui
opendaylight-user@root>feature:install odl-mdsal-apidocs
opendaylight-user@root>feature:install odl-dluxapps-applications
opendaylight-user@root>feature:install odl-faas-all
6)登录管理WEB UI
http://localhost:8181/index.html
用户名和密码都是admin
4.配置mininet,自定义拓扑
1)cd mininet/custom
vim topo-2sw-2host.py #编辑4个主机和4个交换机相连
2)sudo mn --custom topo.py --topo mytopo --controller=remote --switch ovsk,protocols=OpenFlow13 #运行,需要打开控制器才能ping通
3)打开浏览器,输入网址:http://<yourMachineIP>:8181/index.html,可以看到定义的网络拓扑结构等相关信息。
5.负载均衡
# -*- coding: utf-8 -*-
import httplib2
import time
import json
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))
#s4流表
#h2默认发往s4的1口的流表,优先级最高
h2_to_s4_1 ='{"flow": [{"id": "41","match": {"ethernet-match":'\
'{"ethernet-type": {"type": "2048"}},'\
'"ipv4-source":"10.0.0.2/32","ipv4-destination": "10.0.0.1/32"},'\
'"instructions": {"instruction": [{"order": "0",'\
'"apply-actions": {"action": [{"output-action": {'\
'"output-node-connector": "1"},"order": "0"}]}}]},'\
'"priority": "115","cookie": "1","table_id": "0"}]}'
lh2_to_s4_1 ='{"flow": [{"id": "41","match": {"ethernet-match":'\
'{"ethernet-type": {"type": "2048"}},'\
'"ipv4-source":"10.0.0.2/32","ipv4-destination": "10.0.0.1/32"},'\
'"instructions": {"instruction": [{"order": "0",'\
'"apply-actions": {"action": [{"output-action": {'\
'"output-node-connector": "2"},"order": "0"}]}}]},'\
'"priority": "105","cookie": "1","table_id": "0"}]}'
#h3默认发往s4的1口的流表,优先级最高
h3_to_s4_1 ='{"flow": [{"id": "51","match": {"ethernet-match":'\
'{"ethernet-type": {"type": "2048"}},'\
'"ipv4-source":"10.0.0.3/32","ipv4-destination": "10.0.0.1/32"},'\
'"instructions": {"instruction": [{"order": "0",'\
'"apply-actions": {"action": [{"output-action": {'\
'"output-node-connector": "1"},"order": "0"}]}}]},'\
'"priority": "115","cookie": "1","table_id": "0"}]}'
#h4默认发往s4的1口的流表,优先级最高
h4_to_s4_1 ='{"flow": [{"id": "61","match": {"ethernet-match":'\
'{"ethernet-type": {"type": "2048"}},'\
'"ipv4-source":"10.0.0.4/32","ipv4-destination": "10.0.0.1/32"},'\
'"instructions": {"instruction": [{"order": "0",'\
'"apply-actions": {"action": [{"output-action": {'\
'"output-node-connector": "1"},"order": "0"}]}}]},'\
'"priority": "115","cookie": "1","table_id": "0"}]}'
#s4的1口流量满载时h2发的数据包,2,3端口出的流表项优先级高
h2_to_s4_2 ='{"flow": [{"id": "42","match": {"ethernet-match":'\
'{"ethernet-type": {"type": "2048"}},'\
'"ipv4-source":"10.0.0.2/32","ipv4-destination": "10.0.0.1/32"},'\
'"instructions": {"instruction": [{"order": "0",'\
'"apply-actions": {"action": [{"output-action": {'\
'"output-node-connector": "2"},"order": "0"}]}}]},'\
'"priority": "112","cookie": "1","table_id": "0"}]}'
h2_to_s4_3 ='{"flow": [{"id": "43","match": {"ethernet-match":'\
'{"ethernet-type": {"type": "2048"}},'\
'"ipv4-source":"10.0.0.2/32","ipv4-destination": "10.0.0.1/32"},'\
'"instructions": {"instruction": [{"order": "0",'\
'"apply-actions": {"action": [{"output-action": {'\
'"output-node-connector": "3"},"order": "0"}]}}]},'\
'"priority": "110","cookie": "1","table_id": "0"}]}'
#s4的1口流量满载时h3发的数据包,2,3端口出的流表项优先级高
h3_to_s4_2 ='{"flow": [{"id": "52","match": {"ethernet-match":'\
'{"ethernet-type": {"type": "2048"}},'\
'"ipv4-source":"10.0.0.3/32","ipv4-destination": "10.0.0.1/32"},'\
'"instructions": {"instruction": [{"order": "0",'\
'"apply-actions": {"action": [{"output-action": {'\
'"output-node-connector": "2"},"order": "0"}]}}]},'\
'"priority": "112","cookie": "1","table_id": "0"}]}'
h3_to_s4_3 ='{"flow": [{"id": "53","match": {"ethernet-match":'\
'{"ethernet-type": {"type": "2048"}},'\
'"ipv4-source":"10.0.0.3/32","ipv4-destination": "10.0.0.1/32"},'\
'"instructions": {"instruction": [{"order": "0",'\
'"apply-actions": {"action": [{"output-action": {'\
'"output-node-connector": "3"},"order": "0"}]}}]},'\
'"priority": "110","cookie": "1","table_id": "0"}]}'
#s4的1口流量满载时h4发的数据包,2,3端口出的流表项优先级高
h4_to_s4_2 ='{"flow": [{"id": "62","match": {"ethernet-match":'\
'{"ethernet-type": {"type": "2048"}},'\
'"ipv4-source":"10.0.0.4/32","ipv4-destination": "10.0.0.1/32"},'\
'"instructions": {"instruction": [{"order": "0",'\
'"apply-actions": {"action": [{"output-action": {'\
'"output-node-connector": "2"},"order": "0"}]}}]},'\
'"priority": "112","cookie": "1","table_id": "0"}]}'
h4_to_s4_3 ='{"flow": [{"id": "63","match": {"ethernet-match":'\
'{"ethernet-type": {"type": "2048"}},'\
'"ipv4-source":"10.0.0.4/32","ipv4-destination": "10.0.0.1/32"},'\
'"instructions": {"instruction": [{"order": "0",'\
'"apply-actions": {"action": [{"output-action": {'\
'"output-node-connector": "3"},"order": "0"}]}}]},'\
'"priority": "110","cookie": "1","table_id": "0"}]}'
#s2流表
s2_2to1='{"flow": [{"id": "1","match": {"ethernet-match":'\
'{"ethernet-type": {"type": "2048"}},'\
'"ipv4-source":"10.0.0.2/32","ipv4-destination": "10.0.0.1/32"},'\
'"instructions": {"instruction": [{"order": "0",'\
'"apply-actions": {"action": [{"output-action": {'\
'"output-node-connector": "1"},"order": "0"}]}}]},'\
'"priority": "105","cookie": "1","table_id": "0"}]}'
#s3流表
s3_2to1='{"flow": [{"id": "1","match": {"ethernet-match":'\
'{"ethernet-type": {"type": "2048"}},'\
'"ipv4-source":"10.0.0.2/32","ipv4-destination": "10.0.0.1/32"},'\
'"instructions": {"instruction": [{"order": "0",'\
'"apply-actions": {"action": [{"output-action": {'\
'"output-node-connector": "1"},"order": "0"}]}}]},'\
'"priority": "105","cookie": "1","table_id": "0"}]}'
#s1流表
s1_234to1='{"flow": [{"id": "1","match": {"ethernet-match":'\
'{"ethernet-type": {"type": "2048"}},'\
'"ipv4-source":"10.0.0.2/32","ipv4-destination": "10.0.0.1/32"},'\
'"instructions": {"instruction": [{"order": "0",'\
'"apply-actions": {"action": [{"output-action": {'\
'"output-node-connector": "1"},"order": "0"}]}}]},'\
'"priority": "105","cookie": "1","table_id": "0"}]}'
headers = {'Content-type': 'application/json'}
#下发流表,中间的地址可以从ODL控制器页面获得
#先下s2、s3和s1的流表
response, content = http.request(uri='http://127.0.0.1:8181/restconf/config/opendaylight-inventory:nodes/node/openflow:2/flow-node-inventory:table/0/flow/1', body=s2_2to1, method='PUT',headers=headers)
response, content = http.request(uri='http://127.0.0.1:8181/restconf/config/opendaylight-inventory:nodes/node/openflow:3/flow-node-inventory:table/0/flow/1', body=s3_2to1, 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=s1_234to1, method='PUT',headers=headers)
#默认下发h2数据包从s4的1端口出的流表
response, content = http.request(uri='http://127.0.0.1:8181/restconf/config/opendaylight-inventory:nodes/node/openflow:4/flow-node-inventory:table/0/flow/41', body=h2_to_s4_1, method='PUT',headers=headers)
#默认下发h3数据包从s4的1端口出的流表
response, content = http.request(uri='http://127.0.0.1:8181/restconf/config/opendaylight-inventory:nodes/node/openflow:4/flow-node-inventory:table/0/flow/51', body=h3_to_s4_1, method='PUT',headers=headers)
#默认下发h4数据包从s4的1端口出的流表
response, content = http.request(uri='http://127.0.0.1:8181/restconf/config/opendaylight-inventory:nodes/node/openflow:4/flow-node-inventory:table/0/flow/61', body=h4_to_s4_1, method='PUT',headers=headers)
while(True):
#获取s4端口1的流量
uri = 'http://127.0.0.1:8181/restconf/operational/opendaylight-inventory:nodes/node/openflow:4/node-connector/openflow:4:1'
response, content = http.request(uri=uri, method='GET')
content = json.loads(content)
statistics = content['node-connector'][0]['opendaylight-port-statistics:flow-capable-node-connector-statistics']
bytes1 = statistics['bytes']['transmitted']
#1秒后再次获取
time.sleep(1)
response, content = http.request(uri=uri, method='GET')
content = json.loads(content)
statistics = content['node-connector'][0]['opendaylight-port-statistics:flow-capable-node-connector-statistics']
bytes2 = statistics['bytes']['transmitted']
#在检测到s4的1口流量空闲时发的流表
speed=float(bytes2-bytes1)/1
if speed !=0 :#获取有效的速度
if speed < 1000 :
print 'speed =',speed,', s4端口1空闲,数据包往1口通过'
#下发默认的流表
response, content = http.request(uri='http://127.0.0.1:8181/restconf/config/opendaylight-inventory:nodes/node/openflow:4/flow-node-inventory:table/0/flow/41', body=h2_to_s4_1, method='PUT',headers=headers)
response, content = http.request(uri='http://127.0.0.1:8181/restconf/config/opendaylight-inventory:nodes/node/openflow:4/flow-node-inventory:table/0/flow/51', body=h3_to_s4_1, method='PUT',headers=headers)
response, content = http.request(uri='http://127.0.0.1:8181/restconf/config/opendaylight-inventory:nodes/node/openflow:4/flow-node-inventory:table/0/flow/61', body=h4_to_s4_1, method='PUT',headers=headers)
#在检测到s2的1口流量满载时下发新的流表
else :
print 'speed =',speed,', s4端口1满载,数据包改为往2口和3口通过'
#h2数据包的流表
response, content = http.request(uri='http://127.0.0.1:8181/restconf/config/opendaylight-inventory:nodes/node/openflow:4/flow-node-inventory:table/0/flow/41', body=lh2_to_s4_1, method='PUT',headers=headers)
response, content = http.request(uri='http://127.0.0.1:8181/restconf/config/opendaylight-inventory:nodes/node/openflow:4/flow-node-inventory:table/0/flow/42', body=h2_to_s4_2, method='PUT',headers=headers)
response, content = http.request(uri='http://127.0.0.1:8181/restconf/config/opendaylight-inventory:nodes/node/openflow:4/flow-node-inventory:table/0/flow/43', body=h2_to_s4_3, method='PUT',headers=headers)
#h3数据包的流表
response, content = http.request(uri='http://127.0.0.1:8181/restconf/config/opendaylight-inventory:nodes/node/openflow:4/flow-node-inventory:table/0/flow/52', body=h3_to_s4_2, method='PUT',headers=headers)
response, content = http.request(uri='http://127.0.0.1:8181/restconf/config/opendaylight-inventory:nodes/node/openflow:4/flow-node-inventory:table/0/flow/53', body=h3_to_s4_3, method='PUT',headers=headers)
#h4数据包的流表
response, content = http.request(uri='http://127.0.0.1:8181/restconf/config/opendaylight-inventory:nodes/node/openflow:4/flow-node-inventory:table/0/flow/62', body=h4_to_s4_2, method='PUT',headers=headers)
response, content = http.request(uri='http://127.0.0.1:8181/restconf/config/opendaylight-inventory:nodes/node/openflow:4/flow-node-inventory:table/0/flow/63', body=h4_to_s4_3, method='PUT',headers=headers)
odl = OdlUtil('127.0.0.1', '8181')
odl.install_flow()
sdn网络搭建以及负载均衡相关推荐
- nginx+keepalived搭建主从负载均衡并迅速切换
大家都听说过主从服务器或者负载均衡之类的专业术语,作为衡量一个中高级运维工程师的标准,集群和负载是运维工程师必须掌握的技术,然而在一家小公司是根本不会体会到运维的重要性的.首先从理论上讲一下,当用户量 ...
- Nginx系列1: 正向代理和反向代理、Nginx工作原理、Nginx常用命令和升级、搭建Nginx负载均衡
一.什么是正向代理.什么是反向代理 1. 正向代理,意思是一个位于客户端和原始服务器(origin server)之间的服务器,为了从原始服务器取得内容,客户端向代理发送一个请求并指定目标(原始服务器 ...
- 从网络到分布式-负载均衡
从网络到分布式-负载均衡 https://www.toutiao.com/i6939329630021124620/?tt_from=weixin&utm_campaign=client_sh ...
- Pacemaker,Corosync和PCS搭建高可用性负载均衡linux 集群(httpd)
Pacemaker,Corosync和PCS搭建高可用性负载均衡linux 集群(httpd) 一.高可用性的概念: 1.一种机制来定义哪些系统可以被用作集群节点 2.哪些服务或者应用可以在节点间作失 ...
- 搭建K8S-web-MySQL-keepalived 负载均衡集群项目
搭建K8S-web-MySQL-keepalived 负载均衡集群项目 部署目录 搭建K8S-web-MySQL-keepalived 负载均衡集群项目 总体架构图 安装MySQL 下载MySQL镜像 ...
- 《SDN期末作业——实现负载均衡》
队名:取个队名真难 一.网络拓扑(场景二) 二.负载均衡程序 1.建立拓扑的代码 拓扑 2.下发组表流表的代码 下发组表流表 三.演示视频 1.目的 服务器h2,h3,h4上各自有不同的服务,h1是客 ...
- SDN期末作业-通过SDN的应用实现负载均衡
负载均衡程序 1.程序链接:https://github.com/424baopu/software/tree/master/LoadBalance 2.场景 topo: 场景描述: 服务器host ...
- 手把手搭建简易负载均衡集群
前言: 最近在公司部署了一个简易的负载均衡集群,这个集群的主要功能是分摊公司业务服务器的流量,防止服务器因为访问量过大而造成负,导致服务宕机或者响应速度慢等一系列问题.在这里做个简要的记录,方便后续自 ...
- Nginx系列篇二:linux搭建Nginx负载均衡
建议先搭建好Nginx环境 可阅读--->Linux中搭建Nginx 1.准备好三台服务器[标配] 一.nginx负载均衡服务器:192.168.102.110,配置好Nginx 二.tomca ...
最新文章
- HG522-C 刷Openwrt记录
- mapinfo在线地图插件_地图数据获取|2、CAD地图获取
- 【Android】3.19 示例19--全景图HelloWorld
- CSS块级元素和行内元素
- apache个人主页搭建
- java中的线程不安全和实例解析
- Ciesz się Polską
- 企业与黑客攻击 就是一场军备竞赛
- 关于AttachCurrentThread和DetachCurrentThread的故事
- 爬虫实例十三 教你怎么用爬虫一次给女朋友拿下28万张情侣头像
- linux 服务器长ping 加时间戳
- 怎样把小米手机便签内容转存到百度网盘中去?
- 服务器频繁重启怎么解决
- Final Cut Pro X无法导入自家的MOV格式,解决方法。
- 思维导图☆三招十八式
- 有哪些支持 HomeKit 的智能家居生态值得推荐?
- [Webpy]在webpy中使用jinja2模板
- QGIS插件grass使用出现“This algorithm cannot be run:”问题
- 某知名支付系统的架构演进权威分析
- android图片下载到本地
热门文章
- MySQL数据库select语句6大子句(from、where、group by、having、order by 、limit )#经典员工、部门表案例语句练手!
- 通用的后台内部管理系统整站前端源码,功能齐全
- Web滚动字幕(MARQUEE示例)
- 全球及中国装配式建筑行业发展现状及应用价值分析报告2021版
- 时代变了,互联网与房产开发商能否找到真爱
- vue父子传值,slot插槽的使用
- OpenCV计算图像像素最大值、最小值
- kjb文件 解析_在Linux上使用lnav监控和分析Apache日志文件工具
- [UE4C++程序]GameModule与Plugin
- 1.数学建模比赛前期准备