MariaDB: simplify db init process

This PS simplifys the db start and init script, increasing
robustness of the mariadb chart on low end hardware. It also
removes some unrequired annotations and moves to more explicit
qualification of the clustering variables.

Change-Id: I6cff5fb6ccfdcc6c82203b0e5eff445ee98bc298
This commit is contained in:
portdirect 2018-01-07 15:50:07 -05:00
parent b685513d55
commit e0841eecde
7 changed files with 36 additions and 27 deletions

View File

@ -48,4 +48,8 @@ fi
echo "${POD_NAME} ready." 1>&2 echo "${POD_NAME} ready." 1>&2
if [ -e ${BOOTSTRAP_FILE} ]; then
rm -f ${BOOTSTRAP_FILE}
fi
exit 0 exit 0

View File

@ -25,11 +25,13 @@ if [ ! -d /var/lib/mysql/mysql ]; then
if [ "x${FORCE_BOOTSTRAP}" = "xtrue" ]; then if [ "x${FORCE_BOOTSTRAP}" = "xtrue" ]; then
echo 'force_bootstrap set, so will force-initialize node 0.' echo 'force_bootstrap set, so will force-initialize node 0.'
CLUSTER_INIT_ARGS=--wsrep-new-cluster CLUSTER_INIT_ARGS=--wsrep-new-cluster
CLUSTER_BOOTSTRAP=true
elif ! mysql --defaults-file=/etc/mysql/admin_user.cnf \ elif ! mysql --defaults-file=/etc/mysql/admin_user.cnf \
--connect-timeout 2 \ --connect-timeout 2 \
-e 'select 1'; then -e 'select 1'; then
echo 'No other nodes found, so will initialize cluster.' echo 'No other nodes found, so will initialize cluster.'
CLUSTER_INIT_ARGS=--wsrep-new-cluster CLUSTER_INIT_ARGS=--wsrep-new-cluster
CLUSTER_BOOTSTRAP=true
else else
echo 'Found other live nodes, will attempt to join them.' echo 'Found other live nodes, will attempt to join them.'
mkdir /var/lib/mysql/mysql mkdir /var/lib/mysql/mysql
@ -38,6 +40,7 @@ if [ ! -d /var/lib/mysql/mysql ]; then
echo 'Not pod 0, so will avoid upstream database initialization.' echo 'Not pod 0, so will avoid upstream database initialization.'
mkdir /var/lib/mysql/mysql mkdir /var/lib/mysql/mysql
fi fi
chown -R mysql:mysql /var/lib/mysql
fi fi
# Construct cluster config # Construct cluster config
@ -50,8 +53,8 @@ for i in $(seq 1 ${MARIADB_REPLICAS}); do
else else
NUM="$(expr $i - 1)" NUM="$(expr $i - 1)"
fi fi
CANDIDATE_POD="${SERVICE_NAME}-$NUM.${DISCOVERY_SERVICE_NAME}" CANDIDATE_POD="${SERVICE_NAME}-$NUM.$(hostname -d)"
if [ "x${CANDIDATE_POD}" != "x${POD_NAME}.${DISCOVERY_SERVICE_NAME}" ]; then if [ "x${CANDIDATE_POD}" != "x${POD_NAME}.$(hostname -d)" ]; then
if [ -n "${MEMBERS}" ]; then if [ -n "${MEMBERS}" ]; then
MEMBERS+=, MEMBERS+=,
fi fi
@ -60,13 +63,25 @@ for i in $(seq 1 ${MARIADB_REPLICAS}); do
done done
echo "Writing cluster config for ${POD_NAME} to ${CLUSTER_CONFIG_PATH}" echo "Writing cluster config for ${POD_NAME} to ${CLUSTER_CONFIG_PATH}"
cat >> ${CLUSTER_CONFIG_PATH} <<EOF cat >> ${CLUSTER_CONFIG_PATH} << EOF
[mysqld] [mysqld]
wsrep_cluster_address="gcomm://${MEMBERS}" wsrep_cluster_address="gcomm://${MEMBERS}"
wsrep_node_address=${POD_IP} wsrep_node_address=${POD_IP}
wsrep_node_name=${POD_NAME}.${DISCOVERY_SERVICE_NAME} wsrep_node_name=${POD_NAME}.$(hostname -d)
EOF EOF
echo 'Executing upstream docker-entrypoint.' if [ "x${CLUSTER_BOOTSTRAP}" = "xtrue" ]; then
set +e mysql_install_db --user=mysql --datadir=/var/lib/mysql
exec /usr/local/bin/docker-entrypoint.sh mysqld ${CLUSTER_INIT_ARGS}
cat > "${BOOTSTRAP_FILE}" << EOF
DELETE FROM mysql.user ;
CREATE OR REPLACE USER 'root'@'%' IDENTIFIED BY '${MYSQL_ROOT_PASSWORD}' ;
GRANT ALL ON *.* TO 'root'@'%' WITH GRANT OPTION ;
DROP DATABASE IF EXISTS test ;
FLUSH PRIVILEGES ;
EOF
CLUSTER_INIT_ARGS="${CLUSTER_INIT_ARGS} --init-file=${BOOTSTRAP_FILE}"
fi
exec mysqld ${CLUSTER_INIT_ARGS}

View File

@ -90,7 +90,7 @@ binlog_format=ROW
default-storage-engine=InnoDB default-storage-engine=InnoDB
innodb_autoinc_lock_mode=2 innodb_autoinc_lock_mode=2
innodb_flush_log_at_trx_commit=2 innodb_flush_log_at_trx_commit=2
wsrep_cluster_name={{ tuple "oslo_db" "internal" . | include "helm-toolkit.endpoints.hostname_short_endpoint_lookup" }} wsrep_cluster_name={{ tuple "oslo_db" "internal" . | include "helm-toolkit.endpoints.hostname_namespaced_endpoint_lookup" | replace "." "_" }}
wsrep_on=1 wsrep_on=1
wsrep_provider=/usr/lib/galera/libgalera_smm.so wsrep_provider=/usr/lib/galera/libgalera_smm.so
wsrep_provider_options="gmcast.listen_addr=tcp://0.0.0.0:{{ tuple "oslo_db" "internal" "wsrep" . | include "helm-toolkit.endpoints.endpoint_port_lookup" }}" wsrep_provider_options="gmcast.listen_addr=tcp://0.0.0.0:{{ tuple "oslo_db" "internal" "wsrep" . | include "helm-toolkit.endpoints.endpoint_port_lookup" }}"

View File

@ -14,8 +14,6 @@ See the License for the specific language governing permissions and
limitations under the License. limitations under the License.
*/}} */}}
# This service could be used for cluster pod discovery, though instead it's
# primarily here to allow DNS lookups of cluster pods.
{{- if .Values.manifests.service_discovery }} {{- if .Values.manifests.service_discovery }}
{{- $envAll := . }} {{- $envAll := . }}
--- ---

View File

@ -21,14 +21,6 @@ apiVersion: v1
kind: Service kind: Service
metadata: metadata:
name: {{ tuple "oslo_db" "internal" . | include "helm-toolkit.endpoints.hostname_short_endpoint_lookup" }} name: {{ tuple "oslo_db" "internal" . | include "helm-toolkit.endpoints.hostname_short_endpoint_lookup" }}
annotations:
# This is needed to make the peer-finder work properly and to help avoid
# edge cases where instance 0 comes up after losing its data and needs to
# decide whether it should create a new cluster or try to join an existing
# one. If it creates a new cluster when it should have joined an existing
# one, we'd end up with two separate clusters listening at the same service
# endpoint, which would be very bad.
service.alpha.kubernetes.io/tolerate-unready-endpoints: "false"
spec: spec:
ports: ports:
- name: db - name: db

View File

@ -26,7 +26,7 @@ kind: StatefulSet
metadata: metadata:
name: mariadb name: mariadb
spec: spec:
serviceName: "{{ tuple "oslo_db" "internal" . | include "helm-toolkit.endpoints.hostname_short_endpoint_lookup" }}-discovery" serviceName: "{{ tuple "oslo_db" "discovery" . | include "helm-toolkit.endpoints.hostname_short_endpoint_lookup" }}"
replicas: {{ .Values.pod.replicas.server }} replicas: {{ .Values.pod.replicas.server }}
template: template:
metadata: metadata:
@ -58,21 +58,21 @@ spec:
fieldPath: metadata.name fieldPath: metadata.name
- name: FORCE_BOOTSTRAP - name: FORCE_BOOTSTRAP
value: {{ .Values.force_bootstrap | quote }} value: {{ .Values.force_bootstrap | quote }}
- name: BOOTSTRAP_FILE
value: {{ printf "/tmp/%s.sql" (randAlphaNum 8) }}
- name: MARIADB_REPLICAS - name: MARIADB_REPLICAS
value: {{ .Values.pod.replicas.server | quote }} value: {{ .Values.pod.replicas.server | quote }}
- name: WSREP_PORT - name: WSREP_PORT
value: {{ tuple "oslo_db" "internal" "wsrep" . | include "helm-toolkit.endpoints.endpoint_port_lookup" | quote }} value: {{ tuple "oslo_db" "internal" "wsrep" . | include "helm-toolkit.endpoints.endpoint_port_lookup" | quote }}
- name: SERVICE_NAME - name: SERVICE_NAME
value: {{ tuple "oslo_db" "internal" . | include "helm-toolkit.endpoints.hostname_short_endpoint_lookup" }} value: {{ tuple "oslo_db" "internal" . | include "helm-toolkit.endpoints.hostname_short_endpoint_lookup" }}
- name: DISCOVERY_SERVICE_NAME
value: {{ tuple "oslo_db" "discovery" . | include "helm-toolkit.endpoints.hostname_short_endpoint_lookup"}}
- name: MYSQL_ROOT_PASSWORD - name: MYSQL_ROOT_PASSWORD
valueFrom: valueFrom:
secretKeyRef: secretKeyRef:
name: mariadb-db-root-password name: mariadb-db-root-password
key: MYSQL_ROOT_PASSWORD key: MYSQL_ROOT_PASSWORD
readinessProbe: readinessProbe:
initialDelaySeconds: 60 initialDelaySeconds: 30
periodSeconds: 30 periodSeconds: 30
timeoutSeconds: 3 timeoutSeconds: 3
exec: exec:

View File

@ -24,11 +24,11 @@ labels:
pod: pod:
affinity: affinity:
anti: anti:
type: type:
default: preferredDuringSchedulingIgnoredDuringExecution default: preferredDuringSchedulingIgnoredDuringExecution
topologyKey: topologyKey:
default: kubernetes.io/hostname default: kubernetes.io/hostname
replicas: replicas:
server: 3 server: 3
lifecycle: lifecycle: