microservices in java

45
BASEL BERN BRUGG DÜSSELDORF FRANKFURT A.M. FREIBURG I.BR. GENEVA HAMBURG COPENHAGEN LAUSANNE MUNICH STUTTGART VIENNA ZURICH Microservices in Java Anwendungsentwicklung mit K8S/Openshift Anatole Tresch, Principal Consultant @ atsticks

Upload: anatole-tresch

Post on 21-Jan-2018

168 views

Category:

Software


0 download

TRANSCRIPT

Page 1: Microservices in Java

BASEL BERN BRUGG DÜSSELDORF FRANKFURT A.M. FREIBURG I.BR. GENEVA

HAMBURG COPENHAGEN LAUSANNE MUNICH STUTTGART VIENNA ZURICH

Microservices in JavaAnwendungsentwicklung mit K8S/Openshift

Anatole Tresch, Principal Consultant

@atsticks

Page 2: Microservices in Java

About me...

Open Source Enthusiast

JCP Expert Group Member

Apache PPMC Member

Consultant, Trainer, Software Architect

Oracle Star Spec Lead

Contact: [email protected]

Blog: http://maketechsimple.wordpress.com

Slideshare: http://www.slideshare.net/anatoletresch

Twitter: @atsticks

Page 3: Microservices in Java

Agenda

• Microservices

• Run in Docker

• Deploying into Kubernetes

• Fabric8

• DevOps and Advanced Topics

Page 4: Microservices in Java

Microservices

Page 5: Microservices in Java

Microservices

• Single Concerns Principle based on bounded contexts

• Independently deployable

• Isolated

• Clear, minimal API, typically REST

• Development Platforms in Java:

• Java EE

• Microprofile

• OSGI

• Vertx

• Akka

• Dropwizard

• …

Page 6: Microservices in Java

Microservices - Build

Build Tools:

• Maven

• Gradle

Created Artifacts:

• jar, war, ear, rar etc.

• Java EE server RT configuration

Our Focus:

• Maven

• Single Jar Deployment

Page 7: Microservices in Java

Example Application

https://github.com/atsticks/openshift-workshop/tree/master/spring-boot-CRUD-admin-step1

• Spring Boot Application

• Bootstrap/AngularJS UI

• REST API

• MongoDB Backend

• Standard Maven Build

Page 8: Microservices in Java

Starting the Application

1. start a mongodb at @localhost, e.g.

docker run --name mongo -p 27017:27017 -d mongo mongod

2. run mvn clean package spring-boot:run

3. Point your browser at http://localhost:8090/index.html

Page 9: Microservices in Java

Run our app as a Docker Container

Page 10: Microservices in Java

Run the App as a Docker Container

See: https://github.com/atsticks/openshift-workshop/tree/master/spring-boot-CRUD-admin-step2

No changes in your Java Project or Maven

Write a Dockerfile

Use Docker to build a local Docker image

Start DB and APP in Docker

Page 11: Microservices in Java

Run the app as a Docker container

Add Dockerfile to application project dir:

Build and run the db and the app as containers:

docker run --name mongo -p 27017:27017 -d mongo mongod

mvn clean package

docker build -t step2 .

docker run -p 8090:8090 --link mongo:mongo -it --name myapp

Page 12: Microservices in Java

Challenges

• We have to learn to write Docker files

• Testing the application requires manual action (starting Mongo-DB)

• DB and APP must be linked explicitly.

• Deployment is a separate and manual process

• Separate for the Java App and the Mongo DB

• Different for different stages.

Page 13: Microservices in Java

But what we really want is…

• A deployment and packaging technique

• A stable and reliable runtime that

• Supports monitoring and restart components

• Provides easy service location

• Provides automatic fail-over

Container Orchestration

+

Unified Deployment

Page 14: Microservices in Java

Possible Container Orchestration Targets today

• Amazon ECS

• Azure Container Service (ACS)

• Cloud Foundry’s Diego

• CoreOS Fleet

• Docker Swarm

• Google Container Engine

• Kubernetes / Openshift

• Mesosphere Marathon

• Cloud Native Computing Foundation (CNCF) recently selected Google’s

Kubernetes container orchestration tool as its first containerization technology.

• Docker also will support Kubernetes OOTB.

Page 15: Microservices in Java

Deploy our app into Kubernetes

Page 16: Microservices in Java

Deploying into Kubernetes

