build a 12 factor microservice in half an hour · build a 12 factor microservice in half an hour...

26
Build a 12 factor m an h Emily Jiang: Liberty Architect for MicroProfile and CDI, I @emilyfhjiang microservice in half hour IBM

Upload: others

Post on 20-May-2020

4 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Build a 12 factor microservice in half an hour · Build a 12 factor microservice in half an hour Emily Jiang: Liberty Architect for MicroProfileand CDI, IBM ... Contents Basic concept

Build a 12 factor microservice in half an hour

Emily Jiang: Liberty Architect for MicroProfile and CDI, IBM

@emilyfhjiang

Build a 12 factor microservice in half an hour

and CDI, IBM

Page 2: Build a 12 factor microservice in half an hour · Build a 12 factor microservice in half an hour Emily Jiang: Liberty Architect for MicroProfileand CDI, IBM ... Contents Basic concept

Contents

Basic concept of 12 factor app

Demo of creating a 12 factor microservice using MicroProfileMicroProfile

Page 3: Build a 12 factor microservice in half an hour · Build a 12 factor microservice in half an hour Emily Jiang: Liberty Architect for MicroProfileand CDI, IBM ... Contents Basic concept

12 Factors in a nut shell

A methodology

Best PracticesBest Practices

Manifesto

https://12factor.net/ by Heroku

Page 4: Build a 12 factor microservice in half an hour · Build a 12 factor microservice in half an hour Emily Jiang: Liberty Architect for MicroProfileand CDI, IBM ... Contents Basic concept

Why 12 factor?

Define the contract between applications and infrastructure

Application InfrastructureInfrastructure

Page 5: Build a 12 factor microservice in half an hour · Build a 12 factor microservice in half an hour Emily Jiang: Liberty Architect for MicroProfileand CDI, IBM ... Contents Basic concept

What is a Twelve-Factor App?

In the modern era, software is commonly delivered as a service: called web apps, or softwareservice apps that:

declarative formats for setup automation, to minimize time and cost for new developers joining the project;

clean contract with the underlying operating system, offering maximum portability

Are suitable for deployment on modern cloud platforms, obviating the need for servers and systems administration;

Minimize divergence between development and production, enabling continuous deploymentMinimize divergence continuous deployment

scale up without significant changes to tooling, architecture, or development practices.

-factor methodology can be applied to apps written in any programming language, and which use any combination of backi

From https://12factor.net

software-as-a-service. The twelve-factor app is a methodology for building software

formats for setup automation, to minimize time and cost for new developers joining the project;

maximum portability between execution environments;

, obviating the need for servers and systems administration;

continuous deployment for maximum agility;continuous deployment

without significant changes to tooling, architecture, or development practices.

