Skip to main content
Redhat Developers  Logo
  • Products

    Featured

    • Red Hat Enterprise Linux
      Red Hat Enterprise Linux Icon
    • Red Hat OpenShift AI
      Red Hat OpenShift AI
    • Red Hat Enterprise Linux AI
      Linux icon inside of a brain
    • Image mode for Red Hat Enterprise Linux
      RHEL image mode
    • Red Hat OpenShift
      Openshift icon
    • Red Hat Ansible Automation Platform
      Ansible icon
    • Red Hat Developer Hub
      Developer Hub
    • View All Red Hat Products
    • Linux

      • Red Hat Enterprise Linux
      • Image mode for Red Hat Enterprise Linux
      • Red Hat Universal Base Images (UBI)
    • Java runtimes & frameworks

      • JBoss Enterprise Application Platform
      • Red Hat build of OpenJDK
    • Kubernetes

      • Red Hat OpenShift
      • Microsoft Azure Red Hat OpenShift
      • Red Hat OpenShift Virtualization
      • Red Hat OpenShift Lightspeed
    • Integration & App Connectivity

      • Red Hat Build of Apache Camel
      • Red Hat Service Interconnect
      • Red Hat Connectivity Link
    • AI/ML

      • Red Hat OpenShift AI
      • Red Hat Enterprise Linux AI
    • Automation

      • Red Hat Ansible Automation Platform
      • Red Hat Ansible Lightspeed
    • Developer tools

      • Red Hat Trusted Software Supply Chain
      • Podman Desktop
      • Red Hat OpenShift Dev Spaces
    • Developer Sandbox

      Developer Sandbox
      Try Red Hat products and technologies without setup or configuration fees for 30 days with this shared Openshift and Kubernetes cluster.
    • Try at no cost
  • Technologies

    Featured

    • AI/ML
      AI/ML Icon
    • Linux
      Linux Icon
    • Kubernetes
      Cloud icon
    • Automation
      Automation Icon showing arrows moving in a circle around a gear
    • View All Technologies
    • Programming Languages & Frameworks

      • Java
      • Python
      • JavaScript
    • System Design & Architecture

      • Red Hat architecture and design patterns
      • Microservices
      • Event-Driven Architecture
      • Databases
    • Developer Productivity

      • Developer productivity
      • Developer Tools
      • GitOps
    • Secure Development & Architectures

      • Security
      • Secure coding
    • Platform Engineering

      • DevOps
      • DevSecOps
      • Ansible automation for applications and services
    • Automated Data Processing

      • AI/ML
      • Data Science
      • Apache Kafka on Kubernetes
      • View All Technologies
    • Start exploring in the Developer Sandbox for free

      sandbox graphic
      Try Red Hat's products and technologies without setup or configuration.
    • Try at no cost
  • Learn

    Featured

    • Kubernetes & Cloud Native
      Openshift icon
    • Linux
      Rhel icon
    • Automation
      Ansible cloud icon
    • Java
      Java icon
    • AI/ML
      AI/ML Icon
    • View All Learning Resources

    E-Books

    • GitOps Cookbook
    • Podman in Action
    • Kubernetes Operators
    • The Path to GitOps
    • View All E-books

    Cheat Sheets

    • Linux Commands
    • Bash Commands
    • Git
    • systemd Commands
    • View All Cheat Sheets

    Documentation

    • API Catalog
    • Product Documentation
    • Legacy Documentation
    • Red Hat Learning

      Learning image
      Boost your technical skills to expert-level with the help of interactive lessons offered by various Red Hat Learning programs.
    • Explore Red Hat Learning
  • Developer Sandbox

    Developer Sandbox

    • Access Red Hat’s products and technologies without setup or configuration, and start developing quicker than ever before with our new, no-cost sandbox environments.
    • Explore Developer Sandbox

    Featured Developer Sandbox activities

    • Get started with your Developer Sandbox
    • OpenShift virtualization and application modernization using the Developer Sandbox
    • Explore all Developer Sandbox activities

    Ready to start developing apps?

    • Try at no cost
  • Blog
  • Events
  • Videos

Container starting and termination order in a pod

May 19, 2025
Daein Park
Related topics:
ContainersKubernetes
Related products:
Red Hat OpenShift

Share:

    To configure a multi-container application that has dependencies within a single pod, you should control the order in which they start up or shut down to prevent race conditions (e.g., if container A is required to start after container B, or container A needs to delay termination after container B). A classic use case is the Istio proxy, a sidecar container that all traffic traverses through from the main containers. It should start prior to the main containers accepting traffic and terminated after the primary containers drain all remaining traffic.

    In this article, I will take a deeper look into container startup and termination order as a part of the pod lifecycle explained in the Kubernetes upstream documentation. This topic is not brand new. However, it is worth checking out the latest Red Hat OpenShift release notes for any changes.

    The container's lifetime in a pod

    You can organize a normal pod with three container types, each having a lifetime (Figure 1).

    Each container type lifetime in a Pod lifecyle diagram
    Figure 1: The diagram shows us the lifetime of three container types in a pod lifecycle.

    Be aware that the starting order is based on the creation time of each container. Conversely, the termination order is based on issuing the order of the SIGTERM signal to each container. The following table illustrates the order in which each container type starts and stops.

    Type

    Starting order

    Termination order

    Description

    Init containers

    Sequential, one by one in the order defined

    Sequential, one by one in the order defined

    Executed and completed one after the other before the main containers run

    Sidecar containers

    Sequential, one by one in the order defined

    In the opposite order, after all main containers stop

    Run along with the main containers after executed from the Initialization phase

    Main containers

    Sequential without blocking the next 

    Random, due to the time it takes for the termination process to complete

    Run main application processes here

    Next, let’s look into the details of how each container type starts and stops.

    Init containers

    Init containers must start up successfully before the next one starts. This specification makes the starting and termination order sequential. Typical use cases for init containers are for any pre-processes that need to take place prior to running the main containers.

    For instance, all the init containers in the following pod A case would start and terminate A, B, and C in order, and each init container must terminate successfully as a one-off task before the main containers start. If one of the init containers fails, the next one in the list will not start. Then the pod enters an error state, such as Init: Error and Init: CrashLoopBackOff. Examine the pod B case in Figure 2 where this situation is depicted. Init container C did not start after init container B failed. In addition, the main container did not start.

    Init Containers starting order diagram
    Figure 2: This diagram shows the init containers starting sequentially and running to completion.

    Sidecar containers

    Recent versions of Kubernetes as of 1.29 (Red Hat OpenShift 4.16) added the sidecar container type. Both init containers and sidecar containers have similar configurations. However, a key difference between the two is that sidecar containers will set restartPolicy: Always so the container will keep running after the main containers start. This behavior is useful for supporting sidecar-based architectures. The starting order is sequential. If one has not started successfully, the next container will not start, as shown in the prior init containers section. Even if you mix other init containers together, they start in the order specified in the spec.initContainers array in the pod manifest.

    Take a look at Figure 3. Sidecar containers A, B, and C are each started one by one and then run along with the main containers. If any sidecar container or init container fails, the main containers will not start. 

    Sidecar containers starting order diagram
    Figure 3: This diagram shows the sidecar containers starting order.

    Next the termination phase depicted in Figure 4 shows the value of the sidecar containers.

    Sidecar containers termination order diagram
    Figure 4: This diagram shows the termination order of sidecar containers.

    All sidecar containers terminate in the opposite order they started after terminating all the main containers.

    This is helpful in resolving issues of any dependencies between sidecar containers and main containers (e.g., if a logging agent must be terminated after the main application so application logs are processed without any loss). If you’re interested in how this termination order is implemented, refer to this code sample.

    Main containers

    The primary content of your applications are run as the main containers within a pod, and it starts sequentially in order of the spec.containers array in the pod template. The kubelet starts all main containers without checking the status of each container. This is a different behavior than init and sidecar containers as previously mentioned. In other words, the next main container launches regardless of the previously started containers starting status. 

    This logic is quickly processed, so it can appear that all main containers started at the same time. If you want to start all main containers one by one like init containers, you can implement this approach using the postStart lifecycle hook that blocks the start of the next container until the hook is completed.

    Figure 5 illustrates this process.

    Main containers starting order diagram
    Figure 5: This diagram shows the starting order of the main containers.

    One interesting point to note is that all the main containers are terminated randomly in parallel (see Figure 6). As illustrated in this code sample, the termination is invoked concurrently through the Go routine for each of the containers. If you want to implement a specific termination order in the main containers, you can consider configuring preStop lifecycle hooks.

    You can defer the time to issue the SIGTERM signal to the container until the hook is completed. Keep in mind that all the preStop hooks are triggered immediately in parallel as soon as the pod is in terminating status, not one by one for each of the containers. You can find the related code on GitHub. 

    Main containers termination order diagram
    Figure 6: The diagram shows the termination order of the main containers.

    An exception to the termination order

    An additional consideration is that there is an exception to the termination order when the pod expires as a result of the terminationGracePeriodSeconds timeout. This timeout starts to count down as soon as the pod enters the terminating status. If any of the main containers were not terminated within this timeframe, the kubelet forcibly kills the container with the SIGKILL signal.

    Consider the situation where the sidecar containers are running together at that time. What will occur? The sidecar containers will terminate randomly in parallel after terminating all main containers without keeping the expected reverse order. If any of the sidecar containers do not terminate immediately, the kubelet also sends the container kill signal again after waiting an additional 2 seconds. 

    You need to modify the terminationGracePeriodSeconds to keep the expected termination to suit this use case. For more details, refer to the documentation for the Termination of Pods and Pod shutdown and sidecar containers. The diagram in Figure 7 illustrates this process.

    Termination order after terminationGracePeriodSeconds timeout
    Figure 7: This diagram shows the main and sidecar containers terminate immediately after the terminationGracePeriodSeconds timeout.

    Final thoughts

    While the functionality surrounding the lifecycle of a pod was a core capability of Kubernetes for many releases, understanding how it functions enables greater insight and the ability to fine-tune the configurations of your applications and use cases. We provided examples that included real-world use cases you can use within your own environment to harness the true power of Red Hat OpenShift and Kubernetes.

    Related Posts

    • Init Container Build Pattern: Knative build with plain old Kubernetes deployment

    • C# 9 init accessors and records

    • Containers, Kubernetes, and microservices: Start here

    • Troubleshooting Red Hat OpenShift applications with throwaway containers

    Recent Posts

    • GuideLLM: Evaluate LLM deployments for real-world inference

    • Unleashing multimodal magic with RamaLama

    • Integrate Red Hat AI Inference Server & LangChain in agentic workflows

    • Streamline multi-cloud operations with Ansible and ServiceNow

    • Automate dynamic application security testing with RapiDAST

    What’s up next?

    Learn how you can build and populate a database that is running in Red Hat OpenShift with init containers.

    Start the activity
    Red Hat Developers logo LinkedIn YouTube Twitter Facebook

    Products

    • Red Hat Enterprise Linux
    • Red Hat OpenShift
    • Red Hat Ansible Automation Platform

    Build

    • Developer Sandbox
    • Developer Tools
    • Interactive Tutorials
    • API Catalog

    Quicklinks

    • Learning Resources
    • E-books
    • Cheat Sheets
    • Blog
    • Events
    • Newsletter

    Communicate

    • About us
    • Contact sales
    • Find a partner
    • Report a website issue
    • Site Status Dashboard
    • Report a security problem

    RED HAT DEVELOPER

    Build here. Go anywhere.

    We serve the builders. The problem solvers who create careers with code.

    Join us if you’re a developer, software engineer, web designer, front-end designer, UX designer, computer scientist, architect, tester, product manager, project manager or team lead.

    Sign me up

    Red Hat legal and privacy links

    • About Red Hat
    • Jobs
    • Events
    • Locations
    • Contact Red Hat
    • Red Hat Blog
    • Inclusion at Red Hat
    • Cool Stuff Store
    • Red Hat Summit

    Red Hat legal and privacy links

    • Privacy statement
    • Terms of use
    • All policies and guidelines
    • Digital accessibility

    Report a website issue