Kubernetes container affinity based on replica?

8/25/2018

I have a StatefulSet with 10 replicas, each of which have hundreds of GB of data.

I'd like to run a backup command to an object store (e.g. S3) on just 1 of the 10 replicas. (so the data can be loaded in an initContainer step)

Is it possible to either schedule container using affinity based on replica ID? Or is it possible to retrieve the replica ID from an environment variable?

-- vonneumann1903
kubernetes
kubernetes-statefulset

1 Answer

9/4/2018

I've iterated through 2 solutions on this so far:

using the $HOSTNAME env var to check that the current pod is replica 0

This works if you can tolerate taking backups while the pod is running

- "[ $HOSTNAME = 'mypod-0' ] && /start-backup.sh || echo 'noop' && sleep 10000"

a script that administers the cluster

but if the pod must flush state to disk before a backup, then create a script (I used Node.js) to do this:

  1. bring replica count down by 1
  2. attach disk from that replica that was brought down to a backup pod
  3. bring replica count up by 1
-- vonneumann1903
Source: StackOverflow