kubernetes: how to write pod yaml file for mapping volumn from host to container

6/19/2018

I have a command to run docker,

docker run --name pre-core -itdp 8086:80 -v /opt/docker/datalook-pre-core:/usr/application app

In above command, /opt/docker/datalook-pre-core is host directory, /usr/application is container directory. The purpose is that container directory maps to host directory. So when container crashes, the directory functions as storage and data on it would be saved.

When I am going to use kubernetes to create a pod for this containter, how to write pod.yaml file?

I guess it is something like following:

apiVersion: v1
kind: Pod
metadata:
  name: app-ykt
  labels:
    app: app-ykt
    purpose: ykt_production
spec:
  containers:
  - name: app-ykt
    image: app
    imagePullPolicy: IfNotPresent
    ports:
      - containerPort: 80
    volumnMounts:
      - name: volumn-app-ykt
        mountPath: /usr/application
  volumns:
      - name: volumn-app-ykt
      ????

I do not know what's the exact properties in yaml I shall write in my case?

-- user84592
docker
kubernetes

1 Answer

6/20/2018

This would be a hostPath volume: https://kubernetes.io/docs/concepts/storage/volumes/

  volumes:
  - name: volumn-app-ykt
    hostPath:
      # directory location on host
      path: /opt/docker/datalook-pre-core
      # this field is optional
      type: Directory

However remember that while a container crash won't move things, other events can cause a pod to move to a different host so you need to be prepared to both deal with cold caches and to clean up orphaned caches.

-- coderanger
Source: StackOverflow