Set pods controlled by a statefulset to COMPLETED state

256 views Asked by At

Is there any way to keep a pod operated by a statefulset to COMPLETED state after some logic is executed? I understand that a kubernetes JOB is more suitable for such operation instead of a STATEFULSET but I cannot use a job as I need to use volumeclaimtemplate to create separate pvc for each pod of my application.

Currently after execution of my logic, the pod is going to COMPLETED state but the statefulset is restarting the pod. Even if I delete the statefulset programatically after my logic is executed, the pod is not staying in the COMPLETED state.

Can someone help me with this? Thanks in advance!

1

There are 1 answers

4
Lukman On

You can make the StatefulSet's pods to have an init container that performs your logic and then add a dummy long-running container to prevent the pods from being restarted.

apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: hello-world
spec:
  replicas: 1
  selector:
    matchLabels:
      app: hello-world
  template:
    metadata:
      labels:
      app: hello-world
    spec:
      initContainers:
      - name: logic
        image: busybox:latest
        imagePullPolicy: Always
        command:
        - sh
        - -c
        - |
          echo "do something"
      containers:
      - name: dummy
        image: nginx:latest
        imagePullPolicy: Always            
...