A healthy diet for your Java application Devoxx France.pdf
Spring Boot Loves K8s
1. Spring Boot loves k8s
September 2–3, 2020
springone.io
#session-spring-boot-loves-k8s on Slack
1
Stéphane Nicoll @snicoll
Brian Clozel @bclozel
2. Safe Harbor Statement
The following is intended to outline the general direction of VMware's offerings. It is intended for
information purposes only and may not be incorporated into any contract. Any information regarding
pre-release of VMware offerings, future updates or other planned modifications is subject to ongoing
evaluation by VMware and is subject to change. This information is provided without warranty or any
kind, express or implied, and is not a commitment to deliver any material, code, or functionality, and
should not be relied upon in making purchasing decisions regarding VMware's offerings. These
purchasing decisions should only be based on features currently available. The development, release,
and timing of any features or functionality described for VMware's offerings in this presentation remain
at the sole discretion of Pivotal. Pivotal has no obligation to update forward looking information in this
presentation.
2
6. Agenda
Showcasing Spring Boot 2.3 features during a live coding session
● The “Scribe” application
● Efficient Container images
● Liveness and Readiness
● Graceful Shutdown
● Next in Spring Boot 2.4+
6
13. Very Basic Dockerfile for Java apps
10
FROM openjdk:8-jre-alpine
WORKDIR application
ARG JAR_FILE=scribe/target/scribe-*.jar
COPY ${JAR_FILE} application.jar
ENTRYPOINT ["java","-jar","application.jar"]
14. Layers in Container images
11
FROM … [FROM CACHE]
COPY … [FROM CACHE]
COPY … [REBUILDING]
RUN …
Layers represent changes/intermediate images (like git commits)
When rebuilding an image, layers
can be cached and reused if their
content did not change.
15. Optimizing Container images
12
FROM … [FROM CACHE]
COPY application.jar [REBUILDING]
Regrouping resources with similar lifecycle will improve
image build time and storage efficiency.
In our current Dockerfile, the entire
layer needs to be rebuilt for any
change in the application
20. Layered container images
15
FROM …
COPY --from=builder source/snapshot-dependencies/ ./
COPY --from=builder source/spring-boot-loader/ ./
COPY --from=builder source/dependencies/ ./
Spring Boot can build efficient container images.
Developers can customize
the layer arrangement and
reorder, add their own…
Using the Maven/Gradle
build plugins.
COPY --from=builder source/application/ ./
21. Spring Boot + Buildpacks
No Dockerfile required!
Build locally with the CLI:
16
./mvnw spring-boot:build-image
./gradlew bootBuildImage
Also, delegate to your company’s builder instance in your build pipeline.
22. Cloud Native Buildpacks
With buildpacks.io, you can delegate container image building for:
• Balanced control between app devs and platform operators
• Security and compliance requirements handled in one place
• Easier maintenance/upgrades
17
23. What about other solutions?
Buildpacks, Dockerfiles, Jib, s2i, ko…
They have different approaches and features,
see https://buildpacks.io/features/
Choose what fits best for your team!
18
24. Next: Spring to image
Later today, check out the “Spring to image” session with Ben Hale.
https://springone.io/2020/sessions/spring-to-image
This session will cover Spring Boot build plugins, the pack CLI, the kpack
Kubernetes service and more!
19
26. Liveness and Readiness
1. New concepts supported in Spring Boot core.
2. Complement the existing application Health support in Actuator.
3.Empower developers with ApplicationAvailability.
21
27. Liveness and Readiness states
Liveness
“CORRECT” if the internal state of
the app is fine. External
dependencies or the service
itself might not respond
correctly still.
“BROKEN” if the internal state of
the application is broken and
restarting is the only way to fix
it.
22
Readiness
“ACCEPTING_TRAFFIC” is a way
to tell load balancers that the
app is ready to serve requests.
The app is “REFUSING_TRAFFIC”
if it considers that its load is too
high, or depending on its
lifecycle stage (starting up,
shutting down).
28. Liveness and Readiness Probes
Liveness and Readiness states are available as health groups.
Enabled automatically on k8s, or with the config property:
23
https://example.org/actuator/health/liveness
https://example.org/actuator/health/readiness
Probes are available here and in sync with the application lifecycle:
management.endpoint.health.probes.enabled=true
29. Probes as Health Groups
Liveness and readiness probes are using the Health Groups feature.
You can configure additional checks to a probe:
24
management.endpoint.health.group.readiness.include=readinessState,customCheck
⚠ You should be careful about checking for external state in probes.
30. ApplicationAvailability
25
@Component
public class LocalCacheVerifier {
private final ApplicationEventPublisher eventPublisher;
public void checkLocalCache() {
try { //...
} catch (CacheCompletelyBrokenException ex) {
AvailabilityChangeEvent.publish(this.eventPublisher, ex, LivenessState.BROKEN);
}
}
} // see more in reference docs
32. Shutting down app with active requests
27
Scribe
MarkdownConverter
MarkdownConverter
MarkdownConverter
HTTP POST /convert
1. Processing…
33. Shutting down app with active requests
27
Scribe
MarkdownConverter
MarkdownConverter
MarkdownConverter
HTTP POST /convert
1. Processing…
2. Shutdown!
40. Config file processing
Volume mounted configuration trees, multi-document properties files,
profile groups, etc. See config file processing blog post.
31
spring.config.activate.on-cloud-platform=kubernetes
spring.config.import=configtree:/etc/config
spring.config.activate.on-cloud-platform=kubernetes
spring.config.import=configtree:/etc/config
spring.config.activate.on-cloud-platform=kubernetes
spring.config.import=configtree:/etc/config
spring.config.activate.on-cloud-platform=kubernetes
spring.config.import=configtree:/etc/config
spring.config.activate.on-cloud-platform=kubernetes
spring.config.import=configtree:/etc/config
etc/
+- config/
+- my/
| +- application
+- test
41. Graceful shutdown improvements
The team is considering adding a shutdown delay option because most
platforms still route traffic to while routing state is converging.
Currently you can use a PreStop hook to delay the shutdown sequence.
32
42. Layered JARs enabled by default
As of 2.4.0, Spring Boot JARs will ship with the layers.idx by default.
Useful metadata for tools in your CI/CD pipeline.
33
43. Stay Connected.
Q&A #session-spring-boot-loves-k8s on Slack
“Spring to Image” session with Ben Hale
Slides and code:
https://springone.io/2020/sessions/spring-boot-loves-k8s
https://github.com/snicoll/spring-boot-loves-k8s
#springone@s1p