Cyber Deals - Get up to 65% off on CKA, CKAD, CKS, KCNA, KCSA exams and courses!

How to Use Pod Failure Policy in Kubernetes

How to Use Pod Failure Policy in Kubernetes

Introduction

With the release of Kubernetes 1.31, the Pod Failure Policy for Jobs has graduated to General Availability (GA). This new feature provides enhanced control over how Kubernetes handles pod failures within Jobs, allowing for more efficient and cost-effective management of workloads.

Understanding Pod Failure Policy

Running workloads on Kubernetes can lead to pod failures due to various reasons. For workloads such as Jobs, it is essential to handle transient, retriable failures without halting the entire process. Traditionally, the backoffLimit field in Kubernetes Jobs allowed you to specify the number of pod failures to tolerate before stopping the Job. However, setting a high backoffLimit can result in excessive restarts and increased operating costs, especially for large-scale Jobs.

The Pod Failure Policy extends the backoff limit mechanism, offering more granular control to immediately terminate a Job upon a non-retriable pod failure and ignore retriable errors without inflating the backoffLimit value. This policy is particularly useful for scenarios like running workloads on cost-effective spot instances, where pod failures due to node shutdowns can be gracefully ignored.

How Pod Failure Policy Works

A Pod Failure Policy is defined within the Job specification as a list of rules. Each rule specifies conditions and corresponding actions based on container exit codes or pod conditions. The actions can be:

  • Ignore: Does not count the failure towards the backoffLimit .
  • FailJob: Terminates the entire Job and all running pods.
  • FailIndex: Terminates the specific index of the failed pod, useful with the backoff limit per index feature.
  • Count: Counts the failure towards the backoffLimit (default behavior).

Example Specification

Below is an example of a Pod Failure Policy:

apiVersion: batch/v1
kind: Job
metadata:
  name: example-job
spec:
  template:
    spec:
      restartPolicy: Never
      containers:
      - name: example-container
        image: example-image
  backoffLimit: 3
  podFailurePolicy:
    rules:
    - action: Ignore
      onPodConditions:
      - type: DisruptionTarget
    - action: FailJob
      onPodConditions:
      - type: ConfigIssue
    - action: FailJob
      onExitCodes:
        operator: In
        values: [42]

In this example:

  • Pods with the DisruptionTarget condition are ignored and do not count towards the Job’s backoff limits.
  • The Job fails if a pod has a ConfigIssue condition, which might be added by a custom controller or webhook.
  • The Job also fails if any container exits with the code 42.
  • All other pod failures count towards the backoffLimit .

Important Note

When using the Pod Failure Policy, ensure the Job’s pod template is set with restartPolicy: Never . This setting prevents race conditions between the kubelet and Job controller when counting pod failures.

Gineesh Madapparambath

Gineesh Madapparambath

Gineesh Madapparambath is the founder of techbeatly. He is the co-author of The Kubernetes Bible, Second Edition and the author of Ansible for Real Life Automation. He has worked as a Systems Engineer, Automation Specialist, and content author. His primary focus is on Ansible Automation, Containerisation (OpenShift & Kubernetes), and Infrastructure as Code (Terraform). (Read more: iamgini.com)


Disclaimer: Views expressed in articles are those of the respective authors and do not necessarily reflect the views of their employers or this site. While we aim for accuracy, please verify information before relying on it, and let us know if you spot an error.
Share:

Related Posts

OpenShift Operators (Kubernetes Operators)

OpenShift Operators (Kubernetes Operators)

An Operator is a method of packaging, deploying and managing a Kubernetes-native application.

Book Review: -Mastering Kubernetes Fourth Edition- by Gigi Sayfan

Book Review: -Mastering Kubernetes Fourth Edition- by Gigi Sayfan

The book commences by laying a strong foundation with a thorough exploration of Kubernetes core concepts, accompanied by step-by-step instructions for …

Multi-node Kubernets Cluster in 10 minutes

Multi-node Kubernets Cluster in 10 minutes

In this article, you will learn how to deploy a multi-node Kubernetes cluster using Vagrant and VirtualBox.

Kubernetes: What Is It and Its Importance in DevOps

Kubernetes: What Is It and Its Importance in DevOps

Kubernetes is quickly becoming a critical part of many enterprise DevOps initiatives. But what exactly is Kubernetes? And more importantly, how can it …

Conduct Vulnerability Management for Your Kubernetes Applications

Conduct Vulnerability Management for Your Kubernetes Applications

Kubernetes is an open source container orchestration tool initially developed by Google and subsequently handed over to the Cloud Native Computing …

Managing Containers at AWS – Unveiling the Perfect Services for Your Use Cases

Managing Containers at AWS – Unveiling the Perfect Services for Your Use Cases

In this article, we'll explore the key container services offered by AWS and understand the best use cases for each.