Running MySQL on RaspberryPi Docker Swarm and GlusterFS

Today we will setup MySQL on Docker Swarm backed with a Replicated GlusterFS volume that will enable us with data persistence.

I recommend running any database service outside the swarm, but since we are using this for testing purposes, we will run MySQL inside the swarm

Pre-Requirements:

Create the Overlay Network:

If you don't already have a overlay network, create one:

$ docker network create --driver=overlay docknet

Create the Service:

Create the MySQL Service and specify the data directory to the path that is mounted of GlusterFS:

$ docker service create \
--name mysql \
--network docknet \
--mount "type=bind,source=/mnt/volumes/mysql/data,target=/var/lib/mysql,readonly=false" \
--env MYSQL_ROOT_PASSWORD=pass \
--replicas 1 \
hypriot/rpi-mysql  

After some time, you should see the following output:

$ docker service ls
ID                  NAME                MODE                REPLICAS            IMAGE                                                    PORTS  
m6f4tn2ccubq        mysql               replicated          1/1                 hypriot/rpi-mysql:latest  

Create a MySQL Client:

Let's create a service within the same network that will act as a MySQL Client to interact with our MySQL Service:

$ docker service create --name client --network docknet --replicas 1 arm32v6/alpine ping 127.0.0.1

Listing the service, to determine if the service is in the desired replicated state:

$ docker service ls
ID                  NAME                MODE                REPLICAS            IMAGE                                                    PORTS  
m6f4tn2ccubq        mysql               replicated          1/1                 hypriot/rpi-mysql:latest  
zq3pmbnun2fj        client              replicated          1/1                 arm32v6/alpine:latest  

Having a look to see on which node the replica reside:

pi@rpi-01:~ $ docker service ps client  
ID                  NAME                IMAGE                   NODE                DESIRED STATE       CURRENT STATE            ERROR               PORTS  
melrgjjxcac9        client.1            arm32v6/alpine:latest   rpi-02              Running             Running 15 seconds ago  

As we can see the container is running on node rpi-02, let's ssh to the node to exec into the container:

pi@rpi-01:~ $ ssh rpi-02  

Now that we are on the node, lets exec into the container:

pi@rpi-02:~ $ docker exec -it client.1.melrgjjxcac9gor2436cceo7r sh  

We would like to connect to mysql, and as we created a container that is associated to the same network, we will use that container to connect to the mysql service.

In order to do that, we need to install the mysql-client, first update the repository index:

$ apk update
fetch http://dl-cdn.alpinelinux.org/alpine/v3.6/main/armhf/APKINDEX.tar.gz  
fetch http://dl-cdn.alpinelinux.org/alpine/v3.6/community/armhf/APKINDEX.tar.gz  
v3.6.2-80-gd6beefe139 [http://dl-cdn.alpinelinux.org/alpine/v3.6/main]  
v3.6.2-78-gc30bfa742f [http://dl-cdn.alpinelinux.org/alpine/v3.6/community]  
OK: 8274 distinct packages available  

Then install the mysql client:

$ apk add mysql-client
(1/8) Installing mariadb-common (10.1.22-r1)
(2/8) Installing ncurses-terminfo-base (6.0-r8)
(3/8) Installing ncurses-terminfo (6.0-r8)
(4/8) Installing ncurses-libs (6.0-r8)
(5/8) Installing libgcc (6.3.0-r4)
(6/8) Installing libstdc++ (6.3.0-r4)
(7/8) Installing mariadb-client (10.1.22-r1)
(8/8) Installing mysql-client (10.1.22-r1)
Executing busybox-1.26.2-r5.trigger  
OK: 35 MiB in 19 packages  

Finally connect to the MySQL Server:

$ mysql -h mysql -u root -ppass
Welcome to the MariaDB monitor.  Commands end with ; or \g.  
Your MySQL connection id is 1  
Server version: 5.5.54-0+deb7u1 (Debian)

Copyright (c) 2000, 2016, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MySQL [(none)]> exit  
Bye