Course contentsShow
← Docker, Kubernetes & CI/CD
Lesson 227 of 8023. Docker Orchestration and StorageFree lesson

Managing Persistent Storage Across Swarm Nodes

Learners will configure distributed storage drivers to ensure persistent data remains accessible to containers regardless of which node they are scheduled on.

In this lesson, you will learn how to ensure your application data follows your containers as they move across different nodes in a Docker Swarm cluster.

The Challenge of Distributed Nodes

When you scale services across multiple nodes, your containers become "nomadic"—they might be scheduled on Node A one moment and Node B the next. By default, if a container writes data to a local folder (like /var/lib/data) on Node A, that data is physically trapped on that server. If the Swarm scheduler moves your container to Node B, the container will look for its data in the new node's local storage and find nothing, leading to potential data loss or service errors.

Using Distributed Storage Drivers

To solve this, we move away from local host paths and use distributed storage drivers. These drivers (such as NFS, GlusterFS, or cloud-native storage like AWS EFS) act as a shared file system accessible by every node in the cluster simultaneously.

Instead of binding to a local directory, you define a volume that points to an external, shared source. When you deploy your service in your YAML file, you reference this volume:

services:
  web-app:
    image: my-app:latest
    deploy:
      replicas: 3
    volumes:
      - my-shared-storage:/data

volumes:
  my-shared-storage:
    driver: nfs
    driver_opts:
      share: 192.168.1.50:/exports/data
      options: rw,nfsvers=4

Why This Works

In this configuration, every node in your Swarm cluster mounts the remote NFS share to the same path. Because the storage is decoupled from the physical hardware of the nodes, it doesn’t matter which node your container lands on. Each instance of your web-app reads and writes to the exact same centralized location. This ensures that even if a node fails or a container is redeployed elsewhere, the persistent state remains intact and instantly available.

By abstracting the storage layer, we guarantee that our high availability efforts—like horizontal scaling—don't compromise our data integrity.

Key Takeaway: To maintain state in a distributed Swarm, you must decouple your data from local node paths and use a centralized storage driver that makes the same persistent volume available to all nodes in the cluster.