2. Before we begin: Setup
Taming the Cloud Database with Apache jclouds http://rack.
to/ace14db
2
Virtual Machine pre-setup provided
http://rack.to/ace14vm
Avoid downloading during this presentation
3. Before we begin: Setup
Taming the Cloud Database with Apache jclouds http://rack.
to/ace14db
3
Alternatively you need
Oracle Java 7
http://www.webupd8.org/2012/01/install-oracle-java-jdk-7-in-
ubuntu-via.html
maven
http://maven.apache.org/download.cgi
git config --global core.autocrlf false
4. Before we begin: Setup
Taming the Cloud Database with Apache jclouds http://rack.
to/ace14db
4
Testing on your machine (no subscriptions)
Devstack
http://devstack.org/
http://blog.phymata.com/2014/04/18/devstack-icehouse-on-the-
rackspace-cloud/
Providers: Rackspace, HP, others
http://www.openstack.org/marketplace/public-clouds/
http://rack.to/ace14
5. Before we begin: Setup
Taming the Cloud Database with Apache jclouds http://rack.
to/ace14db
5
git clone https://github.com/zack-shoylev/jclouds-
developer-examples.git
Multiple branches
git clone https://github.com/rackerlabs/jclouds-
examples.git
Branch trove-example-only
6. Introductions
Taming the Cloud Database with Apache jclouds http://rack.
to/ace14db
6
➢ Zack Shoylev
➢ irc: zacksh twitter: @zackshoylev
freenode #jclouds
Also email
@rackspace.com
7. Introductions
Taming the Cloud Database with Apache jclouds http://rack.
to/ace14db
7
Developer Experience
https://developer.rackspace.com/support/
8. Goals
Taming the Cloud Database with Apache jclouds http://rack.
to/ace14db
8
➢ jclouds
➢ Create a database in the cloud
➢ Abstractions and how to contribute
➢ How to add support for an API
9. The Cloud
Taming the Cloud Database with Apache jclouds http://rack.
to/ace14db
9
➢ Networked and distributed computing
➢ Variety of services
➢ Compute (servers)
➢ Storage (files)
➢ Databases
➢ Email
➢ …
10. The Cloud
Taming the Cloud Database with Apache jclouds http://rack.
to/ace14db
10
➢ Can’t someone else do it?
11. The Cloud
Taming the Cloud Database with Apache jclouds http://rack.
to/ace14db
11
12. The Cloud
Taming the Cloud Database with Apache jclouds http://rack.
to/ace14db
12
➢ Public cloud (external provider)
➢ Private cloud (internal deployment)
➢ Hybrid cloud
➢ Public + Private
Rackspace
example
13. The Cloud
Taming the Cloud Database with Apache jclouds http://rack.
to/ace14db
13
Your
System
S
D
K
Cloud
AP
I
VM
VM
VM
➢ This can be an application server
➢ Or your home machine
➢ Or belong to one of your end users
➢ Or a cloud VM
➢ Or a smartphone
jclouds
14. The Cloud
Taming the Cloud Database with Apache jclouds http://rack.
to/ace14db
14
➢ Advantages
➢ Metered pricing (pay as you go)
➢ Project scalability (unlimited)
➢ Safer (offsite/redundant)
➢ Economies of scale
➢ Expertise
➢ Support
15. The Cloud
Taming the Cloud Database with Apache jclouds http://rack.
to/ace14db
15
➢ Disadvantages
➢ Less hardware control
➢ Provider-controlled downtime
➢ Virtualization (efficiency)
➢ Provider lock-in
➢ Provider-specific apis/sdks/features
➢ Expensive to switch clouds or deploy locally
➢ jclouds minimizes this disadvantage
16. The Cloud
Taming the Cloud Database with Apache jclouds http://rack.
to/ace14db
16
➢ Database specific advantages
➢ Optimized by provider
➢ Settings
➢ Container virtualization
➢ Automated software updates
➢ Redundant data storage
➢ Backup
➢ Migration
➢ Choice
➢ Clustering (upcoming)
➢ Support
17. OpenStack
Taming the Cloud Database with Apache jclouds http://rack.
to/ace14db
17
➢ Cloud OS
➢ Open-sourced by Rackspace and NASA
(2010)
➢ Free
➢ Supported
➢ AT&T, HP, IBM, Red Hat, Rackspace, Dell, Cisco,
Intel, VMware, …
18. OpenStack
Taming the Cloud Database with Apache jclouds http://rack.
to/ace14db
18
➢ http://stackalytics.com/
19. jclouds
Taming the Cloud Database with Apache jclouds http://rack.
to/ace14db
19
➢ Cloud SDK
➢ Apache project
➢ Java (and Clojure)
➢ Easy
➢ Portable
➢ Cloud-agnostic
➢ Community
➢ Open source
20. jclouds
Taming the Cloud Database with Apache jclouds http://rack.
to/ace14db
20
➢ https://jclouds.apache.org/
21. jclouds
Taming the Cloud Database with Apache jclouds http://rack.
to/ace14db
21
➢ HTTP requests, responses, retries
➢ Authentication and re-authentication
➢ Pagination
➢ Polling
➢ Rate limits
➢ Retries
➢ Abstractions
➢ Logging
➢ Less code
30. POM
Taming the Cloud Database with Apache jclouds http://rack.
to/ace14db
30
mvn dependency:copy-dependencies "-DoutputDirectory=./lib"
31. Logging
Taming the Cloud Database with Apache jclouds http://rack.
to/ace14db
31
// This module is responsible for enabling logging
Iterable<Module> modules = ImmutableSet.<Module> of(new SLF4JLoggingModule());
ComputeServiceContext context = ContextBuilder.newBuilder(provider)
.credentials(username, apiKey)
.modules(modules) // don't forget to add the modules to your context!
.buildView(ComputeServiceContext.class);
logback.xml
<configuration scan="false">
…
<appender name="WIREFILE" class="ch.qos.logback.core.FileAppender">
<file>target/test-data/jclouds-wire.log</file>
<encoder>
<Pattern>%d %-5p [%c] [%thread] %m%n</Pattern>
</encoder>
</appender>
32. Java Project
Taming the Cloud Database with Apache jclouds http://rack.
to/ace14db
32
Skeleton
Project
Create User
Create Instance
Initialize API
Create Database
Test Connection
Cleanup
33. Initialize
Taming the Cloud Database with Apache jclouds http://rack.
to/ace14db
33
private final TroveApi troveApi;
troveApi = ContextBuilder
.newBuilder("rackspace-clouddatabases-us")
.credentials(username, apiKey)
.buildApi(TroveApi.class);
34. Architecture
Taming the Cloud Database with Apache jclouds http://rack.
to/ace14db
34
Cloud Databases
Trove
jclouds-database
Increasing level of
abstraction
Provider
settings:
Endpoint, etc.
Implementation
code
Abstraction
s
35. Architecture
Taming the Cloud Database with Apache jclouds http://rack.
to/ace14db
35
Cloud Databases
Trove
jclouds-database
Increasing level of
abstraction
Provider
settings:
Endpoint, etc.
Implementation
code
Abstraction
s
Not implemented
36. Initialize
Taming the Cloud Database with Apache jclouds http://rack.
to/ace14db
36
private final FlavorApi flavorApi;
flavorApi = troveApi.getFlavorApiForZone(ZONE);
38. Java Project
Taming the Cloud Database with Apache jclouds http://rack.
to/ace14db
38
Skeleton
Project
Create User
Create Instance
Initialize API
Create Database
Test Connection
Cleanup
39. Create Instance
Taming the Cloud Database with Apache jclouds http://rack.
to/ace14db
39
Flavor flavor = Iterables.getFirst(flavorApi.list(), null);
…
Instance instance = instanceApi.create(flavorId, size, name);
flavor.getId()
Volume size in
GB
40. Create Instance
Taming the Cloud Database with Apache jclouds http://rack.
to/ace14db
40
Instance updatedInstance = awaitAvailable(instance, instanceApi);
41. Create Instance
Taming the Cloud Database with Apache jclouds http://rack.
to/ace14db
41
Instance updatedInstance = awaitAvailable(instance, instanceApi);
Polls status, waits until ready. Will not retry by
itself!
43. Actual Code
Taming the Cloud Database with Apache jclouds http://rack.
to/ace14db
43
Utils.
getWorkingInstance(
String zone,
String name,
String flavorId,
int size)
44. Java Project
Taming the Cloud Database with Apache jclouds http://rack.
to/ace14db
44
Skeleton
Project
Create User
Create Instance
Initialize API
Create Database
Test Connection
Cleanup
45. Create Database
Taming the Cloud Database with Apache jclouds http://rack.
to/ace14db
45
troveApi = ContextBuilder.newBuilder(PROVIDER)
.credentials(username, apiKey)
.buildApi(TroveApi.class);
instanceApi = troveApi.getInstanceApiForZone(ZONE);
databaseApi = troveApi
.getDatabaseApiForZoneAndInstance(ZONE,getInstance().getId());
getSomeApiForXandYandZ – fairly common in jclouds
46. Create Database
Taming the Cloud Database with Apache jclouds http://rack.
to/ace14db
46
boolean result = databaseApi.create(NAME);
47. Java Project
Taming the Cloud Database with Apache jclouds http://rack.
to/ace14db
47
Skeleton
Project
Create User
Create Instance
Initialize API
Create Database
Test Connection
Cleanup
48. Create User
Taming the Cloud Database with Apache jclouds http://rack.
to/ace14db
48
userApi = troveApi
.getUserApiForZoneAndInstance(ZONE, instance.getId());
boolean result = userApi.create(UNAME, PASSWORD, DBNAME);
User name
User password
Database
name
49. Java Project
Taming the Cloud Database with Apache jclouds http://rack.
to/ace14db
49
Skeleton
Project
Create User
Create Instance
Initialize API
Create Database
Test Connection
Cleanup
50. Test Connection
Taming the Cloud Database with Apache jclouds http://rack.
to/ace14db
50
Load balancer needed!
CreateLoadBalancer createLB = CreateLoadBalancer.
builder()
.name(NAME)
.protocol("MYSQL")
.port(3306)
.algorithm(LoadBalancer.Algorithm.RANDOM)
.nodes(addNodes)
.virtualIPType(VirtualIP.Type.PUBLIC)
.build();
51. Test Connection
Taming the Cloud Database with Apache jclouds http://rack.
to/ace14db
51
loadBalancer = lbApi.create(createLB);
52. Test Connection
Taming the Cloud Database with Apache jclouds http://rack.
to/ace14db
52
StringBuilder connString = new StringBuilder();
connString.append("jdbc:mysql://");
connString
.append(getVirtualIPv4(getLb().getVirtualIPs()));
connString.append("/");
connString.append(DBNAME); connString.append("?
user=");
connString.append(UNAME);
connString.append("&password=");
connString.append(PASSWORD);
53. Test Connection
Taming the Cloud Database with Apache jclouds http://rack.
to/ace14db
53
Connection conn =
DriverManager.getConnection(connString.toString());
54. Test Connection
Taming the Cloud Database with Apache jclouds http://rack.
to/ace14db
54
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT 3+5");
rs.first();
System.out.format(" 3+5 is %s%n", rs.getInt(1));
59. Going forward
Taming the Cloud Database with Apache jclouds http://rack.
to/ace14db
59
➢ TroveApi
➢ Backup Extension
➢ Settings Extension
➢ Clustering
➢ Abstraction layer
Rackspac
e
Microsof
t
Amazo
n
CouchDB
Salesforc
e
60. Going forward
Taming the Cloud Database with Apache jclouds http://rack.
to/ace14db
60
➢ TroveApi
➢ Backup Extension
➢ Settings Extension
➢ Clustering
➢ Abstraction layer
Rackspac
e
Microsof
t
Amazo
n
CouchDB
Salesforc
e
61. Going forward
Taming the Cloud Database with Apache jclouds http://rack.
to/ace14db
61
Collaborate!
http://jclouds.apache.org/
http://jclouds.apache.org/community/
62. Implementing an API
Taming the Cloud Database with Apache jclouds http://rack.
to/ace14db
62
Fork <link to my fork>
Alternatively, fork jclouds-labs-openstack (for
openstack).
63. Implementing an API
Taming the Cloud Database with Apache jclouds http://rack.
to/ace14db
63
On to the IDE