mysql-master-v4.yaml 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. ---
  2. # 1. ConfigMaps
  3. apiVersion: v1
  4. kind: ConfigMap
  5. metadata:
  6. name: mysql-master-config
  7. data:
  8. my.cnf: |
  9. [mysqld]
  10. server-id=1
  11. log-bin=mysql-bin
  12. binlog-format=ROW
  13. gtid-mode=ON
  14. enforce-gtid-consistency=ON
  15. log-replica-updates=ON
  16. skip-replica-start=ON
  17. default-authentication-plugin=mysql_native_password
  18. bind-address=0.0.0.0
  19. character-set-server=utf8mb4
  20. collation-server=utf8mb4_unicode_ci
  21. max_connections=1000
  22. innodb_buffer_pool_size=256M
  23. ---
  24. # 2. Secrets
  25. apiVersion: v1
  26. kind: Secret
  27. metadata:
  28. name: mysql-secrets
  29. type: Opaque
  30. stringData:
  31. root-password: "Lq123456!"
  32. replica-user: "replica"
  33. replica-password: "Replica123456!"
  34. ---
  35. # 3. Services
  36. apiVersion: v1
  37. kind: Service
  38. metadata:
  39. name: mysql-master
  40. labels:
  41. app: mysql
  42. role: master
  43. spec:
  44. ports:
  45. - port: 3306
  46. name: mysql
  47. selector:
  48. app: mysql
  49. role: master
  50. ---
  51. # 4. Master Deployment (先用Deployment简化调试)
  52. apiVersion: apps/v1
  53. kind: Deployment
  54. metadata:
  55. name: mysql-master
  56. labels:
  57. app: mysql
  58. role: master
  59. spec:
  60. replicas: 1
  61. selector:
  62. matchLabels:
  63. app: mysql
  64. role: master
  65. strategy:
  66. type: Recreate
  67. template:
  68. metadata:
  69. labels:
  70. app: mysql
  71. role: master
  72. spec:
  73. containers:
  74. - name: mysql
  75. image: mysql:8.0.44
  76. env:
  77. - name: MYSQL_ROOT_PASSWORD
  78. value: "Lq123456!"
  79. - name: MYSQL_DATABASE
  80. value: "appdb"
  81. - name: MYSQL_USER
  82. value: "appuser"
  83. - name: MYSQL_PASSWORD
  84. value: "App123456!"
  85. ports:
  86. - containerPort: 3306
  87. volumeMounts:
  88. - name: config
  89. mountPath: /etc/mysql/conf.d/
  90. - name: data
  91. mountPath: /var/lib/mysql
  92. resources:
  93. requests:
  94. memory: "512Mi"
  95. cpu: "250m"
  96. livenessProbe:
  97. tcpSocket:
  98. port: 3306
  99. initialDelaySeconds: 90
  100. periodSeconds: 20
  101. readinessProbe:
  102. exec:
  103. command:
  104. - mysql
  105. - -h127.0.0.1
  106. - -uroot
  107. - -pLq123456!
  108. - -e
  109. - "SELECT 1"
  110. initialDelaySeconds: 60
  111. periodSeconds: 10
  112. lifecycle:
  113. postStart:
  114. exec:
  115. command:
  116. - /bin/bash
  117. - -c
  118. - |
  119. sleep 20
  120. mysql -uroot -pLq123456! <<EOF
  121. CREATE USER IF NOT EXISTS 'replica'@'%' IDENTIFIED WITH mysql_native_password BY 'Replica123456!';
  122. GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO 'replica'@'%';
  123. GRANT SELECT ON *.* TO 'replica'@'%';
  124. FLUSH PRIVILEGES;
  125. SHOW MASTER STATUS\G
  126. EOF
  127. volumes:
  128. - name: config
  129. configMap:
  130. name: mysql-master-config
  131. - name: data
  132. emptyDir: {}