Docker Swarm Persistent Storage Using GlusterFS
Running stateful applications on Docker Swarm is a bit tricky since docker volumes are created on the server where the container runs. When the container is moved to a different server, data will will be inaccessible. Using GlusterFS is a solution for this problem. Through this article you will find simple and easy way to create GlusterFS Cluster. Creating Docker Swarm cluster is out of scope of this document.
I assume that you already have Docker Swarm cluster running:
root@akaftan0:/opt# docker node ls
ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS ENGINE VERSION
gpd1zhyjpdekx8i0ogxrr9fsv * akaftan0 Ready Active Leader 19.03.8
u9t8s7nsjscnw9qcpicrd3104 akaftan1 Ready Active 19.03.8
- Install GlusterFS
sudo apt-get install software-properties-common -y
sudo add-apt-repository ppa:gluster/glusterfs-3.12
sudo apt-get update
sudo apt install glusterfs-server -y
sudo systemctl start glusterd
sudo systemctl enable glusterd
2. Generate SSH Keys
ssh-keygen -t rsa
3. Probe Nodes
Please make sure that servers can resolve their names.
root@akaftan0:/opt# gluster peer probe akaftan1
4. Check GlusterFS Pool
root@akaftan0:/opt# gluster pool list
UUID Hostname State
a54e256b-f0c2-4963-9b3c-a3438ef5f11e akaftan1 Connected
d953dda7-f8ea-4bf6-ad2f-b6cffb01d745 localhost Connected
5. Create the GlusterFS Volume
sudo mkdir -p /opt/mnt-vol
sudo gluster volume volume-data akaftan0:/opt/mnt-vol akaftan1:/opt/mnt-vol force
gluster volume start volume-data
echo 'localhost:/volume-data /opt/mnt glusterfs defaults,_netdev,backupvolfile-server=localhost 0 0' >> /etc/fstab
mount.glusterfs localhost:/volume-data /opt/mnt
6. Create Directory
mkdir -p /opt/mnt/cassandra-data
7. Deploy Your Stateful Application
version: '3.7'
services:
cassandra:
image: cassandra:3.11.6
ports:
- "9042:9042"
restart: unless-stopped
volumes:
- /opt/mnt-vol/cassandra-data:/var/lib/cassandra/data
networks:
default:
driver: "overlay"
Now, enjoy your stateful application on Docker Swarm.