Weitere ähnliche Inhalte Ähnlich wie 12 Factor, or Cloud Native Apps – What EXACTLY Does that Mean for Spring Developers? (20) Mehr von cornelia davis (18) Kürzlich hochgeladen (20) 12 Factor, or Cloud Native Apps – What EXACTLY Does that Mean for Spring Developers?1. SPRINGONE2GX
WASHINGTON, DC
Unless otherwise indicated, these slides are © 2013 -2015 Pivotal Software, Inc. and licensed under a Creative Commons Attributio n-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
12 Factor (Cloud Native) Apps for
Spring Developers
By Cornelia Davis & Josh Kruck
@cdavisafc & @krujos
2. Unless otherwise indicated, these slides are © 2013 -2015 Pivotal Software, Inc. and licensed under a
Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
Cloud Native Applications
“developed specifically for cloud platforms”
highly scalable
mobile
agile
run in containers
microservices
designed for failure
what’s a cloud platform?
seems right
not always, but okay
definitely
implementation detail
probably
without question
3. Unless otherwise indicated, these slides are © 2013 -2015 Pivotal Software, Inc. and licensed under a
Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 3
http://12factor.net/
4. Unless otherwise indicated, these slides are © 2013 -2015 Pivotal Software, Inc. and licensed under a
Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
One codebase tracked in revision control, many deploys
Factor 1 – Codebase
1 Codebase = 1 App
Maybe?
Let’s look at some alternatives:
1 Codebase = * Apps
* Codebase = 1 App
5. Unless otherwise indicated, these slides are © 2013 -2015 Pivotal Software, Inc. and licensed under a
Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
Or, 1 Codebase = what should be * Apps
1 Codebase = * Apps
Relational Database
Data Access
Service
HTML JavaScript MVC
Service
Monolithic ApplicationBrowser
… But is something we can migrate from
6. Unless otherwise indicated, these slides are © 2013 -2015 Pivotal Software, Inc. and licensed under a
Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
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
http://blog.pivotal.io/pivotal-cloud-foundry/case-studies/case-study-refactoring-a-monolith-into-a-cloud-native-app-part-1
7. Unless otherwise indicated, these slides are © 2013 -2015 Pivotal Software, Inc. and licensed under a
Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
For clarification: 1 Codebase = 1 Process
* Codebase = 1 App?
Let’s look at a scenario…
Suspend disbelief for a moment…
… but 2 codebases means (possibly) 2 teams
… and 2 teams necessitates APIs!!!!!
8. Unless otherwise indicated, these slides are © 2013 -2015 Pivotal Software, Inc. and licensed under a
Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
One codebase tracked in revision control, many deploys
Factor 1 – Codebase
1 Codebase = 1 App
?
Probably
9. Unless otherwise indicated, these slides are © 2013 -2015 Pivotal Software, Inc. and licensed under a
Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
How your microservices will communicate
Factor 15 – API First
Design
Develop
Version
Discover
http://www.api-first.com/
10. Unless otherwise indicated, these slides are © 2013 -2015 Pivotal Software, Inc. and licensed under a
Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
Explicitly Declare and Isolate dependencies
Factor 2 – Dependencies
Goal: Developer to avoid dependency hell
While I know this is a developer conference…
… your apps will
(hopefully)
be operated
(by you)
in production
+ Repeatable deployments
11. Unless otherwise indicated, these slides are © 2013 -2015 Pivotal Software, Inc. and licensed under a
Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
Let’s take a little detour for a
moment…
12. Unless otherwise indicated, these slides are © 2013 -2015 Pivotal Software, Inc. and licensed under a
Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
Strictly Separate Stages
Factor 5 – Design, Build, Release, Run
Stage Who? What? Why not n & n+1?
Design Dev Spring/Spring Boot,
Gradle, Maven
Developer best understands the
dependencies
Build CI .war or .jar One build, many deploys
Anti – “it works on my machine”
Release Platform Droplet,
Docker Image
Agile deployments,
Upgrades, Rollbacks
Run Platform Container +
process
Speed
13. Unless otherwise indicated, these slides are © 2013 -2015 Pivotal Software, Inc. and licensed under a
Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
The Deployment Pipeline
Commit
Tests
Integration
Tests
Deployable
Artifact
Deploy to CF
Test Env
Deploy to
Prod
• Developer crafted (before impl.)
• Stubs/mocks external services
• Developer executed (before commit)
• CI executed (after commit)
(after commit)
• Developer or QA crafted
• CI executed
• Runtime context (buildpack applied)
• Binds to test services (DB, messaging, etc.)
• “cf push”
• Runtime context (buildpack applied)
• Binds to test services (DB, messaging, etc.)
• Periodic smoke tests
• “cf push”
• Runtime context (buildpack applied)
• Binds to prod services (DB,
messaging, etc.)
• Periodic smoke tests
• Monitoring
? ?
14. Unless otherwise indicated, these slides are © 2013 -2015 Pivotal Software, Inc. and licensed under a
Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
Now, coming back to
Dependencies…
15. Unless otherwise indicated, these slides are © 2013 -2015 Pivotal Software, Inc. and licensed under a
Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
Remember, its about
Repeatable Deployments
so nothing about the runtime
environment should be assumed.
Explicitly declare dependencies!
16. Unless otherwise indicated, these slides are © 2013 -2015 Pivotal Software, Inc. and licensed under a
Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
Is the runtime provided by the
Developer
or the
Platform?
17. Unless otherwise indicated, these slides are © 2013 -2015 Pivotal Software, Inc. and licensed under a
Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
Let’s look at some
CODE
(finally)
18. Unless otherwise indicated, these slides are © 2013 -2015 Pivotal Software, Inc. and licensed under a
Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
PCF – Each Layer Upgradable with Zero Downtime
* How much provided by dev and how much by platform?
runtime layer*
OS image
application layer
linux host & kernel
App container
PCF Supported
Dev Supported
• Platform Provided
• CVEs addressed by Pivotal
• Canary-style, zero-
downtime upgrades
• Platform or Developer
Provided
• CVEs addressed by Pivotal
(for Platform Provided)
• Blue/Green deployments
• Developer/App team
provided
• Blue/Green deployments
19. Unless otherwise indicated, these slides are © 2013 -2015 Pivotal Software, Inc. and licensed under a
Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
Spring Boot – embedded runtime
...
apply plugin: 'java’
apply plugin: 'spring-boot’
...
jar {
baseName = 'twelvefactor'
version = '0.1.0'
}
...
dependencies {
compile("org.springframework.boot:spring-boot-starter-web") {
exclude module: "spring-boot-starter-tomcat"
}
compile("org.springframework.boot:spring-boot-starter-jetty")
...
20. Unless otherwise indicated, these slides are © 2013 -2015 Pivotal Software, Inc. and licensed under a
Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
Spring Boot – external runtime
...
//apply plugin: 'spring-boot’
apply plugin: 'war'
apply plugin: 'io.spring.dependency-management'
war {
baseName = 'twelvefactor'
version = '0.1.0'
}
...
dependencies {
compile("org.springframework.boot:spring-boot-starter-web")
providedRuntime("org.springframework.boot:spring-boot-starter-tomcat")
...
Omit as it brings embedded Tomcat
21. Unless otherwise indicated, these slides are © 2013 -2015 Pivotal Software, Inc. and licensed under a
Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
Store the Config in the Environment
Factor 3 – Config
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)
22. Unless otherwise indicated, these slides are © 2013 -2015 Pivotal Software, Inc. and licensed under a
Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
Store the Config in the Environment
Factor 3 – Config
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)
23. Unless otherwise indicated, these slides are © 2013 -2015 Pivotal Software, Inc. and licensed under a
Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
Store it in the
Environment
Let’s have a look…
24. Unless otherwise indicated, these slides are © 2013 -2015 Pivotal Software, Inc. and licensed under a
Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
...
import org.springframework.context.EnvironmentAware;
import org.springframework.core.env.Environment;
@RestController
public class HelloController implements EnvironmentAware {
private String name;
@Override
public void setEnvironment(Environment environment) {
this.name = environment.getProperty("who");
}
...
Spring – Picking up Env Vars
25. Unless otherwise indicated, these slides are © 2013 -2015 Pivotal Software, Inc. and licensed under a
Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
Treat Logs as Event Streams
Factor 11 – Logs
Log to stdout and stderr!
the standard implemented by the platform
Let’s have a look…
26. Unless otherwise indicated, these slides are © 2013 -2015 Pivotal Software, Inc. and licensed under a
Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
logging:
level:
org.springframework: ${SPRING_LOG_LEVEL:INFO}
hello: ${LOG_LEVEL:INFO}
In application.yml
Spring – Using ENV to config logging
BUT
Use this property file ONLY as an (hierarchical) abstraction!
Store config in the environment!!
27. Unless otherwise indicated, these slides are © 2013 -2015 Pivotal Software, Inc. and licensed under a
Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
Maximize robustness with fast startup and graceful shutdown
Factor 9 – Disposability:
You cannot…
scale
deploy
release
recover
… fast if you cannot start fast!
28. Unless otherwise indicated, these slides are © 2013 -2015 Pivotal Software, Inc. and licensed under a
Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
Maximize robustness with fast startup and graceful shutdown
Factor 9 – Disposability:
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?
29. Unless otherwise indicated, these slides are © 2013 -2015 Pivotal Software, Inc. and licensed under a
Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
Treat Backing Services as Attached Resources
Factor 4 – Backing services
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
30. Unless otherwise indicated, these slides are © 2013 -2015 Pivotal Software, Inc. and licensed under a
Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
Create an instance of a resource
cf create-service cleardb spark hellodb
service plan service name
31. Unless otherwise indicated, these slides are © 2013 -2015 Pivotal Software, Inc. and licensed under a
Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
Attach the resource to our app
cf bind-service hello-app hellodb
app service name
32. Unless otherwise indicated, these slides are © 2013 -2015 Pivotal Software, Inc. and licensed under a
Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
(declaratively)
Attach the resource to our app
---
applications:
- name: hello-spring-one
memory: 1G
path: build/libs/twelvefactor-0.1.0.jar
random-route: true
services:
- hellodb <- The name of the service we depend on!
33. Unless otherwise indicated, these slides are © 2013 -2015 Pivotal Software, Inc. and licensed under a
Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
How do I consume that?
(demo)
34. Unless otherwise indicated, these slides are © 2013 -2015 Pivotal Software, Inc. and licensed under a
Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
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
35. Unless otherwise indicated, these slides are © 2013 -2015 Pivotal Software, Inc. and licensed under a
Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
Factor 10 – Dev/prod parity
Keep development, staging
And production as similar
as possible
36. Unless otherwise indicated, these slides are © 2013 -2015 Pivotal Software, Inc. and licensed under a
Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
WHY?
37. Unless otherwise indicated, these slides are © 2013 -2015 Pivotal Software, Inc. and licensed under a
Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
Cloud Native & 12 Factor
apps are designed for
continuous deployment
38. Unless otherwise indicated, these slides are © 2013 -2015 Pivotal Software, Inc. and licensed under a
Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
Environment Parity Enables Speed
cmtcmt
v20
1
cmt cmt cmt cmt
v20
2
cmt
… …
Prod
Acceptance
CI
39. Unless otherwise indicated, these slides are © 2013 -2015 Pivotal Software, Inc. and licensed under a
Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
Every Commit is a Candidate for Deployment
cmt
v20
1
cmt cmt cmt cmt
v20
2
cmt
… …
Prod
Acceptance
CI
40. Unless otherwise indicated, these slides are © 2013 -2015 Pivotal Software, Inc. and licensed under a
Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
Every Commit is a Candidate for Deployment
cmt
v20
1
cmt cmt cmt cmt
v20
2
cmt
… …
App
Platform
CVE
Prod
Acceptance
41. Unless otherwise indicated, these slides are © 2013 -2015 Pivotal Software, Inc. and licensed under a
Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
Lean on a platform.
It’s the same
because it’s the
same.
42. Unless otherwise indicated, these slides are © 2013 -2015 Pivotal Software, Inc. and licensed under a
Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
demo
43. Unless otherwise indicated, these slides are © 2013 -2015 Pivotal Software, Inc. and licensed under a
Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
Run admin/management processes as one-off processes
Factor 12 – Admin 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.
44. Unless otherwise indicated, these slides are © 2013 -2015 Pivotal Software, Inc. and licensed under a
Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
Export Services Via Port Bindings
Factor 7 – Port Binding
Apps are deployed into containers
Multiple containers per host
Platform to handle port
assignments and mappings
demo
45. Unless otherwise indicated, these slides are © 2013 -2015 Pivotal Software, Inc. and licensed under a
Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
Runner
NAT
Router
Runner
NAT
foo.com:80
192.168.200.27:61021 192.168.200.33:61021
10.254.0.71:61021 10.254.0.76:61001 10.254.0.89:61021 10.254.0.93:61018
… …
46. Unless otherwise indicated, these slides are © 2013 -2015 Pivotal Software, Inc. and licensed under a
Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
Execute the app as one or more stateless processes
Factor 6 – Processes
47. Unless otherwise indicated, these slides are © 2013 -2015 Pivotal Software, Inc. and licensed under a
Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
Stateless apps allows the
platform
to do all sorts of things for you
demo
48. Unless otherwise indicated, these slides are © 2013 -2015 Pivotal Software, Inc. and licensed under a
Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
Scale out via the process model
Factor 8 – ConcurrencyWorkloaddiversity
Scale (running processes)
hello hello
trader trader trader
enqueue
file xfer file xfer file xfer
trader trader
demo
49. Unless otherwise indicated, these slides are © 2013 -2015 Pivotal Software, Inc. and licensed under a
Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
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
50. Unless otherwise indicated, these slides are © 2013 -2015 Pivotal Software, Inc. and licensed under a
Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
Thank you!
50
51. Unless otherwise indicated, these slides are © 2013 -2015 Pivotal Software, Inc. and licensed under a
Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 51
You can check all of this out: https://github.com/cdavisafc/twelvefactorapp
Other sessions:
• (10:30 W) Spring Boot for Devops (https://2015.event.springone2gx.com/schedule/sessions/spring_boot_for_devops.html)
• (2:30 W) Spring Cloud Services (https://2015.event.springone2gx.com/schedule/sessions/cloud_native_java_with_spring_cloud_services.html)
• Migrating the Monolith (https://2015.event.springone2gx.com/schedule/sessions/migrating_the_monolith.html)
Learn More. Stay Connected.
@springcentral Spring.io/video
@cdavisafc
@krujos
Hinweis der Redaktion Platforms have to make reasonable time assumptions when judging health, if you’re not started, what are you? How:
Complete in flight requests, reject new ones.
Design for failure, mark jobs as complete, in progress, aborted etc.
Transactions, NACK
Service selection and consumption is a big part, can they handle sudden failure?
Be quick about it, you’re on a clock.
Cloud Native, resiliency is big. Healer processes. Transition: So how do we do that?
Blue Green For this deploy. This example is mysql This example is mysql Its better to declare our dependencies with our code
The upside, you’re deploys fail faster if the service can’t be found.
So to recap how that all works: Demo
Head not to disposability, as we do blue green.
https://github.com/cdavisafc/twelvefactorapp/compare/f11_logs...f4_backing_services?expand=1
We made a change to our app, lets take a person name, and save who we greet
Controller change
https://github.com/cdavisafc/twelvefactorapp/blob/f4_backing_services/src/main/java/hello/HelloController.java#L29-L30
Added some config to our app for mysql, flyway and cloud foundry connectors.
https://github.com/cdavisafc/twelvefactorapp/blob/f4_backing_services/build.gradle#L40-L46
Cloud Native & 12 Factor apps are designed for continuous deployment
Why?
Important to emphasize
Dev has env just like
Acceptance and prod
(i.e. McKenty rant)
Cloud Native & 12 Factor apps are designed for continuous deployment
Why? Dev, Acceptance, Prod
The services we use in dev are the services we use in prod – no tool gap
The data we use in dev is derived from prod
We don’t spend any time messing around with data conversions, it’s the same because it’s the same.
If every commit is a canidate, we desire to know that it works in our target environment.
We deploy, the same way to all environments (cf push)
We deploy the same thing, to all environments (war, from artifactory)
If every change is a candidate, there's no time gap, deployments are simple with low overhead, we design and build for continuous deployment. This is an architecture, design and mindset shift.
We minimize the personnel gap.
Developers have the tools (and the responsibility) to deploy and monitoring in production
Ops have the tools and the knowledge to deploy
Of the factors to pay attention too… pragmatism counts. The purist implementation w/ java is harder than the pragmatic one. Don’t take on that complexity until you need it.
https://github.com/cdavisafc/twelvefactorapp/compare/f4_backing_services...f12_admin_processes