TlmaK0
TlmaK0

Reputation: 3896

MountVolume.SetUp failed for volume "mongo" : hostPath type check failed: /mongo/data is not a directory

I'm trying to configure a hostPath to launch Mongodb pod.

I have only one node of kubernetes v1.8.5 installed with rancher last stable version.

I have create folder /mongo/data and allow all permissions to all users. enter image description here

I'm able to run docker image perfectly with docker without sudo:

docker run --name some-mongo -v /mongo/data:/data/db mongo:3.2.1

But when I launch to kubernetes:

sudo kubectl create -f mongodb.yml

I get MountVolume.SetUp failed for volume "mongo" : hostPath type check failed: /mongo/data is not a directory

This is the mongodb.yml:

apiVersion: v1
kind: Pod
metadata:
  name: test-pd
spec:
  containers:
  - image: mongo:3.2.1
    name: test-container
    volumeMounts:
    - mountPath: /data/db
      name: mongo
  volumes:
  - name: mongo
    hostPath:
      # directory location on host
      path: /mongo/data
      # this field is optional
      type: Directory

Any idea where I should looking for?

Upvotes: 20

Views: 21891

Answers (6)

Burhan
Burhan

Reputation: 790

As a Windows Host, changing the format of the path from Windows-style C:\Users\... to Unix-style /C/Users/... worked for me.

Upvotes: 0

Chance
Chance

Reputation: 535

I got the exact same error as you showed.

In my case, the issue was the folder defined in volume hostPath was not created. Once the folder was created in the worker node server, the issue was addressed.

Upvotes: 0

TlmaK0
TlmaK0

Reputation: 3896

Removing type: Directory from the mongodb.yml works as expected

Update:

In new versions, changing type: Directory by type: DirectoryOrCreate, creates the directory.

Upvotes: 16

vladimirror
vladimirror

Reputation: 1068

If by any chance you are using minikube, then don't forget that minikube itself is a container. So hostPath points to a path in that container, not to a path on your host machine. You have to mount your PC path into the minikube container and then into the POD.

Example: minikube start --mount --mount-string="/host/path:/minikubeContainer/path"

Upvotes: 31

Casca
Casca

Reputation: 155

You are creating the docker container directly on the master node. As a result of this you could be able to run docker container with the newly created directory. But when you launch the kubernetes yaml file, it intends to run on a worker node. Since you create the directory on the master node, kubelet cannot find directory on worker node and failing. That's why "DirectoryorCreate" value on type flag is a kind of solution for this.

Upvotes: 5

Kevin
Kevin

Reputation: 2893

Changing type: Directory to type: DirectoryOrCreate works for me.

Upvotes: 15

Related Questions