OOM-1373 Add missing label release to components
[oom.git] / kubernetes / common / mysql / templates / statefulset.yaml
1 {{/*
2 # Copyright © 2017 Amdocs, Bell Canada
3 #
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.
15 */}}
16
17 apiVersion: apps/v1beta1
18 kind: StatefulSet
19 metadata:
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 }}
27 spec:
28   serviceName: {{ .Values.service.name }}
29   replicas: {{ .Values.replicaCount }}
30   template:
31     metadata:
32       labels:
33         app: {{ include "common.name" . }}
34         chart: {{ .Chart.Name }}-{{ .Chart.Version | replace "+" "_" }}
35         release: {{ .Release.Name }}
36         heritage: {{ .Release.Service }}
37     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
56         image: "{{ .Values.dockerHubRepository }}/{{ .Values.image }}"
57         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]}
66           siteId={{ .Values.geoSiteId }}
67           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.
70           echo server-id=$(($siteId*100 + $ordinal)) >> /mnt/conf.d/server-id.cnf
71           # 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.
105           ncat --recv-only {{ include "common.fullname" . }}-$(($ordinal-1)).{{ .Values.service.name }}.{{ include "common.namespace" . }} 3307 | xbstream -x -C {{ .Values.persistence.mysqlPath }}
106           # Prepare the backup.
107           xtrabackup --user=root --password=$MYSQL_ROOT_PASSWORD --prepare --target-dir=/var/lib/mysql
108           ls -l {{ .Values.persistence.mysqlPath }}
109         volumeMounts:
110         - name: {{ include "common.fullname" . }}-mysql
111           mountPath: {{ .Values.persistence.mysqlPath }}
112         - name: conf
113           mountPath: /etc/mysql/conf.d
114
115       containers:
116         - name: {{ include "common.name" . }}
117           image: "{{ .Values.dockerHubRepository }}/{{ .Values.image }}"
118           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:
147           - mountPath: {{ .Values.persistence.mysqlPath }}
148             name: {{ include "common.fullname" . }}-mysql
149           - mountPath: /etc/mysql/conf.d
150             name: conf
151           resources:
152 {{ include "common.resources" . | indent 12 }}
153         {{- 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
179             cd {{ .Values.persistence.mysqlPath }}
180             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
198             [[ `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
205             # 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)"
208               until mysql --user=root --password=$MYSQL_ROOT_PASSWORD -h $mysqlhost -e "SELECT 1"; do sleep 1; done
209
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
213               mysql --user=root --password=$MYSQL_ROOT_PASSWORD -h $mysqlhost <<EOF
214             $(<change_master_to.sql.orig),
215               MASTER_HOST="{{ include "common.fullname" . }}-0.{{ .Values.service.name }}.{{ include "common.namespace" . }}",
216               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 \
225               "xtrabackup --user=root --password=$MYSQL_ROOT_PASSWORD --backup --slave-info --stream=xbstream --host=$mysqlhost"
226           volumeMounts:
227           - name: {{ include "common.fullname" . }}-mysql
228             mountPath: {{ .Values.persistence.mysqlPath }}
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" . }}-mysql
241   {{ if not .Values.persistence.enabled }}
242       - name: {{ include "common.fullname" . }}-mysql
243         emptyDir: {}
244   {{ else }}
245   volumeClaimTemplates:
246   - metadata:
247       name: {{ include "common.fullname" . }}-mysql
248       labels:
249         app: {{ include "common.name" . }}
250         chart: {{ .Chart.Name }}-{{ .Chart.Version | replace "+" "_" }}
251         release: {{ .Release.Name }}
252         heritage: {{ .Release.Service }}
253         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 }}
260       selector:
261         matchLabels:
262           name: {{ include "common.fullname" . }}
263   {{ end }}