Writing in a journal
Writing Kubernetes CronJobs Guide
User avatar
Curated by
3 min read
1 month ago
Kubernetes CronJobs are essential tools for scheduling and automating tasks within a Kubernetes cluster, allowing users to execute jobs at specified times or intervals. This guide will explore the steps and considerations involved in writing and managing CronJobs, ensuring tasks like backups, system updates, or batch processing are performed systematically and efficiently.

Detailed CronJob Manifest Example

Here is a comprehensive example of a Kubernetes CronJob manifest that includes detailed configurations for a scheduled task, incorporating various specifications such as resource requests and limits, environment variables, secrets, a restart policy, and a service account:
apiVersion: batch/v1 kind: CronJob metadata: name: advanced-cronjob spec: schedule: "0 1 * * *" startingDeadlineSeconds: 200 concurrencyPolicy: Forbid successfulJobsHistoryLimit: 5 failedJobsHistoryLimit: 2 jobTemplate: spec: template: spec: serviceAccountName: cronjob-service-account containers: - name: task-container image: ubuntu command: ["/bin/bash", "-c", "echo Running a scheduled task"] resources: requests: memory: "64Mi" cpu: "250m" limits: memory: "128Mi" cpu: "500m" env: - name: SIMPLE_ENV_VAR value: "ExampleValue" envFrom: - secretRef: name: my-secret restartPolicy: OnFailure
Explanation of Each Part of the Manifest:
  • schedule: Specifies the CronJob to run at 1:00 AM every day.
  • startingDeadlineSeconds: Allows a job to start if it's within 200 seconds of its scheduled time; otherwise, it's counted as missed.
  • concurrencyPolicy: Forbid: Prevents new jobs from starting if an older one is still running, ensuring that jobs do not overlap.
  • successfulJobsHistoryLimit and failedJobsHistoryLimit: Control the number of completed and failed jobs to retain in history, respectively.
  • serviceAccountName: Specifies the service account under which the job will run. This is important for defining the permissions that the job has within the cluster.
  • containers:
    • resources: Defines the CPU and memory resources to request and the maximum limits that the container can consume, ensuring the container has enough resources to run effectively but also that it does not consume excessive cluster resources.
    • env: Directly sets a simple environment variable SIMPLE_ENV_VAR with a value of "ExampleValue".
    • envFrom: Uses secretRef to inject all key-value pairs from the Kubernetes secret my-secret as environment variables into the container. This method enhances security by not exposing secret values in the manifest.
  • restartPolicy: OnFailure: Specifies that the container should be restarted if it exits due to an error, which helps in maintaining the job's reliability by attempting to rerun failing containers.
cronitor.io favicon
earthly.dev favicon
komodor.com favicon
5 sources

Applying Kubernetes Manifests Correctly

To apply Kubernetes manifests effectively, follow these structured steps, ensuring adherence to organizational conventions and prerequisites:
  1. Repository Structure: Store your Kubernetes manifests within the infrastructure repository following the specific path convention: kubernetes/<name of cluster>/<name of namespace>/manifests/<name of manifest.yaml>. For instance, if you are working on a CronJob for a web application in the 'web' cluster and namespace, the path would be kubernetes/web/web/manifests/my-cron.yaml.
  2. Prerequisites: Ensure you have the necessary Kubernetes access. Detailed instructions for accessing Kubernetes are available within your organization's Notion documentation. This access is crucial for applying and managing Kubernetes resources.
  3. Applying the Manifest:
    • Navigate to the directory where your manifest is stored.
    • Use the kubectl apply -f <path-to-manifest> command to apply your manifest. For example:
      kubectl apply -f kubernetes/web/web/manifests/my-cron.yaml
    • This command will configure the resources described in your manifest in the Kubernetes cluster.
  4. Verifying the Application:
    • After applying the manifest, verify that the CronJob has been correctly created and configured by running:
      kubectl get cronjob -n <namespace>
    • Replace <namespace> with the appropriate namespace to see the status of your CronJob.
  5. Running a Sample Job:
    • Once the CronJob is in place, you can manually trigger a job to test its configuration using the kubectl create job --from command. For example:
      kubectl create job test-job --from=cronjob/my-cron
    • This command creates a one-time job based on the CronJob definition. It is useful for testing and ensuring that the CronJob will perform as expected when triggered according to its schedule.
By following these steps, you can effectively manage and test Kubernetes CronJobs within your organization's infrastructure, adhering to the prescribed file and access conventions.
earthly.dev favicon
komodor.com favicon
kubernetes.io favicon
5 sources

Contacting the Product Infra Team

If you have any follow-up questions or require further clarification on Kubernetes CronJobs, please do not hesitate to reach out to the product infrastructure team. We are here to assist you with any inquiries or issues you may encounter. Your engagement and understanding are important to us, and we are committed to providing the support you need.
cronitor.io favicon
kubernetes.io favicon
spacelift.io favicon
5 sources
what is the best way to contact the product infra team in kubernetes
how long does it usually take for the product infra team to respond to a request for help in kubernetes
what information should be included in an email to the product infra team in kubernetes