<p>I have an azure container service (aks) cluster. It is migrated to version 1.8.1. I am trying to deploy postgres database and use <code>AzureFileVolume</code> to persist postgres data on.</p>

<p>By default, if I deploy the postgres database without mounting volume, everything is working as excepted, i.e. pod is created and database is initialized.</p>

<p>When I try to mount a volume using the yaml below, I get <strong>initdb: could not access directory "/var/lib/postgresql/data": Permission denied</strong>.</p>

<p>I tried various hacks as suggested in this long <a href="https://github.com/kubernetes/kubernetes/issues/2630" rel="nofollow noreferrer">github thread</a>, like: setting security context for the pod or running chown commands in <em>initContainers</em>. The result was the same - permission denied.</p>

<p>Any ideas would be appreciated.</p>

<pre><code>apiVersion: v1
kind: Service
metadata:
  labels:
    app: myapp
    component: test-db
  name: test-db
spec:
  ports:
    - port: 5432
  selector:
    app: myapp
    component: test-db
---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: test-db
spec:
  template:
    metadata:
      labels:
        app: myapp
        component: test-db
    spec:
      securityContext:
        fsGroup: 999
        runAsUser: 999      
      containers:
      - name: test-db  
        image: postgres:latest  
        securityContext:
          allowPrivilegeEscalation: false          
        ports:
        - containerPort: 5432
        env:
        - name: POSTGRES_DB
          value: myappdb
        - name: POSTGRES_USER
          value: myappdbuser  
        - name: POSTGRES_PASSWORD
          value: qwerty1234
        volumeMounts:
          - name: azure
            mountPath: /var/lib/postgresql/data              
      volumes:
        - name: azure
          azureFile:
            secretName: azure-secret
            shareName: acishare
            readOnly: false
</code></pre>

<p>We came across the same problems and figured out the following solution:</p>

<p>Instead of using an <em>AzureFileVolume</em>, we used an <strong>AzureDisk</strong>. So what we needed in Kubernetes is the following...</p>

<p><strong>Storage Class</strong></p>

<p><a href="https://i.stack.imgur.com/uvMBA.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/uvMBA.jpg" alt="enter image description here"></a></p>

<p>With your Azure account name</p>

<p><strong>Persistent Volume Claim</strong></p>

<p><a href="https://i.stack.imgur.com/zKMLl.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/zKMLl.jpg" alt="Persistent Volume Claim"></a></p>

<p><strong>PostgreSQL Deployment</strong>
Include PVC in the Kubernetes Deployment</p>

<pre><code>- name: postgres-db
  persistentVolumeClaim:
      claimName: pvc-postgresdb
</code></pre>

<p>Additionally we need to point the PGDATA var to a subdirectory of the mounted directory. Because Azure is creating some issues with the AzureDisk type in the base directory.</p>

<pre><code>#... evn definitions... 
- name: PGDATA
  value: /var/lib/postgresql/data/pgdata
volumeMounts:
- mountPath: /var/lib/postgresql/data/
  name: postgres-db
</code></pre>
