Deploying Rackspace Cloud Load Balancers via API

Rackspace Hosting released their beta of Cloud Load Balancers today. Since it is an API-only offering for the time being, I thought it might be helpful to create a quick guide covering the deployment and management of Cloud Load Balancers for all the non-programmers out there.

Here is what you’ll need to get started:

Let’s jump right in. We are going to interact with the RESTful API via curl and XML.

Authenticating to the API
To start, we need to authenticate against the API. In the Rackspace Cloud Control Panel, go to “Your Account” -> “API Access”, and click on “Show Key”. Copy your API key into a text file.

Next, lets create an authentication file called auth.txt since the information generated inside will be helpful to us. Be sure to specify your API key and account username in the appropriate parts of the command below.

curl -D "auth.txt" -H "X-Auth-Key: [api_key]" -H "X-Auth-User: [username]" https://auth.api.rackspacecloud.com/v1.0

Next, lets view the contents of auth.txt and see what we have. The output should look similar to this:

cat auth.txt
HTTP/1.1 204 No Content
Date: Mon, 12 Nov 2011 15:32:21 GMT
Server: Apache
X-Server-Management-Url: https://servers.api.rackspacecloud.com/v1.0/16550
X-Storage-Url: https://storage.clouddrive.com/v1/
CloudFS_9c83b-5ed4
X-CDN-Management-Url: https://cdn.clouddrive.com/v1/
CloudFS_9c83b-5ed4
X-Auth-Token: eaaafd18-0fed-4b3a-81b4-663c99ec1cbb
Content-Length: 0 Content-Type: text/plain; charset=UTF-8

Two things are important to copy here, your Cloud Account Number, which is found at the end of the X-Server-Management-URL (in this case 16550) and your X-Auth-Token. Save these in the same text file as your API key. Note that auth tokens typically last for 24 hours, so as you return to the API to make further changes or additions in the future, you will most likely need to re-authenticate.

Creating a Load Balancer
First we need to create a load balancer definition file which we will refer to as new_lb.xml. In this example, we create an lb named “testing-lb”, tell it to balance port 80 web traffic, and give it the private eth1 IP addresses of our two Cloud Server web nodes (web1 and web2) which we wish to balance.

nano -w new_lb.xml
<loadBalancer xmlns="http://docs.openstack.org/loadbalancers/api/v1.0"
  name="testing-lb"
  port="80"
  protocol="HTTP">
  <virtualIps>
    <virtualIp type="PUBLIC"/>
  </virtualIps>
  <nodes>
    <node address="[private_ip_address]" port="80" condition="ENABLED"/>
    <node address="[private_ip_address]" port="80" condition="ENABLED"/>
  </nodes>
</loadBalancer>

Now, lets use this definition file we created to actually build the Cloud Load Balancer.  Note that this example is for Cloud Servers in the ORD1 Chicago DC.

curl -D "header.txt" -X "POST" -H "X-Auth-Token: [auth_token]" -H "Content-Type: Application/xml" -H "Accept: Application/xml" -k https://ord.loadbalancers.api.rackspacecloud.com/v1.0/[account number]/loadbalancers -d "@new_lb.xml" > new_lb_result.xml

Let’s review the output file and make sure things built correctly.

xmllint --format new_lb_result.xml
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<loadBalancer xmlns="http://docs.openstack.org/loadbalancers/api/v1.0"
  id="4270"
  name="testing-lb"
  algorithm="RANDOM"
  protocol="HTTP"
  port="80"
  status="BUILD">
<virtualIps>
  <virtualIp id="225"
    address="184.106.102.28"
    ipVersion="IPV4"
    type="PUBLIC"/>
</virtualIps>
<nodes>
  <node id="12635"
    address="10.180.52.137"
    port="80"
    condition="ENABLED"
    status="ONLINE"
    weight="1"/>
  <node id="12634"
    address="10.180.43.153"
    port="80"
    condition="ENABLED"
    status="ONLINE"
    weight="1"/>
</nodes>
<cluster name="ztm-n02.lbaas.ord1.rackspace.net"/>
<created time="2011-02-25T01:01:22Z"/>
<updated time="2011-02-25T01:01:22Z"/>
<connectionLogging enabled="false"/>
</loadBalancer>

