011.Kubernetes使用共享存储持久化数据
本次实验是以前面的实验为基础,使用的是模拟使用kubernetes集群部署一个企业版的wordpress为实例进行研究学习,主要的过程如下:
1.mysql deployment部署, wordpress deployment部署, wordpress连接mysql时,mysql的 pod ip易变
2.为mysql创建 service,申请固定 service lp
3. wordpress外部可访问,使用 node port类型的 service
4. nodeport类型的 service需要映射端口,不是很优雅
5.使用 ingressl
6.mysq1的pod旦被重新调度,数据就会丢失
7. hostpath将pod数据挂载至本地,然后使用标签选择器将mysq1lpod固定在一个节点上
8.共享存储以持久化数据,即使po挂掉,被重新拉起,也能自动挂载存储
本次实验解决使用共享存储数据持久化的问题实验使用NFS作为共享存储
一 使用NFS共享存储
1.1 安装NFS
再server3和server1上安装nfs
[ ~]# yum -y install nfs-utils
[ ~]# yum -y install nfs-utils
[ ~]# vi /etc/exports
/data *(rw,no_root_squash)
[ ~]# systemctl restart nfs
[ ~]# systemctl status nfs
[ ~]# showmount -e
Export list for docker-server3: /data *
1.2 到server1上进行挂载测试
[ ~]# mount -t nfs 192.168.132.133:/data /mnt
[ ~]# df -hT
192.168.132.133:/data nfs4 47G 7.3G 40G 16% /mnt
[ ~]# umount /mnt/
nfs配置成功
1.3 数据库使用共享存储挂载卷
[ ~]# mkdir /data/mysql
[ deployment]# vi mysql-deployment.yaml
apiVersion: apps/v1 kind: Deployment metadata: name: mysql namespace: default spec: replicas: 1 selector: matchLabels: app: mysql template: metadata: labels: app: mysql spec: volumes: - name: mydata nfs: server: 192.168.132.133 path: /data/mysql containers: - name: mysql image: mysql:5.7 volumeMounts: - name: mydata mountPath: /var/lib/mysql ports: - containerPort: 3306 env: - name: MYSQL_ROOT_PASSWORD value: RedHat123 - name: MYSQL_DATABASE value: wordpress
[ deployment]# kubectl get pods -o wide
mysql-857589b5d4-j65d2 1/1 Running 0 20s 10.244.2.20 192.168.132.133 <none>
[ ~]# ll /data/mysql
-rw-r----- 1 polkitd ssh_keys 56 Jan 17 22:29 auto.cnf -rw------- 1 polkitd ssh_keys 1676 Jan 17 22:29 ca-key.pem -rw-r--r-- 1 polkitd ssh_keys 1112 Jan 17 22:29 ca.pem -rw-r--r-- 1 polkitd ssh_keys 1112 Jan 17 22:29 client-cert.pem -rw------- 1 polkitd ssh_keys 1680 Jan 17 22:29 client-key.pem -rw-r----- 1 polkitd ssh_keys 1346 Jan 17 22:29 ib_buffer_pool -rw-r----- 1 polkitd ssh_keys 79691776 Jan 17 22:29 ibdata1 -rw-r----- 1 polkitd ssh_keys 50331648 Jan 17 22:29 ib_logfile0 -rw-r----- 1 polkitd ssh_keys 50331648 Jan 17 22:29 ib_logfile1 -rw-r----- 1 polkitd ssh_keys 12582912 Jan 17 22:30 ibtmp1 drwxr-x--- 2 polkitd ssh_keys 4096 Jan 17 22:29 mysql drwxr-x--- 2 polkitd ssh_keys 8192 Jan 17 22:29 performance_schema -rw------- 1 polkitd ssh_keys 1680 Jan 17 22:29 private_key.pem -rw-r--r-- 1 polkitd ssh_keys 452 Jan 17 22:29 public_key.pem -rw-r--r-- 1 polkitd ssh_keys 1112 Jan 17 22:29 server-cert.pem -rw------- 1 polkitd ssh_keys 1680 Jan 17 22:29 server-key.pem drwxr-x--- 2 polkitd ssh_keys 8192 Jan 17 22:29 sys drwxr-x--- 2 polkitd ssh_keys 20 Jan 17 22:29 wordpress
这样,房mysql的pod是被删除的时候,数据不会丢失
这种方式,可以满足效果,但是当直接对接不同的存储,需要使用不同的对接格式,对于K8S而言,不方便,所以使用PV的格式挂载,即不管使用哪一种存储方式,最终创建一个PV出来,然后K8S直接挂载PV,不用关心底层是哪一种存储
二 持久化卷(PV)
2.1 PV及PVC介绍
PersistentVolume (PV) 和 PersistentVolumeClaim (PVC) 提供了方便的持久化卷:PV 提供网络存储资源,而 PVC 请求存储资源。这样,设置持久化的工作流包括配置底层文件系统或者云数据卷、创建持久性数据卷、最后创建 PVC 来将 Pod 跟数据卷关联起来。PV 和 PVC 可以将 pod 和数据卷解耦,pod 不需要知道确切的文件系统或者支持它的持久化引擎。
PersistentVolume(PV)是集群之中的一块网络存储。跟 Node 一样,也是集群的资源。PV 跟 Volume (卷) 类似,不过会有独立于 Pod 的生命周期
1.为了屏蔽 kubernetes对底层存储的差异,有了个申明称之为pv
2. pod在创建时,只需要声明需要一个pV即可,这
3. 创建pv:
- 大小10G
- 独享还是共享
- 从哪种存储申请
4. 创建pvc:
- 大小20G
- 独享还是共享
2.2 创建一个PV
基于NFS创建PV
[ yamls]# mkdir /yamls/storage
[ yamls]# cd /yamls/storage
[ storage]# vim pv1.yaml
apiVersion: v1 kind: PersistentVolume metadata: name: pv1 spec: capacity: storage: 1G accessModes: - ReadWriteMany nfs: server: 192.168.132.133 path: "/data"
[ storage]# kubectl apply -f pv1.yaml
[ storage]# kubectl get pv
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE pv1 1G RWX Retain Available 23s
RECLAIM POLICY回收策略
PV的回收策略(persistentVolumeReclaimPolicy,即PVC释放卷的时候PV该如何操作)也有三种
- Retain,不清理, 保留Volume(需要手动清理)
- Recycle,删除数据,即rm -rf /thevolume/*(只有NFS和HostPath支持)
- Delete,删除存储资源,比如删除AWS EBS卷(只有AWS EBS, GCE PD, Azure Disk和Cinder支持)
2.3 创建一个PVC
[ storage]# vim pvc1.yaml
kind: PersistentVolumeClaim apiVersion: v1 metadata: name: pvc1 spec: accessModes: - ReadWriteMany resources: requests: storage: 1Gi
[ storage]# kubectl apply -f pvc1.yaml
persistentvolumeclaim/pvc1 created
[ storage]# kubectl get pvc -n kube-system
No resources found in kube-system namespace. #pvc只有在创建的命名空间看到
[ storage]# kubectl get pv -n kube-system
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE pv1 1G RWX Retain Available 8m56s
[ storage]# kubectl get pvc
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE pvc1 Pending 2m8s
[ storage]# kubectl describe pvc pvc1
Name: pvc1 Namespace: default StorageClass: Status: Pending Volume: Labels: <none> Annotations: kubectl.kubernetes.io/last-applied-configuration: {"apiVersion":"v1","kind":"PersistentVolumeClaim","metadata":{"annotations":{},"name":"pvc1","namespace":"default"},"spec":{"accessModes":... Finalizers: [kubernetes.io/pvc-protection] Capacity: Access Modes: VolumeMode: Filesystem Mounted By: <none> Events: Type Reason Age From Message ---- ------ ---- ---- ------- Normal FailedBinding 7s (x15 over 3m22s) persistentvolume-controller no persistent volumes available for this claim and no storage class is set #没有完全满足的pv可以绑定
这是因为pv是1G,PVC申请的是1Gi,修改如下
[ storage]# vi pvc1.yaml
kind: PersistentVolumeClaim apiVersion: v1 metadata: name: pvc1 spec: accessModes: - ReadWriteMany resources: requests: storage: 1G
pvc资源只能删除重建
[ storage]# kubectl delete pvc pvc1
persistentvolumeclaim "pvc1" deleted
[ storage]# kubectl apply -f pvc1.yaml
persistentvolumeclaim/pvc1 created
[ storage]# kubectl get pvc
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE pvc1 Bound pv1 1G RWX 5s
已经绑定
[ storage]# kubectl get pv
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE pv1 1G RWX Retain Bound default/pvc1 16m
2.4 mysql使用pvc绑定
[ ~]# mkdir /data/mysql11
apiVersion: v1 kind: PersistentVolume metadata: name: pv-rwo.yaml spec: capacity: storage: 10G accessModes: - ReadWriteOnce nfs: server: 192.168.132.133 path: "/data/mysql11"
[ storage]# kubectl apply -f pv-rwo.yaml
[ storage]# vi pvc-rwo.yaml
kind: PersistentVolumeClaim apiVersion: v1 metadata: name: pvc-rwo spec: accessModes: - ReadWriteOnce resources: requests: storage: 10G
[ storage]# kubectl get pvc
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE pvc-rwo Bound pv-rwo.yaml 10G RWO 6s pvc1 Bound pv1 1G RWX 10m
[ storage]# kubectl get pv
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE pv-rwo.yaml 10G RWO Retain Bound default/pvc-rwo 101s pv1 1G RWX Retain Bound default/pvc1 24m
挂载到数据库
[ storage]# vim /yamls/deployment/mysql-deployment.yaml
apiVersion: apps/v1 kind: Deployment metadata: name: mysql namespace: default spec: replicas: 1 selector: matchLabels: app: mysql template: metadata: labels: app: mysql spec: # volumes: # - name: mydata # nfs: # server: 192.168.132.133 # path: /data/mysql volumes: - name: mydata persistentVolumeClaim: claimName: pvc-rwo containers: - name: mysql image: mysql:5.7 volumeMounts: - name: mydata mountPath: /var/lib/mysql ports: - containerPort: 3306 env: - name: MYSQL_ROOT_PASSWORD value: RedHat123 - name: MYSQL_DATABASE value: wordpress
[ storage]# kubectl apply -f /yamls/deployment/mysql-deployment.yaml
[ ~]# ll /data/mysql11
total 188484 -rw-r----- 1 polkitd ssh_keys 56 Jan 18 00:46 auto.cnf -rw------- 1 polkitd ssh_keys 1676 Jan 18 00:46 ca-key.pem -rw-r--r-- 1 polkitd ssh_keys 1112 Jan 18 00:46 ca.pem -rw-r--r-- 1 polkitd ssh_keys 1112 Jan 18 00:46 client-cert.pem -rw------- 1 polkitd ssh_keys 1680 Jan 18 00:46 client-key.pem -rw-r----- 1 polkitd ssh_keys 1346 Jan 18 00:46 ib_buffer_pool -rw-r----- 1 polkitd ssh_keys 79691776 Jan 18 00:46 ibdata1 -rw-r----- 1 polkitd ssh_keys 50331648 Jan 18 00:46 ib_logfile0 -rw-r----- 1 polkitd ssh_keys 50331648 Jan 18 00:46 ib_logfile1 -rw-r----- 1 polkitd ssh_keys 12582912 Jan 18 00:46 ibtmp1 drwxr-x--- 2 polkitd ssh_keys 4096 Jan 18 00:46 mysql drwxr-x--- 2 polkitd ssh_keys 8192 Jan 18 00:46 performance_schema -rw------- 1 polkitd ssh_keys 1680 Jan 18 00:46 private_key.pem -rw-r--r-- 1 polkitd ssh_keys 452 Jan 18 00:46 public_key.pem -rw-r--r-- 1 polkitd ssh_keys 1112 Jan 18 00:46 server-cert.pem -rw------- 1 polkitd ssh_keys 1680 Jan 18 00:46 server-key.pem drwxr-x--- 2 polkitd ssh_keys 8192 Jan 18 00:46 sys drwxr-x--- 2 polkitd ssh_keys 20 Jan 18 00:46 wordpress
已经重建数据,则PV和PVC简单学习到这里
博主声明:本文的内容来源主要来自誉天教育晏威老师,由本人实验完成操作验证,需要的博友请联系誉天教育(http://www.yutianedu.com/),获得官方同意或者晏老师(https://www.cnblogs.com/breezey/)本人同意即可转载,谢谢!