statefulset.yaml 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. apiVersion: apps/v1
  2. kind: StatefulSet
  3. metadata:
  4. name: mysql
  5. spec:
  6. serviceName: mysql
  7. replicas: 3
  8. selector:
  9. matchLabels:
  10. app: mysql
  11. template:
  12. metadata:
  13. labels:
  14. app: mysql
  15. spec:
  16. initContainers:
  17. - name: init-mysql
  18. image: mysql:5.7
  19. command:
  20. - bash
  21. - "-c"
  22. - |
  23. set -ex
  24. [[ `hostname` =~ -([0-9]+)$ ]] || exit 1
  25. ordinal=${BASH_REMATCH[1]}
  26. echo [mysqld] > /mnt/conf.d/server-id.cnf
  27. echo server-id=$((100 + $ordinal)) >> /mnt/conf.d/server-id.cnf
  28. if [[ $ordinal -eq 0 ]]; then
  29. cp /mnt/config-map/master.cnf /mnt/conf.d/
  30. else
  31. cp /mnt/config-map/slave.cnf /mnt/conf.d/
  32. fi
  33. volumeMounts:
  34. - name: conf
  35. mountPath: /mnt/conf.d
  36. - name: config-map
  37. mountPath: /mnt/config-map
  38. - name: clone-mysql
  39. image: gcr.io/google-samples/xtrabackup:1.0
  40. command:
  41. - bash
  42. - "-c"
  43. - |
  44. set -ex
  45. [[ -d /var/lib/mysql/mysql ]] && exit 0
  46. [[ `hostname` =~ -([0-9]+)$ ]] || exit 1
  47. ordinal=${BASH_REMATCH[1]}
  48. [[ $ordinal -eq 0 ]] && exit 0
  49. ncat --recv-only mysql-$(($ordinal-1)).mysql 3307 | xbstream -x -C /var/lib/mysql
  50. xtrabackup --prepare --target-dir=/var/lib/mysql
  51. volumeMounts:
  52. - name: data
  53. mountPath: /var/lib/mysql
  54. subPath: mysql
  55. - name: conf
  56. mountPath: /etc/mysql/conf.d
  57. containers:
  58. - name: mysql
  59. image: mysql:5.7.15
  60. env:
  61. - name: MYSQL_ALLOW_EMPTY_PASSWORD
  62. value: "1"
  63. ports:
  64. - name: mysql
  65. containerPort: 3306
  66. volumeMounts:
  67. - name: data
  68. mountPath: /var/lib/mysql
  69. subPath: mysql
  70. - name: conf
  71. mountPath: /etc/mysql/conf.d
  72. resources:
  73. requests:
  74. cpu: 1
  75. memory: 1Gi
  76. livenessProbe:
  77. exec:
  78. command: ["mysqladmin", "ping"]
  79. initialDelaySeconds: 30
  80. timeoutSeconds: 5
  81. readinessProbe:
  82. exec:
  83. command: ["mysql", "-h", "127.0.0.1", "-e", "SELECT 1"]
  84. initialDelaySeconds: 5
  85. timeoutSeconds: 1
  86. - name: xtrabackup
  87. image: gcr.io/google-samples/xtrabackup:1.0
  88. ports:
  89. - name: xtrabackup
  90. containerPort: 3307
  91. command:
  92. - bash
  93. - "-c"
  94. - |
  95. set -ex
  96. cd /var/lib/mysql
  97. if [[ -f xtrabackup_slave_info ]]; then
  98. mv xtrabackup_slave_info change_master_to.sql.in
  99. rm -f xtrabackup_binlog_info
  100. elif [[ -f xtrabackup_binlog_info ]]; then
  101. [[ `cat xtrabackup_binlog_info` =~ ^(.*?)[[:space:]]+(.*?)$ ]] || exit 1
  102. rm xtrabackup_binlog_info
  103. echo "CHANGE MASTER TO MASTER_LOG_FILE='${BASH_REMATCH[1]}',\
  104. MASTER_LOG_POS=${BASH_REMATCH[2]}" > change_master_to.sql.in
  105. fi
  106. if [[ -f change_master_to.sql.in ]]; then
  107. echo "Waiting for mysqld to be ready (accepting connections)"
  108. until mysql -h 127.0.0.1 -e "SELECT 1"; do sleep 1; done
  109. echo "Initializing replication from clone position"
  110. mv change_master_to.sql.in change_master_to.sql.orig
  111. mysql -h 127.0.0.1 <<EOF
  112. $(<change_master_to.sql.orig),
  113. MASTER_HOST='mysql-0.mysql',
  114. MASTER_USER='root',
  115. MASTER_PASSWORD='',
  116. MASTER_CONNECT_RETRY=10;
  117. START SLAVE;
  118. EOF
  119. fi
  120. exec ncat --listen --keep-open --send-only --max-conns=1 3307 -c \
  121. "xtrabackup --backup --slave-info --stream=xbstream --host=127.0.0.1 --user=root"
  122. volumeMounts:
  123. - name: data
  124. mountPath: /var/lib/mysql
  125. subPath: mysql
  126. - name: conf
  127. mountPath: /etc/mysql/conf.d
  128. resources:
  129. requests:
  130. cpu: 100m
  131. memory: 100Mi
  132. volumes:
  133. - name: conf
  134. emptyDir: {}
  135. - name: config-map
  136. configMap:
  137. name: mysql
  138. volumeClaimTemplates:
  139. - metadata:
  140. name: data
  141. spec:
  142. accessModes: ["ReadWriteOnce"]
  143. storageClassName: default
  144. resources:
  145. requests:
  146. storage: 10Gi
  147. ---
  148. apiVersion: policy/v1beta1
  149. kind: PodDisruptionBudget
  150. metadata:
  151. name: mysql-pdb
  152. labels:
  153. pdb: mysql
  154. spec:
  155. minAvailable: 2
  156. selector:
  157. matchLabels:
  158. app: mysql