158 lines
4.5 KiB
YAML
158 lines
4.5 KiB
YAML
# ConfigMap holding information about configuration files for primary/secondary and dockerinit
|
|
apiVersion: v1
|
|
kind: ConfigMap
|
|
metadata:
|
|
name: mariadb-configmap
|
|
data:
|
|
|
|
primary.cnf: |
|
|
[mariadb]
|
|
log-bin # enable binary logging
|
|
log-basename=my-mariadb # used to be independent of hostname changes (otherwise is in datadir/mysql)
|
|
|
|
replica.cnf: |
|
|
[mariadb]
|
|
log-basename=my-mariadb # used to be independent of hostname changes (otherwise is in datadir/mysql)
|
|
|
|
primary.sql: |
|
|
CREATE USER 'repluser'@'%' IDENTIFIED BY 'replsecret';
|
|
GRANT REPLICATION REPLICA ON *.* TO 'repluser'@'%';
|
|
CREATE DATABASE primary_db;
|
|
|
|
secondary.sql: |
|
|
# We have to know name of sts (`mariadb-sts`) and
|
|
# service `mariadb-service` in advance as an FQDN.
|
|
# No need to use master_port
|
|
CHANGE MASTER TO
|
|
MASTER_HOST='mariadb-sts-0.mariadb-service.default.svc.cluster.local',
|
|
MASTER_USER='repluser',
|
|
MASTER_PASSWORD='replsecret',
|
|
MASTER_CONNECT_RETRY=10;
|
|
|
|
# Secret holds information about root password
|
|
---
|
|
apiVersion: v1
|
|
kind: Secret
|
|
metadata:
|
|
name: mariadb-secret
|
|
type: Opaque
|
|
data:
|
|
mariadb-root-password: c2VjcmV0 # echo -n 'secret'|base64
|
|
|
|
# Headless service
|
|
---
|
|
apiVersion: v1
|
|
kind: Service
|
|
metadata:
|
|
name: mariadb-service
|
|
labels:
|
|
app: mariadb
|
|
spec:
|
|
ports:
|
|
- port: 3306
|
|
name: mariadb-port
|
|
clusterIP: None
|
|
selector:
|
|
app: mariadb
|
|
|
|
# Statefulset
|
|
---
|
|
apiVersion: apps/v1
|
|
kind: StatefulSet
|
|
metadata:
|
|
name: mariadb-sts
|
|
spec:
|
|
serviceName: "mariadb-service"
|
|
replicas: 3
|
|
selector:
|
|
matchLabels:
|
|
app: mariadb
|
|
template:
|
|
metadata:
|
|
labels:
|
|
app: mariadb
|
|
spec:
|
|
initContainers:
|
|
- name: init-mariadb
|
|
image: mariadb
|
|
imagePullPolicy: Always
|
|
command:
|
|
- bash
|
|
- "-c"
|
|
- |
|
|
set -ex
|
|
echo 'Starting init-mariadb';
|
|
# Check config map to directory that already exists
|
|
# (but must be used as a volume for main container)
|
|
ls /mnt/config-map
|
|
# Statefulset has sticky identity, number should be last
|
|
[[ `hostname` =~ -([0-9]+)$ ]] || exit 1
|
|
ordinal=${BASH_REMATCH[1]}
|
|
# Copy appropriate conf.d files from config-map to
|
|
# mariadb-config volume (emptyDir) depending on pod number
|
|
if [[ $ordinal -eq 0 ]]; then
|
|
# This file holds SQL for connecting to primary
|
|
cp /mnt/config-map/primary.cnf /etc/mysql/conf.d/server-id.cnf
|
|
# Create the users needed for replication on primary on a volume
|
|
# initdb (emptyDir)
|
|
cp /mnt/config-map/primary.sql /docker-entrypoint-initdb.d
|
|
else
|
|
# This file holds SQL for connecting to secondary
|
|
cp /mnt/config-map/replica.cnf /etc/mysql/conf.d/server-id.cnf
|
|
# On replicas use secondary configuration on initdb volume
|
|
cp /mnt/config-map/secondary.sql /docker-entrypoint-initdb.d
|
|
fi
|
|
# Add an offset to avoid reserved server-id=0 value.
|
|
echo server-id=$((3000 + $ordinal)) >> etc/mysql/conf.d/server-id.cnf
|
|
ls /etc/mysql/conf.d/
|
|
cat /etc/mysql/conf.d/server-id.cnf
|
|
volumeMounts:
|
|
- name: mariadb-config-map
|
|
mountPath: /mnt/config-map
|
|
- name: mariadb-config
|
|
mountPath: /etc/mysql/conf.d/
|
|
- name: initdb
|
|
mountPath: /docker-entrypoint-initdb.d
|
|
restartPolicy: Always
|
|
containers:
|
|
- name: mariadb
|
|
image: mariadb
|
|
ports:
|
|
- containerPort: 3306
|
|
name: mariadb-port
|
|
env:
|
|
# Using Secrets
|
|
- name: MARIADB_ROOT_PASSWORD
|
|
valueFrom:
|
|
secretKeyRef:
|
|
name: mariadb-secret
|
|
key: mariadb-root-password
|
|
- name: MYSQL_INITDB_SKIP_TZINFO
|
|
value: "1"
|
|
# Mount volume from persistent volume claim
|
|
volumeMounts:
|
|
- name: datadir
|
|
mountPath: /var/lib/mysql/
|
|
- name: mariadb-config
|
|
mountPath: /etc/mysql/conf.d/
|
|
- name: initdb
|
|
mountPath: /docker-entrypoint-initdb.d
|
|
volumes:
|
|
- name: mariadb-config-map
|
|
configMap:
|
|
name: mariadb-configmap
|
|
#defaultMode: 0544
|
|
- name: mariadb-config
|
|
emptyDir: {}
|
|
- name: initdb
|
|
emptyDir: {}
|
|
|
|
volumeClaimTemplates:
|
|
- metadata:
|
|
name: datadir
|
|
spec:
|
|
accessModes: [ "ReadWriteOnce" ]
|
|
resources:
|
|
requests:
|
|
storage: 300M
|