Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Dockerized Java for Belfast JUG and Docker Belfast meetups

121 views

Published on

Dockerizing Java applications may seem simple, but it meets several challenges related to configuration management, service discovery, licensing, dependency management and scaling. This presentation will cover some practical aspects of running Java inside containers e.g. choosing base image, planning Docker image hierarchy, creating Docker containers for Tomcat, Spring Boot, WebLogic, fat JAR vs. thin JAR, multi-container setups and service discovery.

Published in: Technology
  • Be the first to comment

Dockerized Java for Belfast JUG and Docker Belfast meetups

  1. 1. 01
  2. 2. 02
  3. 3. 03
  4. 4. Let's start! 04
  5. 5. Getting Java 05
  6. 6. Question 06
  7. 7. No! 07
  8. 8. Because... license! 08
  9. 9. Licensing 09
  10. 10. Official Java image? 10
  11. 11. 11
  12. 12. 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
  13. 13. Deprecated 13
  14. 14. Latest OpenJDK > docker run --rm -it openjdk java -version openjdk version "1.8.0_171" OpenJDK Runtime Environment (build 1.8.0_171) OpenJDK 64-Bit Server VM (build 25.171-b11, mixed mode) 01. 02. 03. 04. 14
  15. 15. Java 9 > docker run --rm -it openjdk:9 java -version openjdk version "9.0.4" OpenJDK Runtime Environment (build 9.0.4) OpenJDK 64-Bit Server VM (build 9.0.4, mixed mode) 01. 02. 03. 04. 15
  16. 16. Java 10 > docker run --rm -it openjdk:10 java -version openjdk version "10.0.1" 2018-04-17 OpenJDK Runtime Environment (build 10.0.1) OpenJDK 64-Bit Server VM (build 10.0.1, mixed mode) 01. 02. 03. 04. 16
  17. 17. OpenJDK 17
  18. 18. OpenJDK The reality is that it is easier to get latest version of OpenJDK through Docker Hub than through upstream distribution repositories (apt or yum). “ 18
  19. 19. Oracle Java? 19
  20. 20. Build it yourself! 20
  21. 21. Or... 21
  22. 22. Oracle's official 22
  23. 23. Oracle's official 23
  24. 24. Easy? 24
  25. 25. Easy? docker login container-registry.oracle.com docker pull container-registry.oracle.com/java/serverjre 01. 02. 25
  26. 26. Alternatives? 26
  27. 27. Docker Store 27
  28. 28. Docker Store 28
  29. 29. Docker Store 29
  30. 30. Docker Store docker login docker pull store/oracle/serverjre:8 01. 02. 30
  31. 31. Alternatives? 31
  32. 32. Build it yourself! 32
  33. 33. Or... 33
  34. 34. Azul's Zulu It's a certified OpenJDK build. Offers free extension that makes it on-par with HotSpot. Dockerfiles: https://github.com/zulu-openjdk/zulu-openjdk. • • • 34
  35. 35. Packaging your Java apps35
  36. 36. Java's promise 36
  37. 37. Docker's promise 37
  38. 38. 38
  39. 39. Docker flow 39
  40. 40. Docker flow 40
  41. 41. Question What is the first problem any team runs into when starting using Docker? “ 41
  42. 42. No space left on device 42
  43. 43. 43
  44. 44. Docker images 44
  45. 45. Container packaging 45
  46. 46. Container packaging 46
  47. 47. Container packaging 47
  48. 48. Dockerfile 48
  49. 49. Ship artifacts 49
  50. 50. Multi-staged builds FROM maven:3.5.1 as builder RUN mvn clean install ... FROM openjdk:1.8 COPY --from=builder /target/app.jar ... ... 01. 02. 03. 04. 05. 06. 50
  51. 51. Build tools https://store.docker.com/images/maven https://store.docker.com/images/gradle https://store.docker.com/images/node • • • 51
  52. 52. Running Java 52
  53. 53. Container states 53
  54. 54. Gentle stop docker stop sends SIGTERM docker kill sends SIGKILL • • 54
  55. 55. Shutdown hook Runtime .getRuntime() .addShutdownHook(new Thread() { public void run() { /* my shutdown code here */ } }); 01. 02. 03. 04. 05. 06. 07. 55
  56. 56. Ergonomics Ergonomics 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. “ 56
  57. 57. Server-class A 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 • • 57
  58. 58. Server-class On 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 • • • • 58
  59. 59. 59
  60. 60. Namespaces and cgroups Name spaces limit the view of the system visible to the process Control groups limit resources available to the process • • 60
  61. 61. Memory limits docker run -dit --memory 100M --memory-swap -1 java java -Xmx100M MyServer 01. 02. 03. 04. 05. 61
  62. 62. JVM Memory 62
  63. 63. How much memory is there? These tools lie: free top java (before at least 1.8.121) • • • 63
  64. 64. 64
  65. 65. free 65
  66. 66. No limits 66
  67. 67. With limits 67
  68. 68. Progress 68
  69. 69. Progress 69
  70. 70. Progress 70
  71. 71. Java 9! Yes! 71
  72. 72. But experimental... -XX:+UnlockExperimentalVMOptions -XX:+UseCGroupMemoryLimitForHeap • • 72
  73. 73. But works... 73
  74. 74. How many threads are there in JVM? Application + frameworks JIT (1+) GC (1+) • • • 74
  75. 75. Threading models Thread per request (servlets and friends) Thread per core (vert.x, ratpack, play etc.) • • 75
  76. 76. Default number of threads? Runtime .getRuntime() .availableProcessors(); 01. 02. 03. 76
  77. 77. Progress 77
  78. 78. Progress 78
  79. 79. Progress 79
  80. 80. Progress As of Java SE 8u131, and in JDK 9, the JVM is Docker-aware with respect to Docker CPU limits transparently. “ 80
  81. 81. Go to production! 81
  82. 82. Yeah! 82
  83. 83. Ouch! 83
  84. 84. Application logs docker run --name=petclinic -v /var/log/petclinic:/app/logs ... 01. 02. 03. 84
  85. 85. Log aggregation 85
  86. 86. Elastic Stack 86
  87. 87. ELK ((L)LEK) 87
  88. 88. ELK ((L)LEK) 88
  89. 89. ELK (B(L)EK) 89
  90. 90. ELK (BEK) 90
  91. 91. JSON logging <dependency> <groupId>net.logstash.logback</groupId> <artifactId>logstash-logback-encoder</artifactId> <version>4.9</version> </dependency> 01. 02. 03. 04. 05. 91
  92. 92. JSON logging <appender name="json" class="..."> ... <encoder class="net.logstash.logback.encoder.LogstashEncoder" /> </appender> 01. 02. 03. 04. 05. 92
  93. 93. filebeat.yml filebeat.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. 93
  94. 94. filebeat.yml output.elasticsearch: hosts: ["elasticsearch:9200"] 01. 02. 94
  95. 95. Container output Show container's STDERR and STDOUT. docker logs [-f] [-t] CONTAINER_ID01. 95
  96. 96. Docker logging driver $ docker info | grep 'Logging Driver' Logging Driver: json-file 01. 02. 96
  97. 97. 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 runningrn", "stream":"stdout","time":"2017-02-06T02:15:23.087459653Z"} 01. 02. 03. 04. 05. 06. 97
  98. 98. Stack traces? 98
  99. 99. Useful beats filebeat dockbeat springbeat metricbeat heartbeat packetbeat • • • • • • 99
  100. 100. Summary 100
  101. 101. Take-aways Build 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 the very latest Java 8 and Java 9 for better cgroups-awareness Take care of log aggregation (container's life is short) • • • • • 101
  102. 102. More stuff 102
  103. 103. Demo code http://bit.ly/DOCKERIZED_JAVA_TJUG• 103
  104. 104. References https://blog.docker.com/2018/04/improved-docker-container- integration-with-java-10/ http://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 • • • • 104
  105. 105. References https://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 • • • 105
  106. 106. References https://blogs.oracle.com/java-platform-group/java-se-support-for- docker-cpu-and-memory-limits • 106
  107. 107. References Ergonomics Shutdown hook • • 107
  108. 108. Issues https://bugs.openjdk.java.net/browse/JDK-8170888 https://bugs.openjdk.java.net/browse/JDK-8140793 https://bugs.openjdk.java.net/browse/JDK-8146115 • • • 108
  109. 109. That's all! 109
  110. 110. Thank you! 110
  111. 111. 111

×