factor methodology can be applied to apps written in any programming language, and which use any combination of backing services (database, queue, memory

Page 6: Build a 12 factor microservice in half an hour · Build a 12 factor microservice in half an hour Emily Jiang: Liberty Architect for MicroProfileand CDI, IBM ... Contents Basic concept

THE FACTORS

Codebase

Dependencies

Config

Backing Services

7.

8.

9.

10.

Build, Release, Run

Processes

11.

12.

Port binding

Concurrency

Disposability

10. Dev / Prod parity

11. Logs

12. Admin Processes

Page 7: Build a 12 factor microservice in half an hour · Build a 12 factor microservice in half an hour Emily Jiang: Liberty Architect for MicroProfileand CDI, IBM ... Contents Basic concept

I. Codebase

Dedicate smaller teams to individual applications or microservices.

Following the discipline of single repository for an application forces the teams to analyze the seams of their application, and identify potential monoliths that should be split off into microservices.

“One codebase tracked in revision control, many deploys.”

Use a single source code repository for a single application (1:1 relation). Deployment stages are different tags/branches

i.e. use a central git repo (external Github/GitHub Enterprise also suitable)

Following the discipline of single repository for an application forces the teams to analyze the seams of their application, and identify potential monoliths that should be split off into

Page 8: Build a 12 factor microservice in half an hour · Build a 12 factor microservice in half an hour Emily Jiang: Liberty Architect for MicroProfileand CDI, IBM ... Contents Basic concept

II. Dependencies

A cloud-native application does not rely on the pre-existence of dependencies in a deployment target.

Developer Tools declare and isolate dependencies

Maven and Gradle for Java

“Explicitly declare and isolate dependencies”

Each microservice has its own dependencies declared (e.g.

existence of dependencies in a deployment target.

Each microservice has its own dependencies declared (e.g. pom.xml)

Page 9: Build a 12 factor microservice in half an hour · Build a 12 factor microservice in half an hour Emily Jiang: Liberty Architect for MicroProfileand CDI, IBM ... Contents Basic concept

III. Config

“Store config in the environment”

Changing config should not need to repackage your application

Use Kubernetes configmaps and secrets (rather than environment variables) for container services

MicroProfile Config to inject the config properties into the microservices

App Password=blah

Changing config should not need to repackage your application

and secrets (rather than environment variables) for container services

Config to inject the config properties into the microservices

Password=blah

Page 10: Build a 12 factor microservice in half an hour · Build a 12 factor microservice in half an hour Emily Jiang: Liberty Architect for MicroProfileand CDI, IBM ... Contents Basic concept

IV. Backing services

“Treat backing services as attached resources”

Application

My SQL Amazon S3 Twitter

Page 11: Build a 12 factor microservice in half an hour · Build a 12 factor microservice in half an hour Emily Jiang: Liberty Architect for MicroProfileand CDI, IBM ... Contents Basic concept

V. Build, release, run

“Strictly separate build and run stages”

Source code is used in the build stage. Configuration data is added to define a release stage that can be deployed. Any changes in code or config will result in a new build/release

Needs to be considered in CI pipeline

IBMUrbanCode DeployIBM Cloud Continuous Delivery Service

AWS• AWS CodeBuild• AWS CodeDeploy• AWS CodePipeline

integrated with EKS)

Source code is used in the build stage. Configuration data is added to define a release stage that can be deployed. Any

Azure• Visual Studio Team Services (VSTS)

(includes git)• Web App for Containers feature of

Azure App Service(not yet

integrated with EKS)

Page 12: Build a 12 factor microservice in half an hour · Build a 12 factor microservice in half an hour Emily Jiang: Liberty Architect for MicroProfileand CDI, IBM ... Contents Basic concept

VI. Processes

“Execute the app as one or more stateless processes”

Stateless and share-nothing

Rest API

Page 13: Build a 12 factor microservice in half an hour · Build a 12 factor microservice in half an hour Emily Jiang: Liberty Architect for MicroProfileand CDI, IBM ... Contents Basic concept

VII. Port binding

“Export services via port binding”

Applications are fully self-contained and expose services only through ports. Port assignment is done by the execution environment

Ingress/service definition of k8s manages mapping of ports

Use MP Config to inject ports to microservices for chain-up invocationsUse MP Config to inject ports to microservices for chain-up invocations

@Inject @ConfigProperty(name=”port”, defaultValue=“9080”)

contained and expose services only through ports. Port assignment is done by the execution

Ingress/service definition of k8s manages mapping of ports

up invocationsup invocations

Port=80

Page 14: Build a 12 factor microservice in half an hour · Build a 12 factor microservice in half an hour Emily Jiang: Liberty Architect for MicroProfileand CDI, IBM ... Contents Basic concept

VIII. Concurrency

“Scale out via the process model”

Applications use processes independent from each other to scale out (allowing for load balancing)

To be considered in application design

Cloud autoscaling services: [auto]scaling built into k8s

Build micorservices

Applications use processes independent from each other to scale out (allowing for load balancing)

Page 15: Build a 12 factor microservice in half an hour · Build a 12 factor microservice in half an hour Emily Jiang: Liberty Architect for MicroProfileand CDI, IBM ... Contents Basic concept

IX. Disposability

“Maximize robustness with fast startup and graceful shutdown”

Processes start up fast.

Processes shut down gracefully when requested.

Processes are robust against sudden death

Use MicroProfile Fault Tolerance to make it resilientUse MicroProfile Fault Tolerance to make it resilient

“Maximize robustness with fast startup and graceful shutdown”

From “CERN Data Centre Evolution”

Page 16: Build a 12 factor microservice in half an hour · Build a 12 factor microservice in half an hour Emily Jiang: Liberty Architect for MicroProfileand CDI, IBM ... Contents Basic concept

X. Dev/prod parity

“Keep development, staging, and production as similar as possible”

Development and production are as close as possible (in terms of code, people, and environments)

Can use helm to deploy in repeatable manner

Use (name)spaces for isolation of similar setups

“Keep development, staging, and production as similar as possible”

Development and production are as close as possible (in terms of code, people, and environments)

Page 17: Build a 12 factor microservice in half an hour · Build a 12 factor microservice in half an hour Emily Jiang: Liberty Architect for MicroProfileand CDI, IBM ... Contents Basic concept

XI. Logs

“Treat logs as event streams”

App writes all logs to stdout

Use a structured output for meaningful logs suitable for analysis. Execution environment handles routing and analysis infrastructure

Use a structured output for meaningful logs suitable for analysis. Execution environment handles routing and analysis

Page 18: Build a 12 factor microservice in half an hour · Build a 12 factor microservice in half an hour Emily Jiang: Liberty Architect for MicroProfileand CDI, IBM ... Contents Basic concept

XII. Admin processes

“Run admin/management tasks as one-off processes”

Tooling: standard k8s tooling like “kubectl exec” or Kubernetes Jobs

Also to be considered in solution/application design

For example, if an application needs to migrate data into a database, place this task into a separate component instead of adding it to the main application code at startupadding it to the main application code at startup

exec” or Kubernetes Jobs

For example, if an application needs to migrate data into a database, place this task into a separate component instead of

Page 19: Build a 12 factor microservice in half an hour · Build a 12 factor microservice in half an hour Emily Jiang: Liberty Architect for MicroProfileand CDI, IBM ... Contents Basic concept

THE FACTORS

Codebase

Dependencies

Config

Backing Services

7.

8.

9.

10.

Build, Release, Run

Processes

11.

12.

Port binding

Concurrency

Disposability

10. Dev / Prod parity

11. Logs

12. Admin Processes

Page 20: Build a 12 factor microservice in half an hour · Build a 12 factor microservice in half an hour Emily Jiang: Liberty Architect for MicroProfileand CDI, IBM ... Contents Basic concept

MicroProfile Config

Configure Microservice without repacking the application

– Access configuration via

• Programmatically lookup

Config config =

config.getValue

Specify the configuration in configure sources • Via CDI Injection

@Inject @ConfigPropertyString

Access configuration via

Programmatically lookup

Config config =ConfigProvider.getConfig();

config.getValue(“myProp”, String.class);

Via CDI Injection

@Inject ConfigProperty(name="my.string.propertyString myPropV;

Page 21: Build a 12 factor microservice in half an hour · Build a 12 factor microservice in half an hour Emily Jiang: Liberty Architect for MicroProfileand CDI, IBM ... Contents Basic concept

MicroProfile Config

Static Config

Dynamic Config

@Inject@ConfigProperty(name="myStaticProp")private String staticProp;

@Inject@Inject@ConfigProperty(name="myDynamicProp")private Provider<String> dynamicProp;

microprofile-config.propertiesmyStaticProp=defaultSValuemyDynamicProp=defaultDValue

overrides

Java Options-DmyStaticProp=customSValue-DmyDynamicProp=customDValue

Page 22: Build a 12 factor microservice in half an hour · Build a 12 factor microservice in half an hour Emily Jiang: Liberty Architect for MicroProfileand CDI, IBM ... Contents Basic concept

MicroProfile Fault Tolerance

A solution to build a resilient microservice

Retry - @Retry

Circuit Breaker - @CircuitBreaker

Bulk Head - @BulkheadBulk Head - @Bulkhead

Time out - @Timeout

Fallback - @Fallback

Page 23: Build a 12 factor microservice in half an hour · Build a 12 factor microservice in half an hour Emily Jiang: Liberty Architect for MicroProfileand CDI, IBM ... Contents Basic concept

12 factor app

Use MicroProfile and K8s to build a microservice => 12 factor app

Infrastructuremicroservice

InfrastructureInfrastructureInfrastructure

K8s

Page 24: Build a 12 factor microservice in half an hour · Build a 12 factor microservice in half an hour Emily Jiang: Liberty Architect for MicroProfileand CDI, IBM ... Contents Basic concept

References

http://microprofile.io

http://openliberty.io

https://www.12factor.net/

Infrastructuremicroservice

InfrastructureInfrastructureInfrastructure

K8s

Page 25: Build a 12 factor microservice in half an hour · Build a 12 factor microservice in half an hour Emily Jiang: Liberty Architect for MicroProfileand CDI, IBM ... Contents Basic concept

EclipseCon Sessions – MicroProfile and Jakarta EE

Build 12 Factor MicroService in Half Hour

Speaker: Emily Jiang

Date/Time: Wednesday, June 13, 2018 – 9:45 to 10:20

Practical Cloud Native Java Development with MicroProfile

Speaker: Alasdair Nottingham

Date/Time: Wednesday, June 13, 2018 – 10:45 to 11:20

Jakarta EE: Not Your Parent’s Java EE

Speaker: Kevin Sutter

Date/Time: Wednesday, June 13, 2018 – 14:40 to 15:15

and Jakarta EE

Thursday

JAX-RS 2.1 and Beyond…

Ignite - Wednesday

MicroProfile meets Istio (Ignite)Speaker: Emily JiangDate/Time: Wednesday, June 13, 2018 – 17:15 to 18:00

Speed Dating with Jakarta EE (Ignite)Speaker: Kevin SutterDate/Time: Wednesday, June 13, 2018 – 17:15 to 18:00

JAX-RS 2.1 and Beyond…Speaker: Andy McCrightDate/Time: Thursday, June 14, 2018 - 14:15 to 14:50

Resilient Microservices with Eclipse MicroProfileSpeaker: Emily JiangDate/Time: Thursday, June 14, 2018 - 15:15 to 15:50

Page 26: Build a 12 factor microservice in half an hour · Build a 12 factor microservice in half an hour Emily Jiang: Liberty Architect for MicroProfileand CDI, IBM ... Contents Basic concept

Backup: Using IBM Cloud Private

Source: Github Enterprise, githubImages: any registry, IBM Cloud private registry

Dependency management of language environment; container build process for repeatable inclusion of dependencies

k8s configmaps and secrets

Use configuration (see previous Use configuration (see previous factor) to define target server as used by application

Build, release, run UrbanCode DeployUrbanCode ReleasePlus k8s mechanisms with CI tooling

To be considered in application design

Port binding Application needs to expose ports. Ingress/service definition of k8s manages mapping of ports

Concurrency App design ([auto]scaling built into k8s)

Disposability App design

Dev/prod parity Can use helm to deploy in same way. Namespaces for isolation of similar areasareas

Logs ELK as part of ICP (or RYO)

Admin processes App design; standard k8s tooling like “kubectl exec” or Kubernetes Jobs