2017-04-26 05:09:08 +00:00
.. _tutorial-libcloud:
==============================================================================
Using Apache Libcloud for declarative and procedural multi-cloud orchestration
==============================================================================
2018-02-22 03:47:41 +00:00
.. versionadded :: 2018.3.0
2017-04-26 05:09:08 +00:00
.. note ::
This walkthrough assumes basic knowledge of Salt and Salt States. To get up to speed, check out the
:ref: `Salt Walkthrough <tutorial-salt-walk-through>` .
2017-04-26 20:32:25 +00:00
Apache Libcloud is a Python library which hides differences between different cloud provider APIs and allows
2017-04-28 09:01:21 +00:00
you to manage different cloud resources through a unified and easy to use API. Apache Libcloud supports over
2017-09-19 18:47:16 +00:00
60 cloud platforms, including Amazon, Microsoft Azure, DigitalOcean, Google Cloud Platform and OpenStack.
2017-04-26 05:09:08 +00:00
2017-04-30 10:32:44 +00:00
Execution and state modules are available for Compute, DNS, Storage and Load Balancer drivers from Apache Libcloud in
2017-04-26 20:32:25 +00:00
SaltStack.
2017-04-26 05:09:08 +00:00
2017-04-30 10:32:44 +00:00
* :mod: `libcloud_compute <salt.modules.libcloud_compute>` - Compute -
services such as OpenStack Nova, Amazon EC2, Microsoft Azure VMs
2017-04-26 20:32:25 +00:00
* :mod: `libcloud_dns <salt.modules.libcloud_dns>` - DNS as a Service -
services such as Amazon Route 53 and Zerigo
2017-04-30 10:32:44 +00:00
* :mod: `libcloud_loadbalancer <salt.modules.libcloud_loadbalancer>` - Load Balancers as a Service -
services such as Amazon Elastic Load Balancer and GoGrid LoadBalancers
* :mod: `libcloud_storage <salt.modules.libcloud_storage>` - Cloud Object Storage and CDN -
services such as Amazon S3 and Rackspace CloudFiles, OpenStack Swift
2017-04-26 05:09:08 +00:00
2017-04-26 20:32:25 +00:00
These modules are designed as a way of having a multi-cloud deployment and abstracting simple differences
between platform to design a high-availability architecture.
2017-04-26 05:09:08 +00:00
The Apache Libcloud functionality is available through both execution modules and Salt states.
Configuring Drivers
===================
Drivers can be configured in the Salt Configuration/Minion settings. All libcloud modules expect a list of "profiles" to
be configured with authentication details for each driver.
2017-04-26 20:32:25 +00:00
Each driver will have a string identifier, these can be found in the libcloud.<api>.types.Provider class
for each API, http://libcloud.readthedocs.io/en/latest/supported_providers.html
2017-04-26 05:09:08 +00:00
Some drivers require additional parameters, which are documented in the Apache Libcloud documentation. For example,
GoDaddy DNS expects "`shopper_id` ", which is the customer ID. These additional parameters can be added to the profile settings
and will be passed directly to the driver instantiation method.
.. code-block :: yaml
libcloud_dns:
godaddy:
driver: godaddy
shopper_id: 90425123
key: AFDDJFGIjDFVNSDIFNASMC
secret: FG(#f8vdfgjlkm)
libcloud_storage:
google:
driver: google_storage
key: GOOG4ASDIDFNVIdfnIVW
secret: R+qYE9hkfdhv89h4invhdfvird4Pq3an8rnK
2017-04-26 20:32:25 +00:00
You can have multiple profiles for a single driver, for example if you wanted 2 DNS profiles for Amazon Route53,
naming them "route53_prod" and "route54_test" would help your
2017-04-26 05:09:08 +00:00
administrators distinguish their purpose.
.. code-block :: yaml
libcloud_dns:
route53_prod:
driver: route53
key: AFDDJFGIjDFVNSDIFNASMC
secret: FG(#f8vdfgjlkm)
route53_test:
driver: route53
key: AFDDJFGIjdfgdfgdf
secret: FG(#f8vdfgjlkm)
Using the execution modules
===========================
2017-04-26 20:32:25 +00:00
Amongst over 60 clouds that Apache Libcloud supports, you can add profiles to your Salt configuration to access and control these clouds.
2017-04-30 10:32:44 +00:00
Each of the libcloud execution modules exposes the common API methods for controlling Compute, DNS, Load Balancers and Object Storage.
2017-04-26 20:32:25 +00:00
To see which functions are supported across specific clouds, see the Libcloud `supported methods
<http://libcloud.readthedocs.io/en/latest/supported_providers.html#supported-methods-block-storage> `_ documentation.
2017-04-26 05:09:08 +00:00
2017-04-26 20:32:25 +00:00
The module documentation explains each of the API methods and how to leverage them.
2017-04-26 05:09:08 +00:00
2017-04-30 10:32:44 +00:00
* :mod: `libcloud_compute <salt.modules.libcloud_compute>` - Compute -
services such as OpenStack Nova, Amazon EC2, Microsoft Azure VMs
* :mod: `libcloud_dns <salt.modules.libcloud_dns>` - DNS as a Service -
services such as Amazon Route 53 and Zerigo
* :mod: `libcloud_loadbalancer <salt.modules.libcloud_loadbalancer>` - Load Balancers as a Service -
services such as Amazon Elastic Load Balancer and GoGrid LoadBalancers
* :mod: `libcloud_storage <salt.modules.libcloud_storage>` - Cloud Object Storage and CDN -
services such as Amazon S3 and Rackspace CloudFiles, OpenStack Swift
2017-04-26 05:09:08 +00:00
For example, listing buckets in the Google Storage platform:
.. code-block :: bash
$ salt-call libcloud_storage.list_containers google
local:
|_
----------
extra:
----------
creation_date:
2017-01-05T05:44:56.324Z
name:
anthonypjshaw
2017-04-26 20:32:25 +00:00
The Apache Libcloud storage module can be used to syncronize files between multiple storage clouds,
2017-04-28 09:01:21 +00:00
such as Google Storage, S3 and OpenStack Swift
2017-04-26 05:09:08 +00:00
.. code-block :: bash
$ salt '*' libcloud_storage.download_object DeploymentTools test.sh /tmp/test.sh google_storage
Using the state modules
=======================
2017-04-26 20:32:25 +00:00
For each configured profile, the assets available in the API (e.g. storage objects, containers,
DNS records and load balancers) can be deployed via Salt's state system.
2017-04-26 05:09:08 +00:00
The state module documentation explains the specific states that each module supports
2017-04-26 20:32:25 +00:00
* :mod: `libcloud_storage <salt.states.libcloud_storage>` - Cloud Object Storage and CDN
- services such as Amazon S3 and Rackspace CloudFiles, OpenStack Swift
* :mod: `libcloud_loadbalancer <salt.states.libcloud_loadbalancer>` - Load Balancers as a Service
- services such as Amazon Elastic Load Balancer and GoGrid LoadBalancers
* :mod: `libcloud_dns <salt.states.libcloud_dns>` - DNS as a Service
- services such as Amazon Route 53 and Zerigo
2017-04-26 05:09:08 +00:00
For DNS, the state modules can be used to provide DNS resilience for multiple nameservers, for example:
.. code-block :: yaml
libcloud_dns:
godaddy:
driver: godaddy
shopper_id: 12345
key: 2orgk34kgk34g
secret: fjgoidhjgoim
amazon:
driver: route53
key: blah
secret: blah
And then in a state file:
.. code-block :: yaml
webserver:
libcloud_dns.zone_present:
name: mywebsite.com
profile: godaddy
libcloud_dns.record_present:
name: www
zone: mywebsite.com
type: A
data: 12.34.32.3
profile: godaddy
libcloud_dns.zone_present:
name: mywebsite.com
profile: amazon
libcloud_dns.record_present:
name: www
zone: mywebsite.com
type: A
data: 12.34.32.3
profile: amazon
This could be combined with a multi-cloud load balancer deployment,
.. code-block :: yaml
webserver:
libcloud_dns.zone_present:
2017-04-26 11:10:43 +00:00
- name: mywebsite.com
- profile: godaddy
2017-04-26 05:09:08 +00:00
...
libcloud_loadbalancer.balancer_present:
2017-04-26 11:10:43 +00:00
- name: web_main
- port: 80
- protocol: http
- members:
2017-04-26 05:09:08 +00:00
- ip: 1.2.4.5
port: 80
- ip: 2.4.5.6
port: 80
2017-04-26 11:10:43 +00:00
- profile: google_gce
2017-04-26 05:09:08 +00:00
libcloud_loadbalancer.balancer_present:
2017-04-26 11:10:43 +00:00
- name: web_main
- port: 80
- protocol: http
- members:
2017-04-26 05:09:08 +00:00
- ip: 1.2.4.5
port: 80
- ip: 2.4.5.6
port: 80
2017-04-26 11:10:43 +00:00
- profile: amazon_elb
Extended parameters can be passed to the specific cloud, for example you can specify the region with the Google Cloud API, because
`create_balancer` can accept a `ex_region` argument. Adding this argument to the state will pass the additional command to the driver.
.. code-block :: yaml
lb_test:
libcloud_loadbalancer.balancer_absent:
- name: example
- port: 80
- protocol: http
- profile: google
- ex_region: us-east1
2017-04-26 05:09:08 +00:00
Accessing custom arguments in execution modules
===============================================
Some cloud providers have additional functionality that can be accessed on top of the base API, for example
the Google Cloud Engine load balancer service offers the ability to provision load balancers into a specific region.
2017-04-26 20:32:25 +00:00
Looking at the `API documentation <http://libcloud.readthedocs.io/en/latest/loadbalancer/drivers/gce.html#libcloud.loadbalancer.drivers.gce.GCELBDriver.create_balancer> `_ ,
we can see that it expects an `ex_region` in the `create_balancer` method, so when we execute the salt command, we can add this additional parameter like this:
2017-04-26 05:09:08 +00:00
.. code-block :: bash
$ salt myminion libcloud_storage.create_balancer my_balancer 80 http profile1 ex_region=us-east1
$ salt myminion libcloud_storage.list_container_objects my_bucket profile1 ex_prefix=me
Accessing custom methods in Libcloud drivers
============================================
2017-04-26 20:32:25 +00:00
Some cloud APIs have additional methods that are prefixed with `ex_` in Apache Libcloud, these methods
are part of the non-standard API but can still
2017-04-26 05:09:08 +00:00
be accessed from the Salt modules for `libcloud_storage` , `libcloud_loadbalancer` and `libcloud_dns` .
2017-04-26 20:32:25 +00:00
The extra methods are available via the `extra` command, which expects the name of the method as the
first argument, the profile as the second and then
2017-04-26 05:09:08 +00:00
accepts a list of keyword arguments to pass onto the driver method, for example, accessing permissions in Google Storage objects:
.. code-block :: bash
$ salt myminion libcloud_storage.extra ex_get_permissions google container_name=my_container object_name=me.jpg --out=yaml
2017-04-26 11:10:43 +00:00
Example profiles
================
Google Cloud
~~~~~~~~~~~~
Using Service Accounts with GCE, you can provide a path to the JSON file and the project name in the parameters.
.. code-block :: yaml
google:
driver: gce
user_id: 234234-compute@developer.gserviceaccount.com
key: /path/to/service_account_download.json
auth_type: SA
2017-04-28 09:01:21 +00:00
project: project-name