Refer Spec template variables in Kubernetes for hostpath

4/19/2020

I am writing a manifest for app deployment in Kubernetes v1.18.

I am hoping to have the hostpath directory created with POD name in it. Is this possible? It don't seem to work.

 - name: datastore
    hostPath:
      path: /opt/datastore/${spec.podName}
      type: DirectoryOrCreate
-- Venu S
kubernetes

1 Answer

4/19/2020

You could define an environment variable POD_NAME and refer to it

apiVersion: v1
kind: Pod
metadata:
  name: pod1
spec:
  containers:
  - name: container1
    env:
    - name: POD_NAME
      valueFrom:
        fieldRef:
          apiVersion: v1
          fieldPath: metadata.name
    image: busybox
    command: [ "sh", "-c", "while [ true ]; do echo 'Hello'; sleep 10; done | tee -a /logs/hello.txt" ]
    volumeMounts:
    - name: workdir1
      mountPath: /logs
      subPathExpr: $(POD_NAME)
  restartPolicy: Never
  volumes:
  - name: workdir1
    hostPath:
      path: /var/log/pods
-- Arghya Sadhu
Source: StackOverflow