本文章视频教程地址:https://www.bilibili.com/video/BV1iw411e7ZE/
首先你需要准备一个kubernetes集群和一台nfs服务器,为了方便,我就用 k8s-master 来做nfs服务器了。我的服务器IP地址和用途如图:
IP地址 | 主机名 | 用途 |
---|---|---|
192.168.1.160 | k8s-master | Kubernetes主服务器兼nfs服务器 |
192.168.1.161 | K8s-node01 | Kubernetes的第一个工作节点 |
192.168.1.162 | K8s-node02 | Kubernetes的第二个工作节点 |
如果你还没有Kubernetes集群,请参考我写的文章:https://blog.csdn.net/m0_51510236/article/details/130842122
我之前写过关于在物理机上搭建MySQL主从和在Kubernetes上安装单节点的文章,可以对这篇文章进行参考:
我打算在主服务器上安装nfs,因为需要在两个工作节点上连接nfs,所以工作节点上也要安装nfs:
yum install -y nfs-utils
因为我们要安装三台mysql(一主两从),所以我们要创建三个目录用于这三台服务器。
我之前写过一篇关于动态存储的文章,如果需要使用动态存储请参考:https://blog.csdn.net/m0_51510236/article/details/132641343,动态存储的安装步骤是这篇文章的上半部分
我们直接在nfs服务器(k8s-master)当中创建这三个目录并写入 /etc/exports
文件夹中(创建的目录可以修改):
mkdir -p /data/nfs/{mysql-master,mysql-slaver-01,mysql-slaver-02}
cat >> /etc/exports << EOF
/data/nfs/mysql-master *(rw,sync,no_root_squash)
/data/nfs/mysql-slaver-01 *(rw,sync,no_root_squash)
/data/nfs/mysql-slaver-02 *(rw,sync,no_root_squash)
EOF
之前我们已经搭建好了NFS服务器,接下来我们可以直接在主服务器上启动nfs服务器。这行这行命令:
systemctl enable --now nfs-server
执行后我们可以通过这行命令来检查目录是否暴露成功:
# 注意修改为自己的nfs服务器地址
showmount -e 192.168.1.160
创建一个命名空间来部署MySQL集群,当然你也可以使用默认的Default命名空间。这里就用 deploy-test
命名空间来搭建集群了,首先我们将这个命名空间创建出来:
kubectl create namespace deploy-test
apiVersion: v1
kind: Namespace
metadata:
name: deploy-test
spec: {}
status: {}
创建一个存储了MySQL密码的Secret,我们可以直接使用这行命令生成这个Secret的资源清单文件:
# 注意修改root的密码和命名空间,我的root密码设置为的是root
kubectl create secret generic mysql-password --namespace=deploy-test --from-literal=mysql_root_password=root --dry-run=client -o=yaml
我们之前安装了nfs,现在我们可以给予那些目录创建pv和pvc,创建pv和pvc的资源清单文件(pv和pvc在我之前的一篇文章中有比较详细介绍:https://blog.csdn.net/m0_51510236/article/details/132482351):
apiVersion: v1
kind: PersistentVolume
metadata:
name: deploy-mysql-master-nfs-pv
namespace: deploy-test
spec:
capacity:
storage: 1Gi
accessModes:
- ReadWriteMany
nfs:
# 注意修改IP地址和暴露的目录(如果不一样)
server: 192.168.1.160
path: /data/nfs/mysql-master
storageClassName: "nfs"
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: deploy-mysql-master-nfs-pvc
namespace: deploy-test
spec:
accessModes:
- ReadWriteMany
storageClassName: "nfs"
resources:
requests:
storage: 1Gi
volumeName: deploy-mysql-master-nfs-pv
创建后我们可以用这行命令来查看创建后的pv和pvc:
kubectl get pv,pvc -n deploy-test
我们需要为主节点准备一个 my.cnf
配置文件,文件内容如下:
[mysqld]
skip-host-cache
skip-name-resolve
datadir = /var/lib/mysql
socket = /var/run/mysqld/mysqld.sock
secure-file-priv = /var/lib/mysql-files
pid-file = /var/run/mysqld/mysqld.pid
user = mysql
secure-file-priv = NULL
server-id = 1
log-bin = master-bin
log_bin_index = master-bin.index
binlog_do_db = xiaohh_user
binlog_ignore_db = information_schema
binlog_ignore_db = mysql
binlog_ignore_db = performance_schema
binlog_ignore_db = sys
binlog-format = ROW
[client]
socket = /var/run/mysqld/mysqld.sock
!includedir /etc/mysql/conf.d/
有那么几个配置需要注意一下那么几个配置:
# server id,要注意多个mysql节点唯一
server-id = 1
# 生成的logbin的文件名
log-bin = master-bin
log_bin_index = master-bin.index
# 同步哪个数据库,这里我们为了测试之同步xiaohh_user这个数据库
binlog_do_db = xiaohh_user
# 排除哪个数据库,可以写多行,排除的数据库不会被主从同步,这里写上mysql自带的几个数据库
binlog_ignore_db = information_schema
... # 还有几行省略
# binlog的格式
binlog-format = ROW
接下来将创建一个ConfigMap来存储这个配置文件。可以使用以下配置生成yaml资源清单文件内容:
kubectl create configmap mysql-master-cm -n deploy-test --from-file=my.cnf --dry-run=client -o yaml
我们将在下一个步骤将其创建。
我们直接上mysql主节点的yaml资源清单文件:
apiVersion: v1
data:
my.cnf: |
[mysqld]
skip-host-cache
skip-name-resolve
datadir = /var/lib/mysql
socket = /var/run/mysqld/mysqld.sock
secure-file-priv = /var/lib/mysql-files
pid-file = /var/run/mysqld/mysqld.pid
user = mysql
secure-file-priv = NULL
server-id = 1
log-bin = master-bin
log_bin_index = master-bin.index
binlog_do_db = xiaohh_user
binlog_ignore_db = information_schema
binlog_ignore_db = mysql
binlog_ignore_db = performance_schema
binlog_ignore_db = sys
binlog-format = ROW
[client]
socket = /var/run/mysqld/mysqld.sock
!includedir /etc/mysql/conf.d/
kind: ConfigMap
metadata:
name: mysql-master-cm
namespace: deploy-test
---
apiVersion: v1
kind: Service
metadata:
name: deploy-mysql-master-svc
namespace: deploy-test
labels:
app: mysql-master
spec:
ports:
- port: 3306
name: mysql
targetPort: 3306
nodePort: 30306
selector:
app: mysql-master
type: NodePort
sessionAffinity: ClientIP
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: deploy-mysql-master
namespace: deploy-test
spec:
selector:
matchLabels:
app: mysql-master
serviceName: "deploy-mysql-master-svc"
replicas: 1
template:
metadata:
labels:
app: mysql-master
spec:
terminationGracePeriodSeconds: 10
containers:
- args:
- --character-set-server=utf8mb4
- --collation-server=utf8mb4_unicode_ci
- --lower_case_table_names=1
- --default-time_zone=+8:00
name: mysql
# image: docker.io/library/mysql:8.0.34
image: registry.cn-shenzhen.aliyuncs.com/xiaohh-docker/mysql:8.0.34
ports:
- containerPort: 3306
name: mysql
volumeMounts:
- name: mysql-data
mountPath: /var/lib/mysql
- name: mysql-conf
mountPath: /etc/my.cnf
readOnly: true
subPath: my.cnf
env:
- name: MYSQL_ROOT_PASSWORD
valueFrom:
secretKeyRef:
key: mysql_root_password
name: mysql-password
volumes:
- name: mysql-data
persistentVolumeClaim:
claimName: deploy-mysql-master-nfs-pvc
- name: mysql-conf
configMap:
name: mysql-master-cm
items:
- key: my.cnf
mode: 0644
path: my.cnf
我之前讲过stateful的mysql资源清单文件内容,可以参考我写的文章:https://blog.csdn.net/m0_51510236/article/details/132482351,这里就不再过多讲解。有几个特殊的地方需要拿出来说一下,首先就是这个位置:
为了方便有些拉取不到dockerhub镜像的小伙伴,我将dockerhub的mysql镜像拉取下来并推送到了国内的阿里云。你们可以选择性使用。接下来还需要注意文件的挂载:
然后执行以下命令部署这个yaml文件:
kubectl apply -f mysql-master.yaml
接下来查看一下nfs挂在出来的目录,可以看到log-bin文件已经出现了:
刚刚我们安装了MySQL的主节点,接下来我们安装第一个MySQL从节点。
因为搭建主节点的时候已经说过了,这里直接提供yaml资源清单文件:
apiVersion: v1
kind: PersistentVolume
metadata:
name: deploy-mysql-slave-01-nfs-pv
namespace: deploy-test
spec:
capacity:
storage: 1Gi
accessModes:
- ReadWriteMany
nfs:
server: 192.168.1.160
path: /data/nfs/mysql-slaver-01
storageClassName: "nfs"
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: deploy-mysql-slave-01-nfs-pvc
namespace: deploy-test
spec:
accessModes:
- ReadWriteMany
storageClassName: "nfs"
resources:
requests:
storage: 1Gi
volumeName: deploy-mysql-slave-01-nfs-pv
我们需要为主节点准备一个 my.cnf
配置文件,文件内容如下:
[mysqld]
skip-host-cache
skip-name-resolve
datadir = /var/lib/mysql
socket = /var/run/mysqld/mysqld.sock
secure-file-priv = /var/lib/mysql-files
pid-file = /var/run/mysqld/mysqld.pid
user = mysql
secure-file-priv = NULL
server-id = 2
log-bin = slave-bin
relay-log = slave-relay-bin
relay-log-index = slave-relay-bin.index
[client]
socket = /var/run/mysqld/mysqld.sock
!includedir /etc/mysql/conf.d/
有那么几个配置需要注意一下那么几个配置:
# server id,注意不同节点要不一样
server-id = 2
# 从节点的logbin文件
log-bin = slave-bin
relay-log = slave-relay-bin
relay-log-index = slave-relay-bin.index
接下来将创建一个ConfigMap来存储这个配置文件。可以使用以下配置生成yaml资源清单文件内容:
kubectl create configmap mysql-slave-01-cm -n deploy-test --from-file=my.cnf --dry-run=client -o yaml
我们将在下一个步骤将其创建。
yaml资源清单文件和主节点差不多,这里就直接提供:
apiVersion: v1
data:
my.cnf: |
[mysqld]
skip-host-cache
skip-name-resolve
datadir = /var/lib/mysql
socket = /var/run/mysqld/mysqld.sock
secure-file-priv = /var/lib/mysql-files
pid-file = /var/run/mysqld/mysqld.pid
user = mysql
secure-file-priv = NULL
server-id = 2
log-bin = slave-bin
relay-log = slave-relay-bin
relay-log-index = slave-relay-bin.index
[client]
socket = /var/run/mysqld/mysqld.sock
!includedir /etc/mysql/conf.d/
kind: ConfigMap
metadata:
name: mysql-slave-01-cm
namespace: deploy-test
---
apiVersion: v1
kind: Service
metadata:
name: deploy-mysql-slave-svc
namespace: deploy-test
labels:
app: mysql-slave
spec:
ports:
- port: 3306
name: mysql
targetPort: 3306
nodePort: 30308
selector:
app: mysql-slave
type: NodePort
sessionAffinity: ClientIP
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: deploy-mysql-slave-01
namespace: deploy-test
spec:
selector:
matchLabels:
app: mysql-slave
serviceName: "deploy-mysql-slave-svc"
replicas: 1
template:
metadata:
labels:
app: mysql-slave
spec:
terminationGracePeriodSeconds: 10
containers:
- args:
- --character-set-server=utf8mb4
- --collation-server=utf8mb4_unicode_ci
- --lower_case_table_names=1
- --default-time_zone=+8:00
name: mysql
# image: docker.io/library/mysql:8.0.34
image: registry.cn-shenzhen.aliyuncs.com/xiaohh-docker/mysql:8.0.34
ports:
- containerPort: 3306
name: mysql
volumeMounts:
- name: mysql-data
mountPath: /var/lib/mysql
- name: mysql-conf
mountPath: /etc/my.cnf
readOnly: true
subPath: my.cnf
env:
- name: MYSQL_ROOT_PASSWORD
valueFrom:
secretKeyRef:
key: mysql_root_password
name: mysql-password
volumes:
- name: mysql-data
persistentVolumeClaim:
claimName: deploy-mysql-slave-01-nfs-pvc
- name: mysql-conf
configMap:
name: mysql-slave-01-cm
items:
- key: my.cnf
mode: 0644
path: my.cnf
这里同样只提供相应的yaml资源清单文件:
apiVersion: v1
kind: PersistentVolume
metadata:
name: deploy-mysql-slave-02-nfs-pv
namespace: deploy-test
spec:
capacity:
storage: 1Gi
accessModes:
- ReadWriteMany
nfs:
server: 192.168.1.160
path: /data/nfs/mysql-slaver-02
storageClassName: "nfs"
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: deploy-mysql-slave-02-nfs-pvc
namespace: deploy-test
spec:
accessModes:
- ReadWriteMany
storageClassName: "nfs"
resources:
requests:
storage: 1Gi
volumeName: deploy-mysql-slave-02-nfs-pv
我们需要为主节点准备一个 my.cnf
配置文件,文件内容如下:
[mysqld]
skip-host-cache
skip-name-resolve
datadir = /var/lib/mysql
socket = /var/run/mysqld/mysqld.sock
secure-file-priv = /var/lib/mysql-files
pid-file = /var/run/mysqld/mysqld.pid
user = mysql
secure-file-priv = NULL
server-id = 3
log-bin = slave-bin
relay-log = slave-relay-bin
relay-log-index = slave-relay-bin.index
[client]
socket = /var/run/mysqld/mysqld.sock
!includedir /etc/mysql/conf.d/
有那么几个配置需要注意一下那么几个配置:
# server id,注意不同节点要不一样
server-id = 3
# 从节点的logbin文件
log-bin = slave-bin
relay-log = slave-relay-bin
relay-log-index = slave-relay-bin.index
接下来将创建一个ConfigMap来存储这个配置文件。可以使用以下配置生成yaml资源清单文件内容:
kubectl create configmap mysql-slave-02-cm -n deploy-test --from-file=my.cnf --dry-run=client -o yaml
我们将在下一个步骤将其创建。
注意这个配置文件少了一个service,因为我打算将它与slave1共用一个service,yaml资源清单文件:
apiVersion: v1
data:
my.cnf: |
[mysqld]
skip-host-cache
skip-name-resolve
datadir = /var/lib/mysql
socket = /var/run/mysqld/mysqld.sock
secure-file-priv = /var/lib/mysql-files
pid-file = /var/run/mysqld/mysqld.pid
user = mysql
secure-file-priv = NULL
server-id = 3
log-bin = slave-bin
relay-log = slave-relay-bin
relay-log-index = slave-relay-bin.index
[client]
socket = /var/run/mysqld/mysqld.sock
!includedir /etc/mysql/conf.d/
kind: ConfigMap
metadata:
name: mysql-slave-02-cm
namespace: deploy-test
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: deploy-mysql-slave-02
namespace: deploy-test
spec:
selector:
matchLabels:
app: mysql-slave-02
serviceName: "deploy-mysql-slave-svc"
replicas: 1
template:
metadata:
labels:
app: mysql-slave-02
spec:
terminationGracePeriodSeconds: 10
containers:
- args:
- --character-set-server=utf8mb4
- --collation-server=utf8mb4_unicode_ci
- --lower_case_table_names=1
- --default-time_zone=+8:00
name: mysql
# image: docker.io/library/mysql:8.0.34
image: registry.cn-shenzhen.aliyuncs.com/xiaohh-docker/mysql:8.0.34
ports:
- containerPort: 3306
name: mysql
volumeMounts:
- name: mysql-data
mountPath: /var/lib/mysql
- name: mysql-conf
mountPath: /etc/my.cnf
readOnly: true
subPath: my.cnf
env:
- name: MYSQL_ROOT_PASSWORD
valueFrom:
secretKeyRef:
key: mysql_root_password
name: mysql-password
volumes:
- name: mysql-data
persistentVolumeClaim:
claimName: deploy-mysql-slave-02-nfs-pvc
- name: mysql-conf
configMap:
name: mysql-slave-02-cm
items:
- key: my.cnf
mode: 0644
path: my.cnf
首先我们来到master的mysql,输入下面命令:
kubectl exec -itn deploy-test pod/deploy-mysql-master-0 -- mysql -uroot -p
我们输入这行命令查看master的状态:
show master status;
查看输出结果,我们需要记住 File
和 Position
参数:
接下来我们分别来到两个子几点当中执行这行命令:
change master to master_host='deploy-mysql-master-0.deploy-mysql-master-svc.deploy-test.svc.cluster.local', master_port=3306, master_user='root', master_password='root', master_log_file='master-bin.000003', master_log_pos=157, master_connect_retry=30, get_master_public_key=1;
需要注意下面的几个参数:
pod名称.service名称.命名空间.svc.cluster.local
,所以我们master的mysql地址是 deploy-mysql-master-0.deploy-mysql-master-svc.deploy-test.svc.cluster.local
File
字段Position
字段根据上面参数,如果要修改那么请按照自己的环境进行修改。
我们使用下面这行命令就可以进入第一个mysql从节点:
kubectl exec -itn deploy-test pod/deploy-mysql-slave-01-0 -- mysql -uroot -p
然后我们还要开启slave,执行下面命令:
start slave;
然后我们可以执行这行命令查看slave状态:
show slave status\G
第一个slave节点加入成功
我们使用下面这行命令就可以进入第二个mysql从节点:
kubectl exec -itn deploy-test pod/deploy-mysql-slave-02-0 -- mysql -uroot -p
查看Slave状态:
show slave status\G
首先我们在主节点当中创建一个数据库和一个数据表:
CREATE DATABASE `xiaohh_user`;
USE `xiaohh_user`;
CREATE TABLE `user` (
`user_id` BIGINT UNSIGNED PRIMARY KEY AUTO_INCREMENT COMMENT '用户id',
`username` VARCHAR(50) NOT NULL COMMENT '用户名',
`age` TINYINT UNSIGNED DEFAULT 18 COMMENT '年龄',
`gender` TINYINT UNSIGNED DEFAULT 2 COMMENT '性别;0=男,1=女,2=未知'
) COMMENT '用户表';
我们来到主数据库创建一条数据:
INSERT INTO `user` (`username`, `age`, `gender`) VALUES ('XiaoHH', '18', '0');
好了,Kubernetes上搭建MySQL主从就已经完毕了。祝你们编码愉快~