O slideshow foi denunciado.
Utilizamos seu perfil e dados de atividades no LinkedIn para personalizar e exibir anúncios mais relevantes. Altere suas preferências de anúncios quando desejar.

Dockerized Java for Playtech Discussion Club

143 visualizações

Publicada em

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.

Publicada em: Tecnologia
  • Seja o primeiro a comentar

  • Seja a primeira pessoa a gostar disto

Dockerized Java for Playtech Discussion Club

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

×