Explore Courses
course iconScrum AllianceCertified ScrumMaster (CSM) Certification
  • 16 Hours
Best seller
course iconScrum AllianceCertified Scrum Product Owner (CSPO) Certification
  • 16 Hours
Best seller
course iconScaled AgileLeading SAFe 6.0 Certification
  • 16 Hours
Trending
course iconScrum.orgProfessional Scrum Master (PSM) Certification
  • 16 Hours
course iconScaled AgileSAFe 6.0 Scrum Master (SSM) Certification
  • 16 Hours
course iconScaled Agile, Inc.Implementing SAFe 6.0 (SPC) Certification
  • 32 Hours
Recommended
course iconScaled Agile, Inc.SAFe 6.0 Release Train Engineer (RTE) Certification
  • 24 Hours
course iconScaled Agile, Inc.SAFe® 6.0 Product Owner/Product Manager (POPM)
  • 16 Hours
Trending
course iconKanban UniversityKMP I: Kanban System Design Course
  • 16 Hours
course iconIC AgileICP Agile Certified Coaching (ICP-ACC)
  • 24 Hours
course iconScrum.orgProfessional Scrum Product Owner I (PSPO I) Training
  • 16 Hours
course iconAgile Management Master's Program
  • 32 Hours
Trending
course iconAgile Excellence Master's Program
  • 32 Hours
Agile and ScrumScrum MasterProduct OwnerSAFe AgilistAgile CoachFull Stack Developer BootcampData Science BootcampCloud Masters BootcampReactNode JsKubernetesCertified Ethical HackingAWS Solutions Artchitct AssociateAzure Data Engineercourse iconPMIProject Management Professional (PMP) Certification
  • 36 Hours
Best seller
course iconAxelosPRINCE2 Foundation & Practitioner Certificationn
  • 32 Hours
course iconAxelosPRINCE2 Foundation Certification
  • 16 Hours
course iconAxelosPRINCE2 Practitioner Certification
  • 16 Hours
Change ManagementProject Management TechniquesCertified Associate in Project Management (CAPM) CertificationOracle Primavera P6 CertificationMicrosoft Projectcourse iconJob OrientedProject Management Master's Program
  • 45 Hours
Trending
course iconProject Management Master's Program
  • 45 Hours
Trending
PRINCE2 Practitioner CoursePRINCE2 Foundation CoursePMP® Exam PrepProject ManagerProgram Management ProfessionalPortfolio Management Professionalcourse iconAWSAWS Certified Solutions Architect - Associate
  • 32 Hours
Best seller
course iconAWSAWS Cloud Practitioner Certification
  • 32 Hours
course iconAWSAWS DevOps Certification
  • 24 Hours
course iconMicrosoftAzure Fundamentals Certification
  • 16 Hours
course iconMicrosoftAzure Administrator Certification
  • 24 Hours
Best seller
course iconMicrosoftAzure Data Engineer Certification
  • 45 Hours
Recommended
course iconMicrosoftAzure Solution Architect Certification
  • 32 Hours
course iconMicrosoftAzure Devops Certification
  • 40 Hours
course iconAWSSystems Operations on AWS Certification Training
  • 24 Hours
course iconAWSArchitecting on AWS
  • 32 Hours
course iconAWSDeveloping on AWS
  • 24 Hours
course iconJob OrientedAWS Cloud Architect Masters Program
  • 48 Hours
New
course iconCareer KickstarterCloud Engineer Bootcamp
  • 100 Hours
Trending
Cloud EngineerCloud ArchitectAWS Certified Developer Associate - Complete GuideAWS Certified DevOps EngineerAWS Certified Solutions Architect AssociateMicrosoft Certified Azure Data Engineer AssociateMicrosoft Azure Administrator (AZ-104) CourseAWS Certified SysOps Administrator AssociateMicrosoft Certified Azure Developer AssociateAWS Certified Cloud Practitionercourse iconAxelosITIL 4 Foundation Certification
  • 16 Hours
Best seller
course iconAxelosITIL Practitioner Certification
  • 16 Hours
course iconPeopleCertISO 14001 Foundation Certification
  • 16 Hours
