Running Dapr with a Kubernetes Job

Use Dapr API in a Kubernetes Job context

Kubernetes Job

The Dapr sidecar is designed to be a long running process, in the context of a Kubernetes Job this behaviour can block your job completion. To address this issue the Dapr sidecar has an endpoint to Shutdown the sidecar.

When running a basic Kubernetes Job you will need to call the /shutdown endpoint for the sidecar to gracefully stop and the job will be considered Completed.

When a job is finished without calling Shutdown, your job will be in a NotReady state with only the daprd container running endlessly.

Stopping the dapr sidecar will cause its readiness and liveness probes to fail in your container because the dapr sidecar was shutdown. To prevent Kubernetes from trying to restart your job, set your job’s restartPolicy to Never.

Be sure to use the POST HTTP verb when calling the shutdown HTTP API.

  1. apiVersion: batch/v1
  2. kind: Job
  3. metadata:
  4. name: job-with-shutdown
  5. spec:
  6. template:
  7. metadata:
  8. annotations:
  9. dapr.io/enabled: "true"
  10. dapr.io/app-id: "with-shutdown"
  11. spec:
  12. containers:
  13. - name: job
  14. image: alpine
  15. command: ["/bin/sh", "-c", "apk --no-cache add curl && sleep 20 && curl -X POST localhost:3500/v1.0/shutdown"]
  16. restartPolicy: Never

You can also call the Shutdown from any of the Dapr SDKs

  1. package main
  2. import (
  3. "context"
  4. "log"
  5. "os"
  6. dapr "github.com/dapr/go-sdk/client"
  7. )
  8. func main() {
  9. client, err := dapr.NewClient()
  10. if err != nil {
  11. log.Panic(err)
  12. }
  13. defer client.Close()
  14. defer client.Shutdown()
  15. // Job
  16. }

Last modified December 21, 2022: Docs upmerge 12/16 (#3015) (268da740)