<p>I have the following deployment:</p>
<pre><code>apiVersion: apps/v1
kind: Deployment
metadata:
  name: postgres-deployment
spec:
  replicas: 1
  selector:
    matchLabels:
      component: postgres
  template:
    metadata:
      labels:
        component: postgres
    spec:
      volumes:
        - name: postgres-storage
          persistentVolumeClaim:
            claimName: postgres-persistent-volume-claim
      containers:
        - name: postgres
          image: prikshet/postgres
          ports:
            - containerPort: 5432
          volumeMounts:
            - name: postgres-storage
              mountPath: /var/lib/postgresql/data
              subPath: postgres

</code></pre>
<p>And when I include the lines 23-26 and do <code>kubectl apply</code>, the pod gives an error and doesn't run but when I remove the lines 23-36 the pod runs. I want to create a volume mount with the lines 23-26. The error when checking the logs of this pod is:</p>
<pre><code>postgres: could not access the server configuration file &quot;/var/lib/postgresql/data/postgresql.conf&quot;: No such file or directory
</code></pre>
<p>postgres persistent volume claim is:</p>
<pre><code>apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: postgres-persistent-volume-claim
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 10Gi
</code></pre>
<p>How to fix this?</p>

zendevil.eth
<p>There are actually two possibilities for this error to occur:</p>
<ol>
<li><p>Your file <code>/var/lib/postgresql/data/postgresql.conf</code> does not really actually exist. In this case, you need to create it before mounting it.</p>
</li>
<li><p>As <a href="https://stackoverflow.com/users/9250303/meaningqo">meaningqo</a> good mentioned in the comment, you are using the wrong <a href="https://kubernetes.io/docs/concepts/storage/volumes/#using-subpath" rel="nofollow noreferrer"><code>subPath</code></a>. Your file <code>/var/lib/postgresql/data/postgresql.conf</code> exist, but you are trying to mount wrong file based on <code>subPath</code>. Look at the <a href="https://stackoverflow.com/questions/65399714/what-is-the-difference-between-subpath-and-mountpath-in-kubernetes">explanation</a> the difference between <code>mountPath</code> and <code>subPath</code>:</p>
</li>
</ol>
<blockquote>
<p><code>mountPath</code>  shows where the referenced volume should be mounted in the container. For instance, if you mount a volume to  <code>mountPath: /a/b/c</code>, the volume will be available to the container under the directory  <code>/a/b/c</code>.
Mounting a volume will make all of the volume available under  <code>mountPath</code>. If you need to mount only part of the volume, such as a single file in a volume, you use  <code>subPath</code>  to specify the part that must be mounted. For instance,  <code>mountPath: /a/b/c</code>,  <code>subPath: d</code>  will make whatever  <code>d</code>  is in the mounted volume under directory  <code>/a/b/c</code>
Notice that when <code>subPath</code> is a folder, the content of the folder will be mounted to the <code>mountPath</code></p>
</blockquote>