course iconPeopleCertISO 20000 Certification
  • 16 Hours
course iconPeopleCertISO 27000 Foundation Certification
  • 24 Hours
course iconAxelosITIL 4 Specialist: Create, Deliver and Support Training
  • 24 Hours
course iconAxelosITIL 4 Specialist: Drive Stakeholder Value Training
  • 24 Hours
course iconAxelosITIL 4 Strategist Direct, Plan and Improve Training
  • 16 Hours
ITIL 4 Specialist: Create, Deliver and Support ExamITIL 4 Specialist: Drive Stakeholder Value (DSV) CourseITIL 4 Strategist: Direct, Plan, and ImproveITIL 4 Foundationcourse iconJob OrientedData Science Bootcamp
  • 6 Months
Trending
course iconJob OrientedData Engineer Bootcamp
  • 289 Hours
course iconJob OrientedData Analyst Bootcamp
  • 6 Months
course iconJob OrientedAI Engineer Bootcamp
  • 288 Hours
New
Data Science with PythonMachine Learning with PythonData Science with RMachine Learning with RPython for Data ScienceDeep Learning Certification TrainingNatural Language Processing (NLP)TensorflowSQL For Data Analyticscourse iconIIIT BangaloreExecutive PG Program in Data Science from IIIT-Bangalore
  • 12 Months
course iconMaryland UniversityExecutive PG Program in DS & ML
  • 12 Months
course iconMaryland UniversityCertificate Program in DS and BA
  • 31 Weeks
course iconIIIT BangaloreAdvanced Certificate Program in Data Science
  • 8+ Months
course iconLiverpool John Moores UniversityMaster of Science in ML and AI
  • 750+ Hours
course iconIIIT BangaloreExecutive PGP in ML and AI
  • 600+ Hours
Data ScientistData AnalystData EngineerAI EngineerData Analysis Using ExcelDeep Learning with Keras and TensorFlowDeployment of Machine Learning ModelsFundamentals of Reinforcement LearningIntroduction to Cutting-Edge AI with TransformersMachine Learning with PythonMaster Python: Advance Data Analysis with PythonMaths and Stats FoundationNatural Language Processing (NLP) with PythonPython for Data ScienceSQL for Data Analytics CoursesAI Advanced: Computer Vision for AI ProfessionalsMaster Applied Machine LearningMaster Time Series Forecasting Using Pythoncourse iconDevOps InstituteDevOps Foundation Certification
  • 16 Hours
Best seller
course iconCNCFCertified Kubernetes Administrator
  • 32 Hours
New
course iconDevops InstituteDevops Leader
  • 16 Hours
KubernetesDocker with KubernetesDockerJenkinsOpenstackAnsibleChefPuppetDevOps EngineerDevOps ExpertCI/CD with Jenkins XDevOps Using JenkinsCI-CD and DevOpsDocker & KubernetesDevOps Fundamentals Crash CourseMicrosoft Certified DevOps Engineer ExperteAnsible for Beginners: The Complete Crash CourseContainer Orchestration Using KubernetesContainerization Using DockerMaster Infrastructure Provisioning with Terraformcourse iconTableau Certification
  • 24 Hours
Recommended
course iconData Visualisation with Tableau Certification
  • 24 Hours
course iconMicrosoftMicrosoft Power BI Certification
  • 24 Hours
Best seller
course iconTIBCO Spotfire Training
  • 36 Hours
course iconData Visualization with QlikView Certification
  • 30 Hours
course iconSisense BI Certification
  • 16 Hours
Data Visualization Using Tableau TrainingData Analysis Using Excelcourse iconEC-CouncilCertified Ethical Hacker (CEH v12) Certification
  • 40 Hours
course iconISACACertified Information Systems Auditor (CISA) Certification
  • 22 Hours
course iconISACACertified Information Security Manager (CISM) Certification
  • 40 Hours
course icon(ISC)²Certified Information Systems Security Professional (CISSP)
  • 40 Hours
course icon(ISC)²Certified Cloud Security Professional (CCSP) Certification
  • 40 Hours
