k8smysql数据同步_K8s——MySQL实现数据持久化1、搭建nfs存储
[root@docker-k8s01 ~]# yum -y install nfs-utils
[root@docker-k8s01 ~]# mkdir /nfsdata/mysql -p
[root@docker-k8s01 ~]# cat /etc/exports
/nfsdata *(rw,sync,no_root_squash)
[root@docker-k8s01 ~]# systemctl restart nfs-server
[root@docker-k8s01 ~]# systemctl enable rpcbind
[root@docker-k8s01 ~]# systemctl enable nfs-server
[root@docker-k8s01 ~]# showmount -e
Export list for docker-k8s01:
/nfsdata *
2、创建PV
//编写pv的yaml⽂件
kind: PersistenVolume
apiVersion: v1
metadata:
name: mysql-pv
spec:
accessModes:
- ReadWriteOnce
capacity:
storage: 1Gi
persistentVolumeReclaimPolicy: Retain
storageClassName: nfs
nfs:
path: /nfsdata/mysql
server: 192.168.171.151
//执⾏yaml⽂件
[root@docker-k8s01 mysql]# kubectl apply -f mysql-pv.yaml
3、创建PVC
//编写PVC的yaml⽂件
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
name: mysql-pvc
spec:
accessModes:
- ReadWriteOnce //访问模式定义为只能以读写的⽅式挂载到单个节点
resources:
requests:
storage: 1Gi
storageClassName: nfs //这⾥指定关联的PV名称
[root@docker-k8s01 mysql]# kubectl apply -f mysql-pvc.yaml
4、确认pv及pvc的状态
/
/可以看到pv及pvc的状态都是Bound
[root@docker-k8s01 mysql]# kubectl get pv
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE mysql-pv 1Gi RWO Retain Bound default/mysql-pvc nfs 6m21s
[root@docker-k8s01 mysql]# kubectl get pvc
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
mysql-pvc Bound mysql-pv 1Gi RWO nfs 2m4s
5、创建pod+svc(service)
这个pod是提供的MySQL服务,并将其映射到宿主机,可以做和client端通信
//编写deployment及service的yaml⽂件
---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: mysql
spec:
selector:
matchLabels:
app: mysql
template:
metadata:
labels:
app: mysql
spec:
containers:
- name: mysql
image: mysql:5.7
env: //配置变量,设置mysql的密码
- name: MYSQL_ROOT_PASSWORD
value: 123
ports:
- containerPort: 3306
volumeMounts:
- name: mysql-persistent-storage
mountPath: /var/lib/mysql //MySQL容器的数据都是存在这个⽬录的,要对这个⽬录做数据持久化volumes:
- name: mysql-persistent-storage
persistentVolumeClaim:
claimName: mysql-pvc //指定pvc名称
---
apiVersion: v1
kind: Service
metadata:
name: mysql
spec:
type: NodePort
ports:
- port: 3306
targetPort: 31111
selector:
app: mysql
[root@docker-k8s01 mysql]# kubectl apply -f mysql-pod.yaml
//确定pod运⾏正常
[root@docker-k8s01 mysql]# kubectl get pod -w
NAME READY STATUS RESTARTS AGE
mysql-6d898f8bcb-fc7bb 1/1 Running 0 60s
6、进⼊MySQL数据库,添加测试数据
mysql> create database test;
mysql> use test
mysql> create table my_id(id int(11));
mysql> insert my_id values(1234);
mysql> select * _id; //确定数据添加成功
+------+
| id |
+------+
| 1234 |
+------+
7、⼿动删除节点上的容器,验证数据库内的数据是否还存在
/
/查看Pod运⾏在哪个节点
[root@docker-k8s01 mysql]# kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
mysql-6d898f8bcb-fc7bb 1/1 Running 0 7m6s 10.244.1.2 docker-k8s02
[root@docker-k8s02 ~]# docker ps // 查看mysql容器的id
//删除mysql容器
[root@docker-k8s02 ~]# docker rm -f 7ba3f9d9ad35
//由于Deployment的保护策略,当删除容器后,它会根据pod的yaml⽂件⽣成⼀个新的容器,但新容器的ID号就变了//回到master节点,登录到数据库,查看数据是否还存在
[root@docker-k8s01 mysql]# kubectl exec -it mysql-6d898f8bcb-fc7bb -- mysql -uroot -p123 mysql> select * _id;
+------+mysql下载app
| id |
+------+
| 1234 |
+------+
1 row in set (0.01 sec)
证明:当使⽤PV来做数据持久化的话,容器的新陈代谢并不会造成丢失数据的。
8、模拟MySQL容器所在的节点宕机,验证数据是否会丢失
//查看容器所在节点
[root@docker-k8s01 mysql]# kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
mysql-6d898f8bcb-fc7bb 1/1 Running 0 13m 10.244.1.2 docker-k8s02
/
/将docker-k8s02节点宕机,动态查看Pod的状态
//⼤概node01关机五分钟左右,可以看到pod就被转移到node02上运⾏了
[root@docker-k8s01 ~]# kubectl get pod -w
NAME READY STATUS RESTARTS AGE
mysql-6d898f8bcb-fc7bb 1/1 Running 0 22m
mysql-6d898f8bcb-fc7bb 1/1 Running 0 22m
mysql-6d898f8bcb-fc7bb 1/1 Terminating 0 27m
mysql-6d898f8bcb-2h47v 0/1 Pending 0 0s
mysql-6d898f8bcb-2h47v 0/1 Pending 0 0s
mysql-6d898f8bcb-2h47v 0/1 ContainerCreating 0 0s
mysql-6d898f8bcb-2h47v 1/1 Running 0 33s
9、client端访问MySQL数据库
因为我们在创建pod的yaml⽂件中,将其svc的端⼝映射类型是nodeport,所以,我们是可以通过访问集内的任意主机的31111端⼝(yaml⽂件中⾃定义的端⼝)来登录到数据库的。
[root@docker-k8s01 ~]# mysql -uroot -p123 -h 192.168.171.151 -P 31111
MySQL [(none)]> select * _id; #查看数据
+------+
| id |
+------+
| 1234 |
+------+
1 row in set (0.01 sec)

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。