Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call endpoint from Kubernetes Cron Job

I have a webapp running in a Docker-container in a Kubernetes cluster. The app has an endpoint I want to be called periodically. The app runs at multiple nodes/pods, and it is important that only one node performs the task initiated by the endpoint. I have looked at Kubernetes Cron Jobs, but have not found any documentation on calling endpoints from a Kubernetes Cron Job. Does anybody have any proposal for a solution of this problem? How do you handle scheduling in a cluster where it is crucial that only one node performs the task?

like image 223
user1119371 Avatar asked Nov 09 '17 13:11

user1119371


People also ask

How Kubernetes cron job works?

When a CronJob resource is created, what Kubernetes actually does is to register a schedule. Every 10 seconds the CronJob Controller checks if there are matching schedules to take care of. When the proper time arrives a new Job resource is created to handle the task for that specific run.

How do I trigger a cron job manually in Kubernetes?

To manually run a CronJob as a Job you run the kubectl create job command. You then specify the CronJob to base the job off of using the --from flag. Lastly, you specify a unique name for the job.

What is backoffLimit in Kubernetes?

backoffLimit to specify the number of retries before considering a Job as failed. The back-off limit is set by default to 6. Failed Pods associated with the Job are recreated by the Job controller with an exponential back-off delay (10s, 20s, 40s ...) capped at six minutes.


1 Answers

CronJobs are a good choice. Here's a quick layout that runs 3 nginx pods accepting all traffic. Every minute, a Job curls 1 of the 3 pods (always the same pod).

apiVersion: apps/v1beta1
kind: Deployment
metadata:
  name: main
  labels:
    app: nginx
spec:
  replicas: 2
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.7.9
        ports:
        - containerPort: 80

---

apiVersion: apps/v1beta1
kind: Deployment
metadata:
  name: singleton
  labels:
    app: nginx
    special: singleton
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nginx
      special: singleton
  template:
    metadata:
      labels:
        app: nginx
        special: singleton
    spec:
      containers:
      - name: nginx
        image: nginx:1.7.9
        ports:
        - containerPort: 80

---

kind: Service
apiVersion: v1
metadata:
  name: allpods
spec:
  selector:
    app: nginx
  ports:
  - protocol: TCP
    port: 80
    targetPort: 80

---

kind: Service
apiVersion: v1
metadata:
  name: singleton
spec:
  selector:
    special: singleton
  ports:
  - protocol: TCP
    port: 80
    targetPort: 80

---

apiVersion: batch/v1beta1
kind: CronJob
metadata:
  name: callout
spec:
  schedule: "*/1 * * * *"
  concurrencyPolicy: Forbid
  successfulJobsHistoryLimit: 1
  failedJobsHistoryLimit: 1
  jobTemplate:
    spec:
      template:
        spec:
          containers:
          - name: callout
            image: buildpack-deps:curl
            args:
            - /bin/sh
            - -ec
            - curl http://singleton
          restartPolicy: Never
like image 81
coreypobrien Avatar answered Oct 20 '22 15:10

coreypobrien