Running Dapr with a Kubernetes Job

Use Dapr API in a Kubernetes Job context

The Dapr sidecar is designed to be a long running process. In the context of a Kubernetes Job this behavior 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 need to call the /shutdown endpoint for the sidecar to gracefully stop and the job to be considered Completed.

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

Stopping the Dapr sidecar causes its readiness and liveness probes to fail in your container.

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. For example:

  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. For example, for the Go 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 March 21, 2024: Merge pull request #4082 from newbe36524/v1.13 (f4b0938)