0

I was successfully able to share data between a docker container and host using

docker run -it -v /path/to/host/folder:/container/path image-name

Now I am trying to run this docker image through a Kubernetes cronjob every minute for which my yaml file is as follows :

apiVersion: batch/v1beta1
kind: CronJob
metadata:
  name: automation
spec:
  schedule: "*/1 * * * *"
  jobTemplate:
    spec:
      template:
        spec:
          containers:
          - name: automation
            image: localhost:32000/image-name:registry  
          restartPolicy: OnFailure

But here how do I share data between my local and k8s so as to basically replicate the -v /path/to/host/folder:/container/path functionality from the docker run command ? What should I add to my yaml file ?

Please help.

1 Answers1

1

If you're just playing with one node and need to map a volume from this node to a pod running in the same node, then you need to use a hostPath volume.
In summary, your code will look like this :

apiVersion: batch/v1beta1
kind: CronJob
metadata:
  name: automation
spec:
  schedule: "*/1 * * * *"
  jobTemplate:
    spec:
      template:
        spec:
          containers:
          - name: automation
            image: localhost:32000/image-name:registry  
            volumeMounts:
            - mountPath: /container/path
              name: test-volume
          restartPolicy: OnFailure
          volumes:
          - name: test-volume
            hostPath:
              # directory location on host
              path: /path/to/host/folder
              # this field is optional
              type: Directory

Warning, this will work only if you just have a one node cluster.

If you have a multi-node cluster, then you need to havec a look at distributed storage solution and how to use them with kubernetes.

Here is the doc about volumes in K8S

Marc ABOUCHACRA
  • 3,155
  • 12
  • 19