krishnanand nb
krishnanand nb

Reputation: 81

How do I manually trigger a kubernates job (not a cron) in k8s

I have sample k8s job as soon as you do kubectl apply the job gets triggered and the pods are created . How to control the pod creation?

apiVersion: batch/v1
kind: Job
metadata:
  name: pi-with-timeout
spec:
  backoffLimit: 5
  activeDeadlineSeconds: 100
  template:
    spec:
      containers:
      - name: pi
        image: perl
        command: ["perl",  "-Mbignum=bpi", "-wle", "print bpi(2000)"]
      restartPolicy: Never

Upvotes: 7

Views: 9299

Answers (3)

kapa
kapa

Reputation: 11

You can use suspend: true and then when you're ready to start the Job, you just update suspend to false.

Ref: https://kubernetes.io/blog/2021/04/12/introducing-suspended-jobs/

Upvotes: 1

ragav ramachandran
ragav ramachandran

Reputation: 398

If you want to manually control the pod creation, you can achieve it through parallelism.

Documentation says:

The requested parallelism (.spec.parallelism) can be set to any non-negative value. If it is unspecified, it defaults to 1. If it is specified as 0, then the Job is effectively paused until it is increased.

You can set it to 0 while doing the kubectl apply. Configuration looks something like below

apiVersion: batch/v1
kind: Job
metadata:
  name: pi-with-timeout
spec:
  backoffLimit: 5
  parallelism: 0
  activeDeadlineSeconds: 100
  template:
    spec:
      containers:
      - name: pi
        image: perl
        command: ["perl",  "-Mbignum=bpi", "-wle", "print bpi(2000)"]
      restartPolicy: Never

You can set it to 1 whenever you decide to run.

Upvotes: 5

coderanger
coderanger

Reputation: 54211

The trigger is running kubectl apply. When you create the Job, it runs. You might be looking for a more fully featured background task system like Airflow or Argo.

Upvotes: 1

Related Questions