DEV Community

Cover image for Part-100: ⏰ Implement CronJobs in Google Kubernetes Engine (GKE)
Latchu@DevOps
Latchu@DevOps

Posted on

Part-100: ⏰ Implement CronJobs in Google Kubernetes Engine (GKE)

  • In Kubernetes, CronJobs are used to run tasks on a schedule — just like Linux cron jobs.
  • They are great for automating repetitive tasks like backups, reports, or sending notifications.

🔹 Step 1: What is a CronJob?

  1. A CronJob creates a Job on a schedule.
  2. Jobs then create Pods that run to completion.
  3. Schedule is defined using cron syntax (e.g., */1 * * * * = every 1 minute).
  4. Useful for:
  • Database backups
  • Log rotation
  • Report generation
  • Cleanup tasks

🔹 Step 2: Example CronJob Manifest (cron-job.yaml)

apiVersion: batch/v1
kind: CronJob
metadata:
  name: cron-job-demo
spec:
  schedule: "*/1 * * * *"    # Runs every 1 minute
  concurrencyPolicy: Allow   # Multiple jobs can run at the same time
  startingDeadlineSeconds: 100
  suspend: false             # If true, suspends job execution
  successfulJobsHistoryLimit: 3
  failedJobsHistoryLimit: 1
  jobTemplate:
    spec:
      template:
        spec:
          containers:
          - name: hello
            image: busybox
            args:
            - /bin/sh
            - -c
            - date; echo "Hello, World!"
          restartPolicy: OnFailure
Enter fullscreen mode Exit fullscreen mode

👉 Here, every minute, a new Pod runs and prints:

Hello, World!


🔹 Step 3: Deploy and Verify

# Deploy CronJob
kubectl apply -f cron-job.yaml

# List CronJobs
kubectl get cronjob
kubectl get cj   # shorthand

# Describe CronJob
kubectl describe cronjob cron-job-demo
Enter fullscreen mode Exit fullscreen mode

j1

📌 Observation:

You’ll see events showing the CronJob is scheduled.

Every minute, a new Pod is created and runs to completion.

# List Pods
kubectl get pods
kubectl get pods --watch
Enter fullscreen mode Exit fullscreen mode

Sample Output (Pods created each minute):

NAME                        READY   STATUS      AGE
cron-job-demo-28017695-vkh  0/1     Completed   40s
cron-job-demo-28017700-tnc  0/1     Completed   20s
Enter fullscreen mode Exit fullscreen mode

j2


🔹 Step 4: Clean-Up

When you’re done:

kubectl delete cronjob cron-job-demo
Enter fullscreen mode Exit fullscreen mode

🎯 Final Thoughts

CronJobs are perfect for automated, scheduled tasks in Kubernetes.

Always tune:

  • concurrencyPolicy → Avoid overlapping jobs if not needed.
  • successfulJobsHistoryLimit & failedJobsHistoryLimit → Keep logs clean.
  • suspend → Useful for pausing without deleting.

🌟 Thanks for reading! If this post added value, a like ❤️, follow, or share would encourage me to keep creating more content.


— Latchu | Senior DevOps & Cloud Engineer

☁️ AWS | GCP | ☸️ Kubernetes | 🔐 Security | ⚡ Automation
📌 Sharing hands-on guides, best practices & real-world cloud solutions

Top comments (0)