The purpose of this post is to show how you can build Rackspace cloud load balancers using the API. Building via the API will allow you to script deployments so you can avoid having to use the control panel. This also provides you the building blocks for understanding deployment automation.
This guide will only show you how to create a cloud load balancer. Feel free to review http://docs.rackspace.com for learning about all the possible operations that can be done through the API.
In this example, we are going to deploy a single cloud load balancer called lb.example.com that will be directing HTTP traffic to two web servers:
test01.example.com test02.example.com
When working with the API, I like to use a tool called httpie to simplify things a bit. You can install this by:
yum install httpie
Now that we have httpie installed, lets get an auth token from the API:
echo '{"auth": {"RAX-KSKEY:apiKeyCredentials": {"username": "YOUR_USERNAME","apiKey":"YOUR_API_KEY"}}}' | http post https://identity.api.rackspacecloud.com/v2.0/tokens
The token you need will be listed next to “id” field as shown below
"token": { "expires": "2013-07-09T23:17:08.634-05:00", "id": "2334aasdf5555j3hfhd22245dhsr", "tenant": { "id": "123456", "name": "123456"
To simplify things moving forward, we will set some local variables that we’ll use when communicating with the API:
export token="YOUR_API_TOKEN_RECEIVED_ABOVE" export account="YOUR_RACKSPACE_CLOUD_ACCOUNT_NUMBER" export lb_endpoint="https://ord.loadbalancers.api.rackspacecloud.com/v1.0"
NOTE: Change the endpoints region accordingly (ord or dfw).
Lets prep a json file that we’ll be using to build the cloud load balancer:
cat << EOF > lb.example.com.json { "loadBalancer": { "name": "lb.example.com", "port": 80, "protocol": "HTTP", "virtualIps": [ { "type": "PUBLIC" } ], "nodes": [ { "address": "10.123.123.121", "port": 80, "condition": "ENABLED" } ] } } EOF
Now we execute the build by:
http post $lb_endpoint/$account/loadbalancers X-Auth-Token:$token @lb.example.com.json
This will return the VIP and id of the new load balancer as shown below:
"address": "123.123.123.123", "id": 123456, "name": "lb.example.com",
For the purposes of this guide, I left out adding the second server initially. So here is how we can add it to the load balancer so it will route traffic between test01.example.com and test02.example.com. First create a json file that contains test02.example.com:
cat << EOF > nodes.json {"nodes": [ { "address": "10.123.123.123", "port": 80, "condition": "ENABLED", "type":"PRIMARY" } ] } EOF
Now add this node to the load balancer:
http post $lb_endpoint/$account/loadbalancers/149275/nodes X-Auth-Token:$token @nodes.json
And your done! You can verify everything looks correct by running:
http get $lb_endpoint/$account/loadbalancers/YOUR_LOAD_BALANCER_ID X-Auth-Token:$token