Job / Cronjob

Pod IconPod Icon



Architecture

ex_job.png

Detailed Description

A Job creates one or more Pods and keeps retrying them until a specified number successfully finish. Once the desired number of successful completions is reached, the Job is complete. Deleting a Job also deletes the Pods it created, while suspending it will stop active Pods until the Job is resumed.

For a simple task, you can create a Job to run a single Pod, which will restart if it fails or is deleted (e.g., due to a node failure). Jobs can also run multiple Pods at the same time.

the job name .metadata.name should follow rules for DNS subdomain (ideally stricter rules but cannot be longer 63 chars)

Example: Non-parallel Job (Single Pod):
apiVersion: batch/v1
kind: Job
metadata:
name: single-task-job
spec:
template:
  spec:
    containers:
    - name: worker
      image: busybox
      command: ["echo", "Hello World"]
    restartPolicy: Never
Parallel Job with Fixed Completions:
apiVersion: batch/v1
kind: Job
metadata:
name: fixed-task-job
spec:
completions: 5
parallelism: 2
template:
  spec:
    containers:
    - name: worker
      image: busybox
      command: ["echo", "Processing task"]
    restartPolicy: OnFailure
Parallel Job with Work Queue:
apiVersion: batch/v1
kind: Job
metadata:
name: work-queue-job
spec:
parallelism: 3
template:
  spec:
    containers:
    - name: worker
      image: busybox
      command: ["fetch-and-process-task"]
    restartPolicy: Never

Indexed CompletionMode:
apiVersion: batch/v1
kind: Job
metadata:
name: indexed-job
spec:
completions: 3         # Job completes when all 3 indexed Pods have succeeded
parallelism: 2         # At most 2 Pods run simultaneously
completionMode: Indexed
template:
  spec:
    containers:
    - name: worker
      image: busybox
      command:
      - /bin/sh
      - -c
      - |
        echo "Processing task for index $JOB_COMPLETION_INDEX"
    restartPolicy: Never


If you want to run a Job on a schedule, use a CronJob.


Command Reference Guide

Remeber to use dry-run and tee to check the configuration of each command first.
--dry-run=client -o yaml | tee nginx-deployment.yaml

Create a Namespace using a YAML file (declarative method)
apiVersion: v1
kind: Namespace
metadata:
  name: dev
  labels:
    name: dev
---
apiVersion: v1
kind: Namespace
metadata:
  name: prod
  labels:
    name: prod

# Create namespaces
kubectl create -f namespace.yaml

# Get namespaces
kubectl get namespaces --show-labels

# Add context spaces (First get user and clustername)
kubectl config view
CLUSTER_NAME=$(kubectl config view --raw -o jsonpath='{.clusters[0].name}')
USER_NAME=$(kubectl config view --raw -o jsonpath='{.users[0].name}')
kubectl config set-context dev --namespace=dev --cluster=$CLUSTER_NAME --user=$USER_NAME
kubectl config set-context prod --namespace=prod --cluster=$CLUSTER_NAME --user=$USER_NAME
# We added  two new request contexts (dev and prod)
kubectl config view

# Switch context
kubectl config use-context dev

# Check current context
kubectl config current-context

# Create deployment in context dev and check pods
kubectl create deployment nginx-deployment --image=nginxdemos/hello --port=80
kubectl get pods

# Switch context and check pods
kubectl config use-context prod
kubectl get pods

# Delete context
kubectl config use-context default
kubectl config delete-context dev
kubectl config delete-context prod




Revision #10
Created 24 November 2024 18:55:26 by Admin
Updated 26 November 2024 13:21:53 by Admin