vaibhav_16dec | e04b2fe | 2018-03-22 09:07:12 +0000 | [diff] [blame] | 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 | |
Hao Kuang | 27af4a3 | 2017-12-15 20:38:03 +0000 | [diff] [blame] | 15 | #{{ if not .Values.disableSdncSdncDbhost }} |
| 16 | apiVersion: apps/v1beta1 |
| 17 | kind: StatefulSet |
| 18 | metadata: |
| 19 | name: sdnc-dbhost |
BorislavG | 8bfc6cf | 2018-02-27 15:04:26 +0000 | [diff] [blame] | 20 | namespace: "{{ .Values.nsPrefix }}" |
Hao Kuang | 27af4a3 | 2017-12-15 20:38:03 +0000 | [diff] [blame] | 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. |
BorislavG | 8bfc6cf | 2018-02-27 15:04:26 +0000 | [diff] [blame] | 78 | ncat --recv-only sdnc-dbhost-$(($ordinal-1)).dbhost.{{ .Values.nsPrefix }} 3307 | xbstream -x -C /var/lib/mysql |
Hao Kuang | 27af4a3 | 2017-12-15 20:38:03 +0000 | [diff] [blame] | 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 | subPath: mysql |
| 86 | - name: conf |
| 87 | mountPath: /etc/mysql/conf.d |
| 88 | containers: |
| 89 | - env: |
| 90 | - name: MYSQL_ROOT_PASSWORD |
| 91 | value: openECOMP1.0 |
| 92 | - name: MYSQL_ROOT_HOST |
| 93 | value: '%' |
| 94 | - name: MYSQL_ALLOW_EMPTY_PASSWORD |
| 95 | value: "0" |
| 96 | image: {{ .Values.image.mysql }} |
| 97 | imagePullPolicy: {{ .Values.pullPolicy }} |
| 98 | name: sdnc-db-container |
| 99 | volumeMounts: |
| 100 | - mountPath: /var/lib/mysql |
| 101 | name: sdnc-data |
| 102 | subPath: mysql |
| 103 | - name: conf |
| 104 | mountPath: /etc/mysql/conf.d |
| 105 | ports: |
| 106 | - containerPort: 3306 |
| 107 | resources: |
| 108 | requests: |
| 109 | cpu: 500m |
| 110 | memory: 1Gi |
| 111 | livenessProbe: |
| 112 | exec: |
| 113 | command: ["mysqladmin", "ping"] |
| 114 | initialDelaySeconds: 30 |
| 115 | periodSeconds: 10 |
| 116 | timeoutSeconds: 5 |
| 117 | readinessProbe: |
| 118 | tcpSocket: |
| 119 | port: 3306 |
| 120 | initialDelaySeconds: 5 |
| 121 | periodSeconds: 10 |
| 122 | - name: xtrabackup |
| 123 | image: {{ .Values.image.xtrabackup }} |
| 124 | env: |
| 125 | - name: MYSQL_ROOT_PASSWORD |
| 126 | value: openECOMP1.0 |
| 127 | ports: |
| 128 | - name: xtrabackup |
| 129 | containerPort: 3307 |
| 130 | command: |
| 131 | - bash |
| 132 | - "-c" |
| 133 | - | |
| 134 | set -ex |
| 135 | cd /var/lib/mysql |
| 136 | ls -l |
| 137 | # Determine binlog position of cloned data, if any. |
| 138 | if [[ -f xtrabackup_slave_info ]]; then |
| 139 | echo "Inside xtrabackup_slave_info" |
| 140 | # XtraBackup already generated a partial "CHANGE MASTER TO" query |
| 141 | # because we're cloning from an existing slave. |
| 142 | mv xtrabackup_slave_info change_master_to.sql.in |
| 143 | # Ignore xtrabackup_binlog_info in this case (it's useless). |
| 144 | rm -f xtrabackup_binlog_info |
| 145 | elif [[ -f xtrabackup_binlog_info ]]; then |
| 146 | echo "Inside xtrabackup_binlog_info" |
| 147 | # We're cloning directly from master. Parse binlog position. |
| 148 | [[ `cat xtrabackup_binlog_info` =~ ^(.*?)[[:space:]]+(.*?)$ ]] || exit 1 |
| 149 | rm xtrabackup_binlog_info |
| 150 | echo "CHANGE MASTER TO MASTER_LOG_FILE='${BASH_REMATCH[1]}',\ |
| 151 | MASTER_LOG_POS=${BASH_REMATCH[2]}" > change_master_to.sql.in |
| 152 | fi |
| 153 | |
| 154 | # Check if we need to complete a clone by starting replication. |
| 155 | if [[ -f change_master_to.sql.in ]]; then |
| 156 | echo "Waiting for mysqld to be ready (accepting connections)" |
| 157 | [[ `hostname` =~ -([0-9]+)$ ]] || exit 1 |
| 158 | ordinal=${BASH_REMATCH[1]} |
| 159 | echo $ordinal |
| 160 | until mysql --user=root --password=$MYSQL_ROOT_PASSWORD -h 127.0.0.1 -e "SELECT 1"; do sleep 1; done |
| 161 | |
| 162 | echo "Initializing replication from clone position" |
| 163 | # In case of container restart, attempt this at-most-once. |
| 164 | mv change_master_to.sql.in change_master_to.sql.orig |
| 165 | mysql --user=root --password=$MYSQL_ROOT_PASSWORD -h 127.0.0.1 <<EOF |
| 166 | $(<change_master_to.sql.orig), |
BorislavG | 8bfc6cf | 2018-02-27 15:04:26 +0000 | [diff] [blame] | 167 | MASTER_HOST="sdnc-dbhost-0.dbhost.{{ .Values.nsPrefix }}", |
Hao Kuang | 27af4a3 | 2017-12-15 20:38:03 +0000 | [diff] [blame] | 168 | MASTER_USER="root", |
| 169 | MASTER_PASSWORD="$MYSQL_ROOT_PASSWORD", |
| 170 | MASTER_CONNECT_RETRY=10; |
| 171 | START SLAVE; |
| 172 | EOF |
| 173 | fi |
| 174 | |
| 175 | # Start a server to send backups when requested by peers. |
| 176 | exec ncat --listen --keep-open --send-only --max-conns=1 3307 -c \ |
| 177 | "xtrabackup --user=root --password=$MYSQL_ROOT_PASSWORD --backup --slave-info --stream=xbstream --host=127.0.0.1" |
| 178 | volumeMounts: |
| 179 | - name: sdnc-data |
| 180 | mountPath: /var/lib/mysql |
| 181 | subPath: mysql |
| 182 | - name: conf |
| 183 | mountPath: /etc/mysql/conf.d |
| 184 | resources: |
| 185 | requests: |
| 186 | cpu: 100m |
| 187 | memory: 100Mi |
| 188 | volumes: |
| 189 | - name: conf |
| 190 | emptyDir: {} |
| 191 | - name: config-map |
| 192 | configMap: |
BorislavG | 8bfc6cf | 2018-02-27 15:04:26 +0000 | [diff] [blame] | 193 | name: sdnc-mysql |
Hao Kuang | 27af4a3 | 2017-12-15 20:38:03 +0000 | [diff] [blame] | 194 | - name: localtime |
| 195 | hostPath: |
| 196 | path: /etc/localtime |
BorislavG | 8bfc6cf | 2018-02-27 15:04:26 +0000 | [diff] [blame] | 197 | #{{ if .Values.disableNfsProvisioner }} |
| 198 | - name: sdnc-data |
| 199 | hostPath: |
| 200 | path: /dockerdata-nfs/{{ .Values.nsPrefix }}/sdnc/data |
| 201 | #{{ else }} |
Hao Kuang | 27af4a3 | 2017-12-15 20:38:03 +0000 | [diff] [blame] | 202 | volumeClaimTemplates: |
| 203 | - metadata: |
| 204 | name: sdnc-data |
| 205 | annotations: |
| 206 | volume.beta.kubernetes.io/storage-class: "{{ .Values.nsPrefix }}-sdnc-data" |
| 207 | spec: |
| 208 | accessModes: ["ReadWriteMany"] |
| 209 | resources: |
| 210 | requests: |
| 211 | storage: 1Gi |
| 212 | #{{ end }} |
BorislavG | 8bfc6cf | 2018-02-27 15:04:26 +0000 | [diff] [blame] | 213 | #{{ end }} |