course iconCertified Information Privacy Professional - Europe (CIPP-E) Certification
  • 16 Hours
course iconISACACOBIT5 Foundation
  • 16 Hours
course iconPayment Card Industry Security Standards (PCI-DSS) Certification
  • 16 Hours
course iconIntroduction to Forensic
  • 40 Hours
course iconPurdue UniversityCybersecurity Certificate Program
  • 8 Months
CISSPcourse iconCareer KickstarterFull-Stack Developer Bootcamp
  • 6 Months
Best seller
course iconJob OrientedUI/UX Design Bootcamp
  • 3 Months
Best seller
course iconEnterprise RecommendedJava Full Stack Developer Bootcamp
  • 6 Months
course iconCareer KickstarterFront-End Development Bootcamp
  • 490+ Hours
course iconCareer AcceleratorBackend Development Bootcamp (Node JS)
  • 4 Months
ReactNode JSAngularJavascriptPHP and MySQLcourse iconPurdue UniversityCloud Back-End Development Certificate Program
  • 8 Months
course iconPurdue UniversityFull Stack Development Certificate Program
  • 9 Months
course iconIIIT BangaloreExecutive Post Graduate Program in Software Development - Specialisation in FSD
  • 13 Months
Angular TrainingBasics of Spring Core and MVCFront-End Development BootcampReact JS TrainingSpring Boot and Spring CloudMongoDB Developer Coursecourse iconBlockchain Professional Certification
  • 40 Hours
course iconBlockchain Solutions Architect Certification
  • 32 Hours
course iconBlockchain Security Engineer Certification
  • 32 Hours
course iconBlockchain Quality Engineer Certification
  • 24 Hours
course iconBlockchain 101 Certification
  • 5+ Hours
NFT Essentials 101: A Beginner's GuideIntroduction to DeFiPython CertificationAdvanced Python CourseR Programming LanguageAdvanced R CourseJavaJava Deep DiveScalaAdvanced ScalaC# TrainingMicrosoft .Net Frameworkcourse iconSalary Hike GuaranteedSoftware Engineer Interview Prep
  • 3 Months
Data Structures and Algorithms with JavaScriptData Structures and Algorithms with Java: The Practical GuideLinux Essentials for Developers: The Complete MasterclassMaster Git and GitHubMaster Java Programming LanguageProgramming Essentials for BeginnersComplete Python Programming CourseSoftware Engineering Fundamentals and Lifecycle (SEFLC) CourseTest-Driven Development for Java ProgrammersTypeScript: Beginner to Advanced

Run Kubernetes Jobs : Understanding Jobs in Kubernetes

Updated on 15 October, 2022

8.71K+ views
11 min read

This article on Kubernetes Jobs will take you through what actually is Kubernetes and its concepts. We get you detailed information on configuring the job file, different types of k8s jobs, and k8s pods. Also, check Kubernetes pod failure limit and jobs use cases in this article. If you are a person who is looking to get certified on Kubernetes, then Kubernetes certification training is the course for you.  

Getting Started with Kubernetes Job 

Before diving into jobs, it is better to understand a bit about Kubernetes pods.  

Pods are like the building blocks of Kubernetes. It is the smallest execution unit in Kubernetes. A pod can contain one or more applications. So, when we deploy an application to Kubernetes, we are creating a pod with replicas (similar pods) and publish the containerized image into the pod, and the replicas.   

Usually, pods are not defined to run forever. When a pod is deleted or terminated, there is no way to bring it back. All pods have a status that represents the current status of the pod. They are mentioned below: 

  • Pending: Pod is created and accepted by the cluster. But, one or more of its containers are not running.  
  • Running: Pod is bound to a corresponding node, and the containers are created. Also at least one container is running.  
  • Succeeded: All containers within the pod have successfully terminated. Those terminated pods will not restart.  
  • Unknown: Pod’s state cannot be determined 

Back to jobs, it tracks the successful pod creations/ completions. And when all the defined number of pod creations are succussed, the job is referred to as complete.  

Deleting a Job will clean up the Pods it created. Suspending a Job will delete its active Pods until the Job is resumed. 

Kubernetes Job Definition File 

So, Kubernetes resources are mainly executed with YAML files. So, for jobs also, we will be creating YAML file. Suppose we use VS code for this session. Go ahead and create a new file called sample-job.yml. And if you have the Kubernetes extension installed on VS code, just type job and it will give you the template to select.  

Kubernetes job example is provided above. Let us talk a bit about the definition file. Here we have the apiVersion, which is defined as batch/v1. It is the default version for the job definition file provided by Kubernetes. Then we have the metadata of the job. Metadata is nothing but details about the job. Like name.  

Then we have the specifications of the job. Like the container details, image name, and commands to execute when running the YAML file.  

Once the job is executed, just run the following code to see the completed jobs.  

kubectl get jobs  

Multiple Parallel Jobs (Work Queue)  

You can use queue services like RabbitMQ, Azure Queue storage to run multiple jobs at a time in a parallel manner. This is a time saving approach since the jobs are running parallel. There are couple of variations of this approach. Like Fine parallel processing, Coarse parallel processing …etc.   

Kubernetes Job Failure and Concurrency Considerations 

Kubernetes job is just a container running. As with any container, the final exit code determines if the run was successful or not. So, the job failed with a code other than 0, means the job has failed. There can be many reasons for a k8s job failure. Like running out of disk spaces.

When a k8s job fails, you can easily investigate logs and see what has happened. To do that, you can use;
 

kubectl command. 
kubectl logs job.batch/sample-job 

The Pod Failure Limit

A pod can exceed its memory request if the Node has memory available. But a pod is not allowed to use more than its memory limit. If a pod allocates more memory than its limit, the pod becomes a candidate for termination. If the pod continues to consume memory beyond its limit, the pod is terminated. 

Limiting Kubernetes Job Execution Time 

In another words, limiting the execution time is like giving a fixed deadline for the job to finish.  

The best way to limit job execution time is setting activeDeadlineSeconds within the spec section of the job definition file.

As you can see here, we have set the activeDeadlineSeconds to 20. Which means, this job creation will last only 20 mins. After that, all running pods will be terminated. And the Job status will become type: Failed with reason: DeadlineExceeded. 

Kubernetes Job Deletion and Cleanup 

When a k8s job is referred to as complete, that means it is finished creating pods. Or all the pods are created successfully. So, there is no more pod creation or execution happening. When the pods are created, you can use the kubectl commands to check logs and see errors or warnings in the pods. It is up to the user to delete the jobs. The best way to delete a job is using kubectl command like below. Let us assume that you have a job definition file named sample-job.YAML.

kubectl delete –f sample-job.YAML 

This will delete all pods that were created with the sample-job.YAML file.  

By default, a job will execute uninterrupted unless there is a pod failure. Another way that we can fail the job is by defining a spec.backoffLimit in the job definition file. We use seconds to define that.  Once the spec.backoffLimit is set and time is reached, the job is marked as failed, and any running pods will be terminated.  

Another way of terminating the job is by setting an active deadline. Not just to fail the job, we can use that property to limit the execution time of the job. But the final result is the same. We can do this by setting the spec.activeDeadlineSeconds. Once the time is reached, all running pods created by the job are terminated. And the status of the job is set to type: Failed with reason: DeadlineExceeded 

Note that the activeDeadlineSeconds take priority compared to the backoffLimit. Even if the backoffLimit is not reached and the activeDeadlineSeconds is reached, the job will be terminated.  

Furthermore, if you wish to learn more about Docker and Kubernetes, best way to learn Docker and Kubernetes is a good course for you. Not just the training, it will lead you to a certification as well. 

Kubernetes Jobs Use Cases 

Running Automated Tasks with a CronJob 

Let us look at what a CronJob is. CronJob creates K8s jobs on a repeating schedule. A CronJob object is like a line of a Cron table, written in Cron format. CronJob runs a job periodically on a given schedule. So simply, CronJobs are Kubernetes scheduled jobs.  

Mainly CronJobs are used to perform regular scheduled actions such as report generations, backups, …etc. These tasks should be configured to recur indefinitely. For an example once a day, week, or a month.  

 Let us see how the CronJob definition files look like.  

