blob: c3f8ae236f92f80a9c463138cd38e6f7a63ee16f [file] [log] [blame]
jmaca4ddffa2018-04-08 19:28:01 +00001{{/*
2# Copyright © 2017 Amdocs, Bell Canada
jmac018e37b2018-04-05 18:14:10 +00003#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
jmaca4ddffa2018-04-08 19:28:01 +000015*/}}
jmac018e37b2018-04-05 18:14:10 +000016
Mike Elliott13fed112018-02-28 08:33:33 -050017apiVersion: apps/v1beta1
18kind: StatefulSet
19metadata:
20 name: {{ include "common.fullname" . }}
21 namespace: {{ include "common.namespace" . }}
22 labels:
23 app: {{ include "common.name" . }}
24 chart: {{ .Chart.Name }}-{{ .Chart.Version | replace "+" "_" }}
25 release: {{ .Release.Name }}
26 heritage: {{ .Release.Service }}
27spec:
28 serviceName: {{ .Values.service.name }}
29 replicas: {{ .Values.replicaCount }}
30 template:
31 metadata:
32 labels:
33 app: {{ include "common.name" . }}
sushil masal08ef7092019-02-21 16:21:51 +053034 chart: {{ .Chart.Name }}-{{ .Chart.Version | replace "+" "_" }}
Mike Elliott13fed112018-02-28 08:33:33 -050035 release: {{ .Release.Name }}
sushil masal08ef7092019-02-21 16:21:51 +053036 heritage: {{ .Release.Service }}
Mike Elliott13fed112018-02-28 08:33:33 -050037 spec:
38 initContainers:
39#{{ if not .Values.disableNfsProvisioner }}
40 - name: {{ include "common.name" . }}-readiness
41 command:
42 - /root/ready.py
43 args:
44 - --container-name
45 - {{ .Values.nfsprovisionerPrefix }}-nfs-provisioner
46 env:
47 - name: NAMESPACE
48 valueFrom:
49 fieldRef:
50 apiVersion: v1
51 fieldPath: metadata.namespace
52 image: "{{ .Values.global.readinessRepository }}/{{ .Values.global.readinessImage }}"
53 imagePullPolicy: {{ .Values.global.pullPolicy | default .Values.pullPolicy }}
54#{{ end }}
55 - name: init-mysql
jmaca4ddffa2018-04-08 19:28:01 +000056 image: "{{ .Values.dockerHubRepository }}/{{ .Values.image }}"
Mike Elliott13fed112018-02-28 08:33:33 -050057 imagePullPolicy: {{ .Values.global.pullPolicy | default .Values.pullPolicy }}
58 command:
59 - bash
60 - "-c"
61 - |
62 set -ex
63 # Generate mysql server-id from pod ordinal index.
64 [[ `hostname` =~ -([0-9]+)$ ]] || exit 1
65 ordinal=${BASH_REMATCH[1]}
Neha Jainbaadb382018-04-03 11:34:59 -040066 siteId={{ .Values.geoSiteId }}
Mike Elliott13fed112018-02-28 08:33:33 -050067 echo BASH_REMATCH=${BASH_REMATCH}
68 echo [mysqld] > /mnt/conf.d/server-id.cnf
69 # Add an offset to avoid reserved server-id=0 value.
Neha Jainbaadb382018-04-03 11:34:59 -040070 echo server-id=$(($siteId*100 + $ordinal)) >> /mnt/conf.d/server-id.cnf
Mike Elliott13fed112018-02-28 08:33:33 -050071 # Copy appropriate conf.d files from config-map to emptyDir.
72 if [[ $ordinal -eq 0 ]]; then
73 cp /mnt/config-map/master.cnf /mnt/conf.d/
74 else
75 cp /mnt/config-map/slave.cnf /mnt/conf.d/
76 fi
77 volumeMounts:
78 - name: conf
79 mountPath: /mnt/conf.d
80 - name: config-map
81 mountPath: /mnt/config-map
82
83 - name: clone-mysql
84 image: "{{ .Values.global.xtrabackupRepository | default .Values.xtrabackupRepository }}/{{ .Values.xtrabackupImage }}"
85 imagePullPolicy: {{ .Values.global.pullPolicy | default .Values.pullPolicy }}
86 env:
87 - name: MYSQL_ROOT_PASSWORD
88 valueFrom:
89 secretKeyRef:
90 name: {{ template "common.fullname" . }}
91 key: db-root-password
92 command:
93 - bash
94 - "-c"
95 - |
96 set -ex
97 # Skip the clone if data already exists.
98 [[ -d /var/lib/mysql/mysql ]] && exit 0
99 # Skip the clone on master (ordinal index 0).
100 [[ `hostname` =~ -([0-9]+)$ ]] || exit 1
101 ordinal=${BASH_REMATCH[1]}
102 echo ${BASH_REMATCH}
103 [[ $ordinal -eq 0 ]] && exit 0
104 # Clone data from previous peer.
Mohammadreza Pasandidehcffec6d2018-05-08 17:42:11 -0400105 ncat --recv-only {{ include "common.fullname" . }}-$(($ordinal-1)).{{ .Values.service.name }}.{{ include "common.namespace" . }} 3307 | xbstream -x -C {{ .Values.persistence.mysqlPath }}
Mike Elliott13fed112018-02-28 08:33:33 -0500106 # Prepare the backup.
107 xtrabackup --user=root --password=$MYSQL_ROOT_PASSWORD --prepare --target-dir=/var/lib/mysql
Mohammadreza Pasandidehcffec6d2018-05-08 17:42:11 -0400108 ls -l {{ .Values.persistence.mysqlPath }}
Mike Elliott13fed112018-02-28 08:33:33 -0500109 volumeMounts:
Mohammadreza Pasandidehcffec6d2018-05-08 17:42:11 -0400110 - name: {{ include "common.fullname" . }}-mysql
111 mountPath: {{ .Values.persistence.mysqlPath }}
Mike Elliott13fed112018-02-28 08:33:33 -0500112 - name: conf
113 mountPath: /etc/mysql/conf.d
Neha Jainbaadb382018-04-03 11:34:59 -0400114
Mike Elliott13fed112018-02-28 08:33:33 -0500115 containers:
Mike Elliott13fed112018-02-28 08:33:33 -0500116 - name: {{ include "common.name" . }}
jmaca4ddffa2018-04-08 19:28:01 +0000117 image: "{{ .Values.dockerHubRepository }}/{{ .Values.image }}"
Mike Elliott13fed112018-02-28 08:33:33 -0500118 imagePullPolicy: {{ .Values.global.pullPolicy | default .Values.pullPolicy }}
119 ports:
120 - containerPort: {{ .Values.service.internalPort }}
121 # disable liveness probe when breakpoints set in debugger
122 # so K8s doesn't restart unresponsive container
123 {{- if eq .Values.liveness.enabled true }}
124 livenessProbe:
125 exec:
126 command: ["mysqladmin", "ping"]
127 initialDelaySeconds: {{ .Values.liveness.initialDelaySeconds }}
128 periodSeconds: {{ .Values.liveness.periodSeconds }}
129 timeoutSeconds: {{ .Values.liveness.timeoutSeconds }}
130 {{end -}}
131 readinessProbe:
132 tcpSocket:
133 port: {{ .Values.service.internalPort }}
134 initialDelaySeconds: {{ .Values.readiness.initialDelaySeconds }}
135 periodSeconds: {{ .Values.readiness.periodSeconds }}
136 env:
137 - name: MYSQL_ROOT_PASSWORD
138 valueFrom:
139 secretKeyRef:
140 name: {{ template "common.fullname" . }}
141 key: db-root-password
142 - name: MYSQL_ROOT_HOST
143 value: '%'
144 - name: MYSQL_ALLOW_EMPTY_PASSWORD
145 value: {{ .Values.config.dbAllowEmptyPassword | default "0" | quote }}
146 volumeMounts:
Mohammadreza Pasandidehcffec6d2018-05-08 17:42:11 -0400147 - mountPath: {{ .Values.persistence.mysqlPath }}
148 name: {{ include "common.fullname" . }}-mysql
Mike Elliott13fed112018-02-28 08:33:33 -0500149 - mountPath: /etc/mysql/conf.d
150 name: conf
151 resources:
Mandeep Khinda5e3f36a2018-09-24 15:25:42 +0000152{{ include "common.resources" . | indent 12 }}
Mike Elliott13fed112018-02-28 08:33:33 -0500153 {{- if .Values.nodeSelector }}
154 nodeSelector:
155{{ toYaml .Values.nodeSelector | indent 10 }}
156 {{- end -}}
157 {{- if .Values.affinity }}
158 affinity:
159{{ toYaml .Values.affinity | indent 10 }}
160 {{- end }}
161
162 - name: xtrabackup
163 image: "{{ .Values.global.xtrabackupRepository | default .Values.xtrabackupRepository }}/{{ .Values.xtrabackupImage }}"
164 imagePullPolicy: {{ .Values.global.pullPolicy | default .Values.pullPolicy }}
165 env:
166 - name: MYSQL_ROOT_PASSWORD
167 valueFrom:
168 secretKeyRef:
169 name: {{ template "common.fullname" . }}
170 key: db-root-password
171 ports:
172 - containerPort: {{ .Values.xtrabackup.internalPort }}
173 name: xtrabackup
174 command:
175 - bash
176 - "-c"
177 - |
178 set -ex
Mohammadreza Pasandidehcffec6d2018-05-08 17:42:11 -0400179 cd {{ .Values.persistence.mysqlPath }}
Mike Elliott13fed112018-02-28 08:33:33 -0500180 ls -l
181 # Determine binlog position of cloned data, if any.
182 if [[ -f xtrabackup_slave_info ]]; then
183 echo "Inside xtrabackup_slave_info"
184 # XtraBackup already generated a partial "CHANGE MASTER TO" query
185 # because we're cloning from an existing slave.
186 mv xtrabackup_slave_info change_master_to.sql.in
187 # Ignore xtrabackup_binlog_info in this case (it's useless).
188 rm -f xtrabackup_binlog_info
189 elif [[ -f xtrabackup_binlog_info ]]; then
190 echo "Inside xtrabackup_binlog_info"
191 # We're cloning directly from master. Parse binlog position.
192 [[ `cat xtrabackup_binlog_info` =~ ^(.*?)[[:space:]]+(.*?)$ ]] || exit 1
193 rm xtrabackup_binlog_info
194 echo "CHANGE MASTER TO MASTER_LOG_FILE='${BASH_REMATCH[1]}',\
195 MASTER_LOG_POS=${BASH_REMATCH[2]}" > change_master_to.sql.in
196 fi
197
jmac018e37b2018-04-05 18:14:10 +0000198 [[ `hostname` =~ -([0-9]+)$ ]] || exit 1
199 ordinal=${BASH_REMATCH[1]}
200 echo $ordinal
201
202 mysqlhost={{ include "common.fullname" . }}-$(($ordinal)).{{ .Values.service.name }}.{{ include "common.namespace" . }}
203 echo $mysqlhost
204
Mike Elliott13fed112018-02-28 08:33:33 -0500205 # Check if we need to complete a clone by starting replication.
206 if [[ -f change_master_to.sql.in ]]; then
207 echo "Waiting for mysqld to be ready (accepting connections)"
jmac018e37b2018-04-05 18:14:10 +0000208 until mysql --user=root --password=$MYSQL_ROOT_PASSWORD -h $mysqlhost -e "SELECT 1"; do sleep 1; done
Mike Elliott13fed112018-02-28 08:33:33 -0500209
210 echo "Initializing replication from clone position"
211 # In case of container restart, attempt this at-most-once.
212 mv change_master_to.sql.in change_master_to.sql.orig
jmac018e37b2018-04-05 18:14:10 +0000213 mysql --user=root --password=$MYSQL_ROOT_PASSWORD -h $mysqlhost <<EOF
Mike Elliott13fed112018-02-28 08:33:33 -0500214 $(<change_master_to.sql.orig),
jmac018e37b2018-04-05 18:14:10 +0000215 MASTER_HOST="{{ include "common.fullname" . }}-0.{{ .Values.service.name }}.{{ include "common.namespace" . }}",
Mike Elliott13fed112018-02-28 08:33:33 -0500216 MASTER_USER="root",
217 MASTER_PASSWORD="$MYSQL_ROOT_PASSWORD",
218 MASTER_CONNECT_RETRY=10;
219 START SLAVE;
220 EOF
221 fi
222
223 # Start a server to send backups when requested by peers.
224 exec ncat --listen --keep-open --send-only --max-conns=1 3307 -c \
jmac018e37b2018-04-05 18:14:10 +0000225 "xtrabackup --user=root --password=$MYSQL_ROOT_PASSWORD --backup --slave-info --stream=xbstream --host=$mysqlhost"
Mike Elliott13fed112018-02-28 08:33:33 -0500226 volumeMounts:
Mohammadreza Pasandidehcffec6d2018-05-08 17:42:11 -0400227 - name: {{ include "common.fullname" . }}-mysql
228 mountPath: {{ .Values.persistence.mysqlPath }}
Mike Elliott13fed112018-02-28 08:33:33 -0500229 - 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
Mohammadreza Pasandidehcffec6d2018-05-08 17:42:11 -0400240 - name: {{ include "common.fullname" . }}-mysql
241 {{ if not .Values.persistence.enabled }}
242 - name: {{ include "common.fullname" . }}-mysql
Mike Elliott13fed112018-02-28 08:33:33 -0500243 emptyDir: {}
Mohammadreza Pasandidehcffec6d2018-05-08 17:42:11 -0400244 {{ else }}
245 volumeClaimTemplates:
246 - metadata:
247 name: {{ include "common.fullname" . }}-mysql
248 labels:
sushil masal08ef7092019-02-21 16:21:51 +0530249 app: {{ include "common.name" . }}
250 chart: {{ .Chart.Name }}-{{ .Chart.Version | replace "+" "_" }}
251 release: {{ .Release.Name }}
252 heritage: {{ .Release.Service }}
Mohammadreza Pasandidehcffec6d2018-05-08 17:42:11 -0400253 name: {{ include "common.fullname" . }}
254 spec:
255 accessModes: [ {{ .Values.persistence.accessMode }} ]
256 storageClassName: {{ include "common.fullname" . }}-mysql
257 resources:
258 requests:
259 storage: {{ .Values.persistence.size }}
Alexis de Talhouët634b4552018-10-28 21:56:33 -0400260 selector:
261 matchLabels:
262 name: {{ include "common.fullname" . }}
Mohammadreza Pasandidehcffec6d2018-05-08 17:42:11 -0400263 {{ end }}