k8s之Mysql实现数据持久化

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了k8s之Mysql实现数据持久化相关的知识,希望对你有一定的参考价值。

注意yaml配置文件中的注言,与格式的缩进

一、
1.Persistent Volume,即PV,是集群中已由管理员配置的一段网络存储,相当于一个存储卷,一块存储盘。由管理员或者由StorageClass,存储类自动管理。
2.Persistent Volume Claim,即PVC,是用户存储的请求,相当于Pod,Pod消耗节点资源,而PVC消耗存储资源,通俗点说就是,pv是创建的总空间,pvc从总空间中申请空间使用。
3.PV的访问控制类型(accessModes):
(1)ReadWriteOnce:访问模式为只能以读写的方式挂载到单个节点
(2)ReadWriteMany:访问模式为只能以读写的方式挂载到多个节点
(3)ReadOnlyOnce:访问模式为只能以只读的方式挂载到单个节点
4.PV的空间回收策略(persistentVolumeReclaimPolicy):
(1)Recycle:自动清除数据。
(2)Retain:需要管理员手动回收。
(3)Delete:云存储专用。
5.PV与PVC相互的关联,通过的是accessModes(访问模式)和storageClassName(存储类的名字)来定义的。

二、用mysql实验模拟数据持久化的作用:

**1.首先搭建NFS共享服务:**

1.[root@master?~]#?yum?install?-y?nfs-utils?rpcbind??#这里注意三台都要安装NFS服务。
2.[root@master?~]#?vim?/etc/exports??
3./nfsdata??*(rw,sync,no_root_squash)??
4.[root@master?~]#?mkdir?/nfsdata??
5.[root@master?~]#?systemctl?start?rpcbind??
6.[root@master?~]#?systemctl?start?nfs-server.service???
7.[root@master?~]#?showmount?-e??
8.Export?list?for?master:??
9./nfsdata?*??
**2.创建PV资源对象:**

1.[root@master?~]#?mkdir?yaml??
2.[root@master?~]#?cd?yaml/??
3.[root@master?yaml]#?vim?nfs-pv.yaml???
4.??
5.apiVersion:?v1??
6.kind:?PersistentVolume??
7.metadata:??
8.??name:?lbh-pv??
9.spec:??
10.??capacity:??
11.????storage:?1Gi??
12.??accessModes:??
13.????-?ReadWriteOnce??        #访问模式为只能以读写的方式挂载单个节点。
14.??persistentVolumeReclaimPolicy:?Retain??#PV空间回收策略为手动回收。
15.??storageClassName:?nfs??        #定义存储类的名字
16.??nfs:??
17.????path:?/nfsdata/lbh-pv??
18.????server:?192.168.2.50??
执行yaml文件,查看状态:

1.[root@master?yaml]#?kubectl?apply?-f?nfs-pv.yaml???
2.persistentvolume/lbh-pv?created??
3.[root@master?yaml]#?kubectl?get?pv??
4.NAME?????CAPACITY???ACCESS?MODES???RECLAIM?POLICY???STATUS??????CLAIM???STORAGECLASS???REASON???AGE??
5.lbh-pv???1Gi????????RWO????????????Retain?????????**??Available?**??????????nfs?????????????????????27s??**注意pv的状态Available:**

PV创建成功。

**3.创建PVC资源对象:**

1.[root@master?yaml]#?vim?nfs-pvc.yaml???
2.??
3.apiVersion:?v1??
4.kind:?PersistentVolumeClaim??
5.metadata:??
6.??name:?lbh-pvc??
7.spec:??
8.??accessModes:??
9.????-?ReadWriteOnce?? #这里的访问模式必须与PV资源一致
10.??resources:??
11.????requests:??
12.??????storage:?1Gi??
13.??storageClassName:?nfs??    #存储类的名字,必须与PV资源一致
**执行yaml文件,查看PVC及PV状态:**

