How to pass user credentials to (user-restricted) mounted volume inside Kubernetes Pod?

11/8/2019

I am trying to pass user credentials via Kubernetes secret to a mounted, password protected directory inside a Kubernetes Pod. The NFS folder /mount/protected has user access restrictions, i.e. only certain users can access this folder.

This is my Pod configuration:

apiVersion: v1
kind: Pod
metadata:
  name: my-pod
spec:
  volumes:
  - name: my-volume
    hostPath:
      path: /mount/protected
      type: Directory
    secret:
      secretName: my-secret
  containers:
  - name: my-container
    image: <...>
    command: ["/bin/sh"]
    args: ["-c", "python /my-volume/test.py"]
    volumeMounts:
    - name: my-volume
      mountPath: /my-volume

When applying it, I get the following error:

The Pod "my-pod" is invalid:
* spec.volumes[0].secret: Forbidden: may not specify more than 1 volume type
* spec.containers[0].volumeMounts[0].name: Not found: "my-volume"

I created my-secret according to the following guide:
https://kubernetes.io/docs/tasks/inject-data-application/distribute-credentials-secure/#create-a-secret
So basically:

apiVersion: v1
kind: Secret
metadata:
  name: my-secret
data:
  username: bXktYXBw
  password: PHJlZGFjdGVkPg==

But when I mount the folder /mount/protected with:

spec:
  volumes:
  - name: my-volume
    hostPath:
      path: /mount/protected
      type: Directory

I get a permission denied error python: can't open file '/my-volume/test.py': [Errno 13] Permission denied when running a Pod that mounts this volume path.

My question is how can I tell my Pod that it should use specific user credentials to gain access to this mounted folder?

-- Jonas
kubernetes
kubernetes-secrets

2 Answers

11/18/2019

I eventually figured out how to pass user credentials to a mounted directory within a Pod by using CIFS Flexvolume Plugin for Kubernetes (https://github.com/fstab/cifs). With this Plugin, every user can pass her/his credentials to the Pod. The user only needs to create a Kubernetes secret (cifs-secret), storing the username/password and use this secret for the mount within the Pod. The volume is then mounted as follows:

  (...)
  volumes:
  - name: test
    flexVolume:
      driver: "fstab/cifs"
      fsType: "cifs"
      secretRef:
        name: "cifs-secret"
      options:
        networkPath: "//server/share"
        mountOptions: "dir_mode=0755,file_mode=0644,noperm"
-- Jonas
Source: StackOverflow

11/8/2019

You're trying to tell Kubernetes that my-volume should get its content from both a host path and a Secret, and it can only have one of those.

You don't need to manually specify a host path. Kubernetes will figure out someplace appropriate to put the Secret content and it will still be visible on the mountPath you specify within the container. (Specifying hostPath: at all is usually wrong, unless you can guarantee that the path will exist with the content you expect on every node in the cluster.)

So change:

volumes:
- name: my-volume
  secret:
    secretName: my-secret
  # but no hostPath
-- David Maze
Source: StackOverflow