Search code examples
dockerkubernetescontainersstoragepersistent-volumes

How to Transition Ephemeral Volumes to Persistent Volume Claims


Currently, some of my pods are using ephemeral volumes (those created by Docker at the container level). I want some form of persistent data stored in specific directories on the host (like bind mounts in Docker) such that my pods can restart without losing data. Persistent Volume Claims seem to be the best way to do this. How should I go about transitioning my existing pods to use PVCs that copy across data from the existing ephemeral volumes?


Solution

    1. On the host, create the directory to hold your data. Eg. mkdir /local_data
    2. Copy the data to the local directory. Eg. kubectl cp <namespace>/<pod>:/path/in/the/container /local_data
    3. Check and check again all your data is intact in /local_data
    4. Create a new pod with the following spec.

    Example:

    kind: Pod
    ...
    spec:
      ...
      nodeName: <name>  # <-- if you have multiple nodes this ensure your pod always run on the host that hold the data
      containers:
      - name: ...
        ...
        volumeMounts:
        - name: local_data
          mountPath: /path/in/the/container
        ...
      volumes:
      - name: local_data
        hostPath: /local_data
        type: Directory
    

    Apply and check if your pod runs as expected