Simple volume to volume file sync with Kubernetes

Overview

When running multiple environments on Kubernetes you find yourself in a situation of “how do I sync files from environment A to B”.

Outside of the container ecosystem this has been a difficult task requiring the operator to have:

I have been thinking about this alot problem lately and thought I would write down a little pattern which I feel improves on the traditional model in Kubernetes.

Jobs

With the recent announcement of Kubernetes 1.2 we now have more tools in our belt to facilitate a transaction of that nature.

Jobs! http://kubernetes.io/docs/user-guide/jobs

A job creates one or more pods and ensures that a specified number of them successfully terminate. 

What this means is we now have a framework for running operations and reporting on if they are successful or not.

The Approach

The approach is simple:

The Implementation

Below is a simple definition file which copies from one mount to another. While this is a very simple implementation using the HostPath volume, I see no reason why this approach cannot be used with a mixture of different volume types.

apiVersion: batch/v1
kind: Job
metadata:
  name: sync-demo
spec:
  template:
    metadata:
      name: sync-demo
    spec:
      containers:
      - name: sync-demo
        image: alpine
        command: ["cp",  "-R", "/mnt/a/*", "/mnt/b/"]
        volumeMounts:
        - name: a
          mountPath: "/mnt/a"
        - name: b
          mountPath: "/mnt/b"
      volumes:
      - name: a
        hostPath:
          path: "/home/nick/a"
      - name: b
        hostPath:
          path: "/home/nick/b"
      restartPolicy: OnFailure

The file can then be run with the kubectl create command.

$ kubectl create -f sync-demo.yaml

Once the operation has started we can now inspect on the status of the job.

$ kubectl get jobs
NAME        DESIRED   SUCCESSFUL   AGE
sync-demo   1         1            1m

This basic implemention requires the user to have knowledge of the volume configuration for the mounts. However, there is alot to be gained if implemented as a higher level API project eg.

curl -X POST http://localhost/copy?from=b&to=a

Conclusion

The container movement has provided us with some amazing APIs, improving operations management.

This is another example of taking and old approach and putting a new spin on it, and I like it!