Everything looks good! You’ll notice that you have now been given a VIP to use in your DNS.

Listing Load Balancers
Let’s now make sure we can see our newly created LB in our LB list.

curl -D "header.txt" -X "GET" -H "X-Auth-Token: [auth_token]" -H "Accept: application/xml" https://ord.loadbalancers.api.rackspacecloud.com/v1.0/[account_number]/loadbalancers > loadbalancers.xml

The results show:

xmllint --format loadbalancers.xml
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<loadBalancers xmlns="http://docs.openstack.org/loadbalancers/api/v1.0">
  <loadBalancer id="4270"
    name="testing-lb"
    algorithm="RANDOM"
    protocol="HTTP"
    port="80"
    status="ACTIVE">
  <virtualIps>
    <virtualIp id="225"
      address="184.106.102.28"
      ipVersion="IPV4"
      type="PUBLIC"/>
   </virtualIps>
  <created time="2011-02-25T01:01:22Z"/>
  <updated time="2011-02-25T02:01:31Z"/>
  </loadBalancer>
</loadBalancers>

Adding Additional Servers to the Pool
As the final part of this exercise, lets add an additional web node to the lb pool. To do so, we first create a new_node.xml file with the following contents. Note that you must put the private ip address of the additional cloud web server (web3) in the code below.

nano -w new_node.xml
<nodes xmlns="http://docs.openstack.org/loadbalancers/api/v1.0">
  <node address="[private_ip_address]" port="80" condition="ENABLED" />
</nodes>

Now, we’ll add the node. Note that we take the load balancer ID from a previous command above and enter it below.

curl -D "header.txt" -X "POST" -H "X-Auth-Token: [auth_token]" -H "Content-Type: Application/xml" -H "Accept: Application/xml" -k https://ord.loadbalancers.api.rackspacecloud.com/v1.0/[account_number]
/loadbalancers/[lb_number]/nodes -d "@new_node.xml" > new_node_result.xml

Verify the Node has Been Added

curl -D "header.txt" -X "GET" -H "X-Auth-Token: [auth_token]" -H "Accept: application/xml" https://ord.loadbalancers.api.rackspacecloud.com/v1.0/394681/loadbalancers/4270 > loadbalancer_detail.xml
xmllint --format loadbalancer_detail.xml
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<loadBalancer xmlns="http://docs.openstack.org/loadbalancers/api/v1.0"
  id="4270"
  name="testing-lb"
  algorithm="RANDOM"
  protocol="HTTP"
  port="80"
  status="ACTIVE">
  <virtualIps>
    <virtualIp id="225"
      address="184.106.102.28"
      ipVersion="IPV4"
      type="PUBLIC"/>
  </virtualIps>
  <nodes>
    <node id="12634"
      address="10.180.44.153"
      port="80"
      condition="ENABLED"
      status="ONLINE"/>
    <node id="12655"
      address="10.180.51.95"
      port="80"
      condition="ENABLED"
      status="ONLINE"/>
    <node id="12635"
      address="10.180.51.137"
      port="80"
      condition="ENABLED"
      status="ONLINE"/>
  </nodes>
  <cluster name="ztm-n02.lbaas.ord1.rackspace.net"/>
  <created time="2011-02-25T01:01:22Z"/>
  <updated time="2011-02-25T02:01:31Z"/>
  <connectionLogging enabled="false"/>
</loadBalancer>

This only scratches the surface of what the very flexible Rackspace Cloud Load Balancers API can do but should be a good starting point to help non-programmers understand basic syntax and XML handling. Please view the Rackspace Cloud Load Balancers Developer Guide for detailed documentation on the complete set of features.

2 thoughts on “Deploying Rackspace Cloud Load Balancers via API”

  1. Nice! would you know how to add a public VIP to a cloud server and then share it with other servers.

    I think i have to put in my xml something along the lines below but not quite sure

    Operation: POST
    Path: /servers/id/ips/public

    Any pointers will really help
    Thanks!

    Sam

    1. Hi Sam.

      I don’t believe you can add a vip to a Cloud Server directly with the API. What you would want to do is contact support via a ticket and request a moveable/shared IP and make sure to specify which nodes you want the IP to be shared between. Here is some additional information: http://bit.ly/4yEuAC. I hope this helps!

      Andrei

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>