dockerized java for transylvanian jug

103
01

Upload: andrey-adamovich

Post on 22-Jan-2018

321 views

Category:

Technology


0 download

TRANSCRIPT

01

02

03

Let's start!04

Getting Java05

Question

06

No!07

Because... license!

08

Licensing

09

Official Java image?

10

11

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.

12

Deprecated

13

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.

14

OpenJDKThe reality is that it is easier to get latest version of OpenJDK through

Docker Hub than through upstream distribution repositories (apt or

yum).“15

Oracle Java?16

Build ityourself!

17

Or...18

Oracle's official

19

Oracle's official

20

Easy?

21

Easy?docker login container-registry.oracle.com

docker pull container-registry.oracle.com/java/serverjre

01.

02.

22

Alternatives?23

Build ityourself!

24

Or...25

Azul's ZuluIt's a certified OpenJDK build.

Offers free extension that makes it on-par with HotSpot.

Dockerfiles: https://github.com/zulu-openjdk/zulu-openjdk.

Do not trust me, I haven't tested it! But it looks promising.

••••

26

Packagingyour Java

apps27

Java's promise

28

Docker's promise

29

30

Docker flow

31

Docker flow

32

QuestionWhat is the first problem any team runs into when starting using

Docker?“33

No space lefton device

34

35

Docker images

36

Container packaging

37

Container packaging

38

Container packaging

39

Dockerfile

40

Demo41

Ship artifacts

42

Running Java43

Container states

44

Gentle stopdocker stop sends SIGTERM

docker kill sends SIGKILL••

45

Shutdown hook Runtime

.getRuntime()

.addShutdownHook(new Thread() {

public void run() { /*

my shutdown code here

*/ }

});

01.

02.

03.

04.

05.

06.

07.

46

Demo47

ErgonomicsErgonomics is the process by which the Java Virtual Machine (JVM)

and garbage collection tuning, such as behavior-based tuning, improve

application performance. The JVM provides platform-dependent

default selections for the garbage collector, heap size, and runtime

compiler. These selections match the needs of different types of

applications while requiring less command-line tuning. In addition,

behavior-based tuning dynamically tunes the sizes of the heap to meet

a specified behavior of the application.

“48

Server-classA class of machine referred to as a server-class machine has been

defined as a machine with the following:

2 or more physical processors

2 or more GB of physical memory••

49

Server-classOn server-class 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

••••

50

51

Namespaces and cgroupsName spaces limit the view of the system visible to the process

Control groups limit resources available to the process••

52

Memory limitsdocker run -dit \

--memory 100M \

--memory-swap -1 \

java \

java -Xmx100M MyServer

01.

02.

03.

04.

05.

53

JVM Memory

54

How much memory is there?These tools lie:

free

top

java

•••

55

56

Demo57

Progress?

58

Progress?

59

Progress?

60

Java 9! Yes!61

How many threads in JVM?Application + frameworks

JIT (1+)

GC (1+)

•••

62

Threading modelsThread per request (servlets and friends)

Thread per core (vert.x, ratpack, play etc.)••

63

Default number of threads?Runtime

.getRuntime()

.availableProcessors();

01.

02.

03.

64

Demo65

Progress?

66

Progress?

67

Progress?

68

Interesting libraryhttps://github.com/haosdent/jcgroup•

69

Go toproduction!

70

Yeah!

71

Ouch!

72

Application logs

docker run --name=petclinic \

-v /var/log/petclinic:/app/logs \

...

01.

02.

03.

73

Logaggregation

74

Elastic Stack75

ELK ((L)LEK)

76

ELK ((L)LEK)

77

ELK (B(L)EK)

78

ELK (BEK)

79

JSON logging <dependency>

<groupId>net.logstash.logback</groupId>

<artifactId>logstash-logback-encoder</artifactId>

<version>4.9</version>

</dependency>

01.

02.

03.

04.

05.

80

JSON logging<appender name="json" class="...">

...

<encoder

class="net.logstash.logback.encoder.LogstashEncoder" />

</appender>

01.

02.

03.

04.

05.

81

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.

82

filebeat.ymloutput.elasticsearch:

hosts: ["elasticsearch:9200"]

01.

02.

83

Container outputShow container's STDERR and STDOUT.

docker logs [-f] [-t] CONTAINER_ID01.

84

Docker logging driver$ docker info | grep 'Logging Driver'

Logging Driver: json-file

01.

02.

85

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.

86

Stack traces?87

Useful beatsfilebeat

dockbeat

springbeat

metricbeat

heartbeat

packetbeat

••••••

88

Summary89

Take-awaysBuild 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

Use for the very latest Java 8 and Java 9 for better cgroups-

awareness

Take care of log aggregation (container's life is short)

•••

90

More stuff91

Demo codehttp://bit.ly/DOCKERIZED_JAVA_TJUG•

92

Book: Docker for Java Developers

93

Book: Containerizing CD in Java

94

Referenceshttp://matthewkwilliams.com/index.php/2016/03/17/docker-cgroups-

memory-constraints-and-java-cautionary-tale/

https://www.slideshare.net/kensipe/no-one-puts-java-in-the-container

https://www.slideshare.net/chbatey/docker-and-jvm-a-good-idea

••

95

Referenceshttps://developers.redhat.com/blog/2017/03/14/java-inside-docker/

http://blog.jelastic.com/2017/04/13/java-ram-usage-in-containers-top-

5-tips-not-to-lose-your-memory/

https://www.infoq.com/news/2017/02/java-memory-limit-container

••

96

Issueshttps://bugs.openjdk.java.net/browse/JDK-8170888

https://bugs.openjdk.java.net/browse/JDK-8140793

https://bugs.openjdk.java.net/browse/JDK-8146115

•••

98

Cheatsheets99

Stickers100

That's all!101

Thank you!102

103