k8s1.19.16部署mysql5.7.35主从架构做数据持久化
一、前言在K8s1.19.16上部署mysql5.7.35主从模式,采用StatefulSet模式进行部署
·
一、前言
在K8s1.19.16上部署mysql5.7.35主从模式,采用StatefulSet模式进行部署。
二、准备操作
直接从dockerhub上拉取官方镜像mysql:5.7.35即可
dockerhub地址:Docker Hub
docker pull mysql:5.7.35
三、部署模式
采取statefulset模式进行部署
四、部署过程
1、创建namespace
apiVersion: v1
kind: Namespace
metadata:
name: mysql-ns
labels:
name: mysql-ns
2、创建mysql-master使用的storageclass
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
name: mysql-master-storageclass
provisioner: nfs-storage-01
allowVolumeExpansion: true
reclaimPolicy: Retain
3、创建mysql-master使用的pvc
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: mysql-pvc
annotations:
volume.beta.kubernetes.io/storage-class: "mysql-master-storageclass"
spec:
resources:
requests:
storage: 5Gi
volumeMode: Filesystem
accessModes:
- ReadWriteOnce
4、创建mysql-master的headliness(因为是使用的statefulset控制器)
kind: Service
apiVersion: v1
metadata:
name: mysql-master-hs
namespace: mysql-ns
labels:
app: mysql-master-ss
spec:
selector:
app: mysql-master-ss
clusterIP: None
ports:
- port: 3306
targetPort: 3306
5、创建mysql-master-service对外访问
apiVersion: v1
kind: Service
metadata:
name: mysql-master-service
namespace: mysql-ns
spec:
type: NodePort
selector:
app: mysql-master-ss
ports:
- port: 3306
targetPort: 3306
nodePort: 32222
6、创建mysql-master部署文件
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: mysql-master-ss
namespace: mysql-ns
labels:
app: mysql-master-ss
spec:
replicas: 1
selector:
matchLabels:
app: mysql-master-ss
serviceName: mysql-master-hs
template:
metadata:
labels:
app: mysql-master-ss
spec:
containers:
- name: mysql
image: 192.168.238.152/mysql/mysql:5.7.35 #这个是我自己的harbor地址
ports:
- containerPort: 3306
env:
- name: MYSQL_ROOT_PASSWORD
value: "mysql123"
- name: MYSQL_REPLICATION_USER
value: "repl"
- name: MYSQL_REPLICATION_PASSWORD
value: "repl123"
volumeMounts:
- name: mysql-data
mountPath: /var/lib/mysql
args:
- --log-bin=mysql-bin
- --binlog-ignore-db=mysql
- --server-id=1001
- --symbolic-links=0
- --sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
volumeClaimTemplates:
- metadata:
name: mysql-data
spec:
accessModes: ["ReadWriteOnce"]
storageClassName: mysql-master-storageclass
resources:
requests:
storage: 5Gi
7、创建mysql-slave的storageclass和pvc
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
name: mysql-slave-storageclass
provisioner: nfs-storage-01
allowVolumeExpansion: true
reclaimPolicy: Retain
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: mysql-slave-pvc
annotations:
volume.beta.kubernetes.io/storage-class: "mysql-slave-storageclass"
spec:
resources:
requests:
storage: 5Gi
volumeMode: Filesystem
accessModes:
- ReadWriteOnce
8、创建mysql-slave的headliness和service用来对外访问(slave节点可以不创建service)
kind: Service
apiVersion: v1
metadata:
name: mysql-slave-headliness
namespace: mysql-ns
labels:
app: mysql-slave-ss
spec:
selector:
app: mysql-slave-ss
clusterIP: None
ports:
- port: 3306
targetPort: 3306
---
apiVersion: v1
kind: Service
metadata:
name: mysql-slave-service
namespace: mysql-ns
spec:
type: NodePort
selector:
app: mysql-slave-ss
ports:
- port: 3306
targetPort: 3306
nodePort: 32223
9、创建mysql-slave部署文件(mysql-serverid不能一样一定要区分开)
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: mysql-slave-ss
namespace: mysql-ns
labels:
app: mysql-slave-ss
spec:
replicas: 1
selector:
matchLabels:
app: mysql-slave-ss
serviceName: mysql-slave-headliness
template:
metadata:
labels:
app: mysql-slave-ss
spec:
containers:
- name: mysql
image: 106.12.161.186/mysql/mysql:5.7.35
ports:
- containerPort: 3306
env:
- name: MYSQL_ROOT_PASSWORD
value: "mysql123"
- name: MYSQL_REPLICATION_USER
value: "repl"
- name: MYSQL_REPLICATION_PASSWORD
value: "repl123"
volumeMounts:
- name: mysql-data
mountPath: /var/lib/mysql
args:
- --log-bin=mysql-bin
- --binlog-ignore-db=mysql
- --server-id=2002
- --symbolic-links=0
- --sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
volumeClaimTemplates:
- metadata:
name: mysql-data
spec:
accessModes: ["ReadWriteOnce"]
storageClassName: mysql-slave-storageclass
resources:
requests:
storage: 5Gi
10、查看mysql集群pod状态running为成功
11、进入到kube-dashbord中查看master_host (注意k8s1.19.16版本搭建kube-dashbord需要2系版本我这里是最新版2.5.1的)
12、进入master给slave库授权
kubectl exec -ti -n mysql-ns mysql-master-ss-0 bash
mysql -u root -pmysql123
创建slave库授权账号(生产环境严禁使用弱密码)
GRANT REPLICATION SLAVE ON *.* to 'repl'@'%' IDENTIFIED by 'repl123';
获取二进制日志
show master status;
13、进入slave库启动同步功能
kubectl exec -ti -n mysql-ns mysql-slave-ss-0 bash
mysql -u root -pmysql123
配置slave节点(master_host参考上面红框里的)
change master to master_host='mysql-master-headliness.mysql-ns',master_user='repl',master_password='repl123',master_log_file='mysql-bin.000003',master_log_pos=437 ;
查看同步状态(在slave节点执行)
show slave status \G;
14、验证验证同步功能
在mysql-master pod上登录数据库执行,创建一个库用来验证
CREATE DATABASE IF NOT EXISTS RUNOOB DEFAULT CHARSET utf8 COLLATE utf8_general_ci;
进入到mysql-slave节点查看
发现也有在master节点创建的库主从同步验证成功
更多推荐
已为社区贡献6条内容
所有评论(0)