blob: 0304639cb9d09fe8dd576c1c7da00480347c597f [file] [log] [blame]
jmac018e37b2018-04-05 18:14:10 +00001# Copyright © 2017-2018 Amdocs, Bell Canada
2#
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
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
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.
14
Mike Elliott13fed112018-02-28 08:33:33 -050015apiVersion: apps/v1beta1
16kind: StatefulSet
17metadata:
18 name: {{ include "common.fullname" . }}
19 namespace: {{ include "common.namespace" . }}
20 labels:
21 app: {{ include "common.name" . }}
22 chart: {{ .Chart.Name }}-{{ .Chart.Version | replace "+" "_" }}
23 release: {{ .Release.Name }}
24 heritage: {{ .Release.Service }}
25spec:
26 serviceName: {{ .Values.service.name }}
27 replicas: {{ .Values.replicaCount }}
28 template:
29 metadata:
30 labels:
31 app: {{ include "common.name" . }}
32 release: {{ .Release.Name }}
33 spec:
34 initContainers:
35#{{ if not .Values.disableNfsProvisioner }}
36 - name: {{ include "common.name" . }}-readiness
37 command:
38 - /root/ready.py
39 args:
40 - --container-name
41 - {{ .Values.nfsprovisionerPrefix }}-nfs-provisioner
42 env:
43 - name: NAMESPACE
44 valueFrom:
45 fieldRef:
46 apiVersion: v1
47 fieldPath: metadata.namespace
48 image: "{{ .Values.global.readinessRepository }}/{{ .Values.global.readinessImage }}"
49 imagePullPolicy: {{ .Values.global.pullPolicy | default .Values.pullPolicy }}
50#{{ end }}
51 - name: init-mysql
52 image: "{{ .Values.repository | default .Values.repository }}/{{ .Values.image }}"
53 imagePullPolicy: {{ .Values.global.pullPolicy | default .Values.pullPolicy }}
54 command:
55 - bash
56 - "-c"
57 - |
58 set -ex
59 # Generate mysql server-id from pod ordinal index.
60 [[ `hostname` =~ -([0-9]+)$ ]] || exit 1
61 ordinal=${BASH_REMATCH[1]}
Neha Jainbaadb382018-04-03 11:34:59 -040062 siteId={{ .Values.geoSiteId }}
Mike Elliott13fed112018-02-28 08:33:33 -050063 echo BASH_REMATCH=${BASH_REMATCH}
64 echo [mysqld] > /mnt/conf.d/server-id.cnf
65 # Add an offset to avoid reserved server-id=0 value.
Neha Jainbaadb382018-04-03 11:34:59 -040066 echo server-id=$(($siteId*100 + $ordinal)) >> /mnt/conf.d/server-id.cnf
Mike Elliott13fed112018-02-28 08:33:33 -050067 # Copy appropriate conf.d files from config-map to emptyDir.
68 if [[ $ordinal -eq 0 ]]; then
69 cp /mnt/config-map/master.cnf /mnt/conf.d/
70 else
71 cp /mnt/config-map/slave.cnf /mnt/conf.d/
72 fi
73 volumeMounts:
74 - name: conf
75 mountPath: /mnt/conf.d
76 - name: config-map
77 mountPath: /mnt/config-map
78
79 - name: clone-mysql
80 image: "{{ .Values.global.xtrabackupRepository | default .Values.xtrabackupRepository }}/{{ .Values.xtrabackupImage }}"
81 imagePullPolicy: {{ .Values.global.pullPolicy | default .Values.pullPolicy }}
82 env:
83 - name: MYSQL_ROOT_PASSWORD
84 valueFrom:
85 secretKeyRef:
86 name: {{ template "common.fullname" . }}
87 key: db-root-password
88 command:
89 - bash
90 - "-c"
91 - |
92 set -ex
93 # Skip the clone if data already exists.
94 [[ -d /var/lib/mysql/mysql ]] && exit 0
95 # Skip the clone on master (ordinal index 0).
96 [[ `hostname` =~ -([0-9]+)$ ]] || exit 1
97 ordinal=${BASH_REMATCH[1]}
98 echo ${BASH_REMATCH}
99 [[ $ordinal -eq 0 ]] && exit 0
100 # Clone data from previous peer.
jmac018e37b2018-04-05 18:14:10 +0000101 ncat --recv-only {{ include "common.fullname" . }}-$(($ordinal-1)).{{ .Values.service.name }}.{{ include "common.namespace" . }} 3307 | xbstream -x -C /var/lib/mysql
Mike Elliott13fed112018-02-28 08:33:33 -0500102 # Prepare the backup.
103 xtrabackup --user=root --password=$MYSQL_ROOT_PASSWORD --prepare --target-dir=/var/lib/mysql
104 ls -l /var/lib/mysql
105 volumeMounts:
106 - name: {{ include "common.fullname" . }}-data
107 mountPath: /var/lib/mysql
108 subPath: mysql
109 - name: conf
110 mountPath: /etc/mysql/conf.d
Neha Jainbaadb382018-04-03 11:34:59 -0400111
Mike Elliott13fed112018-02-28 08:33:33 -0500112 containers:
113 #sdnc-db-container
114 - name: {{ include "common.name" . }}
115 image: "{{ .Values.repository | default .Values.repository }}/{{ .Values.image }}"
116 imagePullPolicy: {{ .Values.global.pullPolicy | default .Values.pullPolicy }}
117 ports:
118 - containerPort: {{ .Values.service.internalPort }}
119 # disable liveness probe when breakpoints set in debugger
120 # so K8s doesn't restart unresponsive container
121 {{- if eq .Values.liveness.enabled true }}
122 livenessProbe:
123 exec:
124 command: ["mysqladmin", "ping"]
125 initialDelaySeconds: {{ .Values.liveness.initialDelaySeconds }}
126 periodSeconds: {{ .Values.liveness.periodSeconds }}
127 timeoutSeconds: {{ .Values.liveness.timeoutSeconds }}
128 {{end -}}
129 readinessProbe:
130 tcpSocket:
131 port: {{ .Values.service.internalPort }}
132 initialDelaySeconds: {{ .Values.readiness.initialDelaySeconds }}
133 periodSeconds: {{ .Values.readiness.periodSeconds }}
134 env:
135 - name: MYSQL_ROOT_PASSWORD
136 valueFrom:
137 secretKeyRef:
138 name: {{ template "common.fullname" . }}
139 key: db-root-password
140 - name: MYSQL_ROOT_HOST
141 value: '%'
142 - name: MYSQL_ALLOW_EMPTY_PASSWORD
143 value: {{ .Values.config.dbAllowEmptyPassword | default "0" | quote }}
144 volumeMounts:
145 - mountPath: /var/lib/mysql
146 name: {{ include "common.fullname" . }}-data
147 subPath: mysql
148 - mountPath: /etc/mysql/conf.d
149 name: conf
150 resources:
151{{ toYaml .Values.resources | indent 12 }}
152 {{- if .Values.nodeSelector }}
153 nodeSelector:
154{{ toYaml .Values.nodeSelector | indent 10 }}
155 {{- end -}}
156 {{- if .Values.affinity }}
157 affinity:
158{{ toYaml .Values.affinity | indent 10 }}
159 {{- end }}
160
161 - name: xtrabackup
162 image: "{{ .Values.global.xtrabackupRepository | default .Values.xtrabackupRepository }}/{{ .Values.xtrabackupImage }}"
163 imagePullPolicy: {{ .Values.global.pullPolicy | default .Values.pullPolicy }}
164 env:
165 - name: MYSQL_ROOT_PASSWORD
166 valueFrom:
167 secretKeyRef:
168 name: {{ template "common.fullname" . }}
169 key: db-root-password
170 ports:
171 - containerPort: {{ .Values.xtrabackup.internalPort }}
172 name: xtrabackup
173 command:
174 - bash
175 - "-c"
176 - |
177 set -ex
178 cd /var/lib/mysql
179 ls -l
180 # Determine binlog position of cloned data, if any.
181 if [[ -f xtrabackup_slave_info ]]; then
182 echo "Inside xtrabackup_slave_info"
183 # XtraBackup already generated a partial "CHANGE MASTER TO" query
184 # because we're cloning from an existing slave.
185 mv xtrabackup_slave_info change_master_to.sql.in
186 # Ignore xtrabackup_binlog_info in this case (it's useless).
187 rm -f xtrabackup_binlog_info
188 elif [[ -f xtrabackup_binlog_info ]]; then
189 echo "Inside xtrabackup_binlog_info"
190 # We're cloning directly from master. Parse binlog position.
191 [[ `cat xtrabackup_binlog_info` =~ ^(.*?)[[:space:]]+(.*?)$ ]] || exit 1
192 rm xtrabackup_binlog_info
193 echo "CHANGE MASTER TO MASTER_LOG_FILE='${BASH_REMATCH[1]}',\
194 MASTER_LOG_POS=${BASH_REMATCH[2]}" > change_master_to.sql.in
195 fi
196
jmac018e37b2018-04-05 18:14:10 +0000197 [[ `hostname` =~ -([0-9]+)$ ]] || exit 1
198 ordinal=${BASH_REMATCH[1]}
199 echo $ordinal
200
201 mysqlhost={{ include "common.fullname" . }}-$(($ordinal)).{{ .Values.service.name }}.{{ include "common.namespace" . }}
202 echo $mysqlhost
203
Mike Elliott13fed112018-02-28 08:33:33 -0500204 # Check if we need to complete a clone by starting replication.
205 if [[ -f change_master_to.sql.in ]]; then
206 echo "Waiting for mysqld to be ready (accepting connections)"
jmac018e37b2018-04-05 18:14:10 +0000207 until mysql --user=root --password=$MYSQL_ROOT_PASSWORD -h $mysqlhost -e "SELECT 1"; do sleep 1; done
Mike Elliott13fed112018-02-28 08:33:33 -0500208
209 echo "Initializing replication from clone position"
210 # In case of container restart, attempt this at-most-once.
211 mv change_master_to.sql.in change_master_to.sql.orig
jmac018e37b2018-04-05 18:14:10 +0000212 mysql --user=root --password=$MYSQL_ROOT_PASSWORD -h $mysqlhost <<EOF
Mike Elliott13fed112018-02-28 08:33:33 -0500213 $(<change_master_to.sql.orig),
jmac018e37b2018-04-05 18:14:10 +0000214 MASTER_HOST="{{ include "common.fullname" . }}-0.{{ .Values.service.name }}.{{ include "common.namespace" . }}",
Mike Elliott13fed112018-02-28 08:33:33 -0500215 MASTER_USER="root",
216 MASTER_PASSWORD="$MYSQL_ROOT_PASSWORD",
217 MASTER_CONNECT_RETRY=10;
218 START SLAVE;
219 EOF
220 fi
221
222 # Start a server to send backups when requested by peers.
223 exec ncat --listen --keep-open --send-only --max-conns=1 3307 -c \
jmac018e37b2018-04-05 18:14:10 +0000224 "xtrabackup --user=root --password=$MYSQL_ROOT_PASSWORD --backup --slave-info --stream=xbstream --host=$mysqlhost"
Mike Elliott13fed112018-02-28 08:33:33 -0500225 volumeMounts:
226 - name: {{ include "common.fullname" . }}-data
227 mountPath: /var/lib/mysql
228 subPath: mysql
229 - name: conf
230 mountPath: /etc/mysql/conf.d
231 volumes:
232 - name: conf
233 emptyDir: {}
234 - name: config-map
235 configMap:
236 name: {{ include "common.fullname" . }}-db-configmap
237 - name: localtime
238 hostPath:
239 path: /etc/localtime
240 - name: {{ include "common.fullname" . }}-data
241#{{ if not .Values.disableNfsProvisioner }}
242 volumeClaimTemplates:
243 - metadata:
244 name: {{ include "common.fullname" . }}-data
245 annotations:
246 volume.beta.kubernetes.io/storage-class: "{{ include "common.fullname" . }}-data"
247 spec:
248 accessModes: ["ReadWriteMany"]
249 resources:
250 requests:
251 storage: 1Gi
252#{{ else if .Values.persistence.enabled }}
253 persistentVolumeClaim:
254 claimName: {{ include "common.fullname" . }}-data
255#{{ else }}
256 emptyDir: {}
257#{{ end }}
Neha Jainbaadb382018-04-03 11:34:59 -0400258