k8s mysql 持久化_K8s之MySQL实现数据持久化
完成下列实验需搭建k8s集群环境如下:* master节点IP:192.168.45.129* node01节点IP:192.168.45.141* node02节点IP:192.168.45.142注:这里实验环境提前搭建好了如想搭建k8s集群前参考博文:kubeadm部署kubernetes(k8s)集群1)搭建NFS共享存储本次案例直接在master节点上创建NFS存储![[emailpr.
完成下列实验需搭建k8s集群环境如下:
* master节点IP:192.168.45.129
* node01节点IP:192.168.45.141
* node02节点IP:192.168.45.142
注:这里实验环境提前搭建好了如想搭建k8s集群前参考博文:kubeadm部署kubernetes(k8s)集群
1)搭建NFS共享存储
本次案例直接在master节点上创建NFS存储!
[[email protected] ~]# yum -y install nfs-utils rpcbind
[[email protected] ~]# vim /etc/exports
/nfsdata *(rw,sync,no_root_squash)
[[email protected] ~]# systemctl start nfs-server
[[email protected] ~]# systemctl start rpcbind
[[email protected] ~]# showmount -e
Export list for master:
/nfsdata *
2)创建PV资源对象
[[email protected] ~]# vim mysql-pv.yaml //编写pv的yaml文件
apiVersion: v1
kind: PersistentVolume
metadata:
name: mysql-pv
spec:
capacity:
storage: 1Gi
accessModes:
- ReadWriteOnce //访问模式定义为只能以读写的方式挂载到单个节点
persistentVolumeReclaimPolicy: Retain //注意指定的回收策略为手动回收
storageClassName: nfs
nfs:
path: /nfsdata/mysql-pv
server: 192.168.45.129
[[email protected] ~]# kubectl apply -f mysql-pv.yaml //执行yaml文件
[[email protected] ~]# kubectl get pv
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
mysql-pv 1Gi RWO Retain Available nfs 7s
[[email protected] ~]# mkdir -p /nfsdata/mysql-pv
3)创建PVC资源对象
[[email protected] ~]# vim mysql-pvc.yaml //编写pvcyaml文件
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: mysql-pvc
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 1Gi
storageClassName: nfs //这里指定关联的PV名称
[[email protected] ~]# kubectl apply -f mysql-pvc.yaml //执行yaml文件
[[email protected] ~]# kubectl get pv,pvc
//查看pv和pvc已经关联上了
//PV和PVC的状态类应该全是Bound。
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
persistentvolume/mysql-pv 1Gi RWO Retain Bound default/mysql-pvc nfs 108s
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
persistentvolumeclaim/mysql-pvc Bound mysql-pv 1Gi RWO nfs 21s
4)创建pod资源
[[email protected] ~]# vim mysql-pod.yaml //编写pod的yaml文件
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: mysql-pod
spec:
selector: //设置给予等值的标签选择器
matchLabels:
app: mysql
template:
metadata:
labels:
app: mysql
spec:
containers:
- image: mysql:5.6
name: mysql
env: //设置环境变量,数据库root用户的密码
- name: MYSQL_ROOT_PASSWORD
value: 123.com
volumeMounts:
- name: mysql-storage
mountPath: /var/lib/mysql //这个目录是数据库存放数据的目录(指定的是容器中的目录)
volumes:
- name: mysql-storage
persistentVolumeClaim:
claimName: mysql-pvc //指定pvc的名称
[[email protected] ~]# kubectl apply -f mysql-pod.yaml
[[email protected] ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
mysql-pod-6cc889468b-g8cw6 1/1 Running 0 39s
5)测试数据持久化效果
[[email protected] ~]# kubectl get pod -o wide //查看容器的ID
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
mysql-pod-6cc889468b-g8cw6 1/1 Running 0 3m54s 10.244.2.2 node01
[[email protected] ~]# kubectl exec -it mysql-pod-6cc889468b-g8cw6 -- mysql -u root -p123.com
//直接登录运行mysql数据库的pod中的mysql
//插入数据进行测试
mysql> create database bjq;
Query OK, 1 row affected (0.00 sec)
mysql> use bjq;
Database changed
mysql> create table my_id( id int(4) );
Query OK, 0 rows affected (0.03 sec)
mysql> insert my_id values (9527);
Query OK, 1 row affected (0.01 sec)
mysql> select * from my_id;
+------+
| id |
+------+
| 9527 |
+------+
1 row in set (0.00 sec)
[[email protected] ~]# ls /nfsdata/mysql-pv/ //查看pod对应的NFS的目录,确实有了数据
auto.cnf bjq ibdata1 ib_logfile0 ib_logfile1 mysql performance_schema
6)模拟MySQL容器所在的节点宕机,验证数据是否会丢失
[[email protected] ~]# kubectl get pod -o wide
//查看到pod是运行在node01节点上的
//模拟node01宕机,步骤省略,关机、挂起都可以!
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
mysql-pod-6cc889468b-g8cw6 1/1 Running 0 8m48s 10.244.2.2 node01
[[email protected] ~]# systemctl restart kubelet.service
//重启node02的kubelet服务
//接下来耐心等待pod的转移,可能需要差不多5分钟请耐性等待
[[email protected] ~]# kubectl get pod -o wide //可以看到pod已经运行在node02上
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
mysql-pod-6cc889468b-25ltd 1/1 Running 0 65s 10.244.1.2 node02
mysql-pod-6cc889468b-g8cw6 1/1 Terminating 0 19m 10.244.2.2 node01
[[email protected] ~]# kubectl exec -it mysql-pod-6cc889468b-25ltd -- mysql -u root -p123.com
//再次登录到pod中运行的mysql(注意:pod的名称)
//查看数据是否还存在,发现数据依然存在
mysql> select * from bjq.my_id;
+------+
| id |
+------+
| 9527 |
+------+
1 row in set (0.01 sec)
原文:https://blog.51cto.com/14306186/2522162
更多推荐
所有评论(0)