Example showing how to use a StatefulSet in coordination with a CronJob to run a scheduled command on a specific pod.
Download k8s.yaml locally and then run:
kubectl apply -f k8s.yaml && \
kubectl rollout status statefulset/web && \
sleep 65 && \
for podNum in 0 1 2; do echo -n "Pod $podNum: "; kubectl exec pod/web-$podNum -- ls -1 / | grep cron || echo "<notfound>"; done
Expected output:
statefulset.apps/web created
cronjob.batch/crontest created
Waiting for 3 pods to be ready...
Waiting for 2 pods to be ready...
Waiting for 1 pods to be ready...
partitioned roll out complete: 3 new pods have been updated...
Pod 0: <notfound>
Pod 1: ran-cron // <-- cronjob runs against pod 1 only
Pod 2: <notfound>