7

Several named volumes have been created in docker-compose. How to create these in Kubernetes?

How to transform the following named volume example defined in docker-compose to kubernetes format

version: "3"
services:
  x:
    image: z
    volumes:
      - a:/path/inside/container
  y:
    image: c
    volumes:
      - a:/path/inside/container
volumes:
  a:

1 Answer 1

5

k8s have documentation page about volumes and their types https://kubernetes.io/docs/concepts/storage/volumes/

So in short you need to define spec.volumes and spec.containers[].volumeMounts like:

apiVersion: v1
kind: Pod
metadata:
  name: test-pd
spec:
  containers:
  - image: gcr.io/google_containers/test-webserver
    name: test-container
    volumeMounts:
    - mountPath: /cache
      name: cache-volume
  volumes:
  - name: cache-volume
    emptyDir: {}
5
  • is emptyDir: {} required if content from one container has to be shared with another container?
    – 030
    Aug 21, 2017 at 15:37
  • emptyDir is one of the container types. As stated in linked docs "An emptyDir volume is first created when a Pod is assigned to a Node, and exists as long as that Pod is running on that node." So this is simple directory on storage assigned to the runner. This can be used to share data however this data should be treated as ephemeral.
    – Hauleth
    Aug 21, 2017 at 15:42
  • I try to share a directory from container x in container y
    – 030
    Aug 21, 2017 at 15:48
  • @030 everything depends on what data you want to share and if that containers run on the same node.
    – Hauleth
    Aug 21, 2017 at 16:03
  • What would be the equivalent of emptyDir, but the data is not ephemeral. I don't want to have to define a hostPath or anything similar- I would like k8s to simply allocate some persistent disk resources for me and mount them onto a container. This is for local dev only - not needed for production. I've read the docs, but nothing seems clear and easy for what is so simple in docker-compose world. Jul 21, 2021 at 17:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.