Developing Java based microservices ready for the world of containers

69
Developing Java based microservices ready for the world of containers @davsclaus davsclaus davsclaus.com Claus Ibsen

Transcript of Developing Java based microservices ready for the world of containers

Page 1: Developing Java based microservices ready for the world of containers

Developing Java based microservices

ready for the world of containers

@davsclaus davsclaus

davsclaus.comClaus Ibsen

Page 2: Developing Java based microservices ready for the world of containers

Claus Ibsen• Principal Software Engineer

at Red Hat

• Apache Camel8 years working with Camel

• Author of Camel in Action books

@davsclaus davsclaus

davsclaus.com

Page 3: Developing Java based microservices ready for the world of containers
Page 4: Developing Java based microservices ready for the world of containers
Page 5: Developing Java based microservices ready for the world of containers
Page 6: Developing Java based microservices ready for the world of containers

?

Page 7: Developing Java based microservices ready for the world of containers

?

Page 8: Developing Java based microservices ready for the world of containers

OS Level Virtualization

Docker Orchestration

PaaS Platform on top of Kubernetes

Services and Tools forKubernetes and OpenShift

Page 9: Developing Java based microservices ready for the world of containers

Kubernetes

Page 10: Developing Java based microservices ready for the world of containers

Kubernetes

‣ Open Source orchestration platform for Docker containers

• Rewrite of Google’s internal framework “Borg”‣ Declarative specification of a desired state‣ Self-healing‣ Service discovery‣ Scheduling across hosts‣ Replication

Page 11: Developing Java based microservices ready for the world of containers

Architecture

Page 12: Developing Java based microservices ready for the world of containers

Key Concepts‣Nodes• Worker machine (physical or VM) running pods‣Pods

• Collection of one or more Docker containers‣Replication Controller

• Creates and takes care of Pods‣Services

• Network Proxy for a collection of Pods‣Labels

• Grouping and organization of Objects‣Namespaces

• Virtual clusters backend by same physical cluster

Page 13: Developing Java based microservices ready for the world of containers

Pod

‣ Collection of Docker containers running on the same host.

‣ Pods have unique IPs‣ Containers in a Pod ….

• …. share the same IP address• …. can reach each other via local ports• …. can share data via volumes‣ Pods can have one or more Labels

Page 14: Developing Java based microservices ready for the world of containers

Replication Controller

‣ Controls Pods selected by Labels‣ Ensures that a specified number of Pod replicas is

running‣ Holds Pod templates for creating new Pods‣ Autoscaling‣ Rolling Updates

Page 15: Developing Java based microservices ready for the world of containers

Service

‣ View on a set of Pods with single IP address and port

‣ Pods are selected by Label‣ Services are referenced by

environment variables‣ Service addresses stay stable

• Pods come and go (with different IPs)

Page 16: Developing Java based microservices ready for the world of containers

What is Kubernetes?

http://blog.kubernetes.io/2016/06/illustrated-childrens-guide-to-kubernetes.html

Page 17: Developing Java based microservices ready for the world of containers

https://fabric8.io/

Page 18: Developing Java based microservices ready for the world of containers

fabric8

‣ Opinionated microservices platform based on Docker, Kubernetes and Jenkins

• Management: console, logging, metrics, dashboards, …• Continuous Delivery Pipeline• iPaaS: Camel route visualization, API registry, Messaging as

a Service, …• Tools: Kubernetes/OpenShift build integration, Kubernetes

component test support, CDI extensions• Quickstarts: Ready to use examples, also available as

Maven archetypes

Page 19: Developing Java based microservices ready for the world of containers

Kubernetes Demo Time

Page 20: Developing Java based microservices ready for the world of containers

Java Developer

?

Page 21: Developing Java based microservices ready for the world of containers

Build a Camel Demo

Time

Page 22: Developing Java based microservices ready for the world of containers

Source Code

https://github.com/davsclaus/fabric8-hello

Page 23: Developing Java based microservices ready for the world of containers

Hello Service

Hello ServiceClient

Page 24: Developing Java based microservices ready for the world of containers

Hello Service

Hello Service

name=Claus

Hi $name I am $HOSTNAME

Client

Page 25: Developing Java based microservices ready for the world of containers

Implementation

Client Hello Service

HTTP

WildFySwarmSpring Boot WildFly Swarm

Page 26: Developing Java based microservices ready for the world of containers

Implementation

Hello Service

Client Hello Service

HTTP

Page 27: Developing Java based microservices ready for the world of containers

Implementation

Client Hello Service

HTTP

from timer to http

logfrom undertow

transform

hello service

Page 28: Developing Java based microservices ready for the world of containers

Spring Boot Starter

Page 29: Developing Java based microservices ready for the world of containers

Client

Page 30: Developing Java based microservices ready for the world of containers

WildFly Swarm Generator

Page 31: Developing Java based microservices ready for the world of containers

Hello Service

Page 32: Developing Java based microservices ready for the world of containers

Hello Service

Page 33: Developing Java based microservices ready for the world of containers

Ready to run local

Client Hello Service

HTTP 8080

from timer to netty

logfrom undertow

transform

hello service

Page 34: Developing Java based microservices ready for the world of containers

How to build Docker Image?

Maven Project Docker Image

Page 35: Developing Java based microservices ready for the world of containers

Docker Maven Plugin

