Skip to content

Instantly share code, notes, and snippets.

@heichblatt
Last active February 2, 2016 10:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save heichblatt/ae93908c0bf0344f775d to your computer and use it in GitHub Desktop.
Save heichblatt/ae93908c0bf0344f775d to your computer and use it in GitHub Desktop.
Build a Galera cluster on a Docker Machine-provisioned Docker Swarm cluster

Build a Galera cluster on a Docker Swarm cluster made of Docker Machine-provisioned VMs

This repository contains scripts to create a Docker Swarm cluster of VirtualBox VMs and then start a Galera cluster of Docker containers running on the Swarm cluster.

The script create-cluster.sh will go through the following steps:

  • Create the VMs, one to host the consul container which will act as the keystore for the Docker Swarm cluster, then some more as members of the cluster.
  • Create an overlay network spanning all the Swarm cluster nodes so containers on one node can directly connect to others on other hosts.
  • Create an initial Galera container which will act as an initial master for the Galera cluster.
  • Create additional Galera containers and connect them to the growing Galera cluster.

Schema

.-----------------------------------------------.
|                                               |
| .-------------..-------------..-------------. |
| | Galera Node || Galera Node || Galera Node | |
| '-------------''-------------''-------------' |
| .-------------..-------------..-------------. |
| | Galera Node || Galera Node || Galera Node | |
| '-------------''-------------''-------------' |   Galera Cluster
| .-------------..-------------..-------------. |   on overlay network
| | Galera Node || Galera Node || Galera Node | |
| '-------------''-------------''-------------' |
|                                               |
'-----------------------------------------------'
.-----------------------------------------------.
|  ________   ________   ________   ________    |
| |==|=====| |==|=====| |==|=====| |==|=====|   |
| |  |     | |  |     | |  |     | |  |     |   |
| |  | VM  | |  | VM  | |  | VM  | |  | VM  |   |
| |  |     | |  |     | |  |     | |  |     |   |  Docker Swarm
| |  |     | |  |     | |  |     | |  |     |   |  Cluster     
| |  |====°| |  |====°| |  |====°| |  |====°|   |
| |__|_____| |__|_____| |__|_____| |__|_____|   |
|                                               |
'-----------------------------------------------'

Sources

#!/usr/bin/env bash
set -ex
which docker-machine || (echo docker-machine not found. ; exit 1)
create_host() {
local HNAME=$1
local KEYSTORE=$2
log Create host "$HNAME" with keystore "$KEYSTORE".
docker-machine create \
-d virtualbox \
--swarm --swarm-master \
--swarm-discovery="consul://$(docker-machine ip swarm-keystore):8500" \
--engine-opt="cluster-store=consul://$(docker-machine ip $KEYSTORE):8500" \
--engine-opt="cluster-advertise=eth1:2376" \
$HNAME
}
create_galera_node() {
local NODENAME=$1
local GALERAMASTER=$2
local HOST=$3
log Create Galera Node "$NODENAME" with Galera master "$GALERAMASTER" on host "$HOST".
docker run -itd --name="$NODENAME" -h "$NODENAME" --net=galera-net --env="constraint:node==""$HOST" erkules/galera:basic --wsrep-cluster-name=local-test --wsrep-cluster-address=gcomm://"$GALERAMASTER"
}
log() {
set +x
echo -e "\033[34m[create-cluster] $* \033[0m"
set -x
}
log Create VM for Docker Swarm Keystore
docker-machine create -d virtualbox swarm-keystore
eval "$(docker-machine env swarm-keystore)"
log Run Consul container as Docker Swarm Keystore
docker $(docker-machine config swarm-keystore) run -d \
-p "8500:8500" \
-h "consul" \
progrium/consul -server -bootstrap
log Create VMs
create_host swarm-node0 swarm-keystore
create_host swarm-node1 swarm-keystore
create_host swarm-node2 swarm-keystore
log Show VMs
docker-machine ls
eval $(docker-machine env --swarm swarm-node0)
docker info
log Create overlay network
docker network create --driver overlay galera-net
docker network ls
log Start initial Galera node
docker run -itd --name=galera-node0 -h galera-node0 --net=galera-net --env="constraint:node==swarm-node0" erkules/galera:basic --wsrep-cluster-name=local-test --wsrep-cluster-address=gcomm://
log Start additional Galera node containers
create_galera_node galera-node1 galera-node0 swarm-node0
create_galera_node galera-node2 galera-node0 swarm-node0
create_galera_node galera-node3 galera-node0 swarm-node1
create_galera_node galera-node4 galera-node0 swarm-node1
create_galera_node galera-node5 galera-node0 swarm-node1
create_galera_node galera-node6 galera-node0 swarm-node2
create_galera_node galera-node7 galera-node0 swarm-node2
create_galera_node galera-node8 galera-node0 swarm-node2
log Let the Galera cluster settle for 5s
sleep 5s
log Ask Galera cluster about its size
docker exec -ti galera-node0 mysql -e 'show status like "wsrep_cluster_size"'
#!/usr/bin/bash
set -x
for i in swarm-keystore swarm-node{0..2} ; do
docker-machine rm -f -y $i ;
done
@kubitux
Copy link

kubitux commented Feb 2, 2016

How can you access to your galera cluster from the outside ? What would you configure to set a unique access point in your swarm cluster, to send some mysql requests to galera ? Is it possible without installing anything outside a docker context ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment