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
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
Agenda
• Microservices
• Run in Docker
• Deploying into Kubernetes
• Fabric8
• DevOps and Advanced Topics
Microservices
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
• …
Microservices - Build
Build Tools:
• Maven
• Gradle
Created Artifacts:
• jar, war, ear, rar etc.
• Java EE server RT configuration
Our Focus:
• Maven
• Single Jar Deployment
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
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
Run our app as a Docker Container
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
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
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.
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
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.
Deploy our app into Kubernetes
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
Deploying into Kubernetes
Deployment Descriptors
Service Descriptors
Configuration Descriptor
Still there is one thing missing…
We need a (local)
Kubernetes/Openshift
Cluster
Still there is one pice missing…
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
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
Cool, that was
easy…!
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
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
The Fabric8
Maven Plugin
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>
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
The Fabric8 Maven Plugin Configuration Options
• Zero-Config (using defaults)
• Maven XML configuration (image, deployment)
• External Kubernetes/Openshift resource fragments
• External Dockerfiles
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
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
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>
F8MP: XML Configuration Example
1) Standard docker-maven-plugin
configuration for building one
single Docker image
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.
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:
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
Packaging
38
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!
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
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
Recap
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.
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.
Microservices in Java
@atsticks maketechsimple.wordpress.com
Anatole Tresch
Principal Consultant
Top Related