Git Product home page Git Product logo

docker-flexvol's Introduction

docker-flexvol

A FlexVolume driver for kubernetes which allows you to mount Docker volumes to your kubernetes pods.

Status

Proof of concept

Using

This flex volume plugin can start a fresh docker volume from a specified container image and attach it to the kubernetes pod. This is useful for scenarios where you need your pod to access a bunch of files and if already have that data as a docker container image, you can just specify the container image name and the volume name in your kubernetes pod definition and make it available to whatever is running in the pod.

Installing

In order to use the flexvolume driver, you'll need to install it on every node in the kubelet volume-plugin-dir. By default this is /usr/libexec/kubernetes/kubelet-plugins/volume/exec/

You need a directory for the volume driver vendor, so create it:

mkdir -p /usr/libexec/kubernetes/kubelet-plugins/volume/exec/dims.io~docker-flexvol

Then drop the binary in there and set the execute permission:

mv docker-flexvol.sh /usr/libexec/kubernetes/kubelet-plugins/volume/exec/dims.io~docker-flexvol/docker-flexvol
chmod +x /usr/libexec/kubernetes/kubelet-plugins/volume/exec/dims.io~docker-flexvol/docker-flexvol

You can now use Docker volumes as usual!

Pod Config

An example pod config would look like this. Note the image and name parameters. The image is the name of the container that we need to start. name is the name of the volume we need to mount.

Note that image is mandatory, name is optional. If name is not specified all the contents of the container are made available to the pod.

apiVersion: v1
kind: Pod
metadata:
  name: nginx
  namespace: default
spec:
  containers:
  - name: nginx
    image: nginx
    volumeMounts:
    - name: test
      mountPath: /data
    ports:
    - containerPort: 80
  volumes:
  - name: test
    flexVolume:
      driver: "dims.io/docker-flexvol"
      options:
        image: "my-container-image"
        name: "/data-store"

This will use the /data-store volume on the my-container-image and mount it on the nginx pod under /data directory.

Run docker ps -a and find the container with image "my-container-image", that's the one that has the docker volume. this should be in the Created state. Look for the Source directory, it will be something like /var/lib/docker/volumes/2ec90b1efa7b1f51913882d035192a450810419d72b56814a1332b31457aa356/_data

Run kubectl exec -it nginx -- /bin/bash, cd to /data directory and create a file with say a datestamp. Now look under the directory above and you should see the same file and contents.

Container Image with pre-defined volume

To create a Container Image with Volume, save content below into Dockerfile in an empty directory:

FROM alpine
ADD https://raw.githubusercontent.com/kubernetes/kubernetes/master/README.md /data-store/README.md
VOLUME ["/data-store"]
ENTRYPOINT ["/bin/sh"]

Create a docker image using

docker image build -t my-container-image .

Test the image using docker

docker run --name my-container-1 -it my-container-image

Now this container image is ready to be used with this flexvolume driver. When you use this image with the pod config above, you can exec into the pod and see the file

[dims@bigbox 16:15] ~ ⟩ kubectl exec -it nginx -- /bin/bash
root@nginx:/# cd /data
root@nginx:/data# ls -altr
total 12
-rw-------  1 root root 3241 Jan  1  1970 README.md
drwxr-xr-x  2 root root 4096 Aug 18 20:15 .
drwxr-xr-x 32 root root 4096 Aug 18 20:15 ..

docker-flexvol's People

Contributors

dims avatar micahnz avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

docker-flexvol's Issues

Pod stuck in `ContainerCreating` state in k8 1.12.3

While creating pod using this driver it gives the following error :

  Warning  FailedMount  6s (x5 over 14s)  kubelet, 10.xx.xx.xx  MountVolume.SetUp failed for volume "test" : invalid character '/' looking for beginning of value

The same script docker-flexvol.sh works fine with k8 1.10.11 but not 1.12.3

Unused docker volumes left on disk

A problem I have run in to while using this lately is docker volumes not being cleaned up after the pods are done with them which eventually fills up the disk space.

Right now I am running sudo docker volume prune manually every now and then.

A few issues I had while deploying to GKE

For those who run into the same problems as me here are a few notes.

uuidgen and jq commands are not installed by default, I installed it manually with apt-get install jq uuid-runtime. The jq is noted at the top of the bash script but the uuidgen one is not.

The current ismounted check prevented my pods from terminating because there were multiple entries returned by findmnt, the following worked for me.

ismounted() {
  MOUNT=$(findmnt -n ${MNTPATH} 2>/dev/null)
  if [ ! -z "$MOUNT" ]; then
    echo "1"
  else
    echo "0"
  fi
}

I found only setting "chmod +x" did not allow autodiscovery, I had to set 777 on the whole plugins directory and bash script for it to work.

Private GCR repos do not work because the docker cli is not logged in so if you are going to use private docker images you need to run something like this after adding the FlexVolume driver.

sudo su
docker login \
  -u oauth2accesstoken \
  -p "$(curl "http://metadata.google.internal/computeMetadata/v1/instance/service-accounts/default/token" -H "Metadata-Flavor: Google" | jq -r '.access_token')" \
  https://asia.gcr.io

Otherwise its working great so thanks!!!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.