0

Is there any way to stop restarting pod again and again when inside container fails.

simple way to check this is to pass [ "exit", "1" ] to pod as-

enter code here
apiVersion: v1
kind: Pod
metadata:
  name: myapp-pod
  labels:
    app: myapp
spec:
  containers:
  - name: myapp-container
    image: alpine:latest
    command: ['sh','-c','exit','1']

Note -
RestartPolicy : Never this option is unsupported.

Yogesh Jilhawar
  • 5,605
  • 8
  • 44
  • 59

2 Answers2

2

You will need to change the restart policy of the pod:

A PodSpec has a restartPolicy field with possible values Always, OnFailure, and Never. The default value is Always. restartPolicy applies to all Containers in the Pod. restartPolicy only refers to restarts of the Containers by the kubelet on the same node. Exited Containers that are restarted by the kubelet are restarted with an exponential back-off delay (10s, 20s, 40s …) capped at five minutes, and is reset after ten minutes of successful execution. As discussed in the Pods document, once bound to a node, a Pod will never be rebound to another node.

See more details here and here.

omricoco
  • 823
  • 5
  • 15
  • 1
    This option will not work... Already checked....`The StatefulSet "my-app" is invalid: spec.template.spec.restartPolicy: Unsupported value: "Never": supported values: "Always"` – Yogesh Jilhawar May 20 '20 at 02:23
  • You did not mention you're deploying a statefulSet, If you are looking to run a container to completion (and also combine it with a restart policy), you should be working with a jobs rather than a statefulsets, read more here: https://kubernetes.io/docs/concepts/workloads/controllers/jobs-run-to-completion/ – omricoco May 20 '20 at 07:01
-1

You're using StatefulSet and it's responsible to restart pod if one container is failed. If your container is a sidecar and it doesn't matter if it fails or not, you can wrap the container's command and exit 0 every time it fails and then StatefulSet doesn't detect any failure. For example, you can use this command:

exit 1 & # your container's command should be here
PID=$!
wait $PID
exit 0
alidadar7676
  • 137
  • 6