1
0

libcloud.rst 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. .. _tutorial-libcloud:
  2. ==============================================================================
  3. Using Apache Libcloud for declarative and procedural multi-cloud orchestration
  4. ==============================================================================
  5. .. versionadded:: 2018.3.0
  6. .. note::
  7. This walkthrough assumes basic knowledge of Salt and Salt States. To get up to speed, check out the
  8. :ref:`Salt Walkthrough <tutorial-salt-walk-through>`.
  9. Apache Libcloud is a Python library which hides differences between different cloud provider APIs and allows
  10. you to manage different cloud resources through a unified and easy to use API. Apache Libcloud supports over
  11. 60 cloud platforms, including Amazon, Microsoft Azure, DigitalOcean, Google Cloud Platform and OpenStack.
  12. Execution and state modules are available for Compute, DNS, Storage and Load Balancer drivers from Apache Libcloud in
  13. SaltStack.
  14. * :mod:`libcloud_compute <salt.modules.libcloud_compute>` - Compute -
  15. services such as OpenStack Nova, Amazon EC2, Microsoft Azure VMs
  16. * :mod:`libcloud_dns <salt.modules.libcloud_dns>` - DNS as a Service -
  17. services such as Amazon Route 53 and Zerigo
  18. * :mod:`libcloud_loadbalancer <salt.modules.libcloud_loadbalancer>` - Load Balancers as a Service -
  19. services such as Amazon Elastic Load Balancer and GoGrid LoadBalancers
  20. * :mod:`libcloud_storage <salt.modules.libcloud_storage>` - Cloud Object Storage and CDN -
  21. services such as Amazon S3 and Rackspace CloudFiles, OpenStack Swift
  22. These modules are designed as a way of having a multi-cloud deployment and abstracting simple differences
  23. between platform to design a high-availability architecture.
  24. The Apache Libcloud functionality is available through both execution modules and Salt states.
  25. Configuring Drivers
  26. ===================
  27. Drivers can be configured in the Salt Configuration/Minion settings. All libcloud modules expect a list of "profiles" to
  28. be configured with authentication details for each driver.
  29. Each driver will have a string identifier, these can be found in the libcloud.<api>.types.Provider class
  30. for each API, https://libcloud.readthedocs.io/en/latest/supported_providers.html
  31. Some drivers require additional parameters, which are documented in the Apache Libcloud documentation. For example,
  32. GoDaddy DNS expects "`shopper_id`", which is the customer ID. These additional parameters can be added to the profile settings
  33. and will be passed directly to the driver instantiation method.
  34. .. code-block:: yaml
  35. libcloud_dns:
  36. godaddy:
  37. driver: godaddy
  38. shopper_id: 90425123
  39. key: AFDDJFGIjDFVNSDIFNASMC
  40. secret: FG(#f8vdfgjlkm)
  41. libcloud_storage:
  42. google:
  43. driver: google_storage
  44. key: GOOG4ASDIDFNVIdfnIVW
  45. secret: R+qYE9hkfdhv89h4invhdfvird4Pq3an8rnK
  46. You can have multiple profiles for a single driver, for example if you wanted 2 DNS profiles for Amazon Route53,
  47. naming them "route53_prod" and "route54_test" would help your
  48. administrators distinguish their purpose.
  49. .. code-block:: yaml
  50. libcloud_dns:
  51. route53_prod:
  52. driver: route53
  53. key: AFDDJFGIjDFVNSDIFNASMC
  54. secret: FG(#f8vdfgjlkm)
  55. route53_test:
  56. driver: route53
  57. key: AFDDJFGIjdfgdfgdf
  58. secret: FG(#f8vdfgjlkm)
  59. Using the execution modules
  60. ===========================
  61. Amongst over 60 clouds that Apache Libcloud supports, you can add profiles to your Salt configuration to access and control these clouds.
  62. Each of the libcloud execution modules exposes the common API methods for controlling Compute, DNS, Load Balancers and Object Storage.
  63. To see which functions are supported across specific clouds, see the Libcloud `supported methods
  64. <https://libcloud.readthedocs.io/en/latest/supported_providers.html#supported-methods-block-storage>`_ documentation.
  65. The module documentation explains each of the API methods and how to leverage them.
  66. * :mod:`libcloud_compute <salt.modules.libcloud_compute>` - Compute -
  67. services such as OpenStack Nova, Amazon EC2, Microsoft Azure VMs
  68. * :mod:`libcloud_dns <salt.modules.libcloud_dns>` - DNS as a Service -
  69. services such as Amazon Route 53 and Zerigo
  70. * :mod:`libcloud_loadbalancer <salt.modules.libcloud_loadbalancer>` - Load Balancers as a Service -
  71. services such as Amazon Elastic Load Balancer and GoGrid LoadBalancers
  72. * :mod:`libcloud_storage <salt.modules.libcloud_storage>` - Cloud Object Storage and CDN -
  73. services such as Amazon S3 and Rackspace CloudFiles, OpenStack Swift
  74. For example, listing buckets in the Google Storage platform:
  75. .. code-block:: console
  76. $ salt-call libcloud_storage.list_containers google
  77. local:
  78. |_
  79. ----------
  80. extra:
  81. ----------
  82. creation_date:
  83. 2017-01-05T05:44:56.324Z
  84. name:
  85. anthonypjshaw
  86. The Apache Libcloud storage module can be used to synchronize files between multiple storage clouds,
  87. such as Google Storage, S3 and OpenStack Swift
  88. .. code-block:: bash
  89. $ salt '*' libcloud_storage.download_object DeploymentTools test.sh /tmp/test.sh google_storage
  90. Using the state modules
  91. =======================
  92. For each configured profile, the assets available in the API (e.g. storage objects, containers,
  93. DNS records and load balancers) can be deployed via Salt's state system.
  94. The state module documentation explains the specific states that each module supports
  95. * :mod:`libcloud_storage <salt.states.libcloud_storage>` - Cloud Object Storage and CDN
  96. - services such as Amazon S3 and Rackspace CloudFiles, OpenStack Swift
  97. * :mod:`libcloud_loadbalancer <salt.states.libcloud_loadbalancer>` - Load Balancers as a Service
  98. - services such as Amazon Elastic Load Balancer and GoGrid LoadBalancers
  99. * :mod:`libcloud_dns <salt.states.libcloud_dns>` - DNS as a Service
  100. - services such as Amazon Route 53 and Zerigo
  101. For DNS, the state modules can be used to provide DNS resilience for multiple nameservers, for example:
  102. .. code-block:: yaml
  103. libcloud_dns:
  104. godaddy:
  105. driver: godaddy
  106. shopper_id: 12345
  107. key: 2orgk34kgk34g
  108. secret: fjgoidhjgoim
  109. amazon:
  110. driver: route53
  111. key: blah
  112. secret: blah
  113. And then in a state file:
  114. .. code-block:: yaml
  115. webserver:
  116. libcloud_dns.zone_present:
  117. name: mywebsite.com
  118. profile: godaddy
  119. libcloud_dns.record_present:
  120. name: www
  121. zone: mywebsite.com
  122. type: A
  123. data: 12.34.32.3
  124. profile: godaddy
  125. libcloud_dns.zone_present:
  126. name: mywebsite.com
  127. profile: amazon
  128. libcloud_dns.record_present:
  129. name: www
  130. zone: mywebsite.com
  131. type: A
  132. data: 12.34.32.3
  133. profile: amazon
  134. This could be combined with a multi-cloud load balancer deployment,
  135. .. code-block:: yaml
  136. webserver:
  137. libcloud_dns.zone_present:
  138. - name: mywebsite.com
  139. - profile: godaddy
  140. ...
  141. libcloud_loadbalancer.balancer_present:
  142. - name: web_main
  143. - port: 80
  144. - protocol: http
  145. - members:
  146. - ip: 1.2.4.5
  147. port: 80
  148. - ip: 2.4.5.6
  149. port: 80
  150. - profile: google_gce
  151. libcloud_loadbalancer.balancer_present:
  152. - name: web_main
  153. - port: 80
  154. - protocol: http
  155. - members:
  156. - ip: 1.2.4.5
  157. port: 80
  158. - ip: 2.4.5.6
  159. port: 80
  160. - profile: amazon_elb
  161. Extended parameters can be passed to the specific cloud, for example you can specify the region with the Google Cloud API, because
  162. `create_balancer` can accept a `ex_region` argument. Adding this argument to the state will pass the additional command to the driver.
  163. .. code-block:: yaml
  164. lb_test:
  165. libcloud_loadbalancer.balancer_absent:
  166. - name: example
  167. - port: 80
  168. - protocol: http
  169. - profile: google
  170. - ex_region: us-east1
  171. Accessing custom arguments in execution modules
  172. ===============================================
  173. Some cloud providers have additional functionality that can be accessed on top of the base API, for example
  174. the Google Cloud Engine load balancer service offers the ability to provision load balancers into a specific region.
  175. Looking at the `API documentation <https://libcloud.readthedocs.io/en/latest/loadbalancer/drivers/gce.html#libcloud.loadbalancer.drivers.gce.GCELBDriver.create_balancer>`_,
  176. 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:
  177. .. code-block:: bash
  178. $ salt myminion libcloud_storage.create_balancer my_balancer 80 http profile1 ex_region=us-east1
  179. $ salt myminion libcloud_storage.list_container_objects my_bucket profile1 ex_prefix=me
  180. Accessing custom methods in Libcloud drivers
  181. ============================================
  182. Some cloud APIs have additional methods that are prefixed with `ex_` in Apache Libcloud, these methods
  183. are part of the non-standard API but can still
  184. be accessed from the Salt modules for `libcloud_storage`, `libcloud_loadbalancer` and `libcloud_dns`.
  185. The extra methods are available via the `extra` command, which expects the name of the method as the
  186. first argument, the profile as the second and then
  187. accepts a list of keyword arguments to pass onto the driver method, for example, accessing permissions in Google Storage objects:
  188. .. code-block:: bash
  189. $ salt myminion libcloud_storage.extra ex_get_permissions google container_name=my_container object_name=me.jpg --out=yaml
  190. Example profiles
  191. ================
  192. Google Cloud
  193. ~~~~~~~~~~~~
  194. Using Service Accounts with GCE, you can provide a path to the JSON file and the project name in the parameters.
  195. .. code-block:: yaml
  196. google:
  197. driver: gce
  198. user_id: 234234-compute@developer.gserviceaccount.com
  199. key: /path/to/service_account_download.json
  200. auth_type: SA
  201. project: project-name