← Back to list

CKA-40-Graceful Shutdown in Kubernetes

Implement graceful Kubernetes shutdown with termination signals, lifecycle hooks, and traffic draining.

CloudOpsBlog.com · 2026-07-31 00:00 · 1 claps · 2.7 min read paywalled
#cka #pods #kubernetes #graceful-shutdown #sre
Open on Medium ↗
Wiki topics: ☁️ · DevOps & Cloud

CKA-40-Graceful Shutdown in Kubernetes

Implement graceful Kubernetes shutdown with termination signals, lifecycle hooks, and traffic draining.

For CKA preparation, do not treat this as a definition to memorize. Treat it as a cluster behavior you should be able to inspect, configure, and verify under time pressure. This guide explains the operational purpose, the important moving parts, and a safe way to reason about it.

What you will learn: how graceful shutdown in kubernetes fits into workloads and scheduling, what to check first, and the mistakes that commonly lead to failed changes.

Letting Pods finish useful work before they disappear

Pods are terminated during deployments, scaling, node maintenance, and failures. If an application stops instantly, active requests may fail and queued work may be lost. Graceful shutdown gives the process time to stop accepting new work and finish what it has already started.

When Kubernetes terminates a Pod, it begins the grace period, runs any preStop hook, and sends the container's main process a termination signal. After the grace period expires, remaining processes are forcefully killed.

apiVersion: apps/v1
kind: Deployment
metadata:
  name: graceful-api
spec:
  replicas: 3
  selector:
    matchLabels:
      app: graceful-api
  template:
    metadata:
      labels:
        app: graceful-api
    spec:
      terminationGracePeriodSeconds: 45
      containers:
        - name: api
          image: example/api:1.0
          ports:
            - name: http
              containerPort: 8080
          readinessProbe:
            httpGet:
              path: /health/ready
              port: http
            periodSeconds: 5
          lifecycle:
            preStop:
              exec:
                command: ["sh", "-c", "sleep 5"]
          resources:
            requests:
              cpu: 100m
              memory: 128Mi
            limits:
              memory: 256Mi

The application should handle SIGTERM. A web server typically marks itself unready or stops accepting new connections, finishes in-flight requests, closes background workers, flushes telemetry, and exits before the deadline.

The short preStop delay sometimes gives endpoint updates time to propagate before the process exits. It is not a substitute for signal handling, and the hook consumes part of the same grace period.

One frequent container mistake is wrapping the application in a shell that does not forward signals. Use an exec-form container command or ensure the entrypoint passes signals to the real process.

Choose the grace period from actual behavior. A request-driven API may need tens of seconds; a worker processing long jobs may need checkpointing or a queue lease design rather than an enormous timeout.

Test shutdown deliberately:

kubectl delete pod <pod-name>
kubectl logs -f <pod-name>

Also observe a real rolling update under load. Look for connection errors, unfinished jobs, and termination messages.

Graceful shutdown connects application code with Kubernetes lifecycle behavior. Kubernetes can offer time and signals, but only the application knows how to leave cleanly. A reliable service is not merely one that starts well; it also knows how to say goodbye.

Practical CKA Workflow

When you work with this topic in a live cluster, use a repeatable approach:

  1. Inspect the workload specification and the events emitted by the scheduler or controller.
  2. Confirm labels, resource requests, and node-level constraints with kubectl.
  3. Make one small declarative change, then watch the rollout until the desired state is reached.

Useful starting commands are:

kubectl get pods -A
kubectl get events -A --sort-by=.lastTimestamp
kubectl describe <resource-type> <resource-name> -n <namespace>

Replace the placeholders with the resource relevant to the task. The goal is to use evidence from the API server rather than guessing from a single symptom.

Common Mistakes

  • Changing several variables at once and losing the ability to identify the actual cause.
  • Using a cluster-wide setting when a namespace- or workload-scoped change is sufficient.
  • Applying a manifest without checking labels, selectors, names, and the resulting events.
  • Assuming a resource is healthy because it exists; always verify its observed state and its effect on the workload.

CKA Exam Takeaway

In the exam, read the requested outcome carefully, make the smallest declarative change that achieves it, and validate it with kubectl. A correct manifest is only half the task—the cluster must reach the intended state.

Originally published at https://cloudopsblog.com on July 31, 2026.


메타데이터
post_id
6fb8fb8bacd8
slug
cka-40-graceful-shutdown-in-kubernetes-6fb8fb8bacd8
url
https://medium.com/@manojkumarcloud/cka-40-graceful-shutdown-in-kubernetes-6fb8fb8bacd8
canonical_url
https://medium.com/@manojkumarcloud/cka-40-graceful-shutdown-in-kubernetes-6fb8fb8bacd8
author_url
https://medium.com/@manojkumarcloud
status
ok
fetched_at
2026-08-23 21:37:58