Deploying a MariaDB Galera Cluster
Note: This section assumes that you have set up Ceph storage as described here: Ceph
In this section we will build a custom container image that is configured to be part of a MariaDB 10.4 cluster using Galera. The MariaSB cluster will be deployed as a StatefulSet and will leverage Ceph block devices with an XFS filesystem for persistent storage.
Building the MariaDB Galera Cluster container image
We are going to use podman to build this image. Then, we are going to push it to the image registry of our OpenShift cluster.
mkdir -p ${OKD4_LAB_PATH}/mariadb
cp -r ./MariaDB/* ${OKD4_LAB_PATH}/mariadb
cd ${OKD4_LAB_PATH}/mariadb/Image
ls -l
total 40
-rw-r--r-- 1 user staff 1231 Jan 12 12:33 Dockerfile
-rw-r--r-- 1 user staff 98 Jan 12 12:30 MariaDB.repo
-rw-r--r-- 1 user staff 87 Jan 12 12:26 liveness-probe.sh
-rw-r--r-- 1 user staff 1999 Jan 12 12:37 mariadb-cluster.sh
-rw-r--r-- 1 user staff 98 Jan 12 12:26 readiness-probe.sh
Dockerfile
: This file contains the directions for building our container.MariaDB.repo
: This file is the definition for the MariaDB 10.4 RPM repository. Since our container is based on a CentOS 7 base image, our package installer is RPM based.liveness-probe.sh
: The script called by the OpenShift runtime to check pod liveness. More on this file and the next one later. The liveness probe and readiness probe are used by OpenShift to monitor the state of the running container.readiness-probe.sh
: The script called by the OpenShift runtime to check pod readiness.mariadb-cluster.sh
: This is the main script that configures and starts MariaDB. It is set as the entry point for the container image.
Now, let’s build this image and push it to the OpenShift repository.
-
Make sure that your Docker daemon is running.
On CentOS:
dnf install -y podman
On a desktop OS, start
Docker Desktop
. -
Log into your image registry: (Assuming that you are already logged into your OpenShift cluster)
If you have not exposed the default route for external access to the registry, do that now:
oc patch configs.imageregistry.operator.openshift.io/cluster --patch '{"spec":{"defaultRoute":true}}' --type=merge
Log in:
podman login -u $(oc whoami) -p $(oc whoami -t) --tls-verify=false $(oc get route default-route -n openshift-image-registry --template='')
-
Build the image:
cd ./MariaDB/Image podman build -t $(oc get route default-route -n openshift-image-registry --template='')/openshift/mariadb-galera:10.4 .
-
Push the image to the OpenShift image registry.
podman push $(oc get route default-route -n openshift-image-registry --template='')/openshift/mariadb-galera:10.4 --tls-verify=false
Now, let’s deploy a database cluster:
cd ../Deploy
Deploying a MariaDB Galera cluster into OpenShift:
Let’s look at the OpenShift deployment files in the Deploy folder.
mariadb-galera-configmap.yaml
: ConfigMap with the MariaDB configurationmariadb-galera-headless-svc.yaml
: Headless service for inter-pod communicationsmariadb-galera-loadbalance-svc.yaml
: Service for database connectionsmariadb-statefulset.yaml
: StatefulSet deployment definition
The OpenShift deployment is a StatefulSet. A StatefulSet is a special kind of deployment that ensures that a given pod will always get the same PersistentVolumeClaim across restarts. For more information, see the official documentation here.
Now, let’s deploy MariaDB: (Assuming that you are logged into your OpenShift cluster)
-
Create a new Namespace:
oc new-project mariadb-galera
-
Create the service account for our MariaDB deployment, and add the
anyuid
Security Context Constraint to the service account. This will allow the MariaDB pod to run as UID 27 like we defined in our Dockerfile.oc create sa mariadb -n mariadb-galera oc adm policy add-scc-to-user anyuid -z mariadb -n mariadb-galera
-
Deploy the MariaDB cluster:
oc apply -f mariadb-galera-configmap.yaml -n mariadb-galera oc apply -f mariadb-galera-headless-svc.yaml -n mariadb-galera oc apply -f mariadb-galera-loadbalance-svc.yaml -n mariadb-galera oc apply -f mariadb-statefulset.yaml -n mariadb-galera
You should now see your MariaDB cluster deploying. Each node will start in series after the previous has passed it’s readiness probe. The podManagementPolicy: "OrderedReady"
directive in the StatefulSet ensures that the cluster will always stop and start in a healthy state.