0

I have just started with Kubernetes. I need to run a Deployment in Kubernetes with a container that competes for execution after ~10-15 minutes. When I tried, "restart Policy=Never" doesn't hold true with Deployments. Reason for opting for Deployment is to use Replicas.

Please provide your inputs on how I can achieve multiple replicas of my Deployment with the container that completes execution and not keep running.

Andrzej Sydor
  • 1,373
  • 4
  • 13
  • 28
Sherr
  • 71
  • 8
  • Does this answer your question? [Kubernetes Pod for one time initial task](https://stackoverflow.com/questions/62589553/kubernetes-pod-for-one-time-initial-task) – Michael Freidgeim May 23 '23 at 09:30

1 Answers1

2

You can run a Job as below where the container runs a sleep command for 15m. After 15 minutes the container will exit and pod will be terminated.

apiVersion: batch/v1
kind: Job
metadata:
  name: job
spec:
  template:
    spec:
      containers:
      - command:
        - sh
        - -c
        - sleep 15m
        image: bash:5.1.0
      restartPolicy: Never
Arghya Sadhu
  • 41,002
  • 9
  • 78
  • 107
  • The pod keeps restarting after successful completion. Is there a way I can have this Pod run only once and exit through deployment. ~ ''' apiVersion: apps/v1 kind: Deployment metadata: name: app-dep-test1 labels: app: app-dep-test1 spec: #replicas: 3 template: metadata: labels: app: app-dep-test1 spec: containers: - name: nginx-container image: command: #restartPolicy: "Never" selector: matchLabels: app: app-dep-test1 ''' – Sherr Dec 28 '20 at 06:50
  • No..you should use `Job` instead of `Deployment` – Arghya Sadhu Dec 28 '20 at 06:57
  • Looks like OP wants to run multiple replicas, but how would each replica split the workload between them? https://stackoverflow.com/questions/40530946/what-is-the-difference-between-always-and-on-failure-for-kubernetes-restart-poli explains meaning of "Never" explicitly than https://kubernetes.io/docs/concepts/workloads/pods/pod-lifecycle/#restart-policy – Sameer Naik Dec 28 '20 at 08:21