This project demonstrates how to deploy a MariaDB master-slave replication setup using Kubernetes StatefulSet and Longhorn for persistent storage.
This setup consists of:
- A
StatefulSetwith 2 MariaDB pods:mariadb-0acts as the mastermariadb-1acts as the slave
- Master/Slave configuration is handled via
initContainersandConfigMap - Persistent volumes are provisioned dynamically using Longhorn
- Uses ConfigMaps to inject different
.cnfand.sqlfiles based on pod index
ConfigMap: Contains separate config files and init SQL for master and slaveStatefulSet: Creates two MariaDB pods with specific initialization logicService: Headless service to allow DNS-based internal communication between pods
- Kubernetes cluster (v1.30+ recommended)
- Longhorn installed as the default or named
storageClass kubectlconfigured and connected to your cluster
- Default Credentials:
rootpassword:123- Replication user:
radin - Replication password:
123
- Please make sure to change the default passwords before using this in any real environment.
- This project is not production ready. Stability, fault tolerance, and failover mechanisms are still being improved.
- Longhorn is used with
ReadWriteManymode β make sure your Kubernetes setup and Longhorn are properly configured for this.
- Pod index
0is detected as the master, and the appropriate configuration is mounted from theConfigMap - Pod index
1is treated as a slave, which connects to the master using the headless service DNS name - MariaDB image auto-runs the SQL scripts on first initialization
# Create the namespace
kubectl create namespace db
# Apply the config map
kubectl apply -f configmap.yaml
# Deploy the StatefulSet and Service
kubectl apply -f statefulset.yaml
kubectl apply -f service.yamlReplace the above filenames with your actual manifest filenames if different.
Contributions, issues and feature requests are welcome!