| OLD | NEW |
| 1 .. _elb_tut: | 1 .. _elb_tut: |
| 2 | 2 |
| 3 ========================================================== | 3 ========================================================== |
| 4 An Introduction to boto's Elastic Load Balancing interface | 4 An Introduction to boto's Elastic Load Balancing interface |
| 5 ========================================================== | 5 ========================================================== |
| 6 | 6 |
| 7 This tutorial focuses on the boto interface for Elastic Load Balancing | 7 This tutorial focuses on the boto interface for `Elastic Load Balancing`_ |
| 8 from Amazon Web Services. This tutorial assumes that you have already | 8 from Amazon Web Services. This tutorial assumes that you have already |
| 9 downloaded and installed boto, and are familiar with the boto ec2 interface. | 9 downloaded and installed boto, and are familiar with the boto ec2 interface. |
| 10 | 10 |
| 11 .. _Elastic Load Balancing: http://aws.amazon.com/elasticloadbalancing/ |
| 12 |
| 11 Elastic Load Balancing Concepts | 13 Elastic Load Balancing Concepts |
| 12 ------------------------------- | 14 ------------------------------- |
| 13 Elastic Load Balancing (ELB) is intimately connected with Amazon's Elastic | 15 `Elastic Load Balancing`_ (ELB) is intimately connected with Amazon's `Elastic |
| 14 Compute Cloud (EC2) service. Using the ELB service allows you to create a load | 16 Compute Cloud`_ (EC2) service. Using the ELB service allows you to create a load |
| 15 balancer - a DNS endpoint and set of ports that distributes incoming requests | 17 balancer - a DNS endpoint and set of ports that distributes incoming requests |
| 16 to a set of ec2 instances. The advantages of using a load balancer is that it | 18 to a set of EC2 instances. The advantages of using a load balancer is that it |
| 17 allows you to truly scale up or down a set of backend instances without | 19 allows you to truly scale up or down a set of backend instances without |
| 18 disrupting service. Before the ELB service you had to do this manually by | 20 disrupting service. Before the ELB service, you had to do this manually by |
| 19 launching an EC2 instance and installing load balancer software on it (nginx, | 21 launching an EC2 instance and installing load balancer software on it (nginx, |
| 20 haproxy, perlbal, etc.) to distribute traffic to other EC2 instances. | 22 haproxy, perlbal, etc.) to distribute traffic to other EC2 instances. |
| 21 | 23 |
| 22 Recall that the ec2 service is split into Regions and Availability Zones (AZ). | 24 Recall that the EC2 service is split into Regions, which are further |
| 23 At the time of writing, there are two Regions - US and Europe, and each region | 25 divided into Availability Zones (AZ). |
| 24 is divided into a number of AZs (for example, us-east-1a, us-east-1b, etc.). | 26 For example, the US-East region is divided into us-east-1a, us-east-1b, |
| 25 You can think of AZs as data centers - each runs off a different set of ISP | 27 us-east-1c, us-east-1d, and us-east-1e. You can think of AZs as data centers - |
| 26 backbones and power providers. ELB load balancers can span multiple AZs but | 28 each runs off a different set of ISP backbones and power providers. |
| 27 cannot span multiple regions. That means that if you'd like to create a set of | 29 ELB load balancers can span multiple AZs but cannot span multiple regions. |
| 28 instances spanning both the US and Europe Regions you'd have to create two load | 30 That means that if you'd like to create a set of instances spanning both the |
| 29 balancers and have some sort of other means of distributing requests between | 31 US and Europe Regions you'd have to create two load balancers and have some |
| 30 the two loadbalancers. An example of this could be using GeoIP techniques to | 32 sort of other means of distributing requests between the two load balancers. |
| 31 choose the correct load balancer, or perhaps DNS round robin. Keep in mind also | 33 An example of this could be using GeoIP techniques to choose the correct load |
| 32 that traffic is distributed equally over all AZs the ELB balancer spans. This | 34 balancer, or perhaps DNS round robin. Keep in mind also that traffic is |
| 33 means you should have an equal number of instances in each AZ if you want to | 35 distributed equally over all AZs the ELB balancer spans. This means you should |
| 34 equally distribute load amongst all your instances. | 36 have an equal number of instances in each AZ if you want to equally distribute |
| 37 load amongst all your instances. |
| 38 |
| 39 .. _Elastic Compute Cloud: http://aws.amazon.com/ec2/ |
| 35 | 40 |
| 36 Creating a Connection | 41 Creating a Connection |
| 37 --------------------- | 42 --------------------- |
| 43 |
| 38 The first step in accessing ELB is to create a connection to the service. | 44 The first step in accessing ELB is to create a connection to the service. |
| 39 There are two ways to do this in boto. The first is: | |
| 40 | |
| 41 >>> from boto.ec2.elb import ELBConnection | |
| 42 >>> conn = ELBConnection('<aws access key>', '<aws secret key>') | |
| 43 | |
| 44 There is also a shortcut function in the boto package, called connect_elb | |
| 45 that may provide a slightly easier means of creating a connection: | |
| 46 | 45 |
| 47 >>> import boto | 46 >>> import boto |
| 48 >>> conn = boto.connect_elb() | 47 >>> conn = boto.connect_elb( |
| 48 aws_access_key_id='YOUR-KEY-ID-HERE', |
| 49 aws_secret_access_key='YOUR-SECRET-HERE' |
| 50 ) |
| 49 | 51 |
| 50 In either case, conn will point to an ELBConnection object which we will | |
| 51 use throughout the remainder of this tutorial. | |
| 52 | 52 |
| 53 A Note About Regions and Endpoints | 53 A Note About Regions and Endpoints |
| 54 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | 54 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 55 Like EC2 the ELB service has a different endpoint for each region. By default | |
| 56 the US endpoint is used. To choose a specific region, instantiate the | |
| 57 ELBConnection object with that region's endpoint. | |
| 58 | 55 |
| 59 >>> ec2 = boto.connect_elb(host='eu-west-1.elasticloadbalancing.amazonaws.com') | 56 Like EC2, the ELB service has a different endpoint for each region. By default |
| 57 the US East endpoint is used. To choose a specific region, instantiate the |
| 58 ELBConnection object with that region's information. |
| 59 |
| 60 >>> from boto.regioninfo import RegionInfo |
| 61 >>> reg = RegionInfo( |
| 62 name='eu-west-1', |
| 63 endpoint='elasticloadbalancing.eu-west-1.amazonaws.com' |
| 64 ) |
| 65 >>> conn = boto.connect_elb( |
| 66 aws_access_key_id='YOUR-KEY-ID-HERE', |
| 67 aws_secret_access_key='YOUR-SECRET-HERE', |
| 68 region=reg |
| 69 ) |
| 70 |
| 71 Another way to connect to an alternative region is like this: |
| 72 |
| 73 >>> import boto.ec2.elb |
| 74 >>> elb = boto.ec2.elb.connect_to_region('eu-west-1') |
| 75 |
| 76 Here's yet another way to discover what regions are available and then |
| 77 connect to one: |
| 78 |
| 79 >>> import boto.ec2.elb |
| 80 >>> regions = boto.ec2.elb.regions() |
| 81 >>> regions |
| 82 [RegionInfo:us-east-1, |
| 83 RegionInfo:ap-northeast-1, |
| 84 RegionInfo:us-west-1, |
| 85 RegionInfo:ap-southeast-1, |
| 86 RegionInfo:eu-west-1] |
| 87 >>> elb = regions[-1].connect() |
| 60 | 88 |
| 61 Alternatively, edit your boto.cfg with the default ELB endpoint to use:: | 89 Alternatively, edit your boto.cfg with the default ELB endpoint to use:: |
| 62 | 90 |
| 63 [Boto] | 91 [Boto] |
| 64 elb_endpoint = eu-west-1.elasticloadbalancing.amazonaws.com | 92 elb_region_name = eu-west-1 |
| 93 elb_region_endpoint = elasticloadbalancing.eu-west-1.amazonaws.com |
| 65 | 94 |
| 66 Getting Existing Load Balancers | 95 Getting Existing Load Balancers |
| 67 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | 96 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 68 | 97 |
| 69 To retrieve any exiting load balancers: | 98 To retrieve any exiting load balancers: |
| 70 | 99 |
| 71 >>> conn.get_all_load_balancers() | 100 >>> conn.get_all_load_balancers() |
| 101 [LoadBalancer:load-balancer-prod, LoadBalancer:load-balancer-staging] |
| 72 | 102 |
| 73 You will get back a list of LoadBalancer objects. | 103 You can also filter by name |
| 104 |
| 105 >>> conn.get_all_load_balancers(load_balancer_names=['load-balancer-prod']) |
| 106 [LoadBalancer:load-balancer-prod] |
| 107 |
| 108 :py:meth:`get_all_load_balancers <boto.ec2.elb.ELBConnection.get_all_load_balanc
ers>` |
| 109 returns a :py:class:`boto.resultset.ResultSet` that contains instances |
| 110 of :class:`boto.ec2.elb.loadbalancer.LoadBalancer`, each of which abstracts |
| 111 access to a load balancer. :py:class:`ResultSet <boto.resultset.ResultSet>` |
| 112 works very much like a list. |
| 113 |
| 114 >>> balancers = conn.get_all_load_balancers() |
| 115 >>> balancers[0] |
| 116 [LoadBalancer:load-balancer-prod] |
| 74 | 117 |
| 75 Creating a Load Balancer | 118 Creating a Load Balancer |
| 76 ------------------------ | 119 ------------------------ |
| 77 To create a load balancer you need the following: | 120 To create a load balancer you need the following: |
| 78 #. The specific **ports and protocols** you want to load balancer over, and wha
t port | 121 #. The specific **ports and protocols** you want to load balancer over, and wha
t port |
| 79 you want to connect to all instances. | 122 you want to connect to all instances. |
| 80 #. A **health check** - the ELB concept of a *heart beat* or *ping*. ELB will u
se this health | 123 #. A **health check** - the ELB concept of a *heart beat* or *ping*. ELB will u
se this health |
| 81 check to see whether your instances are up or down. If they go down, the loa
d balancer | 124 check to see whether your instances are up or down. If they go down, the loa
d balancer |
| 82 will no longer send requests to them. | 125 will no longer send requests to them. |
| 83 #. A **list of Availability Zones** you'd like to create your load balancer ove
r. | 126 #. A **list of Availability Zones** you'd like to create your load balancer ove
r. |
| (...skipping 13 matching lines...) Expand all Loading... |
| 97 | 140 |
| 98 This says that the load balancer will listen on two ports - 80 and 443. | 141 This says that the load balancer will listen on two ports - 80 and 443. |
| 99 Connections on 80 will use an HTTP load balancer to forward connections to port | 142 Connections on 80 will use an HTTP load balancer to forward connections to port |
| 100 8080 on instances. Likewise, the load balancer will listen on 443 to forward | 143 8080 on instances. Likewise, the load balancer will listen on 443 to forward |
| 101 connections to 8443 on each instance using the TCP balancer. We need to | 144 connections to 8443 on each instance using the TCP balancer. We need to |
| 102 use TCP for the HTTPS port because it is encrypted at the application | 145 use TCP for the HTTPS port because it is encrypted at the application |
| 103 layer. Of course, we could specify the load balancer use TCP for port 80, | 146 layer. Of course, we could specify the load balancer use TCP for port 80, |
| 104 however specifying HTTP allows you to let ELB handle some work for you - | 147 however specifying HTTP allows you to let ELB handle some work for you - |
| 105 for example HTTP header parsing. | 148 for example HTTP header parsing. |
| 106 | 149 |
| 150 .. _elb-configuring-a-health-check: |
| 107 | 151 |
| 108 Configuring a Health Check | 152 Configuring a Health Check |
| 109 ^^^^^^^^^^^^^^^^^^^^^^^^^^ | 153 ^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 110 A health check allows ELB to determine which instances are alive and able to | 154 A health check allows ELB to determine which instances are alive and able to |
| 111 respond to requests. A health check is essentially a tuple consisting of: | 155 respond to requests. A health check is essentially a tuple consisting of: |
| 112 | 156 |
| 113 * *target*: What to check on an instance. For a TCP check this is comprised of:
: | 157 * *Target*: What to check on an instance. For a TCP check this is comprised of:
: |
| 114 | 158 |
| 115 TCP:PORT_TO_CHECK | 159 TCP:PORT_TO_CHECK |
| 116 | 160 |
| 117 Which attempts to open a connection on PORT_TO_CHECK. If the connection opens | 161 Which attempts to open a connection on PORT_TO_CHECK. If the connection opens |
| 118 successfully, that specific instance is deemed healthy, otherwise it is marke
d | 162 successfully, that specific instance is deemed healthy, otherwise it is marke
d |
| 119 temporarily as unhealthy. For HTTP, the situation is slightly different:: | 163 temporarily as unhealthy. For HTTP, the situation is slightly different:: |
| 120 | 164 |
| 121 HTTP:PORT_TO_CHECK/RESOURCE | 165 HTTP:PORT_TO_CHECK/RESOURCE |
| 122 | 166 |
| 123 This means that the health check will connect to the resource /RESOURCE on | 167 This means that the health check will connect to the resource /RESOURCE on |
| 124 PORT_TO_CHECK. If an HTTP 200 status is returned the instance is deemed healt
hy. | 168 PORT_TO_CHECK. If an HTTP 200 status is returned the instance is deemed healt
hy. |
| 125 * *interval*: How often the check is made. This is given in seconds and default
s to 30. | 169 * *Interval*: How often the check is made. This is given in seconds and default
s |
| 126 The valid range of intervals goes from 5 seconds to 600 seconds. | 170 to 30. The valid range of intervals goes from 5 seconds to 600 seconds. |
| 127 * *timeout*: The number of seconds the load balancer will wait for a check to r
eturn a | 171 * *Timeout*: The number of seconds the load balancer will wait for a check to |
| 128 result. | 172 return a result. |
| 129 * *UnhealthyThreshold*: The number of consecutive failed checks to deem the ins
tance | 173 * *Unhealthy threshold*: The number of consecutive failed checks to deem the |
| 130 as being dead. The default is 5, and the range of valid values lies from 2 to
10. | 174 instance as being dead. The default is 5, and the range of valid values lies |
| 175 from 2 to 10. |
| 131 | 176 |
| 132 The following example creates a health check called *instance_health* that simpl
y checks | 177 The following example creates a health check called *instance_health* that |
| 133 instances every 20 seconds on port 80 over HTTP at the resource /health for 200
successes. | 178 simply checks instances every 20 seconds on port 80 over HTTP at the |
| 179 resource /health for 200 successes. |
| 134 | 180 |
| 135 >>> import boto | |
| 136 >>> from boto.ec2.elb import HealthCheck | 181 >>> from boto.ec2.elb import HealthCheck |
| 137 >>> conn = boto.connect_elb() | 182 >>> hc = HealthCheck( |
| 138 >>> hc = HealthCheck('instance_health', interval=20, target='HTTP:8080/health') | 183 interval=20, |
| 184 healthy_threshold=3, |
| 185 unhealthy_threshold=5, |
| 186 target='HTTP:8080/health' |
| 187 ) |
| 139 | 188 |
| 140 Putting It All Together | 189 Putting It All Together |
| 141 ^^^^^^^^^^^^^^^^^^^^^^^ | 190 ^^^^^^^^^^^^^^^^^^^^^^^ |
| 142 | 191 |
| 143 Finally, let's create a load balancer in the US region that listens on ports 80
and 443 | 192 Finally, let's create a load balancer in the US region that listens on ports |
| 144 and distributes requests to instances on 8080 and 8443 over HTTP and TCP. We wan
t the | 193 80 and 443 and distributes requests to instances on 8080 and 8443 over HTTP |
| 145 load balancer to span the availability zones *us-east-1a* and *us-east-1b*: | 194 and TCP. We want the load balancer to span the availability zones |
| 195 *us-east-1a* and *us-east-1b*: |
| 146 | 196 |
| 147 >>> lb = conn.create_load_balancer('my_lb', ['us-east-1a', 'us-east-1b'], | 197 >>> regions = ['us-east-1a', 'us-east-1b'] |
| 148 [(80, 8080, 'http'), (443, 8443, 'tcp')]) | 198 >>> ports = [(80, 8080, 'http'), (443, 8443, 'tcp')] |
| 199 >>> lb = conn.create_load_balancer('my-lb', regions, ports) |
| 200 >>> # This is from the previous section. |
| 149 >>> lb.configure_health_check(hc) | 201 >>> lb.configure_health_check(hc) |
| 150 | 202 |
| 151 The load balancer has been created. To see where you can actually connect to it,
do: | 203 The load balancer has been created. To see where you can actually connect to |
| 204 it, do: |
| 152 | 205 |
| 153 >>> print lb.dns_name | 206 >>> print lb.dns_name |
| 154 my_elb-123456789.us-east-1.elb.amazonaws.com | 207 my_elb-123456789.us-east-1.elb.amazonaws.com |
| 155 | 208 |
| 156 You can then CNAME map a better name, i.e. www.MYWEBSITE.com to the above addres
s. | 209 You can then CNAME map a better name, i.e. www.MYWEBSITE.com to the |
| 210 above address. |
| 157 | 211 |
| 158 Adding Instances To a Load Balancer | 212 Adding Instances To a Load Balancer |
| 159 ----------------------------------- | 213 ----------------------------------- |
| 160 | 214 |
| 161 Now that the load balancer has been created, there are two ways to add instances
to it: | 215 Now that the load balancer has been created, there are two ways to add |
| 216 instances to it: |
| 162 | 217 |
| 163 #. Manually, adding each instance in turn. | 218 #. Manually, adding each instance in turn. |
| 164 #. Mapping an autoscale group to the load balancer. Please see the Autoscale | 219 #. Mapping an autoscale group to the load balancer. Please see the |
| 165 tutorial for information on how to do this. | 220 :doc:`Autoscale tutorial <autoscale_tut>` for information on how to do this. |
| 166 | 221 |
| 167 Manually Adding and Removing Instances | 222 Manually Adding and Removing Instances |
| 168 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | 223 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 169 | 224 |
| 170 Assuming you have a list of instance ids, you can add them to the load balancer | 225 Assuming you have a list of instance ids, you can add them to the load balancer |
| 171 | 226 |
| 172 >>> instance_ids = ['i-4f8cf126', 'i-0bb7ca62'] | 227 >>> instance_ids = ['i-4f8cf126', 'i-0bb7ca62'] |
| 173 >>> lb.register_instances(instance_ids) | 228 >>> lb.register_instances(instance_ids) |
| 174 | 229 |
| 175 Keep in mind that these instances should be in Security Groups that match the | 230 Keep in mind that these instances should be in Security Groups that match the |
| (...skipping 17 matching lines...) Expand all Loading... |
| 193 To enable zones: | 248 To enable zones: |
| 194 | 249 |
| 195 >>> lb.enable_zones(['us-east-1c']) | 250 >>> lb.enable_zones(['us-east-1c']) |
| 196 | 251 |
| 197 Deleting a Load Balancer | 252 Deleting a Load Balancer |
| 198 ------------------------ | 253 ------------------------ |
| 199 | 254 |
| 200 >>> lb.delete() | 255 >>> lb.delete() |
| 201 | 256 |
| 202 | 257 |
| OLD | NEW |