诀赛:基于SDN的校园网应用服务器负载均衡
shell.sh
定时获取主机的cpu负载情况。
这里应当选取合适的计算公式,当时时间紧迫便只以cpu的负载情况来判断服务器状况。应当将各种因素都考虑进去。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
#/bin/bash num=`cat /proc/cpuinfo |grep "processor"|wc -l` IP=`ifconfig|awk 'NR==2{split($2,a,":");print a[2]}'` co=`uptime|awk -F, '{print ('$num'-$4)*100}'` temp=`cat /home/try/pox/data/Weight.db|grep -c $IP ` if [ $temp -eq 0 ] then echo $IP:$co >> /home/try/pox/data/Weight.db fi while true do co=`uptime|awk -F, '{print ('$num'-$4)*100}'` sed -i "s/^.*$IP.*$/$IP:$co/" /home/try/pox/data/Weight.db sleep 10 done |
topo-3sw-6host.py
拓扑文件代码:定义了三台交换机、6台服务器
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
#!/usr/bin/python """ Script created by VND - Visual Network Description (SDN version) """ from mininet.net import Mininet from mininet.node import Controller, RemoteController, OVSKernelSwitch, UserSwitch from mininet.cli import CLI from mininet.log import setLogLevel from mininet.link import Link, TCLink def topology(): "Create a network." net = Mininet( controller=RemoteController, link=TCLink, switch=OVSKernelSwitch ) print "------Creating nodes------" h1 = net.addHost( 'h1', mac='00:00:00:00:00:01', ip='10.0.0.1/8' ) h2 = net.addHost( 'h2', mac='00:00:00:00:00:02', ip='10.0.0.2/8' ) h3 = net.addHost( 'h3', mac='00:00:00:00:00:03', ip='10.0.0.3/8' ) h4 = net.addHost( 'h4', mac='00:00:00:00:00:04', ip='10.0.0.4/8' ) h5 = net.addHost( 'h5', mac='00:00:00:00:00:05', ip='10.0.0.5/8' ) h6 = net.addHost( 'h6', mac='00:00:00:00:00:06', ip='10.0.0.6/8' ) c1 = net.addController( 'c1', controller=RemoteController, ip='127.0.0.1', port=6633 ) c2 = net.addController( 'c2', controller=RemoteController, ip='127.0.0.1', port=6644 ) s1 = net.addSwitch( 's1', listenPort=6634, mac='00:00:00:00:00:08' ) s2 = net.addSwitch( 's2', listenPort=6635, mac='00:00:00:00:00:09' ) s3 = net.addSwitch( 's3', listenPort=6636, mac='00:00:00:00:00:10' ) print "------Creating links------" net.addLink(h6, s1, 0, 1) net.addLink(h5, s3, 0, 3) net.addLink(h4, s3, 0, 2) net.addLink(h3, s2, 0, 4) net.addLink(h2, s2, 0, 3) net.addLink(h1, s2, 0, 2) net.addLink(s1, s2, 2, 1) net.addLink(s3, s1, 1, 3) print "*** Starting network" net.start() s1.start( [c1] ) s2.start( [c2] ) s3.start( [c2] ) c1.start() c2.start() print "*** Running CLI" CLI( net ) print "*** Stopping network" net.stop() if __name__ == '__main__': setLogLevel( 'info' ) topology() |
webservice.py
这里因为涉及的数据较少,只是进行简单的文件读写,建议应该用数据库,且NoSql首选。
定义REST API接口共前端可视化调用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
#---------------------get weight---------------------------- def _exec_get_Weight(self): ser=open('/home/try/pox/data/Weight.db') t=ser.read() return {'result':t} #-----------------------end---------------------------------- #---------------------get Account---------------------------- def _exec_get_Account(self): ser=open('/home/try/pox/data/Account.db') t=ser.read() return {'result':t} #------------------------end--------------------------------- #---------------------get Priority---------------------------- def _exec_get_Priority(self): ser=open('/home/try/pox/data/Priority.db') t=ser.read() return {'result':t} #------------------------end--------------------------------- |
ip_loadbalancer.py
负载均衡代码,选用的算法为最小连接算法。这里是贴了关于负载均衡算法和主备用服务器切换的代码,关于服务器优先级的定义及部分预处理这里就不贴了。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
def _pick_server (self, key, inport): global cw global n global m global b temp4=[] temp5=[] temp6=[] temp7=[] temp8=[] temp9=[] temp10=[] temp11=[] temp12=[] global serIP global serCPU global Weight global Weight_main global Weight_bak global using_servers global live_main_servers #global Weight #global Account #------------------------------save Account > Account.db-------------------------------- out=open('data/Account.db','w') for k in Account: out.write(str(k)) out.write(":") out.write(str(Account[k])+'\n') out.close() #-------------------------------end-------------------------------------------------- #------------------------------get cpu usage < Weight-------------------------------- ser=open('data/Weight.db') t=ser.read() t=t.replace(":","\n") li=t.split("\n") for k in range(0,len(li)-1,2): k=li[k:k+2] serIP.append(k[0]) serCPU.append(int(k[1])) serIP=[IPAddr(x) for x in serIP] Weight=dict(zip(serIP,serCPU)) for k in Weight: if Weight[k]Weight_main Weight_bak------------- c=Priority.values() c.sort() bak_pri=c[0] Weight_main=Weight.copy() for k in Priority: if Priority[k]==bak_pri: Weight_main.pop(k) temp8.append(k) temp9.append(Weight[k]) Weight_bak=dict(zip(temp8,temp9)) #--------------------------------------end------------------------------------------- #---------------------------------set using servers--------------------------------- for k in self.live_servers: if Weight[k]>60: temp4.append(k) temp5.append(Weight[k]) using_servers=dict(zip(temp4,temp5)) if len(using_servers)>(len(Weight_bak)+1): for k in Priority: if Priority[k]==bak_pri: if k in using_servers: using_servers.pop(k) a=using_servers.keys() #--------------------------------------end-------------------------------------------- #-------------------------get using servers'account----------------------------------- for k in using_servers: temp10.append(k) temp11.append(Account[k]) # C(Si) temp12.append(Weight[k]) # W(Si) using_Account=dict(zip(temp10,temp11)) print temp11 print temp12 #--------------------------------------end-------------------------------------------- print "**************************Account list*******************************" for k in Account: print k,":",Account[k] print "***************************************************************************" print "**************************using srvers list*******************************" for k in using_servers: print k,":",using_servers[k] print "***************************************************************************" print "**************************live srvers list*******************************" for k in self.live_servers: print k,":",self.live_servers[k] print "***************************************************************************" print "**************************main srvers list********************************" for k in Weight_main: print k,":",Weight_main[k] print "***************************************************************************" print "**************************bak srvers list********************************" for k in Weight_bak: print k,":",Weight_bak[k] print "***************************************************************************" #-----------------------------------------get W(Si)------------------------------- #--------------------------------WLC code----------------------------------------- while (1): print temp11 print temp12 for m in range(len(temp11)): if(temp12[m]>0): i=m+1 for i in range(m,len(temp11)): if temp11[m]*temp12[i]>temp11[i]*temp12[m]: m=i b=a[m] Account[b]+=1 print "--------------------Account---------------------" for k in Account: print k,":",Account[k] return b return NULL #---------------------------------end------------------------------------------- |
weight.php
网页端代码主要功能是调用webservice.py定义的接口进行实时的数据可视化
这里就以权重的php代码为例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
<?php include'account.php'; include'priority.php'; $url = 'http://127.0.0.1:8000/OF/'; $data = array("method"=>'get_Weight',"id"=>1 ); $json_data = postData($url,json_encode($data)); $array1 = json_decode($json_data,true); $weight=$array1['result']; $ip_weight=array(); $weights=array(); $ser_weight=array(); $temp=preg_split("/[\s:]+/", $weight); for ($i=0; $i <count($temp) ; $i=$i+2) { array_push($ip_weight, $temp[$i]); } for ($i=1; $i <count($temp) ; $i=$i+2) { array_push($weights, $temp[$i]); } array_pop($ip_weight); $ser_weight=array_combine($ip_weight,$weights); $data[] = $ser_account; $data[] = $ser_weight; $data[] = $ser_priority; echo json_encode($data); function postData($url, $data) { $ch = curl_init(); $timeout = 300; curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, $data); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $handles = curl_exec($ch); curl_close($ch); return $handles; } ?> |