Maven Project Fabric8 Maven Plugin Docker Imagebuild

https://maven.fabric8.io

Page 36: Developing Java based microservices ready for the world of containers

Fabric8 Maven Plugin

https://maven.fabric8.io

Page 37: Developing Java based microservices ready for the world of containers

Install fabric8-maven-plugin

mvn io.fabric8:fabric8-maven-plugin:3.1.37:setup

https://maven.fabric8.io

Page 38: Developing Java based microservices ready for the world of containers

Fabric8 Maven Plugin

Page 39: Developing Java based microservices ready for the world of containers

Build Docker Imagemvn package fabric8:build

Page 40: Developing Java based microservices ready for the world of containers

Local Docker RepositoryHello Service

Client

Page 41: Developing Java based microservices ready for the world of containers

Our Demo

Client Hello Service

HTTP 8080

from timer to http

logfrom undertow

transform

hello service

Page 42: Developing Java based microservices ready for the world of containers

Static vs Dynamic Platform

Client Hello Service

HTTP 8080

from timer to http

logfrom undertow

transform

hello service

Hardcoded hostname and port

Page 43: Developing Java based microservices ready for the world of containers

Dynamic Platform

Hello Service

HTTP 8080

Client

from timer to http

logfrom undertow

transform

hello service

Kubernetes Service

Page 44: Developing Java based microservices ready for the world of containers

Kubernetes Service• Network Connection to one or more Pods

• Unique static IP and port (lifetime of service)

Page 45: Developing Java based microservices ready for the world of containers

Kubernetes Service

HTTP 8080

from timer to http

logfrom undertow

transform

hello service

kube proxy

Kubernetes Master

enlisted on deployment

changes

pod

node

pod

node

Page 46: Developing Java based microservices ready for the world of containers

Kubernetes Servicefrom user point of view

service ip:port

from timer to http

logfrom undertow

transform

hello service

pod pod

Page 47: Developing Java based microservices ready for the world of containers

Out of the box serviceMaven artifactId

WildFly Swarm HTTP port

Page 48: Developing Java based microservices ready for the world of containers

Custom Configured Service service.yml

Container Port = Inside

Service Port = Outside

Service Name

Page 49: Developing Java based microservices ready for the world of containers

Using Kubernetes Service

Client

from timer to http

log

We want to use hello service

How do we do that?

Page 50: Developing Java based microservices ready for the world of containers

Using Kubernetes Service

• Environment Variables

• Hostname

• Port

Client

from timer to http

log

Service Discovery using DNS is available in newer versions of Kubernetes.

Page 51: Developing Java based microservices ready for the world of containers

Service using ENV• Use ENV Client

from timer to http

log

Page 52: Developing Java based microservices ready for the world of containers

Service using DNS• Use DNS Client

from timer to http

log

Page 53: Developing Java based microservices ready for the world of containers

Service using DNSClient

from timer to http

log

Kubernetes Service

Page 54: Developing Java based microservices ready for the world of containers

Ready to run in Kubernetes

Client Hello Service

{{service:hello}}

from timer to netty

logfrom undertow

transform

hello service

kube proxy

Page 55: Developing Java based microservices ready for the world of containers

How to deploy to Kubernetes?

Maven Project

Page 56: Developing Java based microservices ready for the world of containers

How to deploy to Kubernetes?

Maven Project fabric8 Maven Plugin

run

Page 57: Developing Java based microservices ready for the world of containers

Deploy - Hello Service

Hello Service

from undertow transform

hello service

• mvn fabric8:run

Page 58: Developing Java based microservices ready for the world of containers

Deploy - Client

• mvn fabric8:run Client

from timer to http

log

Page 59: Developing Java based microservices ready for the world of containers

Running Kubernetes Locally

• MiniKube

• MiniShift

• Vagrant

• OpenShift CDK

https://fabric8.io/guide/getStarted/index.html

Page 60: Developing Java based microservices ready for the world of containers

Running fabric8• Download gofabric8 https://github.com/fabric8io/gofabric8/releases

• Start fabric8gofabric8 start --minishift --console --memory=2000

https://fabric8.io/guide/getStarted/minishift.html

Page 61: Developing Java based microservices ready for the world of containers

fabric8 Web Console

minishift service fabric8

Page 62: Developing Java based microservices ready for the world of containers

OpenShift CLI• oc get pods

You can also use CLI from docker &

kubernetes

Page 63: Developing Java based microservices ready for the world of containers

OpenShift CLI• oc get service

Page 64: Developing Java based microservices ready for the world of containers

OpenShift CLI• oc logs -f <pod name>

Page 65: Developing Java based microservices ready for the world of containers

• minishift service hello

Access Servicefrom your laptop

Page 66: Developing Java based microservices ready for the world of containers

Scaling• Change deployment replicas

Page 67: Developing Java based microservices ready for the world of containers

Scaling• Service load balancing

Load balancing is random

Page 68: Developing Java based microservices ready for the world of containers

Angry Pods

1st person shooter - Kill your pods

Page 69: Developing Java based microservices ready for the world of containers

Links• fabric8

• http://fabric8.io • Demo source code

• https://github.com/davsclaus/fabric8-hello • Try kubernetes / fabric8

• https://fabric8.io/guide/getStarted/minishift.html • Videos, blogs and more

• https://fabric8.io/community/index.html

@davsclaus davsclaus

davsclaus.com