blob: a70a9c7e4046ded4d62b99bc4936411494d5efe7 [file] [log] [blame]
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="author" content="The Apache Software Foundation">
<meta name="keywords" content="python,libcloud,cloud,cloud computing,rackspace,amazon ec2,cloudfiles,openstack,cloudstack" />
<title>Apache Libcloud | Apache Libcloud</title>
<!-- fav icons -->
<link rel="shortcut icon" href="/images/favicon.png" />
<link rel="apple-touch-icon" href="/images/apple-touch-icon.png" />
<link rel="apple-touch-icon-precomposed" href="/images/apple-touch-icon.png" />
<link href="/blog/atom.xml" type="application/atom+xml" rel="alternate" title="Apache Libcloud Blog Feed" />
<!-- Facebook OpenGraph tags -->
<meta content="Apache Libcloud" property="og:site_name">
<meta content="Apache Libcloud" property="og:title">
<meta content="website" property="og:type">
<meta content="https://libcloud.apache.org/blog/tags/gce" property="og:url">
<link href='/assets/global-1768bfa479597eed443be67c5aec2edc.css' rel='stylesheet' type='text/css' />
</head>
<body data-spy="scroll" data-target=".sidebar-nav" data-offset="80">
<nav class="navbar navbar-fixed-top navbar-inverse" role="navigation">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-ex1-collapse">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="/"><img src="/images/libcloud_logo.png" class="navbar-logo" /> Apache Libcloud</a>
</div>
<div class="collapse navbar-collapse navbar-ex1-collapse">
<ul class="nav navbar-nav">
<li ><a href="/" >Home</a></li>
<li ><a href="/about.html" >About</a></li>
<li ><a href="/getting-started.html" >Quick Start</a></li>
<li ><a href="https://libcloud.readthedocs.org/en/stable/" target="_blank">Documentation</a></li>
<li ><a href="/downloads.html" >Downloads</a></li>
<li ><a href="/community.html" >Community</a></li>
<li ><a href="/blog/" >Blog</a></li>
</ul>
<div class="material-switch pull-right">
<input id="theme-switch" name="theme-switch" type="checkbox" onclick="modeSwitcher()"/>
<label for="theme-switch" class="label-default"></label>
<span id="theme-toggle" class="theme-switch">Dark mode</span>
</div>
</div><!-- /.navbar-collapse -->
</div><!-- /.container -->
</nav>
<div class="container main-container">
<div class="row section page-content">
<div class="col-lg-8 col-lg-offset-2">
<h1>Tag: gce</h1>
<div class="post">
<h2 class="post-title"><a href="/blog/2014/02/18/libcloud-0-14-and-google-cloud-platform.html">Libcloud 0.14 and Google Cloud Platform</a></h2>
<span class="post-date-author">By Rick Wright on Feb 18, 2014</span>
<div class="post-content">
<p><em>This is a guest post by <a href="https://github.com/wrigri">Rick Wright</a>. Rick is an engineer at Google and
the main contributor to the Libcloud Google Compute Engine driver.</em></p>
<p>As anyone following this blog knows, Libcloud 0.14 was <a href="/blog/2014/01/22/libcloud-0-14-0-released.html">recently released</a>.
Included in this release is new support for the Google Compute Engine service
that was recently made generally available.</p>
<p>The first Libcloud driver for Google Compute Engine was initially released last
summer, and has been continually updated to support new Compute Engine
features. Support for the Compute Engine Load Balancer was also recently added.</p>
<p>Libcloud had previously added support for Google Cloud Storage through the S3
compatibility API.</p>
<p>In this post, we’ll show you how to get up and running with Google Compute
Engine with Libcloud, show you how to create some instances (nodes) and point
you to additional resources. We also show you how you can start up 20
instances in under a minute.</p>
<div class="imginline">
<p><a href="https://cloud.google.com/" target="_blank">
<img src="/images/posts/gce/image03.png" class="img-responsive inline" />
</a></p>
</div>
<h2 id="getting-started">Getting Started</h2>
<p>If you haven’t yet signed up for Google Compute Engine, more information can
be found on the <a href="https://cloud.google.com/">Google Cloud Platform</a> site. We hope that you will look at
the Google Cloud Platform and consider Compute Engine for workloads of any
size.</p>
<p>If you are familiar with Libcloud, using the driver for Compute Engine will
also be familiar to you as nearly all of the standard Libcloud API methods for
Compute and Load Balancing are supported by the Compute Engine drivers.</p>
<h2 id="setting-up-authentication">Setting up Authentication</h2>
<p>Google Compute Engine uses OAuth2 for authentication and Libcloud supports two
different authentication methods for Compute Engine: Service Accounts and
Installed Applications. More information can be found about these options in
the <a href="https://libcloud.readthedocs.org/en/latest/compute/drivers/gce.html">driver documentation</a>, but here is a simple walk-through for getting
your Service Account credentials (note that the Service Account authentication
requires the PyCrypto library):</p>
<p>From the Console (<a href="https://cloud.google.com/console">https://cloud.google.com/console</a>), select your project. When
your project is open, select “APIs &amp; auth” and then “Credentials” as shown
below:</p>
<div class="imginline">
<p><img src="/images/posts/gce/image01.png" class="img-responsive inline" /></p>
</div>
<p>To create a new Service Account ID, click on “Create New Client ID”</p>
<div class="imginline">
<p><img src="/images/posts/gce/image04.png" class="img-responsive inline" /></p>
</div>
<p>Select “Service account” and click “Create Client ID”</p>
<div class="imginline">
<p><img src="/images/posts/gce/image00.png" class="img-responsive inline" /></p>
</div>
<p>This will create and download a PCKS-12 private key to your local machine. You
will need to convert this to a .pem format key. (See additional instructions
in the <a href="https://libcloud.readthedocs.org/en/latest/compute/drivers/gce.html">driver documentation</a>).</p>
<div class="imginline">
<p><img src="/images/posts/gce/image02.png" class="img-responsive inline" /></p>
</div>
<p>To connect to Compute Engine from Libclould, you will need to provide both the
“Email address” for the service account and the path to the key file.</p>
<h2 id="creating-an-instance">Creating an Instance</h2>
<p>The Compute Engine driver supports the normal Libcloud ways of doing things.
For example, to create the driver you would do something like this:</p>
<pre>
from libcloud.compute.types import Provider
from libcloud.compute.providers import get_driver
Driver = get_driver(Provider.GCE)
gce = Driver('your_service_account_email', 'path_to_pem_file',
datacenter='us-central1-a',
project='your_project_id')
</pre>
<p>Then, you may want to get a list of machine sizes or images to use for your
node creation:</p>
<pre>
sizes = gce.list_sizes()
images = gce.list_images()
</pre>
<p>and then filter them to find the correct size/image object to create your
node. For example:</p>
<pre>
size_obj = [s for s in sizes if s.id == 'n1-standard-1'][0]
image_obj = [i for i in images if i.name == 'debian-7'][0]
</pre>
<p>Finally, you would create the node using the create_node() method:</p>
<pre>
new_node = gce.create_node(name='my_node', size=size_obj,
image=img_obj)
</pre>
<p>The GCE driver also supports the ability to pass in size/image by name instead
of by object, so you could create your new node like this, without looking up
the sizes/images:</p>
<pre>
new_node = gce.create_node(name='my_node', size='n1-standard-1',
image='debian-7')
</pre>
<p>In this case a new node will be created using the “n1-standard-1” machine
type and will use the latest “debian-7” image that is available.</p>
<h2 id="creating-multiple-instances">Creating Multiple Instances</h2>
<p>We have added an extra method to the Compute Engine driver to help in the
creation of multiple instances/nodes in parallel. This is the
“ex_create_multiple_nodes()” method, and it can be used like this:</p>
<pre>
base_name = 'multiple-nodes'
number = 20
start_time = time.time()
multi_nodes = gce.ex_create_multiple_nodes(
base_name, 'n1-standard-1', 'debian-7', number)
end_time = time.time()
print('Created the following %s nodes in %.2f seconds' % (
number, end_time-start_time))
for node in multi_nodes:
print(' %s' % node.name)
</pre>
<p>Running this gives the following output (timing will likely vary somewhat):</p>
<pre>
Created the following 20 nodes in 35.77 seconds
multiple-nodes-000
multiple-nodes-001
multiple-nodes-002
multiple-nodes-003
multiple-nodes-004
...
</pre>
<h2 id="onward">Onward</h2>
<p>This post showed you how to get started, and additional example code can be
found in the <a href="https://github.com/apache/libcloud/blob/trunk/demos/gce_demo.py">Libcloud Compute Engine demo code</a>. The demo code can show you
how to use most of the features of the driver, including: Creating disks,
firewalls and addresses; setting tags; attaching and detaching disks and
deleting resources. There is <a href="https://github.com/apache/libcloud/blob/trunk/demos/gce_lb_demo.py">separate demo code</a> for the Load Balancer
functions which shows how to quickly set up 3 web servers with a Load Balancer
controlling the traffic to them. In addition, the driver documentation
contains the documentation for the extra methods that cover nearly all of the
features of the Compute Engine API.</p>
<p>We are excited to be able to contribute to the Libcloud project and hope that
you will find it useful. We like working with Libcloud and have used it as the
basis for contributing code to other open source projects such as <a href="http://ansible.com/">Ansible</a>
and <a href="http://www.saltstack.com/">SaltStack</a>. If you run into problems or find bugs, please
<a href="https://issues.apache.org/jira/browse/LIBCLOUD">report them</a> (or send a <a href="https://github.com/apache/libcloud">pull request</a>) and we will be happy to continue
to fix and enhance the Compute Engine driver in Libcloud.</p>
</div>
<div class="row section post-meta">
<div class="col-md-12 post-tags">
<p>Tags: <a href="/blog/tags/articles.html" rel="tag">articles</a>, <a href="/blog/tags/providers.html" rel="tag">providers</a>, <a href="/blog/tags/gce.html" rel="tag">gce</a></p>
</div>
</div>
</div>
</div>
</div>
<hr />
<footer>
<div class="row">
<div class="col-lg-12 text-center">
<div class="footer-links">
<p><a href="http://www.apache.org/licenses/">License</a> | <a
href="/security.html">Security</a> | <a
href="http://www.apache.org/foundation/sponsorship.html">Sponsorship</a> |
<a href="http://www.apache.org/foundation/thanks.html">Thanks</a> |
<a href="https://www.apache.org/events/">Events</a> |
<a href="/credits.html">Credits</a> | <a href="/media.html">Media</a>
</div>
<div class="footer-text">
<p><a class="acevent" data-format="wide"></a></p>
<p class="">Copyright &copy; 2009-2023 <a href="https://www.apache.org/" target="_blank">The Apache Software Foundation</a></p>
<p class="">Apache Libcloud, Libcloud, Apache, the Apache feather, and the Apache Libcloud project logo are trademarks of the Apache Software Foundation. All other marks mentioned may be trademarks or registered trademarks of their respective owners.</p>
<p class="">Site last updated on 2023-09-09 21:33:21 +0000</p>
</div>
</div>
</div>
</footer>
</div><!-- /.container -->
<!-- JavaScript -->
<script src='/assets/global-20157a00c0e17a775f45ed99ccdf79d7.js' type='text/javascript'></script>
<script type="text/javascript">
var _paq = window._paq = window._paq || [];
/* tracker methods like "setCustomDimension" should be called before
"trackPageView" */
/* We explicitly disable cookie tracking to avoid privacy issues */
_paq.push(['disableCookies']);
_paq.push(['trackPageView']);
_paq.push(['enableLinkTracking']);
(function() {
var u="https://analytics.apache.org/";
_paq.push(['setTrackerUrl', u+'matomo.php']);
_paq.push(['setSiteId', '7']);
var d=document, g=d.createElement('script'),
s=d.getElementsByTagName('script')[0];
g.async=true; g.src=u+'matomo.js'; s.parentNode.insertBefore(g,s);
})();
</script>
<script src="https://www.apachecon.com/event-images/snippet.js"></script>
</body>
</html>