I have the next scenario where I needed to setup a MySQL Percona XtraDB Cluster on containers on 3 different servers.
Once that Docker is installed (1.17 as of writing), we have to make an overlay network for Docker Swarm, each replica of the containers will be able to see each other in the same network, this will allow each node to join the cluster.
docker network create \ --driver overlay \ --subnet 10.0.0.1/24 \ percona-cluster
Then we need to initialize Swarm mode, by executing this on the manager node:
docker swarm init --advertise-addr SERVER-IP
this will give us a token and a command to execute on the worker nodes to join the Swarm.
docker swarm join \ --token SWMTKN-1-49nj1cmql0jkz5s954yi3oex3nedyz0fb0xx14ie39trti4wxv-8vxv8rssmk743ojnwacrr2e7c \ MANAGER-SERVER-IP:2377
Now that we have all our nodes joined to the Swarm we can actually configure the cluster.
First we need to run a container to bootstrap the cluster.
docker service create --network percona-cluster -e "CLUSTER_NAME=cluster-name" -e "MYSQL_ROOT_PASSWORD=mysql-password" --name bootstrap percona/percona-xtradb-cluster:latest
This will create a container (service) and pull the latest Percona Docker image from the official Hub, it’s important to pass the variables, as they are needed for the container to run.
Once we have the bootstrap service created we can then create the cluster service:
docker service create -d --publish 3306:3306 --mount /var/lib/mysql:/var/lib/mysql --mount /var/log/mysql:/var/log/mysql--network percona-cluster -e "CLUSTER_NAME=cluster-name" -e "MYSQL_ROOT_PASSWORD=mysql-password" -e "CLUSTER_JOIN=bootstrap,mysql-cluster" --name mysql-cluster percona/percona-xtradb-cluster:latest
Now that we have the service created we can expand the cluster by replicating the containers:
docker service scale mysql-cluster=3
We don’t require the service bootstrap anymore, so we can go ahead and delete the service.
docker service stop bootstrap && docker service rm bootstrap
We can verify the cluster by checking the logs on each container:
docker logs -f IMAGE-ID
This will launch 2 more containers distributed in our 2 hosts.