dockerized java - jeeconf 2017
TRANSCRIPT
01
02
Let's start!03
Getting Java04
Question
05
No!06
Because... license!
07
Licensing
08
Official Java image?
09
10
Getting Java> docker run ‐‐rm ‐it java java ‐version
openjdk version "1.8.0_111"
OpenJDK Runtime Environment (build 1.8.0_111...)
OpenJDK 64‐Bit Server VM (build 25.111‐b14, mixed mode)
01.
02.
03.
04.
11
Deprecated
12
Latest OpenJDK> docker run ‐‐rm ‐it openjdk java ‐version
openjdk version "1.8.0_131"
OpenJDK Runtime Environment (build 1.8.0_131‐8u131...)
OpenJDK 64‐Bit Server VM (build 25.131‐b11, mixed mode)
01.
02.
03.
04.
13
OpenJDKThe reality is that it is easier to get latest version of OpenJDK through
Docker Hub than through upstream distribution repositories (apt or
yum).“14
Oracle Java?15
Build ityourself!
16
Or...17
Oracle's official
18
Oracle's official
19
Easy?
20
Easy?docker login container‐registry.oracle.com
docker pull container‐registry.oracle.com/java/serverjre
01.
02.
21
Alternatives?22
Build ityourself!
23
Or...24
Azul's ZuluIt's a certified OpenJDK build.
Offers free extension that makes it onpar with HotSpot.
Dockerfiles: https://github.com/zuluopenjdk/zuluopenjdk.
Do not trust me, I haven't tested it! But it looks promising.
••••
25
Packagingyour Javaapps
26
Java's promise
27
Docker's promise
28
29
Docker flow
30
Docker flow
31
QuestionWhat is the first problem any team runs into when starting using
Docker?“32
No space lefton device
33
34
Docker images
35
Container packaging
36
Container packaging
37
Container packaging
38
Dockerfile
39
Demo40
Ship artifacts
41
Running Java42
Container states
43
Gentle stopdocker stop sends SIGTERM
docker kill sends SIGKILL••
44
Shutdown hook Runtime
.getRuntime()
.addShutdownHook(new Thread() {
public void run() { /*
my shutdown code here
*/ }
});
01.
02.
03.
04.
05.
06.
07.
45
Demo46
ErgonomicsErgonomics is the process by which the Java Virtual Machine (JVM)
and garbage collection tuning, such as behaviorbased tuning, improve
application performance. The JVM provides platformdependent
default selections for the garbage collector, heap size, and runtime
compiler. These selections match the needs of different types of
applications while requiring less commandline tuning. In addition,
behaviorbased tuning dynamically tunes the sizes of the heap to meet
a specified behavior of the application.
“47
ServerclassA class of machine referred to as a serverclass machine has been
defined as a machine with the following:
2 or more physical processors
2 or more GB of physical memory••
48
ServerclassOn serverclass machines, the following are selected by default:
Throughput garbage collector
Initial heap size of 1/64 of physical memory up to 1 GB
Maximum heap size of 1/4 of physical memory up to 1 GB
Server runtime compiler
••••
49
50
Namespaces and cgroupsName spaces limit the view of the system visible to the process
Control groups limit resources available to the process••
51
Memory limitsdocker run ‐dit \
‐‐memory 100M \
‐‐memory‐swap ‐1 \
java \
java ‐Xmx100M MyServer
01.
02.
03.
04.
05.
52
JVM Memory
53
How much memory is there?These tools lie:
free
top
java
•••
54
55
Demo56
Progress?
57
Progress?
58
Progress?
59
How many threads in JVM?Application + frameworks
JIT (1+)
GC (1+)
•••
60
Threading modelsThread per request (servlets and friends)
Thread per core (vert.x, ratpack, play etc.)••
61
Default number of threads?Runtime
.getRuntime()
.availableProcessors();
01.
02.
03.
62
Demo63
Progress?
64
Progress?
65
Progress?
66
Go toproduction!68
Yeah!
69
Ouch!
70
Application logs
docker run ‐‐name=petclinic \
‐v /var/log/petclinic:/app/logs \
...
01.
02.
03.
71
Logaggregation
72
Elastic Stack73
ELK ((L)LEK)
74
ELK ((L)LEK)
75
ELK (B(L)EK)
76
ELK (BEK)
77
JSON logging <dependency>
<groupId>net.logstash.logback</groupId>
<artifactId>logstash‐logback‐encoder</artifactId>
<version>4.9</version>
</dependency>
01.
02.
03.
04.
05.
78
JSON logging<appender name="json" class="...">
...
<encoder
class="net.logstash.logback.encoder.LogstashEncoder" />
</appender>
01.
02.
03.
04.
05.
79
filebeat.ymlfilebeat.prospectors:
‐ input_type: log
paths: ["...."]
json.keys_under_root: true
json.overwrite_keys: true
json.add_error_key: true
json.message_key: "message"
tags: ["json"]
01.
02.
03.
04.
05.
06.
07.
08.
80
filebeat.ymloutput.elasticsearch:
hosts: ["elasticsearch:9200"]
01.
02.
81
Container outputShow container's STDERR and STDOUT.
docker logs [‐f] [‐t] CONTAINER_ID01.
82
Docker logging driver$ docker info | grep 'Logging Driver'
Logging Driver: json‐file
01.
02.
83
STDOUT as JSON$ docker run ‐itd jenkins
67aa...
$ tail ‐f /var/lib/docker/containers/67aa.../67aa..._json.log
...
{"log":"INFO: Jenkins is fully up and running\r\n",
"stream":"stdout","time":"2017‐02‐06T02:15:23.087459653Z"}
01.
02.
03.
04.
05.
06.
84
Stack traces?85
Demo86
Useful beatsfilebeat
dockbeat
springbeat
metricbeat
heartbeat
packetbeat
••••••
87
Summary88
TakeawaysBuild base Docker images yourself
Plan the image hierarchy to reduce image deliverable sizes
Carefully tune memory and threads to make sure your Java containers
behave well while working on the same host
Wait for Java 9 for better cgroupsawareness
Take care of log aggregation (container's life is short)
•••
••
89
More stuff90
Demo codehttps://github.com/aestasit/talks2017jeeconfdockerizedjavasetup•
91
Book: Docker for Java Developers
92
Book: Containerizing CD in Java
93
Referenceshttp://matthewkwilliams.com/index.php/2016/03/17/dockercgroups
memoryconstraintsandjavacautionarytale/
https://www.slideshare.net/kensipe/nooneputsjavainthecontainer
https://www.slideshare.net/chbatey/dockerandjvmagoodidea
•
••
94
Referenceshttps://developers.redhat.com/blog/2017/03/14/javainsidedocker/
http://blog.jelastic.com/2017/04/13/javaramusageincontainerstop
5tipsnottoloseyourmemory/
https://www.infoq.com/news/2017/02/javamemorylimitcontainer
••
•
95
ReferencesErgonomics
Shutdown hook••
96
Issueshttps://bugs.openjdk.java.net/browse/JDK8170888
https://bugs.openjdk.java.net/browse/JDK8140793
https://bugs.openjdk.java.net/browse/JDK8146115
•••
97
That's all!98
Thank you!99
100