<p>According to <a href="https://kubernetes.io/docs/tasks/configure-pod-container/security-context/" rel="nofollow noreferrer">the kubernetes doc</a>, <code>securityContext.fsGroup</code> can be used to specify the permissions of a mounted volume:</p>

<blockquote>
  <p>Since fsGroup field is specified, all processes of the container are also part of the supplementary group ID 2000. The owner for volume /data/demo and any files created in that volume will be Group ID 2000.</p>
</blockquote>

<p>I can't get that feature to work. </p>

<p><em>test.yaml</em></p>

<pre><code>apiVersion: v1
kind: PersistentVolume
metadata:
  name: task-pv-volume
  labels:
    type: local
spec:
  storageClassName: manual
  capacity:
    storage: 1Gi
  accessModes:
    - ReadWriteOnce
  hostPath:
    path: /home/docker/mypv2
---
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: task-pv-claim
spec:
  storageClassName: manual
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 1Gi
---
apiVersion: v1
kind: Pod
metadata:
  name: mypod
spec:
  securityContext:
    runAsUser: 1001
    runAsGroup: 1001
    fsGroup: 1001
  volumes:
    - name: myvolume
      persistentVolumeClaim:
        claimName: task-pv-claim
  containers:
    - name: mycontainer
      image: myimage:latest
      command: ['sleep', 'infinity']
      volumeMounts:
        - mountPath: "/mytest"
          name: myvolume
</code></pre>

<p><em>Dockerfile</em></p>

<pre><code>FROM python:3.6

RUN groupadd --gid 1001 mygroup
RUN useradd --uid 1001 --gid mygroup --shell /bin/bash --create-home myuser
</code></pre>

<p>When I run a shell on the pod, I'm correctly logged as user 1001 (<code>myuser</code>). However, the mounted volume is owned by group <code>root</code>:</p>

<pre><code>myuser@mypod:/$ ls -la /mytest
total 8
drwxr-xr-x 2 root root 4096 Jun 12 18:49 .
drwxr-xr-x 1 root root 4096 Jun 12 18:49 ..
</code></pre>

<p>Tested on minikube 1.11.0 with kubernetes 1.18.0</p>

<p>Note: In deployment scenarios, I don't have access to the kubernetes host, so I can't run chown or chmod there.</p>

Aurélien Gasser
<p>You can add <strong>initContainers</strong> to set proper permission</p>

<pre><code>  initContainers:
  - name: volume-permission
    image: busybox
    command: ["sh", "-c", "chown -R 1001:1001 /mytest"]
    volumeMounts:
    - name: myvolume
      mountPath: /mytest
    securityContext:
      runAsUser: 0

</code></pre>
