Getting Started with SoftLayer Local Load Balancers

Load balancing is a way to distribute processing and communications evenly across multiple servers within a data center so that a single device does not carry the entire load. Load balancing is essential in situations where it’s difficult to predict the number of requests issued to a server. By distributing requests that would be made to a single server, you can ease the load and minimize latency.

SoftLayer load balancing enables you to employ a variety of balancing methods to distribute traffic, including persistent and non-persistent options that can be changed, activated, and deactivated at any time. For general information, refer to knowledgelayer.softlayer.com/topic/load-balancing.

In this blog, we will discuss how to use the SoftLayer API to provision, configure, and manage local load balancers.

Listing Local Load Balancers

You can gather a list of all local load balancers from the SoftLayer_Account service using the SoftLayer_Account::getAdcLoadBalancers API. This API retrieves an account’s local load balancers.

For example, this sample code retrieves the list of local load balancers that includes their associated locations and IP addresses using an object mask:

import SoftLayer
client = SoftLayer.Client()
mask = ('mask[loadBalancerHardware[datacenter],ipAddress]')
load_bals = client[‘Account’].getAdcLoadBalancers(mask=mask)

Getting Local Load Balancer Details

To get information about a specific load balancer you can use the Network_Application_Delivery_Controller_LoadBalancer_VirtualIpAddress::getObject API, which returns the details of any local load balancer you request. A local load balancer is represented by the Network_Application_Delivery_Controller_LoadBalancer_VirtualIpAddress object, and contains a set of service groups. Each service group contains a list of services.

For example, my load balancer below has two service groups, each containing two services:

1. Service group one has a virtual port of 8080 and is load balancing the traffic between two services using round robin routing method.
2. Service group two has a virtual port of 80 and is load balancing the traffic between two other services.

To retrieve the load balancer’s information, issue the getObject API using the load balancer ID and an object mask. This will return all of the service information:

mask = ('mask[loadBalancerHardware[datacenter], ipAddress, virtualServers[serviceGroups[routingMethod,routingType,services[healthChecks[type], groupReferences, ipAddress]]]]')
lb_svc = client['Network_Application_Delivery_Controller_LoadBalancer_VirtualIpAddress']
loadbal = .getObject(id=loadbal_id, mask=mask)

Creating a SoftLayer Local Load Balancer

To create a new load balancer, you need to place an order using package 0 (additional services), and specify the item price for the load balancer you want to create.
To retrieve the list of load balancers, query package 0 using:

from SoftLayer.utils import query_filter
lb_filter = '*Load Balancer*'
_filter = NestedDict({})
_filter['items']['description'] = query_filter(lb_filter)
kwargs = NestedDict({})
kwargs['id'] = 0  # look at package id 0
kwargs['filter'] = _filter.to_dict()
packages = client['Product_Package'].getItems(**kwargs)

The object filter will sort all packages in package 0 and only return ones for the load balancer. Once you have the list of packages and know which load balancer you want to create (price_item_id), place an order specifying the location of the data center (location_id).

        product_order = {
            'complexType': 'SoftLayer_Container_Product_Order_Network_'
                           'LoadBalancer',
            'quantity': 1,
            'packageId': 0,
            "location": location_id,
            'prices': [{'id': price_item_id}]
        }
        Return client['Product_Order'].placeOrder(product_order)

Congratulations! You just created your first load balancer using the API. Now, you can get the details of your new load balancer using the APIs provided above.

Configuring

To modify the configuration of the load balancer, use the SoftLayer_Network_Application_Delivery_Controller_LoadBalancer_VirtualIpAddress::editObject API. You can edit the load balancer properties by passing in a modified version of the object. In this example, you simply request the load balancer object you wish to edit, and then modify the objects in the service groups and their associated services, and pass the modified object back to the editObject API.

Configure the empty load balancer object that you just created using:

mask = ('mask[virtualServers[serviceGroups[services[groupReferences]]]]')
load_balancer = lb_svc.getObject(id=lb_id, mask=mask)

This will retrieve the existing object. Now, create a new service group and a new set of services using:

import SoftLayer
client = SoftLayer.Client()
lb_id = 23186
ip_address1 = client['Network_Subnet_IpAddress'].getByIpAddress('50.97.92.244')
ip_address2 = client['Network_Subnet_IpAddress'].getByIpAddress('50.97.92.245')
mask = ('mask[virtualServers[serviceGroups[services[groupReferences]]]]')
lb_svc = client['Network_Application_Delivery_Controller_LoadBalancer_VirtualIpAddress']
load_balancer = lb_svc.getObject(id=lb_id, mask=mask)
 
virtual_servers = load_balancer['virtualServers']
template = {
    'port': 8080,  # port of the service group
 
    'allocation': 100,  # how many connections to allocated to the group
    'serviceGroups': [
        {
            'routingTypeId': 2,  # routing type
            'routingMethodId': 10,  # routing method id
            'services': [  # the list of services
                {
                    'enabled': 1,  # enable the service
                    'port': 8080,  # using port 8080
                    'ipAddressId': ip_address1[  # on ipaddress 1
                        'id'
                    ],
                    'healthChecks': [
                        {
                            'healthCheckTypeId': 21  # with the default health check
                        }
                    ],
                    'groupReferences': [
                        {
                            'weight': 1  # giving the service this weight
                        }
                    ]
                },
                {
                    'enabled': 1,
                    'port': 8080,
                    'ipAddressId': ip_address2[
                        'id'
                    ],
                    'healthChecks': [
                        {
                            'healthCheckTypeId': 21
                        }
                    ],
                    'groupReferences': [
                        {
                            'weight': 1
                        }
                    ]
                }
            ]
        }]
}
virtual_servers.append(template)
lb_svc.editObject(load_balancer, id=lb_id)

Listing Related Configuration Values

To be able to configure the load balancer, a valid routing type, routing method, health checker values are needed. Below are some examples on how to retrieve valid values.

Listing Health Check Types

svc = client['SoftLayer_Network_Application_Delivery_Controller_LoadBalancer_Health_Check_Type']
hc_types = svc.getAllObjects()

Listing Routing Types

svc = client['SoftLayer_Network_Application_Delivery_Controller_LoadBalancer_Routing_Type']
return svc.getAllObjects()

Listing Routing Methods

svc = client['SoftLayer_Network_Application_Delivery_Controller_LoadBalancer_Routing_Method']        return svc.getAllObjects()

Cancelling

Cancel the load balancer using the cancelService API. Given the load balancer ID, you can retrieve the billing item associated with the load balancer and cancel the service on the billing item ID.

lb_billing = self.lb_svc.getBillingItem(id=loadbal_id)
billing_id = lb_billing['id']
billing_item = client['Billing_Item']
return billing_item.cancelService(id=billing_id)

-Wissam