arshad75

mysql-statefulset.yaml

Jul 4th, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.07 KB | None | 0 0
  1. apiVersion: apps/v1
  2. kind: StatefulSet
  3. metadata:
  4. name: mysql
  5. spec:
  6. selector:
  7. matchLabels:
  8. app: mysql
  9. serviceName: mysql
  10. replicas: 2
  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. # Generate mysql server-id from pod ordinal index.
  25. [[ `hostname` =~ -([0-9]+)$ ]] || exit 1
  26. ordinal=${BASH_REMATCH[1]}
  27. echo [mysqld] > /mnt/conf.d/server-id.cnf
  28. # Add an offset to avoid reserved server-id=0 value.
  29. echo server-id=$((100 + $ordinal)) >> /mnt/conf.d/server-id.cnf
  30. # Copy appropriate conf.d files from config-map to emptyDir.
  31. if [[ $ordinal -eq 0 ]]; then
  32. cp /mnt/config-map/master.cnf /mnt/conf.d/
  33. else
  34. cp /mnt/config-map/slave.cnf /mnt/conf.d/
  35. fi
  36. volumeMounts:
  37. - name: conf
  38. mountPath: /mnt/conf.d
  39. - name: config-map
  40. mountPath: /mnt/config-map
  41. - name: clone-mysql
  42. image: gcr.io/google-samples/xtrabackup:1.0
  43. command:
  44. - bash
  45. - "-c"
  46. - |
  47. set -ex
  48. # Skip the clone if data already exists.
  49. [[ -d /var/lib/mysql/mysql ]] && exit 0
  50. # Skip the clone on master (ordinal index 0).
  51. [[ `hostname` =~ -([0-9]+)$ ]] || exit 1
  52. ordinal=${BASH_REMATCH[1]}
  53. [[ $ordinal -eq 0 ]] && exit 0
  54. # Clone data from previous peer.
  55. ncat --recv-only mysql-$(($ordinal-1)).mysql 3307 | xbstream -x -C /var/lib/mysql
  56. # Prepare the backup.
  57. xtrabackup --prepare --target-dir=/var/lib/mysql
  58. volumeMounts:
  59. - name: data
  60. mountPath: /var/lib/mysql
  61. subPath: mysql
  62. - name: conf
  63. mountPath: /etc/mysql/conf.d
  64. containers:
  65. - name: mysql
  66. image: mysql:5.7
  67. env:
  68. - name: MYSQL_ALLOW_EMPTY_PASSWORD
  69. value: "1"
  70. ports:
  71. - name: mysql
  72. containerPort: 3306
  73. volumeMounts:
  74. - name: data
  75. mountPath: /var/lib/mysql
  76. subPath: mysql
  77. - name: conf
  78. mountPath: /etc/mysql/conf.d
  79. - name: xtrabackup
  80. image: gcr.io/google-samples/xtrabackup:1.0
  81. ports:
  82. - name: xtrabackup
  83. containerPort: 3307
  84. command:
  85. - bash
  86. - "-c"
  87. - |
  88. set -ex
  89. cd /var/lib/mysql
  90.  
  91. # Determine binlog position of cloned data, if any.
  92. if [[ -f xtrabackup_slave_info && "x$(<xtrabackup_slave_info)" != "x" ]]; then
  93. # XtraBackup already generated a partial "CHANGE MASTER TO" query
  94. # because we're cloning from an existing slave. (Need to remove the tailing semicolon!)
  95. cat xtrabackup_slave_info | sed -E 's/;$//g' > change_master_to.sql.in
  96. # Ignore xtrabackup_binlog_info in this case (it's useless).
  97. rm -f xtrabackup_slave_info xtrabackup_binlog_info
  98. elif [[ -f xtrabackup_binlog_info ]]; then
  99. # We're cloning directly from master. Parse binlog position.
  100. [[ `cat xtrabackup_binlog_info` =~ ^(.*?)[[:space:]]+(.*?)$ ]] || exit 1
  101. rm -f xtrabackup_binlog_info xtrabackup_slave_info
  102. echo "CHANGE MASTER TO MASTER_LOG_FILE='${BASH_REMATCH[1]}',\
  103. MASTER_LOG_POS=${BASH_REMATCH[2]}" > change_master_to.sql.in
  104. fi
  105.  
  106. # Check if we need to complete a clone by starting replication.
  107. if [[ -f change_master_to.sql.in ]]; then
  108. echo "Waiting for mysqld to be ready (accepting connections)"
  109. until mysql -h 127.0.0.1 -e "SELECT 1"; do sleep 1; done
  110.  
  111. echo "Initializing replication from clone position"
  112. mysql -h 127.0.0.1 \
  113. -e "$(<change_master_to.sql.in), \
  114. MASTER_HOST='mysql-0.mysql', \
  115. MASTER_USER='root', \
  116. MASTER_PASSWORD='', \
  117. MASTER_CONNECT_RETRY=10; \
  118. START SLAVE;" || exit 1
  119. # In case of container restart, attempt this at-most-once.
  120. mv change_master_to.sql.in change_master_to.sql.orig
  121. fi
  122.  
  123. # Start a server to send backups when requested by peers.
  124. exec ncat --listen --keep-open --send-only --max-conns=1 3307 -c \
  125. "xtrabackup --backup --slave-info --stream=xbstream --host=127.0.0.1 --user=root"
  126. volumeMounts:
  127. - name: data
  128. mountPath: /var/lib/mysql
  129. subPath: mysql
  130. - name: conf
  131. mountPath: /etc/mysql/conf.d
  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. resources:
  144. requests:
  145. storage: 5Gi
Add Comment
Please, Sign In to add comment