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