Kubernetes
Pod Termination
Exit Code 0
Completed State
Container Orchestration

Why Kubernetes Pod gets into Terminated state giving Completed reason and exit code 0?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Kubernetes, the popular container orchestration platform, has a range of features to help manage and deploy applications. One common concept within Kubernetes is the Pod, which is the smallest deployable unit. Sometimes a Pod may enter a Terminated state with the reason Completed and exit code 0 . This indicates a successful and expected termination of the Pod. Let's explore why this occurs, including a deeper technical explanation, examples, and potential related scenarios.

Understanding Pod Lifecycle

In Kubernetes, a Pod goes through various states as it is scheduled, running, and ultimately terminated. The key phases in a Pod's lifecycle include:

  • Pending: The Pod has been accepted by the Kubernetes system, but one or more containers have not been created.
  • Running: The Pod has been bound to a node and all containers have been created. At least one container is still running, or is in the process of starting or restarting.
  • Succeeded: All containers in the Pod have terminated voluntarily with a zero exit code, and the system is not going to restart the Pod.
  • Failed: All containers in the Pod have terminated, and at least one container has terminated in failure, either because of an error or explicitly terminated.
  • Unknown: The state of the Pod could not be obtained, typically due to an error in communication with the host of the Pod.

The state Terminated with reason Completed typically falls under the Succeeded phase and signifies a normal completion of the Pod's task.

Reasons for Pod Termination with Completed

Reason

  1. Completion of Tasks:
    • A Pod may be designed to perform a batch processing job or a short-lived task, such as running a script to process data. Once the task is completed successfully, Kubernetes marks the Pod as Terminated with a reason of Completed and an exit code of 0 .
  2. Job Resource:
    • Kubernetes provides native support for executing jobs through the Job resource. A Job creates one or more Pods to carry out a specified function. Upon successful execution (all Pods have completed their tasks correctly), the Pods enter the Succeeded state, resulting in a Completed reason and exit code 0 .
  3. CronJob:
    • Similar to a Job , a CronJob allows running tasks at regular intervals based on a schedule. Each execution of a CronJob creates a new Job , which in turn runs its Pod(s) to completion. Successfully terminated Pods note a Completed reason.
  4. Container Exit:
    • Individual containers in Pods can be programmed to exit after accomplishing their function. Such containers should return an exit code of 0 (indicating success), and if all containers in the Pod behave this way, the whole Pod is marked as Terminated with reason Completed .

Example Scenario

Batch Processing Pod

A simple use case could be a Pod tasked with moving data from one database table to another. The process involves:

  • Pod setup with a container running a script.
  • Script connects to source and destination databases.
  • Data is fetched and inserted into the target table.

Once the script finishes without any errors and exits (exit 0 ), Kubernetes recognizes this successful completion. The Pod Status transitions to Terminated with:

  • Reason: Completed
  • Exit Code: 0

YAML snippet defining such a task might look like:

  • name: data-transfer
  • Pod Logs: Assess logs to confirm the activity performed as intended without issues.
  • Monitoring: Tools like Prometheus or built-in Kubernetes metrics should be used to monitor jobs and ensure they complete successfully over time.
  • Re-use of Resources: Implement potential cleanup strategies if the Succeeded Pods accumulate.

Course illustration
Course illustration

All Rights Reserved.