Check status of batch job in init container

12/4/2019

My requirement is to start a pod when a particular batch job is completed.

Batch job yaml file

apiVersion: batch/v1
kind: Job
metadata:
  name: topics
spec:
  ttlSecondsAfterFinished: 100
  template:
    metadata:
      labels:
        app: topics
    spec:
      containers:
      - env:
        name: topics
        image: confluentinc/cp-kafka:5.3.0
        command:
        - sh
        - -c
        - {{.Values.KafkaTopics}}

2 Deployment yaml

apiVersion: apps/v1beta1
kind: Deployment
metadata:
  name: opp
spec:
  replicas: 1
  strategy: {}
  template:
    metadata:
      labels:
        app: opp

      initContainers:
      - name: init
        image: busybox
        command: ['sh', '-c', 'until nc -z someservice:8093; do echo waiting for init; sleep 2; done;']

Init container fine when I am checking for some service to be up. Not able to figure it out for batch job.

-- pythonhmmm
kubernetes

1 Answer

12/4/2019

start a pod

What you have described with a Deployment is a deployment of a service, not only starting a pod.

Watch status of Kubernetes objects

when a particular batch job is completed.

If you want to watch Kubernetes objects and do actions depending on change of status of a particular object, you need to interact with the Kubernetes API server.

Use a Kubernetes client

The easiest way to interact with the Kubernetes API, especially for watch is to use a pre-built client, e.g. client-go or kubernetes-client Java.

Use the Kubernetes REST API

Alternatively you can use the Kubernetes REST API directly.

API Authentication and Authorization

Beware that you should use a Service Account for authentication and set properly RBAC rules for authorization.

Kafka Consumer

An alternative solution, since your Job hints that you are using Kafka. Your Job could publish an event on Kafka, and you can have a Kafka Consumer to subscribe and act upon those events. But if the consumer should deploy a service on an event, it also need an Service Account to interact with the Kubernetes API server.

-- Jonas
Source: StackOverflow