SPRINGONE2GX
WASHINGTON, DC
12 Factor (Cloud Native) Apps for
Spring Developers
By Cornelia Davis & Josh Kruck
@cdavisafc & @krujos
U n l e s s o t h e r w i s e i n d i c a t e d , t h e s e s l i d e s a r e © 2 0 1 3 - 2 0 1 5 P i v o t a l S o f t w a r e , I n c . a n d l i c e n s e d u n d e r a C r e a t i v e C o m m o n s A t t r i b u t i o n -N o n C o m m e r c i a l license: [Link]
Cloud Native Applications
“developed specifically for cloud platforms” what’s a cloud platform?
highly scalable seems right
mobile not always, but okay
agile definitely
run in containers implementation detail
microservices probably
designed for failure without question
Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a
C r e a t i v e C o m m o n s A t t r i b u t i o n -N o n C o m m e r c i a l license: [Link]
[Link]
Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a
C r e a t i v e C o m m o n s A t t r i b u t i o n -N o n C o m m e r c i a l license: [Link] 3
Factor 1 – Codebase
One codebase tracked in revision control, many deploys
1 Codebase = 1 App
Maybe?
Let’s look at some alternatives:
1 Codebase = * Apps
* Codebase = 1 App
Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a
C r e a t i v e C o m m o n s A t t r i b u t i o n -N o n C o m m e r c i a l license: [Link]
1 Codebase = * Apps
Or, 1 Codebase = what should be * Apps
HTML JavaScript MVC
Service Service
Data Access
Browser Monolithic Application Relational Database
… But is something we can migrate from
Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a
C r e a t i v e C o m m o n s A t t r i b u t i o n -N o n C o m m e r c i a l license: [Link]
Migrating the Monolith
Prerequisites
• Adequate test coverage
• Pipelines
• Sane build environment
Choose a business function
• Simple & bounded
• Value in extraction
Extract it
• New repo & implement service
• Use spring boot
Connect it
• Connect to existing app via proxy
[Link]
Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a
C r e a t i v e C o m m o n s A t t r i b u t i o n -N o n C o m m e r c i a l license: [Link]
* Codebase = 1 App?
For clarification: 1 Codebase = 1 Process
Let’s look at a scenario…
Suspend disbelief for a moment…
… but 2 codebases means (possibly) 2 teams
… and 2 teams necessitates APIs!!!!!
Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a
C r e a t i v e C o m m o n s A t t r i b u t i o n -N o n C o m m e r c i a l license: [Link]
Factor 1 – Codebase
One codebase tracked in revision control, many deploys
1 Codebase = 1 App
?
Probably
Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a
C r e a t i v e C o m m o n s A t t r i b u t i o n -N o n C o m m e r c i a l license: [Link]
Factor 15 – API First
How your microservices will communicate
Design
Develop
Version
Discover
[Link]
Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a
C r e a t i v e C o m m o n s A t t r i b u t i o n -N o n C o m m e r c i a l license: [Link]
Factor 2 – Dependencies
Explicitly Declare and Isolate dependencies
Goal: Developer to avoid dependency hell
+ Repeatable deployments
While I know this is a developer conference…
… your apps will
(hopefully)
be operated
(by you)
in production
Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a
C r e a t i v e C o m m o n s A t t r i b u t i o n -N o n C o m m e r c i a l license: [Link]
Let’s take a little detour for a
moment…
Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a
C r e a t i v e C o m m o n s A t t r i b u t i o n -N o n C o m m e r c i a l license: [Link]
Factor 5 – Design, Build, Release, Run
Strictly Separate Stages
Stage Who? What? Why not n & n+1?
Design Dev Spring/Spring Boot, Developer best understands the
Gradle, Maven dependencies
Build CI .war or .jar One build, many deploys
Anti – “it works on my machine”
Release Platform Droplet, Agile deployments,
Docker Image Upgrades, Rollbacks
Run Platform Container + Speed
process
Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a
C r e a t i v e C o m m o n s A t t r i b u t i o n -N o n C o m m e r c i a l license: [Link]
The Deployment Pipeline • “cf push”
• Runtime context (buildpack applied)
• Binds to prod services (DB,
(after commit)
messaging, etc.)
• Developer or QA crafted
• Periodic smoke tests
• CI executed
• Monitoring
• Runtime context (buildpack applied)
• Binds to test services (DB, messaging, etc.)
Commit Integration Deployable ? Deploy to CF ? Deploy to
Tests Tests Artifact Test Env Prod
• Developer crafted (before impl.) • “cf push”
• Stubs/mocks external services • Runtime context (buildpack applied)
• Developer executed (before commit) • Binds to test services (DB, messaging, etc.)
• CI executed (after commit) • Periodic smoke tests
Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a
C r e a t i v e C o m m o n s A t t r i b u t i o n -N o n C o m m e r c i a l license: [Link]
Now, coming back to
Dependencies…
Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a
C r e a t i v e C o m m o n s A t t r i b u t i o n -N o n C o m m e r c i a l license: [Link]
Remember, its about
Repeatable Deployments
so nothing about the runtime
environment should be assumed.
Explicitly declare dependencies!
Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a
C r e a t i v e C o m m o n s A t t r i b u t i o n -N o n C o m m e r c i a l license: [Link]
Is the runtime provided by the
Developer
or the
Platform?
Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a
C r e a t i v e C o m m o n s A t t r i b u t i o n -N o n C o m m e r c i a l license: [Link]
Let’s look at some
CODE
(finally)
Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a
C r e a t i v e C o m m o n s A t t r i b u t i o n -N o n C o m m e r c i a l license: [Link]
PCF – Each Layer Upgradable with Zero Downtime
• Developer/App team
provided
application layer • Blue/Green deployments
• Platform or Developer
Provided
Dev Supported runtime layer*
• CVEs addressed by Pivotal
(for Platform Provided)
PCF Supported • Blue/Green deployments
OS image
App container • Platform Provided
• CVEs addressed by Pivotal
• Canary-style, zero-
linux host & kernel downtime upgrades
* How much provided by dev and how much by platform?
Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a
C r e a t i v e C o m m o n s A t t r i b u t i o n -N o n C o m m e r c i a l license: [Link]
Spring Boot – embedded runtime
...
apply plugin: 'java’
apply plugin: 'spring-boot’
...
jar {
baseName = 'twelvefactor'
version = '0.1.0'
}
...
dependencies {
compile("[Link]:spring-boot-starter-web") {
exclude module: "spring-boot-starter-tomcat"
}
compile("[Link]:spring-boot-starter-jetty")
...
Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a
C r e a t i v e C o m m o n s A t t r i b u t i o n -N o n C o m m e r c i a l license: [Link]
Spring Boot – external runtime
...
//apply plugin: 'spring-boot’ ß Omit as it brings embedded Tomcat
apply plugin: 'war'
apply plugin: '[Link]-management'
war {
baseName = 'twelvefactor'
version = '0.1.0'
}
...
dependencies {
compile("[Link]:spring-boot-starter-web")
providedRuntime("[Link]:spring-boot-starter-tomcat")
...
Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a
C r e a t i v e C o m m o n s A t t r i b u t i o n -N o n C o m m e r c i a l license: [Link]
Factor 3 – Config
Store the Config in the Environment
What is Configuration?
Resource handles to databases and other backing services
Credentials to external sources (e.g. S3, Twitter, ...)
Per-deploy values (e.g. canonical hostname for deploy)
ANYTHING that’s likely to vary between deploys (dev, test,
stage, prod)
Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a
C r e a t i v e C o m m o n s A t t r i b u t i o n -N o n C o m m e r c i a l license: [Link]
Factor 3 – Config
Store the Config in the Environment
Where NOT to store it:
In the CODE (Captain Obvious)
In PROPERTIES FILES (That’s code...)
In the BUILD (ONE build, MANY deploys)
In the APP SERVER (e.g. JNDI datasources)
Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a
C r e a t i v e C o m m o n s A t t r i b u t i o n -N o n C o m m e r c i a l license: [Link]
Store it in the
Environment
Let’s have a look…
Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a
C r e a t i v e C o m m o n s A t t r i b u t i o n -N o n C o m m e r c i a l license: [Link]
Spring – Picking up Env Vars
...
import [Link];
import [Link];
@RestController
public class HelloController implements EnvironmentAware {
private String name;
@Override
public void setEnvironment(Environment environment) {
[Link] = [Link]("who");
}
...
Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a
C r e a t i v e C o m m o n s A t t r i b u t i o n -N o n C o m m e r c i a l license: [Link]
Factor 11 – Logs
Treat Logs as Event Streams
Log to stdout and stderr!
the standard implemented by the platform
Let’s have a look…
Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a
C r e a t i v e C o m m o n s A t t r i b u t i o n -N o n C o m m e r c i a l license: [Link]
Spring – Using ENV to config logging
In [Link]
logging:
level:
[Link]: ${SPRING_LOG_LEVEL:INFO}
hello: ${LOG_LEVEL:INFO}
BUT
Use this property file ONLY as an (hierarchical) abstraction!
Store config in the environment!!
Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a
C r e a t i v e C o m m o n s A t t r i b u t i o n -N o n C o m m e r c i a l license: [Link]
Factor 9 – Disposability:
Maximize robustness with fast startup and graceful shutdown
You cannot…
scale
deploy
release
recover
… fast if you cannot start fast!
Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a
C r e a t i v e C o m m o n s A t t r i b u t i o n -N o n C o m m e r c i a l license: [Link]
Factor 9 – Disposability:
Maximize robustness with fast startup and graceful shutdown
You cannot start if you did not shutdown
gracefully!
Where did all my db connections go?
Why are all my jobs locked?
Uhh, that job was in progress?
Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a
C r e a t i v e C o m m o n s A t t r i b u t i o n -N o n C o m m e r c i a l license: [Link]
Factor 4 – Backing services
Treat Backing Services as Attached Resources
Access services through a URL, never
locally!
Store the Locator in the config (see F3)
Resource locations can be changed
according to the fancy of the operator
Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a
C r e a t i v e C o m m o n s A t t r i b u t i o n -N o n C o m m e r c i a l license: [Link]
Create an instance of a resource
service plan service name
cf create-service cleardb spark hellodb
Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a
C r e a t i v e C o m m o n s A t t r i b u t i o n -N o n C o m m e r c i a l license: [Link]
Attach the resource to our app
app service name
cf bind-service hello-app hellodb
Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a
C r e a t i v e C o m m o n s A t t r i b u t i o n -N o n C o m m e r c i a l license: [Link]
Attach the resource to our app
(declaratively)
---
applications:
- name: hello-spring-one
memory: 1G
path: build/libs/[Link]
random-route: true
services:
- hellodb <- The name of the service we depend on!
Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a
C r e a t i v e C o m m o n s A t t r i b u t i o n -N o n C o m m e r c i a l license: [Link]
How do I consume that?
(demo)
Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a
C r e a t i v e C o m m o n s A t t r i b u t i o n -N o n C o m m e r c i a l license: [Link]
Our code builds with knowledge of a config
entry named hellodb
Every release has a config entry named
hellodb
hellodb is fetched at runtime and provides
URL & credentials for our resource
Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a
C r e a t i v e C o m m o n s A t t r i b u t i o n -N o n C o m m e r c i a l license: [Link]
Factor 10 – Dev/prod parity
Keep development, staging
And production as similar
as possible
Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a
C r e a t i v e C o m m o n s A t t r i b u t i o n -N o n C o m m e r c i a l license: [Link]
WHY?
Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a
C r e a t i v e C o m m o n s A t t r i b u t i o n -N o n C o m m e r c i a l license: [Link]
Cloud Native & 12 Factor
apps are designed for
continuous deployment
Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a
C r e a t i v e C o m m o n s A t t r i b u t i o n -N o n C o m m e r c i a l license: [Link]
Environment Parity Enables Speed
… cmt cmt cmt v201
cmt cmt cmt v202 cmt …
CI
Acceptance
Prod
Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a
C r e a t i v e C o m m o n s A t t r i b u t i o n -N o n C o m m e r c i a l license: [Link]
Every Commit is a Candidate for Deployment
… cmt cmt cmt v201 cmt cmt v202 cmt …
CI
Acceptance
Prod
Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a
C r e a t i v e C o m m o n s A t t r i b u t i o n -N o n C o m m e r c i a l license: [Link]
Every Commit is a Candidate for Deployment
App
… cmt cmt cmt v201 cmt cmt v202 cmt …
Platform
CVE
Acceptance
Prod
Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a
C r e a t i v e C o m m o n s A t t r i b u t i o n -N o n C o m m e r c i a l license: [Link]
Lean on a platform.
It’s the same
because it’s the
same.
Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a
C r e a t i v e C o m m o n s A t t r i b u t i o n -N o n C o m m e r c i a l license: [Link]
demo
Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a
C r e a t i v e C o m m o n s A t t r i b u t i o n -N o n C o m m e r c i a l license: [Link]
Factor 12 – Admin Processes
Run admin/management processes as one-off processes
Admin / Management processes run against a release
The “should” run in an identical environment as the release.
They use the same codebase and config
They ship live with the code to avoid synch issues.
This one has a lot of rules, be pragmatic.
Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a
C r e a t i v e C o m m o n s A t t r i b u t i o n -N o n C o m m e r c i a l license: [Link]
Factor 7 – Port Binding
Export Services Via Port Bindings
Apps are deployed into containers
Multiple containers per host
Platform to handle port
assignments and mappings
demo
Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a
C r e a t i v e C o m m o n s A t t r i b u t i o n -N o n C o m m e r c i a l license: [Link]
[Link]
Router
[Link]:61021 [Link]:61021
NAT NAT
[Link]:61021 [Link]:61001 [Link]:61021 [Link]:61018
… …
Runner Runner
Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a
C r e a t i v e C o m m o n s A t t r i b u t i o n -N o n C o m m e r c i a l license: [Link]
Factor 6 – Processes
Execute the app as one or more stateless processes
Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a
C r e a t i v e C o m m o n s A t t r i b u t i o n -N o n C o m m e r c i a l license: [Link]
Stateless apps allows the
platform
to do all sorts of things for you
demo
Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a
C r e a t i v e C o m m o n s A t t r i b u t i o n -N o n C o m m e r c i a l license: [Link]
Factor 8 – Concurrency
Scale out via the process model
file xfer file xfer file xfer
demo
Workload diversity
enqueue
trader trader trader trader trader
hello hello
Scale (running processes)
Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a
C r e a t i v e C o m m o n s A t t r i b u t i o n -N o n C o m m e r c i a l license: [Link]
But that’s not all…
• Factor 13 – Audit
• Every app should be designed with audit in mind
o What versions running at what ports
o Event stream (start, stop, crash…)
• Factor 14 – AuthN/AuthZ
• Every app should have RBAC applied
@jmckenty
Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a
C r e a t i v e C o m m o n s A t t r i b u t i o n -N o n C o m m e r c i a l license: [Link]
Thank you!
Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a
C r e a t i v e C o m m o n s A t t r i b u t i o n -N o n C o m m e r c i a l license: [Link] 50
@cdavisafc
@krujos
Learn More. Stay Connected.
@springcentral [Link]/video
You can check all of this out: [Link]
Other sessions:
• (10:30 W) Spring Boot for Devops ([Link]
• (2:30 W) Spring Cloud Services ([Link]
• Migrating the Monolith ([Link]
Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a
C r e a t i v e C o m m o n s A t t r i b u t i o n -N o n C o m m e r c i a l license: [Link] 51