1.[root@master?yaml]#?kubectl?apply?-f?nfs-pvc.yaml???
2.persistentvolumeclaim/lbh-pvc?created??
3.[root@master?yaml]#?kubectl?get?pv??
4.NAME?????CAPACITY???ACCESS?MODES???RECLAIM?POLICY???STATUS???CLAIM?????????????STORAGECLASS???REASON???AGE??
5.lbh-pv???1Gi????????RWO????????????Retain???????????**Bound?**???default/lbh-pvc???nfs?????????????????????3m55s??
6.[root@master?yaml]#?kubectl?get?pvc??
7.NAME??????STATUS???VOLUME???CAPACITY???ACCESS?MODES???STORAGECLASS???AGE??
8.lbh-pvc??**?Bound**????lbh-pv???1Gi????????RWO????????????nfs????????????10s??
```注意是否关联Bound:

4.创建Deployment资源对象,镜像为mysql:5.6
节点提前下载镜像:

1.[root@node01?~]#?docker?pull?mysql:5.6??
2.[root@node02?~]#?docker?pull?mysql:5.6??

**创建Deployment资源对象:**

1.[root@master?yaml]#?vim?mysql.yaml???
2.??
3.apiVersion:?extensions/v1beta1??
4.kind:?Deployment??
5.metadata:??
6.??name:?lbh-mysql??
7.spec:??
8.??selector:??
9.????matchLabels:??
10.??????app:?mysql??
11.??template:??
12.????metadata:??
13.??????labels:??
14.????????app:?mysql??
15.????spec:??
16.??????containers:??
17.??????-?image:?mysql:5.6??
18.????????name:?mysql??
19.????????env:??       #定义变量,这里定义MySQL数据库的密码
20.????????-?name:?MYSQL_ROOT_PASSWORD??
21.??????????value:?123.com??
22.????????volumeMounts:??
23.????????-?name:?mysql-storage??
24.??????????mountPath:?/var/lib/mysql??        #数据库的数据存放目录,对这个目录做持久化
25.??????volumes:??
26.??????-?name:?mysql-storage??
27.????????persistentVolumeClaim:??
28.??????????claimName:?lbh-pvc??       #指定PVC资源。
**执行yaml文件,查看状态:**
1.[root@master?yaml]#?kubectl?apply?-f?mysql.yaml???
2.deployment.extensions/lbh-mysql?created??
3.[root@master?yaml]#?kubectl?get?pod??
4.NAME?????????????????????????READY???STATUS??????????????RESTARTS???AGE??
5.lbh-mysql-59778fd8d6-xhk7h???0/1?????ContainerCreating???0??????????3m7s??
**这时候容器一直处于容器创建中,可以采用四种方式来排错:
(1)使用kubectl describe命令来查看Pod的详细信息。
(2)使用kubectl logs命令来查看Pod的日志,因为容器没有创建成功,所以不存在日志。
(3)查看本机的message日志
(4)查看kubelet的日志。**
使用kubectl describe命令查看Pod的详细信息:
[root@master yaml]# kubectl describe pod lbh-mysql-59778fd8d6-xhk7h 
最后一条信息:
mount.nfs: mounting 192.168.2.50:/nfsdata/lbh-pv failed, reason given by server: No such file or directory
根据提示得出,在挂载NFS存储目录时,指定的目录不存在。
进行创建目录,再次查看Pod‘状态:

1.[root@master?yaml]#?mkdir?-p?/nfsdata/lbh-pv??
2.[root@master?yaml]#?kubectl?get?pod??
3.NAME?????????????????????????READY???STATUS????RESTARTS???AGE??
4.lbh-mysql-59778fd8d6-xhk7h???1/1?????Running???0??????????12m??

Deployment资源创建成功。

**5.进入MySQL数据库,创建测试数据。**

1.[root@master?yaml]#?kubectl?exec?-it?lbh-mysql-59778fd8d6-xhk7h?--?mysql?-uroot?-p123.com??
2.mysql>?show?databases;??  #查看数据库。
3.+--------------------+??
4.|?Database???????????|??
5.+--------------------+??
6.|?information_schema?|??
7.|?mysql??????????????|??
8.|?performance_schema?|??
9.+--------------------+??
10.3?rows?in?set?(0.01?sec)??
11.??
12.mysql>?create?database?lbh;??    #创建数据库。
13.??
14.mysql>?use?lbh;??        #选择使用的数据库。
15.Database?changed??
16.mysql>?create?table?lbh_id(?id?int(4));??    #创建表。
17.??
18.mysql>?insert?lbh_id?values(9224);??     #在表中插入数据。
19.??
20.mysql>?select?*?from?lbh_id;??           #查看表中所以数据。
21.+------+??
22.|?id???|??
23.+------+??
24.|?9224?|??
25.+------+??
26.1?row?in?set?(0.00?sec)??
27.??
28.mysql>?exit??

在本地查看数据:
1.[root@master?yaml]#?ls?/nfsdata/lbh-pv/??
2.auto.cnf??ibdata1??ib_logfile0??ib_logfile1??lbh??mysql??performance_schema??

数据存在。
**6.查看Pod运行在哪个节点,关闭对应的节点上的kubelet,查看是否重新创建Pod,创建后的Pod数据是否还存在:**

1.[root@master?yaml]#?kubectl?get?pod?-o?wide??
2.NAME?????????????????????????READY???STATUS????RESTARTS???AGE???IP???????????NODE?????NOMINATED?NODE???READINESS?GATES??
3.lbh-mysql-59778fd8d6-xhk7h???1/1?????Running???0??????????26m???10.244.1.4???node01???<none>???????????<none>??
4.??
5.[root@node01?~]#?systemctl?stop?kubelet.service???
6.??
7.[root@master?yaml]#?kubectl?get?pod?-o?wide?-w??
8.lbh-mysql-59778fd8d6-xhk7h???1/1?????Running???0??????????28m???10.244.1.4???node01???<none>???????????<none>??
9.lbh-mysql-59778fd8d6-xhk7h???1/1?????Terminating???0??????????33m???10.244.1.4???node01???<none>???????????<none>??
10.lbh-mysql-59778fd8d6-cf6g4???0/1?????Pending???????0??????????0s????<none>???????<none>???<none>???????????<none>??
11.lbh-mysql-59778fd8d6-cf6g4???0/1?????Pending???????0??????????0s????<none>???????node02???<none>???????????<none>??
12.lbh-mysql-59778fd8d6-cf6g4???0/1?????ContainerCreating???0??????????1s????<none>???????node02???<none>???????????<none>??
13.lbh-mysql-59778fd8d6-cf6g4???1/1?????Running?????????????0??????????2s????10.244.2.9???node02???<none>???????
**Pod重新创建成功,进入Pod查看数据是否存在:**

1.[root@master?yaml]#?kubectl?get?pod?-o?wide??
2.NAME?????????????????????????READY???STATUS????????RESTARTS???AGE???IP????????????NODE?????NOMINATED?NODE???READINESS?GATES??
3.lbh-mysql-59778fd8d6-cf6g4???1/1?????Running???????0??????????12s???10.244.2.10???node02???<none>???????????<none>??
4.lbh-mysql-59778fd8d6-xhk7h???1/1?????Terminating???0??????????44m???10.244.1.4????node01???<none>???????????<none>??
5.??
6.[root@master?yaml]#?kubectl?exec?-it?lbh-mysql-59778fd8d6-cf6g4?--?mysql?-uroot?-p123.com??
7.mysql>?show?databases;??
8.+--------------------+??
9.|?Database???????????|??
10.+--------------------+??
11.|?information_schema?|??
12.|?lbh????????????????|??
13.|?mysql??????????????|??
14.|?performance_schema?|??
15.+--------------------+??
16.4?rows?in?set?(0.01?sec)??
17.??
18.mysql>?use?lbh??
19.Database?changed??
20.mysql>?select?*?from?lbh_id;??
21.+------+??
22.|?id???|??
23.+------+??
24.|?9224?|??
25.+------+??
26.1?row?in?set?(0.00?sec)??
数据还存在,再次查看本地的文件:
1.[root@master?yaml]#?ls?/nfsdata/lbh-pv/??
2.auto.cnf??ibdata1??ib_logfile0??ib_logfile1??lbh??mysql??performance_schema??

MySQL的数据持久化完成。

以上是关于k8s之Mysql实现数据持久化的主要内容,如果未能解决你的问题,请参考以下文章

K8s数据持久化之自动创建PV

k8s之针对有状态服务实现数据持久化

k8s数据持久化之Secret和configMap

k8s数据持久化之statefulset的数据持久化,并自动创建PV与PVC

Kubernetes进阶之hostpath及emptyDir数据卷

Kubernetes之存储卷