Chuyển đến nội dung chính

Lesson 2: Jobs, CronJobs & Resource Management

Jobs (batch tasks), CronJobs (scheduled tasks). Resource requests and limits, LimitRange, ResourceQuota. QoS classes for the CKAD exam.

Jobs and CronJobs — completions, parallelism, concurrencyPolicy

1. Jobs

A Job creates one or more Pods and ensures they complete successfully. When a Job completes, Pods are not deleted (for log inspection).

apiVersion: batch/v1
kind: Job
metadata:
  name: data-processor
spec:
  completions: 3       # Run 3 successful completions
  parallelism: 2       # Run 2 pods at a time
  backoffLimit: 4      # Retry up to 4 times on failure
  template:
    spec:
      restartPolicy: Never  # OnFailure or Never (required for Job)
      containers:
      - name: processor
        image: busybox
        command: ['sh', '-c', 'echo Processing; sleep 5']
FieldMeaningDefault
completionsNumber of required completions1
parallelismNumber of concurrent Pods1
backoffLimitNumber of retries on failure6
activeDeadlineSecondsOverall Job timeoutUnlimited

Exam tip: Job Pods must have restartPolicy: Never or OnFailure. You cannot use Always (which is the default for regular Pods). CKAD frequently tests creating Jobs and checking completion status.

2. CronJobs

apiVersion: batch/v1
kind: CronJob
metadata:
  name: nightly-backup
spec:
  schedule: "0 2 * * *"  # Cron syntax: minute hour day month weekday
  concurrencyPolicy: Forbid  # Allow | Forbid | Replace
  successfulJobsHistoryLimit: 3
  failedJobsHistoryLimit: 1
  jobTemplate:
    spec:
      template:
        spec:
          restartPolicy: OnFailure
          containers:
          - name: backup
            image: backup-tool:1.0
            command: ['./backup.sh']
concurrencyPolicyBehavior
AllowAllows concurrent Jobs (default)
ForbidSkips new Job if previous hasn't finished
ReplaceCancels previous Job, starts new one

3. Resource Requests & Limits

spec:
  containers:
  - name: app
    image: myapp
    resources:
      requests:
        cpu: "250m"      # 0.25 CPU core (minimum guaranteed)
        memory: "128Mi"  # 128 MiB minimum
      limits:
        cpu: "500m"      # Max 0.5 CPU core
        memory: "256Mi"  # Max 256 MiB (OOM if exceeded)
QoS Classes (based on requests/limits):

Guaranteed:  requests == limits (both CPU and memory)
             → Last to be evicted under pressure

Burstable:   requests < limits (or only one set)
             → Middle priority for eviction

BestEffort:  NO requests, NO limits
             → First to be evicted under pressure

Exam tip: For a Pod to have Guaranteed QoS class: you must set both cpu and memory in both requests and limits, and they must be equal. Every container in the Pod must satisfy this condition.

4. LimitRange & ResourceQuota

ObjectScopePurpose
LimitRangeNamespaceSet default requests/limits for Pods/Containers in a namespace
ResourceQuotaNamespaceLimit total resources a namespace can use
ResourceQuota example:
apiVersion: v1
kind: ResourceQuota
metadata:
  name: dev-quota
  namespace: development
spec:
  hard:
    requests.cpu: "4"
    requests.memory: "8Gi"
    limits.cpu: "8"
    limits.memory: "16Gi"
    pods: "20"

5. Cheat Sheet

Exam QuestionAnswer
What restartPolicy does a Job need?Never or OnFailure
CronJob every 5 minutes?*/5 * * * *
Container OOM Killed — why?Exceeded limits.memory
What's needed for Guaranteed QoS?requests == limits (both CPU and Memory)
Limit namespace resources?ResourceQuota

6. Practice Questions

Q1: A Job is configured with completions: 5 and parallelism: 2. How does it execute?

  • A) Creates 5 Pods simultaneously until all complete
  • B) Runs 2 Pods at a time, creating new ones as old ones complete, until 5 total completions ✓
  • C) Runs 5 Pods sequentially one by one
  • D) Creates 2 Pods, each completing 2.5 times

Explanation: completions=5 means 5 Pods must exit successfully. parallelism=2 means at most 2 run at once. As each Pod completes, a new one starts until the completion count is reached. Total Pods created could be more if some fail.

Q2: A Pod has no resource requests or limits set. What QoS class is it assigned and how does this affect eviction?

  • A) Guaranteed — it will be last to be evicted
  • B) Burstable — it has medium eviction priority
  • C) BestEffort — it will be first to be evicted under resource pressure ✓
  • D) NoQoS — it has no eviction priority

Explanation: Pods with no resource requests or limits get BestEffort QoS class. When nodes face resource pressure, Kubernetes evicts BestEffort Pods first to free resources for higher-priority workloads.

Q3: A CronJob is scheduled every hour. A previous job is still running when the next scheduled time arrives. With concurrencyPolicy: Forbid, what happens?

  • A) The running job is cancelled; the new one starts
  • B) Both jobs run concurrently
  • C) The new job is skipped; the running job continues ✓
  • D) The CronJob is suspended until the running job completes

Explanation: Forbid policy prevents a new job from starting if the previous job is still running. The scheduled run is skipped. Use Allow to permit concurrent runs or Replace to cancel the old and start the new one.