Google Load Balancer Driver Documentation

Loadbalancing in Compute Engine is native to Google Compute Engine.

../../_images/gcp.png

Connecting to Compute Engine Load Balancer

Refer to Google Compute Engine Driver Documentation for information about setting up authentication for GCE.

In order to instantiate a driver for the Load Balancer, you can either pass in the same authentication information as you would to the GCE driver, or you can instantiate the GCE driver and pass that to the Load Balancer driver. The latter is preferred (since you are probably getting a GCE driver anyway), but the former aligns more closely to the Libcloud API.

Examples

Additional example code can be found in the “demos” directory of Libcloud here: https://github.com/apache/libcloud/blob/trunk/demos/gce_lb_demo.py

1. Getting Driver with GCE Driver

from libcloud.compute.types import Provider
from libcloud.compute.providers import get_driver

from libcloud.loadbalancer.types import Provider as LBProvider
from libcloud.loadbalancer.providers import get_driver as lb_get_driver

ComputeEngine = get_driver(Provider.GCE)
gce_driver = ComputeEngine(
    "service_account_email_or_client_id",
    "pem_file_or_client_secret",
    project="your_project_id",
)

LoadBalancer = lb_get_driver(LBProvider.GCE)
lb_driver = LoadBalancer(gce_driver=gce_driver)

2. Getting Driver with Authentication Information

With local key file:

from libcloud.loadbalancer.types import Provider
from libcloud.loadbalancer.providers import get_driver

LoadBalancer = get_driver(Provider.GCE)
driver = LoadBalancer(
    "service_account_email_or_client_id",
    "pem_file_or_client_secret",
    project="your_project_id",
)

With Service Account credentials as dict:

from libcloud.loadbalancer.types import Provider
from libcloud.loadbalancer.providers import get_driver

credentials = {
    "type": "service_account",
    "project_id": "my_project",
    "private_key": "-----BEGIN PRIVATE KEY-----\nmy_private_key_data\n"
    "-----END PRIVATE KEY-----\n",
    "client_email": "my_email",
}

LoadBalancer = get_driver(Provider.GCE)
driver = LoadBalancer(
    "your_service_account_email", credentials, project="your_project_id"
)

API Docs

class libcloud.loadbalancer.drivers.gce.GCELBDriver(*args, **kwargs)[source]
Parameters
  • key (str) – API key or username to be used (required)

  • secret (str) – Secret password to be used (required)

  • secure (bool) – Whether to use HTTPS or HTTP. Note: Some providers only support HTTPS, and it is on by default.

  • host (str) – Override hostname used for connections.

  • port (int) – Override port used for connections.

  • api_version (str) – Optional API version. Only used by drivers which support multiple API versions.

  • region (str) – Optional driver region. Only used by drivers which support multiple regions.

Return type

None

balancer_attach_compute_node(balancer, node)[source]

Attach a compute node as a member to the load balancer.

Parameters
  • balancer (LoadBalancer) – LoadBalancer which should be used

  • node (Node) – Node to join to the balancer

Returns

Member after joining the balancer.

Return type

Member

balancer_attach_member(balancer, member)[source]

Attach a member to balancer

Parameters
  • balancer (LoadBalancer) – LoadBalancer which should be used

  • member (Member) – Member to join to the balancer

Returns

Member after joining the balancer.

Return type

Member

balancer_detach_member(balancer, member)[source]

Detach member from balancer

Parameters
  • balancer (LoadBalancer) – LoadBalancer which should be used

  • member (Member) – Member which should be used

Returns

True if member detach was successful, otherwise False

Return type

bool

balancer_list_members(balancer)[source]

Return list of members attached to balancer

Parameters

balancer (LoadBalancer) – LoadBalancer which should be used

Return type

list of Member

connectionCls

alias of libcloud.compute.drivers.gce.GCEConnection

create_balancer(name, port, protocol, algorithm, members, ex_region=None, ex_healthchecks=None, ex_address=None, ex_session_affinity=None)[source]

Create a new load balancer instance.

For GCE, this means creating a forwarding rule and a matching target pool, then adding the members to the target pool.

Parameters
  • name (str) – Name of the new load balancer (required)

  • port (str) – Port or range of ports the load balancer should listen on, defaults to all ports. Examples: ‘80’, ‘5000-5999’

  • protocol (str) – Load balancer protocol. Should be ‘tcp’ or ‘udp’, defaults to ‘tcp’.

  • members (list of Member or Node) – List of Members to attach to balancer. Can be Member objects or Node objects. Node objects are preferred for GCE, but Member objects are accepted to comply with the established libcloud API. Note that the ‘port’ attribute of the members is ignored.

  • algorithm (Algorithm or None) – Load balancing algorithm. Ignored for GCE which uses a hashing-based algorithm.

  • ex_region (C{GCERegion} or str) – Optional region to create the load balancer in. Defaults to the default region of the GCE Node Driver.

  • ex_healthchecks (list of GCEHealthCheck or list of str) – Optional list of healthcheck objects or names to add to the load balancer.

  • ex_address (C{GCEAddress}) – Optional static address object to be assigned to the load balancer.

  • ex_session_affinity (str) – Optional algorithm to use for session affinity. This will modify the hashing algorithm such that a client will tend to stick to a particular Member.

Returns

LoadBalancer object

Return type

LoadBalancer

destroy_balancer(balancer)[source]

Destroy a load balancer.

For GCE, this means destroying the associated forwarding rule, then destroying the target pool that was attached to the forwarding rule.

Parameters

balancer (LoadBalancer) – LoadBalancer which should be used

Returns

True if successful

Return type

bool

ex_balancer_attach_healthcheck(balancer, healthcheck)[source]

Attach a healthcheck to balancer

Parameters
  • balancer (LoadBalancer) – LoadBalancer which should be used

  • healthcheck (GCEHealthCheck) – Healthcheck to add

Returns

True if successful

Return type

bool

ex_balancer_detach_healthcheck(balancer, healthcheck)[source]

Detach healtcheck from balancer

Parameters
  • balancer (LoadBalancer) – LoadBalancer which should be used

  • healthcheck (GCEHealthCheck) – Healthcheck to remove

Returns

True if successful

Return type

bool

ex_balancer_list_healthchecks(balancer)[source]

Return list of healthchecks attached to balancer

Parameters

balancer (LoadBalancer) – LoadBalancer which should be used

Return type

list of HealthChecks

get_balancer(balancer_id)[source]

Return a LoadBalancer object.

Parameters
  • balancer_id – Name of load balancer you wish to fetch. For GCE, this is the name of the associated forwarding rule.

  • balancer_idstr

Return type

LoadBalancer

list_balancers(ex_region=None)[source]

List all loadbalancers

Parameters

ex_region (str or GCERegion or None) – The region to return balancers from. If None, will default to self.region. If ‘all’, will return all balancers.

Return type

list of LoadBalancer

list_protocols()[source]

Return a list of supported protocols.

For GCE, this is simply a hardcoded list.

Return type

list of str

list_supported_algorithms()

Return algorithms supported by this driver.

Return type

list of str

update_balancer(balancer, **kwargs)

Sets the name, algorithm, protocol, or port on a load balancer.

Parameters
  • balancer (LoadBalancer) – LoadBalancer which should be used

  • name (str) – New load balancer name

  • algorithm (Algorithm) – New load balancer algorithm

  • protocol (str) – New load balancer protocol

  • port (int) – New load balancer port

Return type

LoadBalancer