As you can see here, the file is somewhat the same as the K8s job definition file. One noticeable change is the kind. Here the kind is defined as “CronJob” while in k8s job, the kind is just “Job” 

And another noticeable thing is the schedule property.  Let us talk about the value “* * * *” 

  • The first * is for the minutes. And the value can be from 0 – 59 
  • Second * is for the hour. The value can be from 0 – 23 
  • Third * is for the day of the month. Value can be from 1 – 31 
  • Fourth * is for the month. Value can be from 1 – 12 
  • Additionally, you can specify another star here, just to mention the day of the week  

(E.g.: Monday, Tuesday …etc.). Value can be from 0 - 6 

Let us take 0 0 13 * 5 as an example. This means, the task should start on every Friday at midnight, as well as on the 13th of each month at midnight.  

Coarse Parallel Processing Using a Work Queue 

As discussed we are using a queue service like RabbitMQ or Azure Queue storage to store tasks. When we execute the job with parallel processing, and when each pod is created, it picks up one unit of work from the queue, completes it, and then deletes it, and finally exits.  

As the first step, we create Queue, and fill it up with the tasks that need to be executed. After that, the job starts several pods. Every pod picks a single task from the queue and processes it. This process is repeated until no more tasks are left in the queue.  

Talking about the advantages of coarse parallel processing, you don’t have to modify the worker program, AKA the pods, to be aware of the queue service.  

But this method does require you to run a message queue service like RabbitMQ. If you don’t want to use a queue service, it is better to use another pattern like Queue with pod per work item, Queue with variable pod count ...etc.  

Fine Parallel Processing Using a Work Queue

This is another parallel processing method associated with a queue service like RabbitMQ or Azure queue storage. Here, when a pod is created, the pod picks up a task from the queue and processes it. This process is repeated until there are no more tasks left in the queue.  

Indexed Job for Parallel Processing with Static Work Assignment

As per the name, you can see that here an index is used. Each worker is a container in its own pod. And each pod has an index. The index allows each pod to identify and work on a part of the overall task. 

This index is a string consisting of a dynamic value to create the index, k8s have defined a method called the downward API mechanism. Once the index is created, you will be able to see a variable called JOB_COMPLETION_INDEX in the environment variable section.  

Parallel Processing Using Expansions 

What this simply means is you can use a common job template to run multiple jobs. Of course, you must change some values when you are executing the job file. But you can use it as a template. Let us see an example. 

Here you can see a Kubernetes job template named as job-template.yml 

This is not a valid manifest file yet. You need to replace $ITEM with a value. We can create two new files with this template:

Now, we have two valid manifest files that were created from the template. To execute the files, you just need to use kubectl command, but this time with the path to the files. We have these two files inside a folder called expansion. So, the command can be executed as below: 

kubectl apply -f ./expansion  

See? Easy. 

Conclusion

Now that we have explained everything in detail let us sum up what we learned. We investigated Kubernetes pods as a start and then learned what a Kubernetes job is along with the definition file. Then we investigated work queues, like job and pod failures. Also, we investigated pod failure limits as well.  

Furthermore, we investigated limiting job execution time, job deletion and cleanup, and some solid use cases of Kubernetes jobs including CronJobs, Parallel processing with queues. If you are planning to get a certification for your preferred DevOps path, you can go for KnowledgeHut best devops courses online. It has more areas that can guide you to the certificates.

Frequently Asked Questions (FAQs)

1. How Do You Execute Kubernetes Job?

Since the Kubernetes job is defined in an YML file, you can use kubectl apply command. Let’s say we have a job definition file named sample-job.yml. So, we can execute the job with 

kubectl apply -f sample-job.yml 

2. What is Difference Between Job and CronJob in Kubernetes?

K8s jobs are used to create pods. CronJob does the same thing but with a predefined schedule. More likely a batch job. If the scheduled time is exceeded, all pods that were created by the CronJob, will be terminated at the end. 

3. What is the Difference Between Job and Pod?

Pod is the simplest building block of Kubernetes. And the Kubernetes job is used to create pods.