mysql-master-pvc-v2.yaml 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. ---
  2. # 1. ConfigMaps
  3. apiVersion: v1
  4. kind: ConfigMap
  5. metadata:
  6. name: mysql-master-config
  7. namespace: mysql
  8. data:
  9. my.cnf: |
  10. [mysqld]
  11. server-id=1
  12. log-bin=mysql-bin
  13. binlog-format=ROW
  14. gtid-mode=ON
  15. enforce-gtid-consistency=ON
  16. log-replica-updates=ON
  17. skip-replica-start=ON
  18. default-authentication-plugin=mysql_native_password
  19. bind-address=0.0.0.0
  20. character-set-server=utf8mb4
  21. collation-server=utf8mb4_unicode_ci
  22. max_connections=1000
  23. innodb_buffer_pool_size=256M
  24. ---
  25. # 2. Secrets
  26. apiVersion: v1
  27. kind: Secret
  28. metadata:
  29. name: mysql-secrets
  30. namespace: mysql
  31. type: Opaque
  32. stringData:
  33. root-password: "Lq123456!"
  34. replica-user: "replica"
  35. replica-password: "Replica123456!"
  36. ---
  37. # 3. PersistentVolume
  38. apiVersion: v1
  39. kind: PersistentVolume
  40. metadata:
  41. name: mysql-master-pv
  42. namespace: mysql
  43. labels:
  44. type: local
  45. app: mysql
  46. role: master
  47. spec:
  48. storageClassName: manual
  49. capacity:
  50. storage: 10Gi
  51. accessModes:
  52. - ReadWriteOnce
  53. hostPath:
  54. path: "/home/lq/mysql/master/data" # 本地目录路径
  55. type: DirectoryOrCreate
  56. ---
  57. # 4. PersistentVolumeClaim
  58. apiVersion: v1
  59. kind: PersistentVolumeClaim
  60. metadata:
  61. name: mysql-master-pvc
  62. namespace: mysql
  63. labels:
  64. app: mysql
  65. role: master
  66. spec:
  67. storageClassName: manual
  68. accessModes:
  69. - ReadWriteOnce
  70. resources:
  71. requests:
  72. storage: 10Gi
  73. selector:
  74. matchLabels:
  75. app: mysql
  76. role: master
  77. ---
  78. # 5. Services
  79. apiVersion: v1
  80. kind: Service
  81. metadata:
  82. name: mysql-master
  83. namespace: mysql
  84. labels:
  85. app: mysql
  86. role: master
  87. spec:
  88. ports:
  89. - port: 3306
  90. name: mysql
  91. selector:
  92. app: mysql
  93. role: master
  94. ---
  95. # 6. Master Deployment (先用Deployment简化调试)
  96. apiVersion: apps/v1
  97. kind: Deployment
  98. metadata:
  99. name: mysql-master
  100. namespace: mysql
  101. labels:
  102. app: mysql
  103. role: master
  104. spec:
  105. replicas: 1
  106. selector:
  107. matchLabels:
  108. app: mysql
  109. role: master
  110. strategy:
  111. type: Recreate
  112. template:
  113. metadata:
  114. labels:
  115. app: mysql
  116. role: master
  117. spec:
  118. containers:
  119. - name: mysql
  120. image: mysql:8.0.44
  121. env:
  122. - name: MYSQL_ROOT_PASSWORD
  123. value: "Lq123456!"
  124. - name: MYSQL_DATABASE
  125. value: "appdb"
  126. - name: MYSQL_USER
  127. value: "appuser"
  128. - name: MYSQL_PASSWORD
  129. value: "App123456!"
  130. ports:
  131. - containerPort: 3306
  132. volumeMounts:
  133. - name: config
  134. mountPath: /etc/mysql/conf.d/
  135. - name: data
  136. mountPath: /var/lib/mysql
  137. resources:
  138. requests:
  139. memory: "512Mi"
  140. cpu: "250m"
  141. livenessProbe:
  142. tcpSocket:
  143. port: 3306
  144. initialDelaySeconds: 90
  145. periodSeconds: 20
  146. readinessProbe:
  147. exec:
  148. command:
  149. - mysql
  150. - -h127.0.0.1
  151. - -uroot
  152. - -pLq123456!
  153. - -e
  154. - "SELECT 1"
  155. initialDelaySeconds: 60
  156. periodSeconds: 10
  157. lifecycle:
  158. postStart:
  159. exec:
  160. command:
  161. - /bin/bash
  162. - -c
  163. - |
  164. sleep 20
  165. mysql -uroot -pLq123456! <<EOF
  166. CREATE USER IF NOT EXISTS 'replica'@'%' IDENTIFIED WITH mysql_native_password BY 'Replica123456!';
  167. GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO 'replica'@'%';
  168. GRANT SELECT ON *.* TO 'replica'@'%';
  169. FLUSH PRIVILEGES;
  170. SHOW MASTER STATUS\G
  171. EOF
  172. volumes:
  173. - name: config
  174. configMap:
  175. name: mysql-master-config
  176. - name: data
  177. persistentVolumeClaim:
  178. claimName: mysql-master-pvc # 使用PVC