Can I have different host mount in pods from same deployment?

6/4/2019

For logs, I mount a volume from host on to the pod. This is written in the deployment yaml. But, if my 2 pods run on the same host, there will be conflict as both pods will produce log files with same name. Can I use some dynamic variables in deployment file so that mount on host is created with different name for different pods?

-- Tarun Dev
containers
kubernetes

3 Answers

5/13/2020

I had to spend hours for this, your solution worked like a charm! Had tried with, none worked despite being given in multiple documents. subPathExpr: "$POD_NAME" subPathExpr: $POD_NAME subPathExpr: ${POD_NAME}

Finally this worked, subPathExpr: $(POD_NAME)

-- Chetan
Source: StackOverflow

6/5/2019

look at pod affinity/anti affinity to not to schedule the replica on the same node. that way each replica of a specific deployment gets deployed on separate node. you will not have to bother about same folder being used by multiple pods.

-- P Ekambaram
Source: StackOverflow

6/4/2019

you can use subPathExpr to achieve the uniqueness in the absolute path, this is one of the use case of the this feature. As of now its is alpha in k1.14.

In this example, a Pod uses subPathExpr to create a directory pod1 within the hostPath volume /var/log/pods, using the pod name from the Downward API. The host directory /var/log/pods/pod1 is mounted at /logs in the container.

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
-- Suresh Vishnoi
Source: StackOverflow