# Centos8下 K8S 安装配置Mysql8 多主MGR

> 作者/来源: UCloud 运营管理员
> 发布时间: 2023-01-11T05:20:00.000Z
> 分类: 其他
> 标签: AI, Docker
> 原文链接: http://117.50.162.249:3000/yun/articles/888

---

# Centos8下 K8S 安装配置Mysql8 多主MGR

> 来源: https://www.ucloud.cn/yun/130047.html
> 作者: IT那活儿
> 发布日期: 发布于2023-01-11 13:20

Centos8下 K8S 安装配置Mysql8 多主MGR

![](https://ucloud-blog.cn-bj.ufileos.com/articles/130047/images/130047_000.png)

一.环境配置

k8s环境：1台master节点，3台node节点。

|  |
| --- |
| 192.168.37.128 k8s1  192.168.37.130 k8s2  192.168.37.131 k8s3  192.168.37.129 k8s4  ![](https://ucloud-blog.cn-bj.ufileos.com/articles/130047/images/130047_001.png) |

二.搭建步骤

![](https://ucloud-blog.cn-bj.ufileos.com/articles/130047/images/130047_011.png)

1、准备MySQL镜像

|  |
| --- |
| 拉取MySQL最新的镜像 docker pull mysql |

![](https://ucloud-blog.cn-bj.ufileos.com/articles/130047/images/130047_011.png)

2、准备相关yaml文件

**准备namespace文件**

|  |
| --- |
| cat namespace.yaml  apiVersion: v1 kind: Namespace metadata:   name: mysqldb |

**准备service文件**

|  |
| --- |
| cat service.yaml  apiVersion: v1 kind: Service metadata:  name: mgrtest   namespace: mysqldb spec:  clusterIP: None   selector:    name: mysql-mgr    ports:   - name: foo     port: 3306     targetPort: 3306 |

**MySQL参数配置文件configmap(节点1)**

|  |
| --- |
| 节点1： cat mysql-mgr-cnf-0.yaml  apiVersion: v1 kind: ConfigMap metadata:   name: mysql-mgr-0-cnf  namespace: mysqldb data:   mysql-mgr-0.cnf: |    [mysqld]     port = 3306     character\_set\_server = utf8     server\_id = 1     log\_bin = mysql-bin    relay-log = relay-bin     #back\_log = 500    #max\_connections = 3000     #wait\_timeout = 5022397    interactive\_timeout = 5022397     max\_connect\_errors = 1000    relay-log-recovery=1     #max\_allowed\_packet = 32M    sort\_buffer\_size = 4M     read\_buffer\_size = 4M    join\_buffer\_size = 8M     thread\_cache\_size = 64    #tmp\_table\_size = 256M     log\_slave\_updates=1    long\_query\_time = 1     slow\_query\_log = 1    skip-name-resolve     innodb\_buffer\_pool\_size=700M    #innodb\_data\_file\_path = ibdata1:1024M:autoextend    innodb\_flush\_log\_at\_trx\_commit=1     innodb\_log\_buffer\_size = 16M     innodb\_log\_file\_size = 256M    innodb\_log\_files\_in\_group = 2     innodb\_max\_dirty\_pages\_pct = 50     sync\_binlog=1     master\_info\_repository=TABLE    relay\_log\_info\_repository=TABLE     log\_timestamps=SYSTEM    gtid\_mode = ON     enforce\_gtid\_consistency = ON    master\_info\_repository = TABLE     relay\_log\_info\_repository = TABLE     log\_slave\_updates = ON     binlog\_checksum = NONE     log\_slave\_updates = ON    slave\_parallel\_type=LOGICAL\_CLOCK    slave\_parallel\_workers=8     slave-preserve-commit-order=on    #group\_replication\_compression\_threshold=200000    plugin\_load\_add=group\_replication.so    transaction\_write\_set\_extraction = XXHASH64    loose-group\_replication\_group\_name="01e5fb97-be64-41f7-bafd-3afc7a6ab555"    loose-group\_replication\_start\_on\_boot=off    loose-group\_replication\_local\_address="k8s2.mgrtest.mysqldb.svc.cluster.local:13306"    loose-group\_replication\_group\_seeds="k8s2.mgrtest.mysqldb.svc.cluster.local:13306,k8s3.mgrtest.mysqldb.svc.cluster.local:13306,k8s4.mgrtest.mysqldb.svc.cluster.local:13306"    loose-group\_replication\_bootstrap\_group = off    loose-group\_replication\_ip\_whitelist=10.96.0.0/16,172.16.0.0/16,192.168.0.0/16,172.17.0.0/16,127.0.0.1/8    report\_host = k8s2.mgrtest.mysqldb.svc.cluster.local    [mysqldump]     quick     max\_allowed\_packet = 32M |

**MySQL参数配置文件configmap(节点2)**

|  |
| --- |
| cat mysql-mgr-cnf-1.yaml   apiVersion: v1 kind: ConfigMap metadata:   name: mysql-mgr-1-cnf  namespace: mysqldb data:   mysql-mgr-1.cnf: |    [mysqld]     port = 3306     character\_set\_server = utf8     server\_id = 2     log\_bin = mysql-bin    relay-log = relay-bin     #back\_log = 500    #max\_connections = 3000     #wait\_timeout = 5022397    interactive\_timeout = 5022397     max\_connect\_errors = 1000    relay-log-recovery=1     #max\_allowed\_packet = 32M    sort\_buffer\_size = 4M     read\_buffer\_size = 4M    join\_buffer\_size = 8M     thread\_cache\_size = 64    #tmp\_table\_size = 256M     log\_slave\_updates=1    long\_query\_time = 1     slow\_query\_log = 1    skip-name-resolve     innodb\_buffer\_pool\_size=700M    #innodb\_data\_file\_path = ibdata1:1024M:autoextend    innodb\_flush\_log\_at\_trx\_commit=1     innodb\_log\_buffer\_size = 16M     innodb\_log\_file\_size = 256M    innodb\_log\_files\_in\_group = 2     innodb\_max\_dirty\_pages\_pct = 50     sync\_binlog=1     master\_info\_repository=TABLE    relay\_log\_info\_repository=TABLE     log\_timestamps=SYSTEM    gtid\_mode = ON     enforce\_gtid\_consistency = ON    master\_info\_repository = TABLE     relay\_log\_info\_repository = TABLE     log\_slave\_updates = ON     binlog\_checksum = NONE     log\_slave\_updates = ON    slave\_parallel\_type=LOGICAL\_CLOCK    slave\_parallel\_workers=8     slave-preserve-commit-order=on    #group\_replication\_compression\_threshold=200000    transaction\_write\_set\_extraction = XXHASH64    loose-group\_replication\_group\_name="01e5fb97-be64-41f7-bafd-3afc7a6ab555"    loose-group\_replication\_start\_on\_boot=off    loose-group\_replication\_local\_address="k8s3.mgrtest.mysqldb.svc.cluster.local:13306"    loose-group\_replication\_group\_seeds="k8s2.mgrtest.mysqldb.svc.cluster.local:13306,k8s3.mgrtest.mysqldb.svc.cluster.local:13306,k8s4.mgrtest.mysqldb.svc.cluster.local:13306"    loose-group\_replication\_bootstrap\_group = off    loose-group\_replication\_ip\_whitelist=10.96.0.0/16,172.16.0.0/16,192.168.0.0/16,172.17.0.0/16,127.0.0.1/8    report\_host = k8s3.mgrtest.mysqldb.svc.cluster.local    [mysqldump]     quick     max\_allowed\_packet = 32M |

**MySQL参数配置文件configmap(节点3)**

|  |
| --- |
| cat mysql-mgr-cnf-2.yaml   apiVersion: v1 kind: ConfigMap metadata:   name: mysql-mgr-2-cnf  namespace: mysqldb data:   mysql-mgr-2.cnf: |    [mysqld]     port = 3306     character\_set\_server = utf8     server\_id = 3     log\_bin = mysql-bin    relay-log = relay-bin     #back\_log = 500    #max\_connections = 3000     #wait\_timeout = 5022397    interactive\_timeout = 5022397     max\_connect\_errors = 1000    relay-log-recovery=1     #max\_allowed\_packet = 32M    sort\_buffer\_size = 4M     read\_buffer\_size = 4M    join\_buffer\_size = 8M     thread\_cache\_size = 64    #tmp\_table\_size = 256M     log\_slave\_updates=1    long\_query\_time = 1     slow\_query\_log = 1    skip-name-resolve     innodb\_buffer\_pool\_size=700M    #innodb\_data\_file\_path = ibdata1:1024M:autoextend    innodb\_flush\_log\_at\_trx\_commit=1     innodb\_log\_buffer\_size = 16M     innodb\_log\_file\_size = 256M    innodb\_log\_files\_in\_group = 2     innodb\_max\_dirty\_pages\_pct = 50     sync\_binlog=1     master\_info\_repository=TABLE    relay\_log\_info\_repository=TABLE     log\_timestamps=SYSTEM    gtid\_mode = ON     enforce\_gtid\_consistency = ON    master\_info\_repository = TABLE     relay\_log\_info\_repository = TABLE     log\_slave\_updates = ON     binlog\_checksum = NONE     log\_slave\_updates = ON    slave\_parallel\_type=LOGICAL\_CLOCK    slave\_parallel\_workers=8     slave-preserve-commit-order=on    #group\_replication\_compression\_threshold=200000    transaction\_write\_set\_extraction = XXHASH64    loose-group\_replication\_group\_name="01e5fb97-be64-41f7-bafd-3afc7a6ab555"    loose-group\_replication\_start\_on\_boot=off    loose-group\_replication\_local\_address="k8s4.mgrtest.mysqldb.svc.cluster.local:13306"    loose-group\_replication\_group\_seeds="k8s2.mgrtest.mysqldb.svc.cluster.local:13306,k8s3.mgrtest.mysqldb.svc.cluster.local:13306,k8s4.mgrtest.mysqldb.svc.cluster.local:13306"    loose-group\_replication\_bootstrap\_group = off    loose-group\_replication\_ip\_whitelist=10.96.0.0/16,172.16.0.0/16,192.168.0.0/16,172.17.0.0/16,127.0.0.1/8    report\_host = k8s4.mgrtest.mysqldb.svc.cluster.local    [mysqldump]     quick     max\_allowed\_packet = 32M |

**准备pod文件节点1**

|  |
| --- |
| cat node1\_pod.yaml  apiVersion: v1 kind: Pod metadata:  name: k8s2   namespace: mysqldb   labels:    name: mysql-mgr spec:   nodeName: k8s2   hostname: k8s2   subdomain: mgrtest   containers:   - image: mysql:latest     name: mysql-mgr-0     imagePullPolicy: IfNotPresent         command: [ "/bin/bash", "-ce", "cd /usr/bin && mysqld --initialize-insecure --user=mysql && mysqld\_safe && tail -f /dev/null" ]     ports:       - containerPort: 3306    volumeMounts:     - name: tz-config       mountPath: /etc/localtime     - name: mysql-data       mountPath: /data/mysql/data/     - name: mysql-config      mountPath: /etc/my.cnf       subPath: my.cnf   volumes:    - name: tz-config       hostPath:         path: /etc/localtime     - name: mysql-data       hostPath:        path: /data/mysql/data/     - name: mysql-config      configMap:         name: mysql-mgr-0-cnf        items:           - key: mysql-mgr-0.cnf            path: my.cnf |

**准备pod文件节点2**

|  |
| --- |
| cat node2\_pod.yaml   apiVersion: v1 kind: Pod metadata:  name: k8s3   namespace: mysqldb   labels:    name: mysql-mgr spec:   nodeName: k8s3   hostname: k8s3   subdomain: mgrtest   containers:   - image: mysql:latest     name: mysql-mgr-1     imagePullPolicy: IfNotPresent     command: [ "/bin/bash", "-ce", "cd /usr/bin && mysqld --initialize-insecure --user=mysql && mysqld\_safe && tail -f /dev/null" ]     ports:       - containerPort: 3306    volumeMounts:     - name: tz-config       mountPath: /etc/localtime     - name: mysql-data       mountPath: /data/mysql/data     - name: mysql-config      mountPath: /etc/my.cnf       subPath: my.cnf   volumes:    - name: tz-config       hostPath:         path: /etc/localtime     - name: mysql-data       hostPath:        path: /data/mysql/data/     - name: mysql-config      configMap:         name: mysql-mgr-1-cnf        items:           - key: mysql-mgr-1.cnf            path: my.cnf |

**准备pod文件节点3**

|  |
| --- |
| cat node3\_pod.yaml   apiVersion: v1 kind: Pod metadata:  name: k8s4   namespace: mysqldb   labels:    name: mysql-mgr spec:   nodeName: k8s4   hostname: k8s4   subdomain: mgrtest   containers:   - image: mysql:latest     name: mysql-mgr-2     imagePullPolicy: IfNotPresent     command: [ "/bin/bash", "-ce", "cd /usr/bin && mysqld --initialize-insecure --user=mysql && mysqld\_safe && tail -f /dev/null" ]     ports:       - containerPort: 3306    volumeMounts:     - name: tz-config       mountPath: /etc/localtime     - name: mysql-data       mountPath: /data/mysql/data     - name: mysql-config      mountPath: /etc/my.cnf       subPath: my.cnf   volumes:    - name: tz-config       hostPath:         path: /etc/localtime     - name: mysql-data       hostPath:        path: /data/mysql/data/     - name: mysql-config      configMap:         name: mysql-mgr-2-cnf        items:           - key: mysql-mgr-2.cnf            path: my.cnf |

![](https://ucloud-blog.cn-bj.ufileos.com/articles/130047/images/130047_011.png)

3、创建namespace，service，configmap，pod

|  |
| --- |
| kubectl create -f namespace.yaml  kubectl create -f service.yaml kubectl create -f mysql-mgr-cnf-0.yaml  kubectl create -f mysql-mgr-cnf-1.yaml  kubectl create -f mysql-mgr-cnf-2.yaml  kubectl create -f node1\_pod.yaml  kubectl create -f node2\_pod.yaml  kubectl create -f node3\_pod.yaml |

![](https://ucloud-blog.cn-bj.ufileos.com/articles/130047/images/130047_011.png)

4、创建完成后查看状态

### 

|  |
| --- |
| 1.查看pods状态(看到node节点的mysql已经在运行) # kubectl get pods -n mysqldb -o wide  NAME  READY   STATUS    RESTARTS   AGE    IP               NODE   NOMINATED NODE   READINESS GATES  k8s2  1/1     Running   0          112s   172.16.109.65    k8s2               k8s3  1/1     Running   0          112s   172.16.219.1     k8s3               k8s4  1/1     Running   0          111s   172.16.106.129   k8s4 |

![](https://ucloud-blog.cn-bj.ufileos.com/articles/130047/images/130047_011.png)

5、配置MySQL8MGR

**#节点1操作**

|  |
| --- |
| kubectl exec -it k8s2 -n mysqldb /bin/bash  alter user root@localhost identified by Mysql123!@#; set sql\_log\_bin=0; create user rpl\_user@% identified by Rpl\_pass@123; grant replication slave on \*.\* to rpl\_user@%; flush privileges; set sql\_log\_bin=1; change master to master\_user=rpl\_user,master\_password=Rpl\_pass@123 for channel group\_replication\_recovery; install PLUGIN group\_replication SONAME group\_replication.so;  reset master; set global group\_replication\_single\_primary\_mode=FALSE; set global group\_replication\_enforce\_update\_everywhere\_checks=TRUE; set global group\_replication\_bootstrap\_group=ON; start group\_replication; set global group\_replication\_bootstrap\_group=OFF; select \* from performance\_schema.replication\_group\_members;  1节点执行完后可以看到状态图  ![](https://ucloud-blog.cn-bj.ufileos.com/articles/130047/images/130047_007.png) |

**节点2操作**

|  |
| --- |
| kubectl exec -it k8s3 -n mysqldb /bin/bash  set sql\_log\_bin=0; create user rpl\_user@% identified by Rpl\_pass@123; grant replication slave on \*.\* to rpl\_user@%; flush privileges; set sql\_log\_bin=1; change master to master\_user=rpl\_user,master\_password=Rpl\_pass@123 for channel group\_replication\_recovery; install PLUGIN group\_replication SONAME group\_replication.so;  reset master; set global group\_replication\_single\_primary\_mode=FALSE; set global group\_replication\_enforce\_update\_everywhere\_checks=TRUE; set global group\_replication\_recovery\_get\_public\_key=on; start group\_replication; select \* from performance\_schema.replication\_group\_members;  ![](https://ucloud-blog.cn-bj.ufileos.com/articles/130047/images/130047_008.png) |

**节点3操作**

|  |
| --- |
| kubectl exec -it k8s4 -n mysqldb /bin/bash  set sql\_log\_bin=0; create user rpl\_user@% identified by Rpl\_pass@123; grant replication slave on \*.\* to rpl\_user@%; flush privileges; set sql\_log\_bin=1; change master to master\_user=rpl\_user,master\_password=Rpl\_pass@123 for channel group\_replication\_recovery; install PLUGIN group\_replication SONAME group\_replication.so;  reset master; set global group\_replication\_single\_primary\_mode=FALSE; set global group\_replication\_enforce\_update\_everywhere\_checks=TRUE; set global group\_replication\_recovery\_get\_public\_key=on; start group\_replication; select \* from performance\_schema.replication\_group\_members;  ![](https://ucloud-blog.cn-bj.ufileos.com/articles/130047/images/130047_009.png) |

**配置完MGR后状态****为多主状态**

![](https://ucloud-blog.cn-bj.ufileos.com/articles/130047/images/130047_010.png)

![](https://ucloud-blog.cn-bj.ufileos.com/articles/130047/images/130047_011.png)

6、配置过程中问题及处理

### 

**问题namespaces"mysqldb" already exists**

创建mysqldb的namespace时,因为已经存在一个mysqldb的namespace,报错如下:

|  |
| --- |
| kubectl create -f namespace.yaml  Error from server (AlreadyExists): error when creating "namespace.yaml": object is being deleted: namespaces "mysqldb" already exists    查看状态也是Terminating的  ![](https://ucloud-blog.cn-bj.ufileos.com/articles/130047/images/130047_012.png) |

**解决方式**

|  |
| --- |
| 1.获取mysqldb的namespace下json格式文件  # kubectl get namespace mysqldb -o json > /tmp/mysqldb.json  2.删除掉spec里面指定的内容  ![](https://ucloud-blog.cn-bj.ufileos.com/articles/130047/images/130047_013.png)  3.新开一个终端窗口执行kubectl proxy --port=8081,监听本地  # kubectl proxy --port=8081  Starting to serve on 127.0.0.1:8081  4.执行    # curl -k -H "Content-Type: application/json" -X PUT --data-binary @mysqldb.json http://127.0.0.1:8081/api/v1/namespaces/mysqldb/finalize  注意以上替换mysqldb名字  5.执行完后,查看namespace,mysqldb的Terminating状态已经没有了  ![](https://ucloud-blog.cn-bj.ufileos.com/articles/130047/images/130047_014.png) |

***End***