How to set persistent volume for container with kubernetes on docker for windows

3/27/2019

I want to use a local folder for a container with kubernetes on docker for windows. When I used hostPath in nginx.yaml, it works well. But I used persistentVolumeClaim instead of hostPath, the container could not mount the volume.

pv.yaml

apiVersion: v1
kind: PersistentVolume
metadata:
  name: nfs-os012
  labels:
    type: local
spec:
  capacity:
    storage: 1Gi
  accessModes:
    - ReadWriteMany
  persistentVolumeReclaimPolicy: Recycle
  storageClassName: hostpath
  mountOptions:
    - hard
  # hostPath:
  #   path: "/c/k"
  nfs:
    path: /c/k/share
    server: localhost

pvc.yaml

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: nfs-claim1
spec:
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 1Gi
  storageClassName: hostpath

nginx.yaml

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: nginx
spec:
  replicas: 2
  template:
    metadata:
      labels:
        run: nginx
    spec:
      containers:
      - name: nginx
        image: nginx
        ports:
        - containerPort: 80
        volumeMounts:
        - mountPath: "/usr/share/nginx/html"
          name: mydate
      volumes:
        - name: mydate
          persistentVolumeClaim:
            claimName: nfs-claim1

log of "kubectl describe pod nginx"

Name:               nginx-dep-6b46bc497f-cqkl7
Namespace:          default
Priority:           0
PriorityClassName:  <none>
Node:               docker-desktop/192.168.65.3
Start Time:         Wed, 27 Mar 2019 16:45:22 +0900
Labels:             pod-template-hash=6b46bc497f
                    run=nginx
Annotations:        <none>
Status:             Pending
IP:
Controlled By:      ReplicaSet/nginx-dep-6b46bc497f
Containers:
  nginx:
    Container ID:
    Image:          nginx
    Image ID:
    Port:           80/TCP
    Host Port:      0/TCP
    State:          Waiting
      Reason:       ContainerCreating
    Ready:          False
    Restart Count:  0
    Environment:    <none>
    Mounts:
      /usr/share/nginx/html from mydate (rw)
      /var/run/secrets/kubernetes.io/serviceaccount from default-token-dccpv (ro)
Conditions:
  Type              Status
  Initialized       True
  Ready             False
  ContainersReady   False
  PodScheduled      True
Volumes:
  mydate:
    Type:       PersistentVolumeClaim (a reference to a PersistentVolumeClaim in the same namespace)
    ClaimName:  nfs-claim11
    ReadOnly:   false
  default-token-dccpv:
    Type:        Secret (a volume populated by a Secret)
    SecretName:  default-token-dccpv
    Optional:    false
QoS Class:       BestEffort
Node-Selectors:  <none>
Tolerations:     node.kubernetes.io/not-ready:NoExecute for 300s
                 node.kubernetes.io/unreachable:NoExecute for 300s
Events:
  Type     Reason       Age                  From                     Message
  ----     ------       ----                 ----                     -------
  Warning  FailedMount  23m (x41 over 173m)  kubelet, docker-desktop  MountVolume.SetUp failed for volume "nfs-os012" : mount failed: exit status 32
Mounting command: mount
Mounting arguments: -t nfs -o hard localhost:/c/k/share /var/lib/kubelet/pods/46d3fd62-5064-11e9-830a-00155d429c07/volumes/kubernetes.io~nfs/nfs-os012
Output: mount.nfs: Connection refused
  Warning  FailedMount  3m54s (x76 over 173m)  kubelet, docker-desktop  Unable to mount volumes for pod "nginx-dep-6b46bc497f-cqkl7_default(46d3fd62-5064-11e9-830a-00155d429c07)": timeout expired waiting for volumes to attach or mount for pod "default"/"nginx-dep-6b46bc497f-cqkl7". list of unmounted volumes=[mydate]. list of unattached volumes=[mydate default-token-dccpv]

Please tell me how to fix this problem.

-- k_trader
kubernetes

1 Answer

3/27/2019

You can either try hostPath set direct in the POD:

apiVersion: v1
kind: Pod
metadata:
  name: test-pd
spec:
  containers:
  - image: k8s.gcr.io/test-webserver
    name: test-container
    volumeMounts:
    - mountPath: /test-pd
      name: test-volume
  volumes:
  - name: test-volume
    hostPath:
      # directory location on host
      path: /data
      # this field is optional
      type: Directory

PS: I am not sure if you set hostpath as a class to a PersistentVolume, you should try the local-storage class intead

or local:

apiVersion: v1
kind: PersistentVolume
metadata:
  name: example-pv
spec:
  capacity:
    storage: 100Gi
  # volumeMode field requires BlockVolume Alpha feature gate to be enabled.
  volumeMode: Filesystem
  accessModes:
  - ReadWriteOnce
  persistentVolumeReclaimPolicy: Delete
  storageClassName: local-storage
  local:
    path: /mnt/disks/ssd1
  nodeAffinity:
    required:
      nodeSelectorTerms:
      - matchExpressions:
        - key: kubernetes.io/hostname
          operator: In
          values:
          - example-node
-- Diego Mendes
Source: StackOverflow