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 finish without calling Shutdown your job will be in a NotReady state with only the daprd container running endlessly.

Be sure and use the POST HTTP verb when calling the shutdown 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 SDK

  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 June 23, 2022: Merge pull request #2550 from ItalyPaleAle/cosmosdb-harcoded-dapr-version (cf03237)