<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Stephan Hartmann on Medium]]></title>
        <description><![CDATA[Stories by Stephan Hartmann on Medium]]></description>
        <link>https://medium.com/@beffe?source=rss-f284c9388302------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*ScMOWMdZ1RRWMJ0cH46IGg.jpeg</url>
            <title>Stories by Stephan Hartmann on Medium</title>
            <link>https://medium.com/@beffe?source=rss-f284c9388302------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 02 Dec 2026 20:22:04 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@beffe/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Java Application Optimization on Kubernetes on the Example of a Spring Boot Microservice]]></title>
            <link>https://faun.pub/java-application-optimization-on-kubernetes-on-the-example-of-a-spring-boot-microservice-cf3737a2219c?source=rss-f284c9388302------2</link>
            <guid isPermaLink="false">https://medium.com/p/cf3737a2219c</guid>
            <category><![CDATA[docker]]></category>
            <category><![CDATA[troubleshooting]]></category>
            <category><![CDATA[kubernetes]]></category>
            <category><![CDATA[spring-boot]]></category>
            <category><![CDATA[java]]></category>
            <dc:creator><![CDATA[Stephan Hartmann]]></dc:creator>
            <pubDate>Tue, 02 Jul 2019 19:20:43 GMT</pubDate>
            <atom:updated>2019-07-03T09:27:36.680Z</atom:updated>
            <content:encoded><![CDATA[<p>Deploying Microservices on Kubernetes is not as easy as 1–2–3. Many of them suffer from frequent OutOfMemory kills and long startup times. In particular, Java applications with their resource-hungry JVM need special attention.</p><p>In this article we’ll analyse the CPU and memory resource consumption of a Spring Boot microservice during startup and runtime. Then we’ll find out how to tweak resource requests and limits to tune startup and prevent OOM kills.</p><p>And last but not least the curious among you will get an explanation of the mysterious 137 and 142 exit codes ;)</p><h3><strong>But first let’s start with some dry Theory</strong></h3><p>Before you begin to configure your pod you should know that Kubernetes will assign it a Quality of Service class, depending on its resource requests and limits. And there are three of them: <strong><em>Guaranteed</em></strong>, <strong><em>Burstable</em></strong> and <strong><em>Best Effort</em></strong>. When things start to go wrong in your cluster because of insufficient resources, Kubernetes will look at the Quality of Service classes of pods to decide which could be evicted first to free resources.</p><p><strong><em>Note:</em></strong> You can see the class of your pods e.g. with the “describe pod” command of kubectl.</p><p>The decision which Quality of Service class is assigned to a pod is made by its resource requests and resource limits.</p><p>To gain the very best classification — <strong><em>Guaranteed</em></strong> — your pod (or exactly each of its containers) needs to meet the following requirements:</p><ul><li>CPU request and limit must be given and be the same</li><li>Memory request and limit must be given and be the same</li></ul><p><strong><em>Note:</em></strong> if there is no request but only a limit for a resource, then Kubernetes will set a request equal to the limit!</p><p>A pod that becomes classified <strong><em>Burstable</em></strong> does not meet the requirements for Guaranteed but has at least one resource request on one of its containers.</p><p>Pods that neither have a request nor a limit are classified <strong><em>Best Effort</em></strong>.</p><p>The following contracts apply to each class:</p><p><strong>Guaranteed: </strong>will only be killed when it exceeds its memory limit<br><strong>Burstable:</strong> might get killed when node runs out of resources and exceeds requests<br><strong>Best Effort:</strong> first to be killed when node runs out of resources</p><p>You see that it is important to make well-considered settings for a pod’s resource requests and limits to make sure that Kubernetes does not overprovision a node with pods and that your pods won’t get evicted due to resource overuse.</p><h3>Now let’s get hands-on</h3><p>For our analysis I’ve taken a very nice Spring Boot microservice demo from here: <a href="https://github.com/springframeworkguru/springboot_swagger_example">https://github.com/springframeworkguru/springboot_swagger_example</a></p><p>I’ve built the artifact with “mvn package” and created a Docker image with the following Dockerfile:</p><pre>FROM openjdk:alpine<br>COPY spring-boot-web-0.0.1-SNAPSHOT.jar /<br>ENTRYPOINT [&quot;java&quot;, &quot;-jar&quot;, &quot;/spring-boot-web-0.0.1-SNAPSHOT.jar&quot;]</pre><p>Knowing nothing about the resource usage of the microservice, we start googling some examples and reach that page: <a href="https://kubernetes.io/docs/concepts/configuration/manage-compute-resources-container/">https://kubernetes.io/docs/concepts/configuration/manage-compute-resources-container/</a></p><p>Here we find out that a MySQL database gets half a CPU and 128 MB RAM. So we decide to give our microservice the same CPU and a little bit more RAM:</p><pre><strong>resources:<br>  requests:<br>    memory: &quot;512Mi&quot;<br>    cpu: &quot;500m&quot;<br>  limits:<br>    memory: &quot;512Mi&quot;<br>    cpu: &quot;500m&quot;</strong></pre><p>Note that we want to reach <strong><em>Guaranteed</em></strong> Quality of service class so we set requests equal to limits.</p><h3>CPU utilization</h3><p>When we now launch a pod with that microservice, we recognize that it takes a very looooong time to come up — about 60 seconds. That’s really bad. Imagine a production environment where you have scaled your microservice with a deployment to have 10 replicas and you want to deploy a new version with a rolling update. It will take more than 10 minutes to complete! Now imagine that you encounter a problem and need to rollback. Another 10 minutes pass by. You’ll make your DevOps tear their hair.</p><p>But what’s the problem now? The microservice demo is really not that complex, it has an in-momory database and no dependencies.<br>To figure it out we make some experiments with Docker. Let’s start a docker container of your demo app on our local machine, limiting the CPU to 0.5:</p><pre>docker run --cpus 0.5 --rm spring-boot-test:3</pre><p>The result is the same, a startup time of more than 80 seconds:</p><blockquote>2019–03–18 20:24:31.182 INFO 1 — — [ main] g.s.SpringBootWebApplication : Started SpringBootWebApplication in 82.214 seconds (JVM running for 86.134)</blockquote><p>Now we start the container without CPU limit and monitor in a separate terminal window its CPU usage with</p><pre>docker stats</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*rzwt6CHfiQYtrP0yAT6k6g.png" /><figcaption>Top: log output from Spring Boot app during startup. Bottom: output from docker stats</figcaption></figure><p>From the output of docker stats you can see that the Spring Boot app is using more than 3 CPUs during startup!</p><p>And here is the startup duration — only 12 seconds:</p><blockquote>2019–03–18 20:33:45.682 INFO 1 — — [ main] g.s.SpringBootWebApplication : Started SpringBootWebApplication in 11.919 seconds (JVM running for 12.725)</blockquote><p>Here is a table that summarizes startup duration for different CPU limits (in millicores):</p><ul><li>500m — 80 seconds</li><li>1000m — 35 seconds</li><li>1500m — 22 seconds</li><li>2500m — 17 seconds</li><li>3000m — 12 seconds</li></ul><p>We can see that CPU limit directly affects startup times.</p><p>Okay, no problem you say. Just adjust the pod’s CPU resource request and limit to 3 CPUs (3000m). But this approach has two drawbacks. First, your microservice will likely never use that much of CPU power during its normal workload (otherwise you should consider a redesign). But second and much more important, your cluster nodes will be fully occupied with much less pods. E.g. on a node with 8 CPU cores, Kubernetes would only be able to schedule 2 of your pods.</p><p>But what else could we do? First, we should get a rough idea of what is the normal CPU usage of the pod under real conditions. A good tool for that purpose is Prometheus (and Grafana). However you could also try to guess and put a small extra on top.</p><p>Next we set that value as our CPU <strong>request</strong> and leave the CPU limit away! And that’s a really good decision, although you will lose the <strong><em>Guaranteed</em></strong> class.</p><p>Let’s say we configure a CPU request of half a CPU core (500m) for all our microservices. This means that on our 8-CPU-node, Kubernetes could schedule 16 pods. The good thing is, that not all pods might use that CPU power all the time. For example, without any traffic, our demo app uses not even a tenth of a CPU core. So while some of your pods are idling, others could burst their request and use more CPU power (e.g. during startup). Kubernetes won’t kill a pod only if it uses more CPU than requested.</p><blockquote><strong>Excursion on compressible and non-compressible resources</strong><br>CPU is considered a “compressible” resource while memory is “non-compressible”.<br>Compressible means that pods can work with less of the resource although they would like to use more of it. For example, if you deploy a pod with a request of 1 CPU and no limit, it can use more than that if available. But when other pods on the same node get busy, they will have to share the available CPUs and might get throttled back to their request. However, they won’t be evicted and can still do their job.<br>For memory on the other hand, when a pod has a resource request for memory but no limit, it also might use more RAM than requested. However, when resources get low, it can’t be throttled back to use only the requested amount of memory and free up the rest. There is a possiblity that Kubernetes will evict such pods. Therefor it is crucial to always set a memory resource limit and take care that your microservice will never exceed that limit.</blockquote><h3>Memory utilization</h3><p>For RAM our considerations need to be a little different. As noted above, RAM is a non-compressible resource. It is crucial to determine how much memory your microservice will use. Then you should set your requests and limits to that value.</p><p>To make sure that your Java microservice won’t use more RAM than what you have configured, you also need to set some JVM settings accordingly.</p><p>By default, the JVM will take up to a quarter of the physically available RAM for its heap (<a href="https://docs.oracle.com/javase/8/docs/technotes/guides/vm/gc-ergonomics.html">https://docs.oracle.com/javase/8/docs/technotes/guides/vm/gc-ergonomics.html</a>). The problem is that Java 8 will look at the nodes complete memory to compute that value and as a consequence, eventually it will use more RAM than allowed by the pod’s limit.</p><p>A solution might be to use the JVM switch</p><blockquote>-XX:+UseCGroupMemoryLimitForHeap</blockquote><p>However, this will result in a waste of RAM — remember that then it will take only a quarter of what you have configured as limit.</p><p>In the end it is best practice to determine the needed heap space and the total RAM consumption by monitoring your microservice for some time. Then set the heap with <strong>-Xmx</strong> and the total RAM with <strong>-XX:MaxRAM</strong>.</p><p>More background information about Java 8 in a container: <a href="https://developers.redhat.com/blog/2017/04/04/openjdk-and-containers/">https://developers.redhat.com/blog/2017/04/04/openjdk-and-containers/</a></p><h3>Readiness and Livenss probes</h3><p>For a detailed explanation, see <a href="https://medium.com/faun/understanding-how-kubernetes-readiness-and-liveness-probes-do-correlate-or-better-how-not-81d0ad15fd39">https://medium.com/faun/understanding-how-kubernetes-readiness-and-liveness-probes-do-correlate-or-better-how-not-81d0ad15fd39</a></p><h3>Trouble shooting pod restarts</h3><p>Trouble shooting your microservice can be a hassle. If you see that one of your pods has frequent restarts, you will likely use <strong><em>kubectl describe pod</em></strong> to find out what caused the restart.</p><p>If the reason is OOMKilled then it is quite clear. Your pod has used more RAM than its limit and was evicted. Tune your JVM settings or the pod’s memory limit.</p><p>However, there are some more subtile causes that might be not so clear, especially when you see an exit code of 137 or 143 which occurs quite often for a Java microservice.</p><blockquote><strong>Excursion: Java exit codes<br></strong>A Java application can terminate explicitely and return a concrete exit code by calling <strong>System.exit(n)</strong> where n is the exit code.<br>However, it can also terminate on receiving external signals like SIGTERM or SIGKILL. In that case, the JVM will exit with a code calculated by <br>EXIT-CODE = 128 + SIGNAL-CODE<br>The code for SIGKILL is 9 whereas the code for SIGTERM is 15.</blockquote><p>So when your microservice has exited with code 137, you know now that it received a kill signal (SIGKILL). With code 143 it received a term signal (SIGTERM).<br>The difference is that your Java application can intercept a SIGTERM with a shutdown hook and perform some cleanup while it cannot intercept a SIGKILL.</p><p>Kubernetes will always first send a SIGTERM to give your pod the chance to shutdown gracefully. Then if your pod is still running it will send a SIGKILL.</p><p>Process of termination:</p><ul><li>Run preStop hooks</li><li>send SIGTERM to containers</li><li>wait for a grace period of by default 30 seconds to give the containers time for cleanup</li><li>Still running after grace period =&gt; send SIGKILL to containers</li></ul><p>The most common unplanned reasons why kubernetes would terminate a pod are:</p><ul><li>node runs out of resources</li><li>liveness probe failed (is the initialDelaySeconds value of the probe lower than the startup duration? Maybe because of a CPU limit? Hint: don’t use t-instance-types on AWS in production!)</li></ul><p>For completeness, there are also some common reasons for planned terminations:</p><ul><li>rolling update of a deployment</li><li>draining a node</li></ul><h3>Conclusions and Recommendations</h3><ul><li>Use CPU requests, but no limit</li><li>Use memory limit equal to memory request</li><li>Set JVM Heap, e.g. 80 percent of the memory limit</li><li>Set JVM MaxRAM near to memory limit</li><li>Implement readiness and liveness probes, ideally not just a simple health endpoint but one for each purpose</li><li>Implement a shutdown hook</li></ul><h3>Links</h3><p>Java signals, shutdown hooks and exit codes:<br><a href="http://journal.thobe.org/2013/02/jvms-and-kill-signals.html">http://journal.thobe.org/2013/02/jvms-and-kill-signals.html</a></p><p>openjdk in a container:<br><a href="https://developers.redhat.com/blog/2017/04/04/openjdk-and-containers/">https://developers.redhat.com/blog/2017/04/04/openjdk-and-containers/</a></p><p>Java memory calculation:<br><a href="https://docs.oracle.com/javase/8/docs/technotes/guides/vm/gc-ergonomics.html">https://docs.oracle.com/javase/8/docs/technotes/guides/vm/gc-ergonomics.html</a></p><p>Kubernetes: termination of pods:<br><a href="https://kubernetes.io/docs/concepts/workloads/pods/pod/#termination-of-pods">https://kubernetes.io/docs/concepts/workloads/pods/pod/#termination-of-pods</a></p><p>Kubernetes: readiness and liveness probes:<br><a href="https://kubernetes.io/docs/tasks/configure-pod-container/configure-liveness-readiness-probes/">https://kubernetes.io/docs/tasks/configure-pod-container/configure-liveness-readiness-probes/</a></p><p>Kubernetes: Quality of Service:<br><a href="https://kubernetes.io/docs/tasks/configure-pod-container/quality-service-pod/">https://kubernetes.io/docs/tasks/configure-pod-container/quality-service-pod/</a></p><p>Kubernetes: managing compute resources (limits and requests):<br><a href="https://kubernetes.io/docs/concepts/configuration/manage-compute-resources-container/">https://kubernetes.io/docs/concepts/configuration/manage-compute-resources-container/</a></p><p>Spring Boot example app:<br><a href="https://github.com/springframeworkguru/springboot_swagger_example">https://github.com/springframeworkguru/springboot_swagger_example</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=cf3737a2219c" width="1" height="1" alt=""><hr><p><a href="https://faun.pub/java-application-optimization-on-kubernetes-on-the-example-of-a-spring-boot-microservice-cf3737a2219c">Java Application Optimization on Kubernetes on the Example of a Spring Boot Microservice</a> was originally published in <a href="https://faun.pub/">FAUN.dev() 🐾</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Understanding How Kubernetes Readiness and Liveness Probes do Correlate — or Better How Not]]></title>
            <link>https://faun.pub/understanding-how-kubernetes-readiness-and-liveness-probes-do-correlate-or-better-how-not-81d0ad15fd39?source=rss-f284c9388302------2</link>
            <guid isPermaLink="false">https://medium.com/p/81d0ad15fd39</guid>
            <category><![CDATA[kubernetes]]></category>
            <category><![CDATA[microservices]]></category>
            <category><![CDATA[containers]]></category>
            <category><![CDATA[orchestration]]></category>
            <dc:creator><![CDATA[Stephan Hartmann]]></dc:creator>
            <pubDate>Thu, 28 Feb 2019 10:58:07 GMT</pubDate>
            <atom:updated>2019-07-19T16:37:24.155Z</atom:updated>
            <content:encoded><![CDATA[<h3>Understanding How Kubernetes Readiness and Liveness Probes do Correlate — or Better How Not</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*cInkA8QiusTwQxg3" /><figcaption>“An overhead shot of Saint Peter Port with shipping containers next to the calm sea.” by <a href="https://unsplash.com/@enrapture?utm_source=medium&amp;utm_medium=referral">Enrapture Media</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><h3>The misunderstanding</h3><p>There seems to be a widely spread misunderstanding of how readiness and liveness probes in k8s belong together. Many people <a href="https://github.com/kubernetes/kubernetes/issues/27114">think</a> that those probes are tested in a successive manner.</p><ol><li>start pod — container initializes, maybe for a long time (Spring boot, sigh)</li><li>perform readiness probe periodically until it succeeds -&gt; pod is ready</li><li>start performing liveness probe periodically until it fails -&gt; kill pod</li></ol><p>In other words, they think there is an initialization phase, then there is a ready state and then there is a liveness phase until the pod dies or gets deleted.</p><p>What people confuses here most is that k8s may start to perform liveness probes although the pod is still in his initialization phase and has not become ready, yet — and hence gets killed.</p><h3>How it really works</h3><p>The key point is that both types of probes don’t belong together at all — they serve totally different purposes.</p><p>The <strong>readiness probe</strong> checks if a pod is able to serve requests. If it succeeds, k8s will route traffic to the pod. If it fails, k8s won’t route traffic to it. Of course, during the start up phase of a pod (e.g. Spring boot initialization) it is not yet ready to serve requests. However, a pod may also become later during his lifetime not ready. An unreachable database might be the reason. The pod itself has no problems and could do its work, but because of its dependency it can’t. No reason to kill the pod as it won’t solve the problem. Instead, stop routing traffic to the pod but keep checking with the readiness probe. When it succeeds, the pod is ready again and can server requests.</p><p>In contrast, the <strong>liveness probe</strong> checks if a pod is alive. If it fails, the pod is assumed to have serious problems and won’t recover. Thus the pod will be killed.</p><h3>Conclusion</h3><p>Now that we understand for which different purposes the two probes serve, we can see that it would be best to have two different endpoints (or commands) to check for — one for readiness and one for liveness.</p><p>In real life, apps usually provide only one health endpoint. If you use that endpoint for both probes, you just have to consider two things: <br>For the readiness probe, choose a small value for initialDelaySeconds — no matter how often it fails, eventually it will succeed and the pod will be supposed to be ready and get traffic. <br>For the liveness probe, choose a big value for initialDelaySeconds to give the pod enough time to initialize (e.g. Spring boot).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/727/0*Piks8Tu6xUYpF4DU" /></figure><p><strong>Follow us on </strong><a href="https://twitter.com/joinfaun"><strong>Twitter</strong></a><strong> </strong>🐦<strong> and </strong><a href="https://www.facebook.com/faun.dev/"><strong>Facebook</strong></a><strong> </strong>👥<strong> and join our </strong><a href="https://www.facebook.com/groups/364904580892967/"><strong>Facebook Group</strong></a><strong> </strong>💬<strong>.</strong></p><p><strong>To join our community Slack </strong>🗣️ <strong>and read our weekly Faun topics </strong>🗞️,<strong> click here⬇</strong></p><figure><a href="https://www.faun.dev/join/?utm_source=medium.com%2Ffaun&amp;utm_medium=medium&amp;utm_campaign=faunmediumbanner"><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*oSdFkACJxs5iy1oR" /></a></figure><h4>If this post was helpful, please click the clap 👏 button below a few times to show your support for the author! ⬇</h4><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=81d0ad15fd39" width="1" height="1" alt=""><hr><p><a href="https://faun.pub/understanding-how-kubernetes-readiness-and-liveness-probes-do-correlate-or-better-how-not-81d0ad15fd39">Understanding How Kubernetes Readiness and Liveness Probes do Correlate — or Better How Not</a> was originally published in <a href="https://faun.pub/">FAUN.dev() 🐾</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>