🏫 The School›☸️ Kubernetes›⏰ धडा 18 — Jobs आणि CronJobs: गृहपाठ आणि सकाळची घंटा
🖼️ See the drawing + lab 🏠 Course home 🌿 Branch on GitHub ✏️ View source
🖼️ आकृती आणि labThe drawing + lab पूर्ण पानावर उघडा ↗Open full page ↗

⏰ धडा 18 — Jobs आणि CronJobs: गृहपाठ आणि सकाळची घंटा

📍 तुम्ही इथे आहात: धडा 18 / 26 · मागील: lesson-17-rbac · पुढील: lesson-19-network-policies


📦 या ब्रँचमध्ये काय आहे

आधीचे सगळे, आणि server नसलेले काम: एकदाच चालणारे Jobs, वेळापत्रकानुसार चालणारे CronJobs — आणि एक खरी देणगी:

🧒 5 वर्षांच्या मुलाला समजावल्यासारखे

आत्तापर्यंत सगळी मुले दिवसभर आपल्या जागी उभी राहणारी होती (servers: Deployments त्यांना तिथे कायम ठेवतात). पण शाळेत आणखी दोन प्रकारचे काम असते:

आपली घंटा: दर रात्री एक pod जन्माला येतो, database वर pg_dump चालवतो, file संग्रहात पाठवतो, आणि मरतो. सुंदर, कंटाळवाणे, धडा 12 च्या "प्रत्येक शाळेचे backups" गोष्टीला हवे होते तेच.

🗺️ आकृती

flowchart LR
    cj["🔔 CronJob<br/>schedule: 0 2 * * *<br/>(every night, 2 AM)"]
    j["📝 Job<br/>tonight's homework<br/>retries: backoffLimit 2"]
    p["🪑 pod<br/>pg_dump → upload<br/>then EXITS 0"]
    done["✅ Completed<br/>kept for inspection,<br/>then tidied by history limits"]
    cj -->|"1 on schedule, creates"| j -->|"2 creates"| p -->|"3 finishes"| done

❓ काय

🤔 का

CronJobs शिवाय, ठरलेले काम कोणाच्या तरी VM च्या crontab मध्ये राहते जे कोणालाच आठवत नाही (drift! ArgoCD कोर्सचा धडा 02 नमस्कार म्हणतो) — किंवा त्याहून वाईट, एका pod वर app च्याच आत ("cleanup कोणती replica चालवते?" — 2 replicas असतील तर दोन्ही, दोनदा 😱). Manifest म्हणून, backup versioned, reviewed, स्वतः बरा होणारा, आणि सगळ्यांना दिसणारा असतो — बाकी सगळ्यासारखाच GitOps-योग्य.

🧪 करून पाहा

# homework: one Job, watch it complete
kubectl -n school create job hello-hw --image=busybox -- sh -c 'echo homework done; sleep 2'
kubectl -n school get jobs,pods | grep hello
kubectl -n school logs job/hello-hw            # "homework done"

# the bell, sped up — every minute, so you can watch:
kubectl -n school create cronjob tick --image=busybox --schedule="*/1 * * * *" \
  -- sh -c 'date; echo the bell rang'
kubectl -n school get jobs -w                  # a new Job appears each minute; Ctrl+C
kubectl -n school delete cronjob tick && kubectl -n school delete job hello-hw

# the REAL one — read it, then apply (safe: suspended):
kubectl apply -f k8s/backup-cronjob.yaml
kubectl -n school get cronjob school-db-backup     # SUSPEND: True
# when you have a real DB: flip suspend to false, or fire one manually:
kubectl -n school create job backup-now --from=cronjob/school-db-backup

🔧 या धड्यासाठी kubectl

kubectl get cronjob,job · kubectl create job --from=

🔗 यावर आधारित: 🪪 AWS L14 (S3 backups) · L19 (घंटा → Lambda)

✅ तपासा — तुम्हाला काय दिसले पाहिजे

