Add Helm Chart "template" Starter
[oom.git] / kubernetes / sdnc / templates / db-statefulset.yaml
1 #{{ if not .Values.disableSdncSdncDbhost }}
2 apiVersion: apps/v1beta1
3 kind: StatefulSet
4 metadata:
5   name: sdnc-dbhost
6   namespace: "{{ .Values.nsPrefix }}-sdnc"
7 spec:
8   serviceName: "dbhost"
9   replicas: {{ .Values.numberOfDbReplicas }}
10   selector:
11     matchLabels:
12       app: sdnc-dbhost
13   template:
14     metadata:
15       labels:
16         app: sdnc-dbhost
17       name: sdnc-dbhost
18     spec:
19       initContainers:
20       - name: init-mysql
21         image: {{ .Values.image.mysql }}
22         imagePullPolicy: {{ .Values.pullPolicy }}
23         command:
24         - bash
25         - "-c"
26         - |
27           set -ex
28           # Generate mysql server-id from pod ordinal index.
29           [[ `hostname` =~ -([0-9]+)$ ]] || exit 1
30           ordinal=${BASH_REMATCH[1]}
31           echo BASH_REMATCH=${BASH_REMATCH}
32           echo [mysqld] > /mnt/conf.d/server-id.cnf
33           # Add an offset to avoid reserved server-id=0 value.
34           echo server-id=$((100 + $ordinal)) >> /mnt/conf.d/server-id.cnf
35           # Copy appropriate conf.d files from config-map to emptyDir.
36           if [[ $ordinal -eq 0 ]]; then
37             cp /mnt/config-map/master.cnf /mnt/conf.d/
38           else
39             cp /mnt/config-map/slave.cnf /mnt/conf.d/
40           fi
41         volumeMounts:
42         - name: conf
43           mountPath: /mnt/conf.d
44         - name: config-map
45           mountPath: /mnt/config-map
46       - name: clone-mysql
47         image: {{ .Values.image.xtrabackup }}
48         env:
49         - name: MYSQL_ROOT_PASSWORD
50           value: openECOMP1.0
51         command:
52         - bash
53         - "-c"
54         - |
55           set -ex
56           # Skip the clone if data already exists.
57           [[ -d /var/lib/mysql/mysql ]] && exit 0
58           # Skip the clone on master (ordinal index 0).
59           [[ `hostname` =~ -([0-9]+)$ ]] || exit 1
60           ordinal=${BASH_REMATCH[1]}
61           echo ${BASH_REMATCH}
62           [[ $ordinal -eq 0 ]] && exit 0
63           # Clone data from previous peer.
64           ncat --recv-only sdnc-dbhost-$(($ordinal-1)).dbhost.{{ .Values.nsPrefix }}-sdnc 3307 | xbstream -x -C /var/lib/mysql
65           # Prepare the backup.
66           xtrabackup --user=root --password=$MYSQL_ROOT_PASSWORD --prepare --target-dir=/var/lib/mysql
67           ls -l /var/lib/mysql
68         volumeMounts:
69         - name: sdnc-data
70           mountPath: /var/lib/mysql
71           subPath: mysql
72         - name: conf
73           mountPath: /etc/mysql/conf.d
74       containers:
75       - env:
76         - name: MYSQL_ROOT_PASSWORD
77           value: openECOMP1.0
78         - name: MYSQL_ROOT_HOST
79           value: '%'
80         - name: MYSQL_ALLOW_EMPTY_PASSWORD
81           value: "0"
82         image: {{ .Values.image.mysql }}
83         imagePullPolicy: {{ .Values.pullPolicy }}
84         name: sdnc-db-container
85         volumeMounts:
86         - mountPath: /var/lib/mysql
87           name: sdnc-data
88           subPath: mysql
89         - name: conf
90           mountPath: /etc/mysql/conf.d
91         ports:
92         - containerPort: 3306
93         resources:
94           requests:
95             cpu: 500m
96             memory: 1Gi
97         livenessProbe:
98           exec:
99             command: ["mysqladmin", "ping"]
100           initialDelaySeconds: 30
101           periodSeconds: 10
102           timeoutSeconds: 5
103         readinessProbe:
104           tcpSocket:
105             port: 3306
106           initialDelaySeconds: 5
107           periodSeconds: 10
108       - name: xtrabackup
109         image: {{ .Values.image.xtrabackup }}
110         env:
111         - name: MYSQL_ROOT_PASSWORD
112           value: openECOMP1.0
113         ports:
114         - name: xtrabackup
115           containerPort: 3307
116         command:
117         - bash
118         - "-c"
119         - |
120           set -ex
121           cd /var/lib/mysql
122           ls -l
123           # Determine binlog position of cloned data, if any.
124           if [[ -f xtrabackup_slave_info ]]; then
125             echo "Inside xtrabackup_slave_info"
126             # XtraBackup already generated a partial "CHANGE MASTER TO" query
127             # because we're cloning from an existing slave.
128             mv xtrabackup_slave_info change_master_to.sql.in
129             # Ignore xtrabackup_binlog_info in this case (it's useless).
130             rm -f xtrabackup_binlog_info
131           elif [[ -f xtrabackup_binlog_info ]]; then
132             echo "Inside xtrabackup_binlog_info"
133             # We're cloning directly from master. Parse binlog position.
134             [[ `cat xtrabackup_binlog_info` =~ ^(.*?)[[:space:]]+(.*?)$ ]] || exit 1
135             rm xtrabackup_binlog_info
136             echo "CHANGE MASTER TO MASTER_LOG_FILE='${BASH_REMATCH[1]}',\
137                   MASTER_LOG_POS=${BASH_REMATCH[2]}" > change_master_to.sql.in
138           fi
139
140           # Check if we need to complete a clone by starting replication.
141           if [[ -f change_master_to.sql.in ]]; then
142             echo "Waiting for mysqld to be ready (accepting connections)"
143             [[ `hostname` =~ -([0-9]+)$ ]] || exit 1
144             ordinal=${BASH_REMATCH[1]}
145             echo $ordinal
146             until mysql --user=root --password=$MYSQL_ROOT_PASSWORD -h 127.0.0.1 -e "SELECT 1"; do sleep 1; done
147
148             echo "Initializing replication from clone position"
149             # In case of container restart, attempt this at-most-once.
150             mv change_master_to.sql.in change_master_to.sql.orig
151             mysql --user=root --password=$MYSQL_ROOT_PASSWORD -h 127.0.0.1 <<EOF
152           $(<change_master_to.sql.orig),
153             MASTER_HOST="sdnc-dbhost-0.dbhost.{{ .Values.nsPrefix }}-sdnc",
154             MASTER_USER="root",
155             MASTER_PASSWORD="$MYSQL_ROOT_PASSWORD",
156             MASTER_CONNECT_RETRY=10;
157           START SLAVE;
158           EOF
159           fi
160
161           # Start a server to send backups when requested by peers.
162           exec ncat --listen --keep-open --send-only --max-conns=1 3307 -c \
163             "xtrabackup --user=root --password=$MYSQL_ROOT_PASSWORD --backup --slave-info --stream=xbstream --host=127.0.0.1"
164         volumeMounts:
165         - name: sdnc-data
166           mountPath: /var/lib/mysql
167           subPath: mysql
168         - name: conf
169           mountPath: /etc/mysql/conf.d
170         resources:
171           requests:
172             cpu: 100m
173             memory: 100Mi
174       volumes:
175       - name: conf
176         emptyDir: {}
177       - name: config-map
178         configMap:
179           name: mysql
180       - name: localtime
181         hostPath:
182           path: /etc/localtime
183   volumeClaimTemplates:
184   - metadata:
185       name: sdnc-data
186       annotations:
187         volume.beta.kubernetes.io/storage-class: "{{ .Values.nsPrefix }}-sdnc-data"
188     spec:
189       accessModes: ["ReadWriteMany"]
190       resources:
191         requests:
192           storage: 1Gi
193 #{{ end }}