Add Standardized Configuration to POLICY
[oom.git] / kubernetes / sdnc / templates / db-statefulset.yaml
1 # Copyright © 2017 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
15 #{{ if not .Values.disableSdncSdncDbhost }}
16 apiVersion: apps/v1beta1
17 kind: StatefulSet
18 metadata:
19   name: sdnc-dbhost
20   namespace: "{{ .Values.nsPrefix }}"
21 spec:
22   serviceName: "dbhost"
23   replicas: {{ .Values.numberOfDbReplicas }}
24   selector:
25     matchLabels:
26       app: sdnc-dbhost
27   template:
28     metadata:
29       labels:
30         app: sdnc-dbhost
31       name: sdnc-dbhost
32     spec:
33       initContainers:
34       - name: init-mysql
35         image: {{ .Values.image.mysql }}
36         imagePullPolicy: {{ .Values.pullPolicy }}
37         command:
38         - bash
39         - "-c"
40         - |
41           set -ex
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/
52           else
53             cp /mnt/config-map/slave.cnf /mnt/conf.d/
54           fi
55         volumeMounts:
56         - name: conf
57           mountPath: /mnt/conf.d
58         - name: config-map
59           mountPath: /mnt/config-map
60       - name: clone-mysql
61         image: {{ .Values.image.xtrabackup }}
62         env:
63         - name: MYSQL_ROOT_PASSWORD
64           value: openECOMP1.0
65         command:
66         - bash
67         - "-c"
68         - |
69           set -ex
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]}
75           echo ${BASH_REMATCH}
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
79           # Prepare the backup.
80           xtrabackup --user=root --password=$MYSQL_ROOT_PASSWORD --prepare --target-dir=/var/lib/mysql
81           ls -l /var/lib/mysql
82         volumeMounts:
83         - name: sdnc-data
84           mountPath: /var/lib/mysql
85 #{{ if not .Values.disableNfsProvisioner }}
86           subPath: mysql
87 #{{ end }}
88         - name: conf
89           mountPath: /etc/mysql/conf.d
90       containers:
91       - env:
92         - name: MYSQL_ROOT_PASSWORD
93           value: openECOMP1.0
94         - name: MYSQL_ROOT_HOST
95           value: '%'
96         - name: MYSQL_ALLOW_EMPTY_PASSWORD
97           value: "0"
98         image: {{ .Values.image.mysql }}
99         imagePullPolicy: {{ .Values.pullPolicy }}
100         name: sdnc-db-container
101         volumeMounts:
102         - mountPath: /var/lib/mysql
103           name: sdnc-data
104 #{{ if not .Values.disableNfsProvisioner }}
105           subPath: mysql
106 #{{ end }}
107         - name: conf
108           mountPath: /etc/mysql/conf.d
109         ports:
110         - containerPort: 3306
111         resources:
112           requests:
113             cpu: 500m
114             memory: 1Gi
115         livenessProbe:
116           exec:
117             command: ["mysqladmin", "ping"]
118           initialDelaySeconds: 30
119           periodSeconds: 10
120           timeoutSeconds: 5
121         readinessProbe:
122           tcpSocket:
123             port: 3306
124           initialDelaySeconds: 5
125           periodSeconds: 10
126       - name: xtrabackup
127         image: {{ .Values.image.xtrabackup }}
128         env:
129         - name: MYSQL_ROOT_PASSWORD
130           value: openECOMP1.0
131         ports:
132         - name: xtrabackup
133           containerPort: 3307
134         command:
135         - bash
136         - "-c"
137         - |
138           set -ex
139           cd /var/lib/mysql
140           ls -l
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
156           fi
157
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]}
163             echo $ordinal
164             until mysql --user=root --password=$MYSQL_ROOT_PASSWORD -h 127.0.0.1 -e "SELECT 1"; do sleep 1; done
165
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 }}",
172             MASTER_USER="root",
173             MASTER_PASSWORD="$MYSQL_ROOT_PASSWORD",
174             MASTER_CONNECT_RETRY=10;
175           START SLAVE;
176           EOF
177           fi
178
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"
182         volumeMounts:
183         - name: sdnc-data
184           mountPath: /var/lib/mysql
185 #{{ if not .Values.disableNfsProvisioner }}
186           subPath: mysql
187 #{{ end }}
188         - name: conf
189           mountPath: /etc/mysql/conf.d
190         resources:
191           requests:
192             cpu: 100m
193             memory: 100Mi
194       volumes:
195       - name: conf
196         emptyDir: {}
197       - name: config-map
198         configMap:
199           name: sdnc-mysql
200       - name: localtime
201         hostPath:
202           path: /etc/localtime
203 #{{ if .Values.disableNfsProvisioner }}
204       - name: sdnc-data
205         hostPath:
206           path: {{ .Values.dataRootDir }}/{{ .Values.nsPrefix }}/sdnc/data/mysql
207 #{{ else }}
208   volumeClaimTemplates:
209   - metadata:
210       name: sdnc-data
211       annotations:
212         volume.beta.kubernetes.io/storage-class: "{{ .Values.nsPrefix }}-sdnc-data"
213     spec:
214       accessModes: ["ReadWriteMany"]
215       resources:
216         requests:
217           storage: 1Gi
218 #{{ end }}
219 #{{ end }}
220