Last active
August 13, 2025 16:31
-
-
Save kayoch1n/6f4e547e412d3a41d04f75f0f097d4fb to your computer and use it in GitHub Desktop.
Deploy MySQL source and replica servers in k8s 在 k8s 集群中部署MySQL主从server
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # applied under the namespace "ms" | |
| apiVersion: apps/v1 | |
| kind: StatefulSet | |
| metadata: | |
| name: mysql | |
| spec: | |
| selector: | |
| matchLabels: | |
| app: mysql | |
| serviceName: mysql | |
| replicas: 3 | |
| template: | |
| metadata: | |
| labels: | |
| app: mysql | |
| spec: | |
| # imagePullSecrets: | |
| # - name: regcred | |
| containers: | |
| - name: mysql | |
| image: mysql:8.0 | |
| # image: ccr.ccs.tencentyun.com/fxck/mysql:8.0 | |
| command: | |
| - bash | |
| - -c | |
| - exec /usr/local/bin/docker-entrypoint.sh mysqld --server-id ${SERVER_ID} | |
| ports: | |
| - containerPort: 3306 | |
| name: mysql | |
| env: | |
| - name: MYSQL_ROOT_PASSWORD | |
| valueFrom: | |
| secretKeyRef: | |
| name: mysql-credentials | |
| key: mysql-root-password | |
| - name: POD_INDEX | |
| valueFrom: | |
| fieldRef: | |
| fieldPath: metadata.labels['apps.kubernetes.io/pod-index'] | |
| volumeMounts: | |
| - name: mysql-conf | |
| mountPath: /etc/my.cnf | |
| subPath: my.cnf | |
| volumes: | |
| - name: mysql-conf | |
| configMap: | |
| name: mysql-conf-v1 | |
| items: | |
| - key: my.cnf | |
| path: my.cnf | |
| --- | |
| apiVersion: v1 | |
| kind: Service | |
| metadata: | |
| name: mysql | |
| spec: | |
| selector: | |
| app: mysql | |
| clusterIP: None | |
| type: ClusterIP | |
| --- | |
| apiVersion: v1 | |
| kind: ConfigMap | |
| metadata: | |
| name: mysql-conf-v1 | |
| data: | |
| my.cnf: | | |
| [mysqld] | |
| log_bin = mysql-bin # 打开binlog | |
| skip-host-cache | |
| # skip-name-resolve # 注释掉这个之后才能用域名作为用户名连接DB | |
| datadir=/var/lib/mysql | |
| socket=/var/run/mysqld/mysqld.sock | |
| secure-file-priv=/var/lib/mysql-files | |
| user=mysql | |
| pid-file=/var/run/mysqld/mysqld.pid | |
| [client] | |
| socket=/var/run/mysqld/mysqld.sock | |
| !includedir /etc/mysql/conf.d/ | |
| --- | |
| apiVersion: v1 | |
| kind: Secret | |
| metadata: | |
| name: mysql-credentials | |
| type: Opaque | |
| data: | |
| mysql-root-password: WTB1clA0c3N3MHJk |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| -- 在从服务器,也就是replica上执行以下语句。 | |
| -- 需要根据实际的k8s namespace填写下面的域名 | |
| change replication source to | |
| source_user='repl', | |
| source_password='Y0urP4ssw0rd', | |
| source_host='mysql-0.mysql.ms.svc.cluster.local'; | |
| start replica; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| -- 在主服务器,也就是source上执行以下语句 | |
| -- 需要根据实际的k8s namespace填写下面的域名 | |
| create user 'repl'@'%.mysql.ms.svc.cluster.local' identified by 'Y0urP4ssw0rd'; | |
| grant replication slave on *.* to 'repl'@'%.mysql.ms.svc.cluster.local'; | |
| flush privileges; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment