From e0841eecdeebcf23f1fe34ee55235a50f1dfd42f Mon Sep 17 00:00:00 2001 From: portdirect Date: Sun, 7 Jan 2018 15:50:07 -0500 Subject: [PATCH] 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 --- mariadb/templates/bin/_readiness.sh.tpl | 4 ++++ mariadb/templates/bin/_start.sh.tpl | 29 ++++++++++++++++++------ mariadb/templates/etc/_00-base.cnf.tpl | 2 +- mariadb/templates/service-discovery.yaml | 2 -- mariadb/templates/service.yaml | 8 ------- mariadb/templates/statefulset.yaml | 8 +++---- mariadb/values.yaml | 10 ++++---- 7 files changed, 36 insertions(+), 27 deletions(-) diff --git a/mariadb/templates/bin/_readiness.sh.tpl b/mariadb/templates/bin/_readiness.sh.tpl index d026774923..4bb8f24616 100644 --- a/mariadb/templates/bin/_readiness.sh.tpl +++ b/mariadb/templates/bin/_readiness.sh.tpl @@ -48,4 +48,8 @@ fi echo "${POD_NAME} ready." 1>&2 +if [ -e ${BOOTSTRAP_FILE} ]; then + rm -f ${BOOTSTRAP_FILE} +fi + exit 0 diff --git a/mariadb/templates/bin/_start.sh.tpl b/mariadb/templates/bin/_start.sh.tpl index 00a21f345e..49e3bf4a05 100644 --- a/mariadb/templates/bin/_start.sh.tpl +++ b/mariadb/templates/bin/_start.sh.tpl @@ -25,11 +25,13 @@ if [ ! -d /var/lib/mysql/mysql ]; then if [ "x${FORCE_BOOTSTRAP}" = "xtrue" ]; then echo 'force_bootstrap set, so will force-initialize node 0.' CLUSTER_INIT_ARGS=--wsrep-new-cluster + CLUSTER_BOOTSTRAP=true elif ! mysql --defaults-file=/etc/mysql/admin_user.cnf \ --connect-timeout 2 \ -e 'select 1'; then echo 'No other nodes found, so will initialize cluster.' CLUSTER_INIT_ARGS=--wsrep-new-cluster + CLUSTER_BOOTSTRAP=true else echo 'Found other live nodes, will attempt to join them.' 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.' mkdir /var/lib/mysql/mysql fi + chown -R mysql:mysql /var/lib/mysql fi # Construct cluster config @@ -50,8 +53,8 @@ for i in $(seq 1 ${MARIADB_REPLICAS}); do else NUM="$(expr $i - 1)" fi - CANDIDATE_POD="${SERVICE_NAME}-$NUM.${DISCOVERY_SERVICE_NAME}" - if [ "x${CANDIDATE_POD}" != "x${POD_NAME}.${DISCOVERY_SERVICE_NAME}" ]; then + CANDIDATE_POD="${SERVICE_NAME}-$NUM.$(hostname -d)" + if [ "x${CANDIDATE_POD}" != "x${POD_NAME}.$(hostname -d)" ]; then if [ -n "${MEMBERS}" ]; then MEMBERS+=, fi @@ -60,13 +63,25 @@ for i in $(seq 1 ${MARIADB_REPLICAS}); do done echo "Writing cluster config for ${POD_NAME} to ${CLUSTER_CONFIG_PATH}" -cat >> ${CLUSTER_CONFIG_PATH} <> ${CLUSTER_CONFIG_PATH} << EOF [mysqld] wsrep_cluster_address="gcomm://${MEMBERS}" wsrep_node_address=${POD_IP} -wsrep_node_name=${POD_NAME}.${DISCOVERY_SERVICE_NAME} +wsrep_node_name=${POD_NAME}.$(hostname -d) EOF -echo 'Executing upstream docker-entrypoint.' -set +e -exec /usr/local/bin/docker-entrypoint.sh mysqld ${CLUSTER_INIT_ARGS} +if [ "x${CLUSTER_BOOTSTRAP}" = "xtrue" ]; then + mysql_install_db --user=mysql --datadir=/var/lib/mysql + + 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} diff --git a/mariadb/templates/etc/_00-base.cnf.tpl b/mariadb/templates/etc/_00-base.cnf.tpl index 2d5389e69d..ae40c0a44a 100644 --- a/mariadb/templates/etc/_00-base.cnf.tpl +++ b/mariadb/templates/etc/_00-base.cnf.tpl @@ -90,7 +90,7 @@ binlog_format=ROW default-storage-engine=InnoDB innodb_autoinc_lock_mode=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_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" }}" diff --git a/mariadb/templates/service-discovery.yaml b/mariadb/templates/service-discovery.yaml index 758fe9d7a4..7884331956 100644 --- a/mariadb/templates/service-discovery.yaml +++ b/mariadb/templates/service-discovery.yaml @@ -14,8 +14,6 @@ See the License for the specific language governing permissions and 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 }} {{- $envAll := . }} --- diff --git a/mariadb/templates/service.yaml b/mariadb/templates/service.yaml index 81ce709765..2a313ed140 100644 --- a/mariadb/templates/service.yaml +++ b/mariadb/templates/service.yaml @@ -21,14 +21,6 @@ apiVersion: v1 kind: Service metadata: 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: ports: - name: db diff --git a/mariadb/templates/statefulset.yaml b/mariadb/templates/statefulset.yaml index a5b81c9741..0210c3fbcd 100644 --- a/mariadb/templates/statefulset.yaml +++ b/mariadb/templates/statefulset.yaml @@ -26,7 +26,7 @@ kind: StatefulSet metadata: name: mariadb 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 }} template: metadata: @@ -58,21 +58,21 @@ spec: fieldPath: metadata.name - name: FORCE_BOOTSTRAP value: {{ .Values.force_bootstrap | quote }} + - name: BOOTSTRAP_FILE + value: {{ printf "/tmp/%s.sql" (randAlphaNum 8) }} - name: MARIADB_REPLICAS value: {{ .Values.pod.replicas.server | quote }} - name: WSREP_PORT value: {{ tuple "oslo_db" "internal" "wsrep" . | include "helm-toolkit.endpoints.endpoint_port_lookup" | quote }} - name: SERVICE_NAME 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 valueFrom: secretKeyRef: name: mariadb-db-root-password key: MYSQL_ROOT_PASSWORD readinessProbe: - initialDelaySeconds: 60 + initialDelaySeconds: 30 periodSeconds: 30 timeoutSeconds: 3 exec: diff --git a/mariadb/values.yaml b/mariadb/values.yaml index dec7119412..896032e932 100644 --- a/mariadb/values.yaml +++ b/mariadb/values.yaml @@ -24,11 +24,11 @@ labels: pod: affinity: - anti: - type: - default: preferredDuringSchedulingIgnoredDuringExecution - topologyKey: - default: kubernetes.io/hostname + anti: + type: + default: preferredDuringSchedulingIgnoredDuringExecution + topologyKey: + default: kubernetes.io/hostname replicas: server: 3 lifecycle: