简体   繁体   中英

Expose folder from a container as a volume in a Kubernetes Pod

I have a pod which contains two containers. One container is a web application and another store some static data for this web application.

The data here is a set of files which are stored in the folder of this container with name /data and that's only function of the container to store this data and expose them to the web application.

I'm looking for the way to share the content of this folder with web application container in this pod.

If I'm using the YAML spec below the folder in both containers is empty. Is there a way to share the data from container folder without cleaning it up?

apiVersion: v1
kind: Pod
metadata:
  name: my-pod
  labels:
    app: my-app
    version: 1.2.3
spec:
  volumes:
    - name: my-app-data-volume

  containers:
    - name: my-app-server
      image: my-app-server-container-name
      volumeMounts:
        - name: my-app-data-volume
          mountPath: /data
      ports:
        - containerPort: 8080

    - name: my-app-data
      image: my-app-data-container-name
      volumeMounts:
        - name: my-app-data-volume
          mountPath: /data

You can use an EmptyDir volume for this. Specify the container that contains the files as an initContainer , then copy the files into the EmptyDir volume. Finally, mount that volume in the web app container.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM