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 Transylvanian JUG

427 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 Transylvanian JUG

  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_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
  15. 15. 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). “ 15
  16. 16. Oracle Java? 16
  17. 17. Build it yourself! 17
  18. 18. Or... 18
  19. 19. Oracle's official 19
  20. 20. Oracle's official 20
  21. 21. Easy? 21
  22. 22. Easy? docker login container-registry.oracle.com docker pull container-registry.oracle.com/java/serverjre 01. 02. 22
  23. 23. Alternatives? 23
  24. 24. Build it yourself! 24
  25. 25. Or... 25
  26. 26. 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. Do not trust me, I haven't tested it! But it looks promising. • • • • 26
  27. 27. Packaging your Java apps27
  28. 28. Java's promise 28
  29. 29. Docker's promise 29
  30. 30. 30
  31. 31. Docker flow 31
  32. 32. Docker flow 32
  33. 33. Question What is the first problem any team runs into when starting using Docker? “ 33
  34. 34. No space left on device 34
  35. 35. 35
  36. 36. Docker images 36
  37. 37. Container packaging 37
  38. 38. Container packaging 38
  39. 39. Container packaging 39
  40. 40. Dockerfile 40
  41. 41. Demo 41
  42. 42. Ship artifacts 42
  43. 43. Running Java 43
  44. 44. Container states 44
  45. 45. Gentle stop docker stop sends SIGTERM docker kill sends SIGKILL • • 45
  46. 46. Shutdown hook Runtime .getRuntime() .addShutdownHook(new Thread() { public void run() { /* my shutdown code here */ } }); 01. 02. 03. 04. 05. 06. 07. 46
  47. 47. Demo 47
  48. 48. 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. “ 48
  49. 49. 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 • • 49
  50. 50. 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 • • • • 50
  51. 51. 51
  52. 52. Namespaces and cgroups Name spaces limit the view of the system visible to the process Control groups limit resources available to the process • • 52
  53. 53. Memory limits docker run -dit --memory 100M --memory-swap -1 java java -Xmx100M MyServer 01. 02. 03. 04. 05. 53
  54. 54. JVM Memory 54
  55. 55. How much memory is there? These tools lie: free top java • • • 55
  56. 56. 56
  57. 57. Demo 57
  58. 58. Progress? 58
  59. 59. Progress? 59
  60. 60. Progress? 60
  61. 61. Java 9! Yes! 61
  62. 62. How many threads in JVM? Application + frameworks JIT (1+) GC (1+) • • • 62
  63. 63. Threading models Thread per request (servlets and friends) Thread per core (vert.x, ratpack, play etc.) • • 63
  64. 64. Default number of threads? Runtime .getRuntime() .availableProcessors(); 01. 02. 03. 64
  65. 65. Demo 65
  66. 66. Progress? 66
  67. 67. Progress? 67
  68. 68. Progress? 68
  69. 69. Interesting library https://github.com/haosdent/jcgroup• 69
  70. 70. Go to production! 70
  71. 71. Yeah! 71
  72. 72. Ouch! 72
  73. 73. Application logs docker run --name=petclinic -v /var/log/petclinic:/app/logs ... 01. 02. 03. 73
  74. 74. Log aggregation 74
  75. 75. Elastic Stack 75
  76. 76. ELK ((L)LEK) 76
  77. 77. ELK ((L)LEK) 77
  78. 78. ELK (B(L)EK) 78
  79. 79. ELK (BEK) 79
  80. 80. JSON logging <dependency> <groupId>net.logstash.logback</groupId> <artifactId>logstash-logback-encoder</artifactId> <version>4.9</version> </dependency> 01. 02. 03. 04. 05. 80
  81. 81. JSON logging <appender name="json" class="..."> ... <encoder class="net.logstash.logback.encoder.LogstashEncoder" /> </appender> 01. 02. 03. 04. 05. 81
  82. 82. 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. 82
  83. 83. filebeat.yml output.elasticsearch: hosts: ["elasticsearch:9200"] 01. 02. 83
  84. 84. Container output Show container's STDERR and STDOUT. docker logs [-f] [-t] CONTAINER_ID01. 84
  85. 85. Docker logging driver $ docker info | grep 'Logging Driver' Logging Driver: json-file 01. 02. 85
  86. 86. 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. 86
  87. 87. Stack traces? 87
  88. 88. Useful beats filebeat dockbeat springbeat metricbeat heartbeat packetbeat • • • • • • 88
  89. 89. Summary 89
  90. 90. 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 for the very latest Java 8 and Java 9 for better cgroups- awareness Take care of log aggregation (container's life is short) • • • • • 90
  91. 91. More stuff 91
  92. 92. Demo code http://bit.ly/DOCKERIZED_JAVA_TJUG• 92
  93. 93. Book: Docker for Java Developers 93
  94. 94. Book: Containerizing CD in Java 94
  95. 95. References 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 • • • 95
  96. 96. 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 • • • 96
  97. 97. References Ergonomics Shutdown hook • • 97
  98. 98. 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 • • • 98
  99. 99. Cheatsheets 99
  100. 100. Stickers 100
  101. 101. That's all! 101
  102. 102. Thank you! 102
  103. 103. 103

×