0

I have a stateful set with a volume that uses a subPath: $(POD_NAME) I've also tried $HOSTNAME which also doesn't work. How does one set the subPath of a volumeMount to the name of the pod or the $HOSTNAME?

Here's what I have:

apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: ravendb
  namespace: pltfrmd
  labels:
    app: ravendb
spec:
  serviceName: ravendb
  template:
    metadata:
      labels:
        app: ravendb
    spec:
      containers:
        - command:
            # ["/bin/sh", "-ec", "while :; do echo '.'; sleep 6 ; done"]
            - /bin/sh
            - -c
            - /opt/RavenDB/Server/Raven.Server --log-to-console  --config-path /configuration/settings.json
          image: ravendb/ravendb:latest
          imagePullPolicy: Always
          name: ravendb
          env:
            - name: POD_HOST_NAME
              valueFrom:
                fieldRef:
                  fieldPath: metadata.name
            - name: RAVEN_Logs_Mode
              value: Information
          ports:
            - containerPort: 8080
              name: http-api
              protocol: TCP
            - containerPort: 38888
              name: tcp-server
              protocol: TCP
            - containerPort: 161
              name: snmp
              protocol: TCP
          terminationMessagePath: /dev/termination-log
          terminationMessagePolicy: File
          volumeMounts:
            - mountPath: /data
              name: data
              subPath: $(POD_NAME)
            - mountPath: /configuration
              name: configuration
              subPath: ravendb
            - mountPath: /certificates
              name: certificates
      dnsPolicy: ClusterFirst
      restartPolicy: Always
      terminationGracePeriodSeconds: 120
      volumes:
        - name: certificates
          secret:
            secretName: ravendb-certificate
        - name: configuration
          persistentVolumeClaim:
            claimName: configuration
        - name: data
          persistentVolumeClaim:
            claimName: ravendb

And the Persistent Volume:

apiVersion: v1
kind: PersistentVolume
metadata:
  namespace: pltfrmd
  name: ravendb
  labels:
    type: local
spec:
  storageClassName: local-storage
  capacity:
    storage: 30Gi
  accessModes:
    - ReadWriteOnce
  hostPath:
    path: /volumes/ravendb
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  namespace: pltfrmd
  name: ravendb
spec:
  storageClassName: local-storage
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 30Gi

$HOSTNAME used to work, but doesn't anymore for some reason. Wondering if it's a bug in the host path storage provider?

James Hancock
  • 3,348
  • 5
  • 34
  • 59

2 Answers2

1

Ok, so after great experimentation I found a way that still works:

Step one, map an environment variable:

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

This creates $(POD_HOST_NAME) based on the field metadata.name

Then in your mount you do this:

  volumeMounts:
    - mountPath: /data
      name: data
      subPathExpr: $(POD_HOST_NAME)

It's important to use subPathExpr as subPath (which worked before) doesn't work. Then it will use the environment variable you created and properly expand it.

James Hancock
  • 3,348
  • 5
  • 34
  • 59
0
apiVersion: v1
kind: Service
metadata:
  name: nginx
  labels:
    app: nginx
spec:
  ports:
  - port: 80
    targetPort: 80
  selector:
    app: nginx
---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: nginx
spec:
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.10
        ports:
        - containerPort: 80
        volumeMounts:
        - name: nginx
          mountPath: /usr/share/nginx/html
          subPath: $(POD_NAME)
      volumes:
      - name: nginx
        configMap:
          name: nginx
Adriaan
  • 17,741
  • 7
  • 42
  • 75
  • Welcome to Stack Overflow! Please read [answer] and [edit] your answer to contain an explanation as to why this code would actually solve the problem at hand. Always remember that you're not only solving the problem, but are also educating the OP and any future readers of this post. – Adriaan Oct 07 '22 at 07:33
  • Using $(POD_NAME) in the sub path just ends up creating a folder $(POD_Name) in the volume not the actual pod name. – James Hancock Oct 13 '22 at 18:01