<p>I use <strong>Kubernetes</strong> in my project, specially <strong>HPA</strong>. So, every minute in project we started <code>check-status</code> request for checking if all microservices are available. Availability is defined by simple response from one of replicas (not all) each microservice. <br />
But I have one moment related to HPA. When HPA automatically decides to remove some pods from cluster and my <code>check-status</code> request comes to server at the same time then very often occurs that my API-gateway service push it to deleted pod and doesn't get any response. It means that microservice is unavailable for our server.<br />
My question is what is the best way for setting autoscaler to avoid this cases.</p>

<p>It is not related to HPA in this case but more on how you graceful shut down your pods.</p>
<p>In short, your service/LB is not aware if your pod is ready to accept new requests, so on a <code>SIGTERM</code> signal, your pod should set your <code>readiness probe</code> to false, and give some time for the app to shutdown. If your readiness probe is not healthy, the service won't send new requests to your pod.
Then you can shut it down once all requests have been addressed AND the pod won't receive new requests.</p>
<p>I would advise you of reading these sources:</p>
<ul>
<li><a href="https://cloud.google.com/blog/products/containers-kubernetes/kubernetes-best-practices-terminating-with-grace" rel="nofollow noreferrer">https://cloud.google.com/blog/products/containers-kubernetes/kubernetes-best-practices-terminating-with-grace</a></li>
<li><a href="https://pracucci.com/graceful-shutdown-of-kubernetes-pods.html" rel="nofollow noreferrer">https://pracucci.com/graceful-shutdown-of-kubernetes-pods.html</a></li>
</ul>
