0

I've searched the internet for tutorials / guides on how one can share volumes across multiple pods - and only found a few articles explaining how to share volumes among containers inside same pod. Then I came across this SO question, but the accepted answer wasn't clear to me. Let's assume the following scenario:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: deployment-one
spec:
  selector:
    matchLabels:
      app: one
  template:
    metadata:
      labels:
        app: one
    spec:
      containers:
        - name: one
          image: some_username/container_one:latest
          volumeMounts:
            - name: volume-one
              mountPath: /one
      volumes:
        - name: volume-one
          persistentVolumeClaim:
            claimName: awesome-pvc
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: deployment-two
spec:
  selector:
    matchLabels:
      app: two
  template:
    metadata:
      labels:
        app: two
    spec:
      containers:
        - name: two
          image: some_username/container_two:latest
          volumeMounts:
            - name: volume-two
              mountPath: /two
      volumes:
        - name: volume-two
          persistentVolumeClaim:
            claimName: awesome-pvc

So awesome-pvc is shared between 2 pods in the above example. So how can one "access" /two from first pod? In the simplest word: how can one copy files from /two to /one?

msrumon
  • 1,250
  • 1
  • 10
  • 27
  • You don't need to copy files. Since you're sharing the same PVC, the data that are in the `/one` path of `deployment-one` should already be in the `/two` path of `deployment-two`. – Kamol Hasan Oct 13 '21 at 14:11
  • The accepted answer of the question that you linked to is good. **Avoid sharing a volume between services**. Try to design your application differently. – Jonas Oct 13 '21 at 15:46
  • @jonas Let me illustrate an example. The NATS streaming server can be configured to use SQL data store. And for that, you need to pre-populate the database with required tables. The SQL script for that comes with the server itself, and we need to run it in the other container. How do you think you can tackle it without sharing volumes between those 2 pods? – msrumon Oct 13 '21 at 18:00
  • SQL script is typically sent over the network by a client to the database, that is a service that listen on a port. Only the database mount the volume. That is what I imagine about what you tell - without having investigated any documentation. – Jonas Oct 13 '21 at 18:07
  • [Here](https://docs.nats.io/nats-streaming-server/configuring/persistence/sql_store) goes the documentation. Let's discuss what you'd do to prepare the database without any manual steps as explained in the doc. – msrumon Oct 13 '21 at 19:36
  • I understand why you need two pods sharing the same volume in that case. The `psql` is the client and `postgresql` is the database (service) and only the PostgreSQL container probably need to write to a persistent disk volume? If I understand it correctly. – Jonas Oct 13 '21 at 22:22
  • Yes you're right. But that's separate from this topic. Here we're trying to get files of one pod accessible to another one. Take a look at [this](https://github.com/msrumon/microservice-architecture/blob/bfd998a84a188dce55807d728c19f1759cf2302a/k8s/common/nats.yaml) manifest. Although, later I came to know that the SQL file isn't inside the container to the specified path at all, but let's forget that for the sake of argument for now. – msrumon Oct 14 '21 at 05:37
  • Thanks. I haven't tested it yet, though it's definitely in my todo list - just busy with other projects. I'll add a solution here when I find one (or accept someone's if that perfectly answers my question). – msrumon Oct 19 '21 at 11:51
  • @msrumon Have you already tested your solution? – Andrew Skorkin Nov 16 '21 at 11:44
  • No I didn't. I'll be busy for the rest of the year 2021. – msrumon Nov 17 '21 at 16:54
  • Hello @msrumon On my opinion, since you are using the same persistentVolumeClaim `awesome-pvc` for 2 pods, the data should be the same for them under respective `mountPath` for each. Only difference is the folders names. For the 2st pod the data will be available in `/one` folder, for the 2nd - in `/two`. So, I think there is no need to request`/two` folder from the 1st pod. Did you check that previously? – Andrew Skorkin Dec 26 '21 at 02:19
  • Thanks for the heads up @andrew-skorkin. I haven't checked it yet. Once I do that, I'll come back here with my findings. – msrumon Dec 28 '21 at 11:21

0 Answers0