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 - JEEConf 2017

751 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 - JEEConf 2017

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

×