1 # Copyright © 2017 Amdocs, Bell Canada
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
15 #{{ if not .Values.disableSdncSdncDbhost }}
16 apiVersion: apps/v1beta1
20 namespace: "{{ .Values.nsPrefix }}"
23 replicas: {{ .Values.numberOfDbReplicas }}
35 image: {{ .Values.image.mysql }}
36 imagePullPolicy: {{ .Values.pullPolicy }}
42 # Generate mysql server-id from pod ordinal index.
43 [[ `hostname` =~ -([0-9]+)$ ]] || exit 1
44 ordinal=${BASH_REMATCH[1]}
45 echo BASH_REMATCH=${BASH_REMATCH}
46 echo [mysqld] > /mnt/conf.d/server-id.cnf
47 # Add an offset to avoid reserved server-id=0 value.
48 echo server-id=$((100 + $ordinal)) >> /mnt/conf.d/server-id.cnf
49 # Copy appropriate conf.d files from config-map to emptyDir.
50 if [[ $ordinal -eq 0 ]]; then
51 cp /mnt/config-map/master.cnf /mnt/conf.d/
53 cp /mnt/config-map/slave.cnf /mnt/conf.d/
57 mountPath: /mnt/conf.d
59 mountPath: /mnt/config-map
61 image: {{ .Values.image.xtrabackup }}
63 - name: MYSQL_ROOT_PASSWORD
70 # Skip the clone if data already exists.
71 [[ -d /var/lib/mysql/mysql ]] && exit 0
72 # Skip the clone on master (ordinal index 0).
73 [[ `hostname` =~ -([0-9]+)$ ]] || exit 1
74 ordinal=${BASH_REMATCH[1]}
76 [[ $ordinal -eq 0 ]] && exit 0
77 # Clone data from previous peer.
78 ncat --recv-only sdnc-dbhost-$(($ordinal-1)).dbhost.{{ .Values.nsPrefix }} 3307 | xbstream -x -C /var/lib/mysql
80 xtrabackup --user=root --password=$MYSQL_ROOT_PASSWORD --prepare --target-dir=/var/lib/mysql
84 mountPath: /var/lib/mysql
85 #{{ if not .Values.disableNfsProvisioner }}
89 mountPath: /etc/mysql/conf.d
92 - name: MYSQL_ROOT_PASSWORD
94 - name: MYSQL_ROOT_HOST
96 - name: MYSQL_ALLOW_EMPTY_PASSWORD
98 image: {{ .Values.image.mysql }}
99 imagePullPolicy: {{ .Values.pullPolicy }}
100 name: sdnc-db-container
102 - mountPath: /var/lib/mysql
104 #{{ if not .Values.disableNfsProvisioner }}
108 mountPath: /etc/mysql/conf.d
110 - containerPort: 3306
117 command: ["mysqladmin", "ping"]
118 initialDelaySeconds: 30
124 initialDelaySeconds: 5
127 image: {{ .Values.image.xtrabackup }}
129 - name: MYSQL_ROOT_PASSWORD
141 # Determine binlog position of cloned data, if any.
142 if [[ -f xtrabackup_slave_info ]]; then
143 echo "Inside xtrabackup_slave_info"
144 # XtraBackup already generated a partial "CHANGE MASTER TO" query
145 # because we're cloning from an existing slave.
146 mv xtrabackup_slave_info change_master_to.sql.in
147 # Ignore xtrabackup_binlog_info in this case (it's useless).
148 rm -f xtrabackup_binlog_info
149 elif [[ -f xtrabackup_binlog_info ]]; then
150 echo "Inside xtrabackup_binlog_info"
151 # We're cloning directly from master. Parse binlog position.
152 [[ `cat xtrabackup_binlog_info` =~ ^(.*?)[[:space:]]+(.*?)$ ]] || exit 1
153 rm xtrabackup_binlog_info
154 echo "CHANGE MASTER TO MASTER_LOG_FILE='${BASH_REMATCH[1]}',\
155 MASTER_LOG_POS=${BASH_REMATCH[2]}" > change_master_to.sql.in
158 # Check if we need to complete a clone by starting replication.
159 if [[ -f change_master_to.sql.in ]]; then
160 echo "Waiting for mysqld to be ready (accepting connections)"
161 [[ `hostname` =~ -([0-9]+)$ ]] || exit 1
162 ordinal=${BASH_REMATCH[1]}
164 until mysql --user=root --password=$MYSQL_ROOT_PASSWORD -h 127.0.0.1 -e "SELECT 1"; do sleep 1; done
166 echo "Initializing replication from clone position"
167 # In case of container restart, attempt this at-most-once.
168 mv change_master_to.sql.in change_master_to.sql.orig
169 mysql --user=root --password=$MYSQL_ROOT_PASSWORD -h 127.0.0.1 <<EOF
170 $(<change_master_to.sql.orig),
171 MASTER_HOST="sdnc-dbhost-0.dbhost.{{ .Values.nsPrefix }}",
173 MASTER_PASSWORD="$MYSQL_ROOT_PASSWORD",
174 MASTER_CONNECT_RETRY=10;
179 # Start a server to send backups when requested by peers.
180 exec ncat --listen --keep-open --send-only --max-conns=1 3307 -c \
181 "xtrabackup --user=root --password=$MYSQL_ROOT_PASSWORD --backup --slave-info --stream=xbstream --host=127.0.0.1"
184 mountPath: /var/lib/mysql
185 #{{ if not .Values.disableNfsProvisioner }}
189 mountPath: /etc/mysql/conf.d
203 #{{ if .Values.disableNfsProvisioner }}
206 path: {{ .Values.dataRootDir }}/{{ .Values.nsPrefix }}/sdnc/data/mysql
208 volumeClaimTemplates:
212 volume.beta.kubernetes.io/storage-class: "{{ .Values.nsPrefix }}-sdnc-data"
214 accessModes: ["ReadWriteMany"]