Reference Implementation of Server Load Balancing in Virtualized Environment

  

The author recently did a virtualization-related demo involving server load balancing in a virtualized environment. We use a simple case where live migration occurs when the load is "unbalanced." Since it is only a demo, our load temporarily only considers the utilization of the CPU. In actual preparation, due to the lack of client pressure, it is difficult to achieve precise control of CPU utilization. Here I wrote a simple script program, through adaptive adjustment, to achieve the server CPU utilization within a certain range, in order to ensure that when a server CPU is too high, the virtual machine will be automatically migrated to Tests were performed on other machines with low CPU utilization.

The script implemented by python is as follows. This script requires input of 5 numeric parameters, respectively:

Minimum CPU utilization, maximum CPU utilization, initial thread count, thread for each adjustment Quantity, the time each thread sleeps (milliseconds)

Of course, to a large extent, these parameters are highly empirical due to the different hardware environments.

#!/usr/bin/python 
  • import threading import time
  • import os import string
  • import sys class ControlThread(threading.Thread):
  • def __init__(self): threading.Thread.__init__(self)
  • self.runflag = True #Thread running flag, used to end def run(self) when reducing threads in the future:
  • while self.runflag: os.popen('usleep ' sys.argv[5])
  • #time.sleep(string.atof(sys.argv[5])) #This is the usleep in the shell under linux, not the sleep function that comes with python.
  • # In contrast, usleep is quite powerful, and python sleep is in seconds. Although you can input floating point numbers,
    is still relatively weak def stop(self):
  • self.runflag = False # Let it terminate the loop normally
  • threadList=[] print 'Start Thread Number:' sys.argv[3] '\\tSleep Time(ms):' sys.argv[5]
  • #Initialize a certain amount The thread, otherwise starting from zero, may take a long time to reach the specified range for i in range(0,string.atoi(sys.argv[3])):
  • thread = ControlThread() threadList.append( Thread)
  • thread.start() # Use sar here to capture cpu utilization, which refers to the total cpu utilization. Then make an adaptive adjustment by comparing
  • while True: output = 100 - string.atof(os.popen('sar 1 1 |  Grep ^Average |  Awk \\'
    {print $8}\\'').read())
  • print 'CPU Usage:' str(output) '\\tCurrent Thread Number:' str(len(threadList)) if output < String.atoi(sys.argv[1]):#Add thread
  • for i in range(0,string.atoi(sys.argv[4])): thread = ControlThread()
  • thread.start( threadList.append(thread)
  • print " " if output > string.atoi(sys.argv[2]):#reduce thread
  • for i in range(0,string.atoi(sys.argv[ ,null,null,3],4])): thread = threadList.pop()
  • thread.stop() print "-----"


  • In general, this script is relatively simple. And on different machines, the operator's experience is required to initialize to achieve the best results. But this is my first python program, and it has achieved the intended purpose. The above is just the core code that can run. As for usage, error handling, exiting the program, etc., it is not given:)

  • Copyright © Windows knowledge All Rights Reserved