kubectl create job --from=cronjob/school-db-backup now → job चा pod pg_dump चालवतो, upload करतो, आणि Completed दाखवतो.

🧹 साफसफाई

kubectl delete job now; CronJob ठेवा (खात्री नसेल तर suspend: true)

⚠️ नेहमीच्या चुका

⏭️ पुढे

सध्या प्रत्येक pod प्रत्येक दुसऱ्या pod शी कुजबुजू शकतो — database शीसुद्धा. चिठ्ठ्या पाठवण्याच्या नियमांची वेळ: NetworkPolicies.

git checkout lesson-19-network-policies

⏰ Lesson 18 — Jobs & CronJobs: homework and the morning bell

📍 You are here: Lesson 18 of 26 · Previous: lesson-17-rbac · Next: lesson-19-network-policies


📦 What's in this branch

Everything before, plus work that isn't a server: run-once Jobs, scheduled CronJobs — and a real deliverable:

🧒 Explain like I'm 5

Everything so far was kids who stand at their post all day (servers: Deployments keep them there forever). But schools have two other kinds of work:

Our bell: every night, a pod is born, runs pg_dump against the database, ships the file to the archive, and dies. Beautiful, boring, exactly what lesson 12's "per-school backups" story needed.

🗺️ Diagram

flowchart LR
    cj["🔔 CronJob<br/>schedule: 0 2 * * *<br/>(every night, 2 AM)"]
    j["📝 Job<br/>tonight's homework<br/>retries: backoffLimit 2"]
    p["🪑 pod<br/>pg_dump → upload<br/>then EXITS 0"]
    done["✅ Completed<br/>kept for inspection,<br/>then tidied by history limits"]
    cj -->|"1 on schedule, creates"| j -->|"2 creates"| p -->|"3 finishes"| done

❓ What

🤔 Why

Without CronJobs, scheduled work lives in some VM's crontab nobody remembers (drift! lesson 02 of the ArgoCD course says hi) — or worse, inside the app on one pod ("which replica runs the cleanup?" — with 2 replicas, both do, twice 😱). As a manifest, the backup is versioned, reviewed, self-healing, and visible to everyone — GitOps-able like everything else.

🧪 Try it

# homework: one Job, watch it complete
kubectl -n school create job hello-hw --image=busybox -- sh -c 'echo homework done; sleep 2'
kubectl -n school get jobs,pods | grep hello
kubectl -n school logs job/hello-hw            # "homework done"

# the bell, sped up — every minute, so you can watch:
kubectl -n school create cronjob tick --image=busybox --schedule="*/1 * * * *" \
  -- sh -c 'date; echo the bell rang'
kubectl -n school get jobs -w                  # a new Job appears each minute; Ctrl+C
kubectl -n school delete cronjob tick && kubectl -n school delete job hello-hw

# the REAL one — read it, then apply (safe: suspended):
kubectl apply -f k8s/backup-cronjob.yaml
kubectl -n school get cronjob school-db-backup     # SUSPEND: True
# when you have a real DB: flip suspend to false, or fire one manually:
kubectl -n school create job backup-now --from=cronjob/school-db-backup

🔧 kubectl for this lesson

kubectl get cronjob,job · kubectl create job --from=

🔗 Builds on: 🪪 AWS L14 (S3 backups) · L19 (the bell → Lambda)

✅ Verify — what you should see

kubectl create job --from=cronjob/school-db-backup now → the job pod runs pg_dump, uploads, and shows Completed.

🧹 Clean up

kubectl delete job now; keep the CronJob (suspend: true if unsure)

⚠️ Common mistakes

⏭️ Next

Right now every pod can whisper to every other pod — even to the database. Time for passing-notes rules: NetworkPolicies.

git checkout lesson-19-network-policies
← PreviousrbacNext →network policies

This page is the lesson's README from the lesson-18-jobs-cronjobs branch, shown here so the whole School stays on one site. Code files open on GitHub at the same branch.