Docs / Kubernetes & Orchestration / Kubernetes CronJobs for Scheduled Tasks

Kubernetes CronJobs for Scheduled Tasks

By Admin · Apr 1, 2026 · Updated Apr 23, 2026 · 5 views · 3 min read

Managing cronjobs effectively is a crucial skill for any system administrator. This tutorial provides step-by-step instructions for scheduled configuration, along with best practices for production environments.

Deploying the Application

The default configuration works well for development environments, but production servers require additional tuning. Pay particular attention to connection limits, timeout values, and logging settings.


# deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: cronjobs-app
  labels:
    app: cronjobs
spec:
  replicas: 2
  selector:
    matchLabels:
      app: cronjobs
  template:
    metadata:
      labels:
        app: cronjobs
    spec:
      containers:
      - name: cronjobs
        image: cronjobs:latest
        ports:
        - containerPort: 8080
        resources:
          requests:
            memory: "128Mi"
            cpu: "250m"
          limits:
            memory: "256Mi"
            cpu: "500m"

These commands should be run as root or with sudo privileges. If you're using a non-root user, prefix each command with sudo.

Configuring Services and Ingress

The scheduled component plays a crucial role in the overall architecture. Understanding how it interacts with cronjobs will help you make better configuration decisions.


# Apply the configuration
kubectl apply -f deployment.yaml
kubectl apply -f service.yaml

# Verify the deployment
kubectl get pods -l app=cronjobs
kubectl describe deployment cronjobs-app
kubectl logs -f deployment/cronjobs-app

Make sure to restart the service after applying these changes. Some settings require a full restart rather than a reload to take effect.

Setting Up Persistent Storage

For production deployments, consider implementing high availability by running multiple instances behind a load balancer. This approach provides both redundancy and improved performance under heavy load.


# service.yaml
apiVersion: v1
kind: Service
metadata:
  name: cronjobs-service
spec:
  selector:
    app: cronjobs
  ports:
  - port: 80
    targetPort: 8080
  type: ClusterIP

Note that file paths may vary depending on your Linux distribution. The examples here are for Debian/Ubuntu; adjust paths accordingly for RHEL/CentOS-based systems.

Important Notes

Security should be a primary consideration when configuring cronjobs. Always use strong passwords, keep software updated, and restrict network access to only the necessary ports and IP addresses.

Scaling and Resource Management

Performance benchmarks show that properly tuned cronjobs can handle significantly more concurrent connections than the default configuration. The key improvements come from adjusting worker processes and connection pooling.


# deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: cronjobs-app
  labels:
    app: cronjobs
spec:
  replicas: 2
  selector:
    matchLabels:
      app: cronjobs
  template:
    metadata:
      labels:
        app: cronjobs
    spec:
      containers:
      - name: cronjobs
        image: cronjobs:latest
        ports:
        - containerPort: 8080
        resources:
          requests:
            memory: "128Mi"
            cpu: "250m"
          limits:
            memory: "256Mi"
            cpu: "500m"

Make sure to restart the service after applying these changes. Some settings require a full restart rather than a reload to take effect.

Common Issues and Solutions

  • High memory usage: Review the configuration for memory-related settings. Reduce worker counts or buffer sizes if running on a low-RAM VPS.
  • Connection timeout: Verify your firewall rules allow traffic on the required ports. Use ss -tlnp to confirm the service is listening on the expected port.
  • Service won't start: Check the logs with journalctl -xe -u cronjobs. Common causes include port conflicts, missing configuration files, or insufficient permissions.

Wrapping Up

Following this guide, your cronjobs setup should be production-ready. Keep an eye on resource usage as your traffic grows and don't forget to test your backup and recovery procedures periodically.

Was this article helpful?