Google Load Balancer Driver Documentation
Loadbalancing in Compute Engine is native to Google Compute Engine.
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 usednode (
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 usedmember (
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 usedmember (
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
ofMember
- connectionCls
alias of
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
ofMember
orNode
) – 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
orNone
) – 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
ofGCEHealthCheck
orlist
ofstr
) – 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 usedhealthcheck (
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 usedhealthcheck (
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
ofHealthChecks
- 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_id –
str
- Return type:
LoadBalancer
- list_balancers(ex_region=None)[source]
List all loadbalancers
- Parameters:
ex_region (
str
orGCERegion
orNone
) – The region to return balancers from. If None, will default to self.region. If ‘all’, will return all balancers.- Return type:
list
ofLoadBalancer
- list_protocols()[source]
Return a list of supported protocols.
For GCE, this is simply a hardcoded list.
- Return type:
list
ofstr
- list_supported_algorithms()
Return algorithms supported by this driver.
- Return type:
list
ofstr
- update_balancer(balancer, **kwargs)
Sets the name, algorithm, protocol, or port on a load balancer.
- Parameters:
balancer (
LoadBalancer
) – LoadBalancer which should be usedname (
str
) – New load balancer namealgorithm (
Algorithm
) – New load balancer algorithmprotocol (
str
) – New load balancer protocolport (
int
) – New load balancer port
- Return type:
LoadBalancer