# Workload HA

## General Principles of Application High Availability

* Applications adhere to [The Twelve-Factor App](https://12factor.net/zh_cn/)
* Employ Services and deploy applications with multiple replica Pods
* Multiple replicas use anti-affinity to avoid application failure due to single node faults
* Utilize PodDisruptionBudget to prevent application downtime resulting from evictions
* Use preStopHook and health check probes to ensure smooth service updates

## Graceful Shutdown

Configure terminationGracePeriodSeconds for Pods and use a preStop hook to delay the shutting down of container applications to avoid application interruptions during events such as `kubectl drain`:

```yaml
restartPolicy: Always
terminationGracePeriodSeconds: 30
containers:
- image: nginx
  lifecycle:
    preStop:
      exec:
        command: [
          "sh", "-c",
          # Introduce a delay to the shutdown sequence to wait for the
          # pod eviction event to propagate. Then, gracefully shutdown
          # nginx.
          "sleep 5 && /usr/sbin/nginx -s quit",
        ]
```

For the detailed principle, you can refer to the following series of articles:

* [1. Zero Downtime Server Updates For Your Kubernetes Cluster](https://blog.gruntwork.io/zero-downtime-server-updates-for-your-kubernetes-cluster-902009df5b33)
* [2. Gracefully Shutting Down Pods in a Kubernetes Cluster](https://blog.gruntwork.io/gracefully-shutting-down-pods-in-a-kubernetes-cluster-328aecec90d)
* [3. Delaying Shutdown to Wait for Pod Deletion Propagation](https://blog.gruntwork.io/delaying-shutdown-to-wait-for-pod-deletion-propagation-445f779a8304)
* [4. Avoiding Outages in Your Kubernetes Cluster Using PodDisruptionBudgets](https://blog.gruntwork.io/avoiding-outages-in-your-kubernetes-cluster-using-poddisruptionbudgets-ef6a4baa5085)

## Reference Documents

* [Kubernetes Pod Lifecycle](https://kubernetes.io/docs/concepts/workloads/pods/pod-lifecycle/)
* [Kubernetes PodDisruptionBudget](https://kubernetes.io/docs/concepts/workloads/pods/disruptions/)

***

Ensuring that your apps never miss a beat, even while you update your digital orchestra, is what high availability is all about. Let's consider the finely-tuned best practices:

* Treat your applications to the modern methodology of [The Twelve-Factor App](https://12factor.net/zh_cn/), ensuring they're as resilient as they are refined.
* Set up your software symphony using Services and a chorus of multiple replica Pods, each singing the same tune.
* Should one Pod face the music alone, anti-affinity keeps the performance going by avoiding single node failures that hit a sour note.
* Employ PodDisruptionBudget like a metronome, keeping rhythm and preventing evictions from throwing you off-beat.
* PreStopHook and health check probes act like the meticulous conductor, guaranteeing each transition is as smooth as the last.

When it's time for a swan song, here's how you can bring the curtain down on your Pods with elegance and poise:

Set the stage with `terminationGracePeriodSeconds` and cue the preStop hook to dim the lights gently. This way, when the crowd gets rowdy with `kubectl drain` commands, your application can exit stage left without dropping a line.

```yaml
restartPolicy: Always
terminationGracePeriodSeconds: 30
containers:
- image: nginx
  lifecycle:
    preStop:
      exec:
        command: [
          "sh", "-c",
          # This little intermission gives your Pods time to pass the news
          # of their departure. Then, like the grand finale of a symphony,
          # Nginx takes a graceful bow.
          "sleep 5 && /usr/sbin/nginx -s quit",
        ]
```

Intrigued by the mechanics behind the show? Curtain up! Peek behind the scenes with this enlightening series of articles:

* Enlightening [Series 1](https://blog.gruntwork.io/zero-downtime-server-updates-for-your-kubernetes-cluster-902009df5b33): No intermissions in your server performance.
* Graceful [Series 2](https://blog.gruntwork.io/gracefully-shutting-down-pods-in-a-kubernetes-cluster-328aecec90d): Pods that know how to exit the stage properly.
* Timely [Series 3](https://blog.gruntwork.io/delaying-shutdown-to-wait-for-pod-deletion-propagation-445f779a8304): Ensuring every Pod gets the memo before the lights go out.
* Safeguarding [Series 4](https://blog.gruntwork.io/avoiding-outages-in-your-kubernetes-cluster-using-poddisruptionbudgets-ef6a4baa5085): Keep the show going, no matter what happens offstage.

For the theory and technical know-how that underpins your application's resilience, sift through these reference documents:

* [Understanding Pod Lifecycle](https://kubernetes.io/docs/concepts/workloads/pods/pod-lifecycle/): The biology of your digital creatures.
* [Navigating Pod Disruptions](https://kubernetes.io/docs/concepts/workloads/pods/disruptions/): When the digital seas get rough, keep your Pods afloat.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://kubernetes.feisky.xyz/en/practice/app-ha.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
