From 7536fbb781044d8444f80ea49bd1bd3e5f71d0cc Mon Sep 17 00:00:00 2001 From: Jonathan Muller Date: Thu, 27 Apr 2017 13:16:39 +0200 Subject: [PATCH] Add samples and Kubernetes compat --- README.md | 28 +++++-------- examples/docker-cloud/stackfile.yml | 15 +++++++ examples/docker-compose/docker-compose.yml | 27 ++++++++++++ examples/kubernetes/service.yml | 12 ++++++ examples/kubernetes/statefulset.yml | 48 ++++++++++++++++++++++ run.sh | 13 +++++- 6 files changed, 123 insertions(+), 20 deletions(-) create mode 100644 examples/docker-cloud/stackfile.yml create mode 100644 examples/docker-compose/docker-compose.yml create mode 100644 examples/kubernetes/service.yml create mode 100644 examples/kubernetes/statefulset.yml diff --git a/README.md b/README.md index 233dd9a..0a4fae3 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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) diff --git a/examples/docker-cloud/stackfile.yml b/examples/docker-cloud/stackfile.yml new file mode 100644 index 0000000..d487c76 --- /dev/null +++ b/examples/docker-cloud/stackfile.yml @@ -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' \ No newline at end of file diff --git a/examples/docker-compose/docker-compose.yml b/examples/docker-compose/docker-compose.yml new file mode 100644 index 0000000..34abcc2 --- /dev/null +++ b/examples/docker-compose/docker-compose.yml @@ -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: \ No newline at end of file diff --git a/examples/kubernetes/service.yml b/examples/kubernetes/service.yml new file mode 100644 index 0000000..ea4df1b --- /dev/null +++ b/examples/kubernetes/service.yml @@ -0,0 +1,12 @@ +apiVersion: v1 +kind: Service +metadata: + name: mariadb + labels: + app: mariadb +spec: + ports: + - name: mariadb + port: 3306 + selector: + app: mariadb diff --git a/examples/kubernetes/statefulset.yml b/examples/kubernetes/statefulset.yml new file mode 100644 index 0000000..4e1b66b --- /dev/null +++ b/examples/kubernetes/statefulset.yml @@ -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: {} diff --git a/run.sh b/run.sh index f68c922..0edad21 100755 --- a/run.sh +++ b/run.sh @@ -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