StatefulSet - Get starting pod during volumemount

10/18/2018

I have a StatefulSet that starts a MYSQL cluster. The only downside at it for the moment is that for every replica I need to create a Persistent Volume and a Persistent Volume Claim with a select that matches label and podindex. This means I cannot dynamically add replicas whithout manual interaction.

For this reason I'm searching for a soluction that gives me the option to have only 1 Volume and 1 Claim. And during the pod creation it knows his own pod name for the subPath during mount. (initContainer would be used to check and create the directories on the volume before the application container starts).

So I search a correct way for a code like:

volumeMounts:
- name: mysql-datadir
  mountPath: /var/lib/mysql
  subPath: "${PODNAME}/datadir"
-- Yannick De Backer
kubernetes

1 Answer

10/18/2018

You can get POD_NAME from the metadata ( the downward API ) by setting ENV var:

  env:
   - name: MY_POD_NAME
      valueFrom:
        fieldRef:
          fieldPath: metadata.name

But, you you cannot use ENV vars in volumes declarations (as far as i know...). So, everything else could be reached via workarounds. One of the workarounds is described here

-- Konstantin Vustin
Source: StackOverflow