Skip to content

Commit

Permalink
Add samples and Kubernetes compat
Browse files Browse the repository at this point in the history
  • Loading branch information
Jonathan Muller committed Apr 27, 2017
1 parent bcab7b8 commit 7536fbb
Show file tree
Hide file tree
Showing 6 changed files with 123 additions and 20 deletions.
28 changes: 10 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,29 +1,21 @@
# mariadb-galera
Clusterable, auto-discoverable MariaDB galera cluster - made for Docker Cloud
Clusterable, auto-discoverable MariaDB galera cluster.

Built with [the useful topic from Withblue.ink](http://withblue.ink/2016/03/09/galera-cluster-mariadb-coreos-and-docker-part-1.html).

# Project description
Goal of this project is to create an easily deployable MariaDB cluster, that can scale up and down without configuration/setup assle.
It uses `HOSTNAME` (or `DOCKERCLOUD_CONTAINER_HOSTNAME` on Docker Cloud) env vars to discover other hosts (mariadb-1, mariadb-2, mariadb-3, etc.). Container named with `{name}-1` will be the galera cluster creator.
It uses `HOSTNAME` (or `DOCKERCLOUD_CONTAINER_HOSTNAME` on Docker Cloud) env vars to discover other hosts (mariadb-1, mariadb-2, mariadb-3, etc.). Container named with `{name}-1` (`{name-0}` when on Kubernetes) will be the galera cluster creator.

The container is ready to use with Docker Cloud.
The container is ready to use with Docker Cloud, Docker Compose and simple Docker commands. Support for Kubernetes is in beta.

# Build and run
## Docker Cloud/Docker Compose YML
```yml
mariadb-production:
environment:
- MYSQL_ROOT_PASSWORD=password
image: 'pukoren/mariadb-galera:latest'
ports:
- '3306:3306'
volumes:
- '/data/mysql:/data'
```
Then it should automatically grow as you scale it up.

## Manual (to try it locally)
- [Docker Cloud](/examples/docker-cloud)
- [Docker Compose](/examples/docker-compose)
- [Kubernetes](/examples/kubernetes)

## Manual (to try it locally - manual deployments with Docker)
```sh
docker network create --driver bridge galera
docker run -it -e MYSQL_ROOT_PASSWORD=root --name=mariadb-1 -e HOSTNAME=mariadb-1 --rm --network galera -p 3306:3306 pukoren/mariadb-galera:latest
Expand All @@ -47,9 +39,9 @@ docker run -it -e MYSQL_ALLOW_EMPTY_PASSWORD=true --name=mariadb-2 -e HOSTNAME=m
# Things to know
## Cluster shut down and clustering recovery

Never shut down the whole cluster - if you do so the nodes may not be able to recover. If for some reason you need to shut down the whole cluster and restart it later, make sure to delete the file named `clustered` located in shared volume of the first container (`{name}-1`).
Never shut down the whole cluster - if you do so the nodes may not be able to recover. If for some reason you need to shut down the whole cluster and restart it later, make sure to delete the file named `clustered` located in shared volume of the first container (`{name}-1` or `{name}-0` in Kubernetes).

Always set up a rolling deployment process to avoid any clustering shutdown in case of an update.
Always set up a rolling deployment process to avoid any clustering shutdown when updating.

# Setup examples
## Behind a reverse proxy (HAProxy), secured by IP filtering (Docker Cloud)
Expand Down
15 changes: 15 additions & 0 deletions examples/docker-cloud/stackfile.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
mariadb-production:
# don't use :latest on production deployments
image: 'pukoren/mariadb-galera:latest'
environment:
- MYSQL_ROOT_PASSWORD=password
- MYSQL_DATABASE=db1
# use at least 3 containers for HA deployments
target_num_containers: 3
deployment_strategy: high_availability
# if you want to expose ports to the internet
ports:
- '3306:3306'
# if you want to persist data on host machine
volumes:
- '/data/mysql:/data'
27 changes: 27 additions & 0 deletions examples/docker-compose/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
version: '3'

services:
mariadb-production:
# don't use :latest tag on production deployments
image: 'pukoren/mariadb-galera:latest'
environment:
- MYSQL_ROOT_PASSWORD=password
- MYSQL_DATABASE=db1
deploy:
# always use at least 3 containers for HA deployments
replicas: 3
update_config:
# there should always be at least 1 container UP until another one rejoin the cluster
# otherwise you will have downtimes
parallelism: 2
delay: 30s
networks:
- galera
ports:
- '3306:3306'
# if you want to persist data on host machine
volumes:
- '/data/mysql:/data'

networks:
galera:
12 changes: 12 additions & 0 deletions examples/kubernetes/service.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
apiVersion: v1
kind: Service
metadata:
name: mariadb
labels:
app: mariadb
spec:
ports:
- name: mariadb
port: 3306
selector:
app: mariadb
48 changes: 48 additions & 0 deletions examples/kubernetes/statefulset.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
apiVersion: apps/v1beta1
kind: StatefulSet
metadata:
name: mariadb
spec:
serviceName: "mariadb"
# use at least 3 for HA deployments
replicas: 3
template:
metadata:
labels:
app: mariadb
spec:
terminationGracePeriodSeconds: 30
containers:
- name: mariadb
imagePullPolicy: Always
# don't use latest on production deployments
image: pukoren/mariadb-galera:latest
ports:
- containerPort: 3306
name: mariadb
volumeMounts:
- name: mariadb-data
mountPath: /var/lib/mysql
env:
# root password for the cluster
- name: MYSQL_ROOT_PASSWORD
value: 'password'
# default database
- name: MYSQL_DATABASE
value: 'db1'

# always redeploy one at a time
readinessProbe:
failureThreshold: 5
tcpSocket:
port: 3306
initialDelaySeconds: 30
periodSeconds: 10
livenessProbe:
tcpSocket:
port: 3306
initialDelaySeconds: 30
periodSeconds: 10
volumes:
- name: mariadb-data
emptyDir: {}
13 changes: 11 additions & 2 deletions run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,23 @@ cat /etc/mysql/conf.d/mysql_server.cnf
DATADIR=/data
set -- mysqld "$@"

if [ -z $HOSTNAME ]; then
if [ -v $DOCKERCLOUD_CONTAINER_HOSTNAME ]; then
HOSTNAME=$DOCKERCLOUD_CONTAINER_HOSTNAME
fi

MASTER_FIRST='-1'
if [ -v $KUBERNETES_SERVICE_HOST ]; then
MASTER_FIRST='-0'
fi

if [ -z $HOSTNAME ]; then
HOSTNAME=mariadb-1
fi

LIMIT=$(echo $HOSTNAME | sed 's/[^0-9]*//g')

MASTER=0
if [ "${HOSTNAME:(-2)}" = '-1' ]; then
if [ "${HOSTNAME:(-2)}" = $MASTER_FIRST ]; then
echo "[MASTER]"
MASTER=1
fi
Expand Down

0 comments on commit 7536fbb

Please sign in to comment.