• Kubernetes requires container images for

• The mongo DB (available in Dockerhub)

• The app container (available in a private Docker registry)

• For each of them we require

• A deployment defining

• The container image to use

• The Number of replicas

• Volume mounts

• Environment and Config

• A service making a deployment accessible

Page 17: Microservices in Java

Deploying into Kubernetes

Deployment Descriptors

Service Descriptors

Configuration Descriptor

Page 18: Microservices in Java

Still there is one thing missing…

Page 19: Microservices in Java

We need a (local)

Kubernetes/Openshift

Cluster

Still there is one pice missing…

Page 20: Microservices in Java

Local Kubernetes/Openshift

• Minishift (https://github.com/minishift/minishift )

minishift start --cpus 2 --memory 6000 --vm-driver=virtualbox

$ minishift start

Starting local OpenShift cluster using 'kvm' hypervisor...

...

OpenShift server started.

The server is accessible via web console at:

https://192.168.99.100:8443

You are logged in as:

User: developer

Password: developer

To login as administrator:

oc login -u system:admin

Page 21: Microservices in Java
Page 22: Microservices in Java

Manually deploying into Kubernetes

1. Setup Openshift/Kubernetes/Docker environment

2. Create mongodb service : oc create -f kube/mongoservice.yml

3. Start mongodb deployment: oc create -f kube/mongodeployment.yml

4. Deploy config map : oc create -f kube/configmap.yml

5. Create app service: oc create -f kube/service.yml

6. Build the project: mvn clean package

7. Build the docker image: docker build -t step3 .

8. Tag the built docker image: docker tag step3 $(minishift openshift

registry)/myproject/step3

9. Push the built image: docker push $(minishift openshift

registry)/myproject/step3

10. Create app deployment: oc create -f kube/deployment.yml

11. get $NODEPORT : oc export svc step3 | grep nodePort

12. Verify service: http://$(minishift ip):$NODEPORT/index.html

Page 23: Microservices in Java

Cool, that was

easy…!

Page 24: Microservices in Java
Page 25: Microservices in Java

Fabric8

• Integrated Development Platform for Kubernetes

• Targeting the creation of cloud native applications and microservices

• Build, test and deploy

• Continuous Delivery pipelines

• Run and manage

• Continuous Improvement and ChatOps

Page 26: Microservices in Java

The parts of Fabric8

• Developer Console: web app to create, edit, build, deploy and test Microservices

• Continuous Integration and Continous Delivery: Jenkins Workflows

• Application Management:

• Logging and Metrics, ChatOps and Chaos Monkey

• Integration of Hawtio and Jolokia

• Integration Platform As A Service:

• Visualisation of your Apache Camel integration services

• API Registry

• Messaging As A Service (ActiveMQ)

• Java Tools helps the Java community take full advantage of Kubernetes:

• Maven Plugin

• Junit Kubernetes Support based on Arquillian

• Java Libraries and support for working with Kubernetes

Page 27: Microservices in Java

The Fabric8

Maven Plugin

Page 28: Microservices in Java

The Fabric8 Maven Plugin

Brings Java apps on to K8S and Openshift.

Leverages existing maven config.

Builds Docker images and K8S/Openshift manifests.

Supports different levels of customization

Integrates also the Fabric8 Docker Plugin

<plugin>

<groupId>io.fabric8</groupId>

<artifactId>fabric8-maven-plugin</artifactId>

<version>${latest.version}</version>

</plugin>

Page 29: Microservices in Java

The Fabric8 Maven Plugin Goals

Build Goals

fabric8:build

fabric8:resource

fabric8:apply

fabric8:resource-apply

fabric8:push

fabric8:helm

fabric8:distro

fabric8:app-catalog

Development goals

fabric8:run

fabric8:deploy

fabric8:undeploy

fabric8:start

fabric8:stop

fabric8:log

fabric8:debug

fabric8:watch

Page 30: Microservices in Java

The Fabric8 Maven Plugin Configuration Options

• Zero-Config (using defaults)

• Maven XML configuration (image, deployment)

• External Kubernetes/Openshift resource fragments

• External Dockerfiles

Page 31: Microservices in Java

F8MP: Zero-Config Example

1) Setting up dependencies and plugins.

2) Enables a simple embedded Tomcat.

3) Responsible for packaging the application.

4) Generation of a Docker image and

Kubernetes / OpenShift descriptors.

To build the Docker image:

mvn package fabric8:build

To deploy to a cluster:

mvn fabric8:resource fabric8:deploy

Page 32: Microservices in Java

See https://maven.fabric8.io/#generator-spring-boot

F8MP: Zero-Config Example

What happened:

As base image fabric8/java-jboss-openjdk8-jdk is chosen

enables Jolokia, jmx_exporter.

sophisticated startup script.

Creates resource objects:

Kubernetes Deployment

Kubernetes Service

Exports port

8080 as the application service port

8778 for Jolokia

9779 for jmx_exporter access

Page 33: Microservices in Java

F8MP: XML Configuration Example

• Docker

• <images> define the Docker images to build

→ Similar for Fabric8 Docker Plugin, except <run> and <external> (ignored)

• <resource> define the resource descriptors for OpenShift or Kuberneres.

• <generator> configures generators.

• <enricher> configured enrichers.

<configuration>…</configuration>

Page 34: Microservices in Java

F8MP: XML Configuration Example

1) Standard docker-maven-plugin

configuration for building one

single Docker image

Page 35: Microservices in Java

F8MP: XML Configuration Example2) Kubernetes / OpenShift

resources to create

3) Labels applied globally to all

resource objects

1) Deployment Definition

2) Container to include in the

deployment

3) Container alias to correlate

image with the image definition

in the <images> section. Can

be omitted if only a single

image is used

4) Volume definitions used in the

ReplicaSet

5) One or more Service

definitions.

Page 36: Microservices in Java

F8MP: Configuration with external resources

External resource descriptors (YAML/JSON)

in the src/main/fabric8 and

src/main/docker

Each resource gets ist own file, which

contains some skeleton of a resource

description.

The plugin will pick up the resource, enriches

it and then combines all to a single

kubernetes.yml and openshift.yml.

Allows free use of any Kubernetes feature.

Supports resource filtering.

src/main/fabric8/deployment.xml:

Page 37: Microservices in Java

Run the App with the Fabric8 Plugin

See: https://github.com/atsticks/openshift-workshop/tree/master/spring-boot-CRUD-admin-step5

Mongo DB deployment has been added to the kube folder

Start DB and APP in Kubernetes/Openshift:

1. oc login -u developer -p developer

2. mvn clean install fabric8:deploy

3. Evaluate the $ROUTE-URL: minishift openshift service list -n myproject

4. Open $ROUTE-URL/index.html in your browser

Page 38: Microservices in Java

Packaging

38

Page 39: Microservices in Java

More Details:

https://docs.openshift.com/enterprise/3.0/architecture/core_concepts/builds_and_image_streams.html

https://github.com/openshift/source-to-image

Openshift: Source to Image (S2I)

Source

Build

S2I build image Image Stream

DeploymentPod

ContainerReplication Controller

All customizable with Jenkins pipelines!

Page 40: Microservices in Java

Helm

• https://helm.sh = Package Manager for Kubernetes

• The latest version of Helm is maintained by the CNCF - in collaboration

with Microsoft, Google, Bitnami and the Helm contributor community.

• With Helm you can

• Package your complete apps into Helm Charts

• Manage Charts in a repository

• Install Charts on CLI or with the helm UI (monocular)

• Update and Rollback installations

• Audit your installations

40

Page 41: Microservices in Java

Kubernetes Templates

https://docs.openshift.org/latest/dev_guide/templates.html

Templates can contain any number of Kubernetes Descriptors

Descriptors can be parametrized

Templates can be used with the CLI as well

as the Web Console

Page 42: Microservices in Java

Recap

Page 43: Microservices in Java

Recap

Moving from Java to Kubernetes/Openshift is easy!

Fabric 8 allows to gradually move along your available skills.

Minishift/Minikube provide an easy to use/setup local environment.

S2I/Templates gives you powerful DevOps Support.

Helm provides a package format applications including dependencies.

Page 44: Microservices in Java

Topics not covered

How to modernize your monoliths.

How to establish a DevOps culture.

How to test microservice based applications.

Operating microservice based applications:

Tracing, Monitoring, Log Collection, Alerting

Application and Service Security.

Data Persistence options in a microservice based architecture.

Legacy Integration, Hybrid Clouds, Scaling, Data Security etc.

Page 45: Microservices in Java

Microservices in Java

@atsticks maketechsimple.wordpress.com

[email protected]

Anatole Tresch

Principal Consultant