Search code examples
kuberneteskubernetes-health-check

Monitoring a kubernetes job


I have kubernetes jobs that takes variable amount of time to complete. Between 4 to 8 minutes. Is there any way i can know when a job have completed, rather than waiting for 8 minutes assuming worst case. I have a test case that does the following:

1) Submits the kubernetes job.
2) Waits for its completion.
3) Checks whether the job has had the expected affect.

Problem is that in my java test that submits the deployment job in the kubernetes, I am waiting for 8 minutes even if the job has taken less than that to complete, as i dont have a way to monitor the status of the job from the java test.


Solution

  • <kube master>/apis/batch/v1/namespaces/default/jobs 
    

    endpoint lists status of the jobs. I have parsed this json and retrieved the name of the latest running job that starts with "deploy...".

    Then we can hit

    <kube master>/apis/batch/v1/namespaces/default/jobs/<job name retrieved above>
    

    And monitor the status field value which is as below when the job succeeds

    "status": {
        "conditions": [
          {
            "type": "Complete",
            "status": "True",
            "lastProbeTime": "2016-09-22T13:59:03Z",
            "lastTransitionTime": "2016-09-22T13:59:03Z"
          }
        ],
        "startTime": "2016-09-22T13:56:42Z",
        "completionTime": "2016-09-22T13:59:03Z",
        "succeeded": 1
      }
    

    So we keep polling this endpoint till it completes. Hope this helps someone.