虚拟化容器,大数据,DBA,中间件,监控。

k8s持久化存储pv、pvc(七)

11 11月
作者:admin|分类:容器虚拟化

k8s持久化存储pv、pvc

1.PV和PVC持久化存储简介

概念:一个应用肯定会跑多个pod,多个pod里面肯定会存放数据,当一个pod死了,数据也会跟着消失,因此k8s就有了pv的概念,pv就是持久化存储,相当于docker的volume,pvc就是分配pv的容量给pod。

在docker的设计实现中,容器中的数据是临时的,即当容器被销毁时,其中的数据将会小时,如果需要持久化数据,需要使用docker数据卷挂载宿主机上的文件或者目录到容器中,在kubernetes中,当pod重建的时候,数据也会丢失,kubernetes也是通过数据卷来挂载pod数据来实现持久化

在编写pvc配置文件时,如果不指定pv是谁,默认随机分配

2.部署基于nfs的持久化存储

2.1.部署nfs

1)在master节点安装nfs
[root@k8s-master ~]# yum -y install nfs-utils

2)准备nfs存储目录
[root@k8s-master ~]# mkdir /data2/kod_pv -p

3)准备配置文件
[root@k8s-master ~]# vim /etc/exports
/data2 192.168.81.0/24(rw,sync,no_root_squash,no_all_squash)

4)启动服务
[root@k8s-master ~]# systemctl restart rpcbind
[root@k8s-master ~]# systemctl enable rpcbind
[root@k8s-master ~]# systemctl restart nfs
[root@k8s-master ~]# systemctl enable nfs

5)查看nfs信息
[root@k8s-master ~]# showmount -e
Export list for k8s-master:
/data2 192.168.81.0/24

6)在node节点安装nfs客户端
[root@k8s-node1 ~]# yum -y install nfs-utils
[root@k8s-node2 ~]# yum -y install nfs-utils

2.2.编写pv yaml文件

1)编写yaml文件
[root@k8s-master pv]# vim k8s_pv.yaml
apiVersion: v1				//api版本
kind: PersistentVolume			//资源类型是pv
metadata:
  name: kod-pv				//pv的名字
  labels:				//标签,用于pvc指定
    type: kod-pv
spec:					//pv的属性
  capacity:
    storage: 10Gi			//pv的容量
  accessModes:				//pv的访问模式
    - ReadWriteMany				//允许多个客户端读写
  persistentVolumeReclaimPolicy: Recycle		//pvc的回收策略
  nfs:				//用nfs提供存储空间
    path: "/data2/kod_pv"		//nfs存储目录
    server: 192.168.81.210		//nfs地址
    readOnly: false			//关闭只读
	

2)查看pv
状态为Available
[root@k8s-master pv]# kubectl get pv
NAME      CAPACITY   ACCESSMODES   RECLAIMPOLICY   STATUS      CLAIM     REASON    AGE
kod-pv    10Gi       RWX           Recycle         Available                       18s

3)查看pv详细信息
[root@k8s-master pv]# kubectl describe pv kod-pv 
Name:		kod-pv
Labels:		type=kod-pv
StorageClass:	
Status:		Available
Claim:		
Reclaim Policy:	Recycle
Access Modes:	RWX
Capacity:	10Gi
Message:	
Source:
    Type:	NFS (an NFS mount that lasts the lifetime of a pod)
    Server:	192.168.81.210
    Path:	/data2/kod_pv
    ReadOnly:	false
No events.

在这里插入图片描述

2.3.编写pvc yaml文件

1)编写pvc yaml文件
[root@k8s-master pv]# vim k8s_pvc.yaml 
apiVersion: v1							//api版本
kind: PersistentVolumeClaim					//资源类型是pvc
metadata:					
  name: kod-pvc				//pvc的名称
spec:						
  accessModes:					//pvc的权限
    - ReadWriteMany				//多个客户端可读可写
  resources:
    requests:
      storage: 5Gi				//pvc的资源限制为5G

2)创建pvc
[root@k8s-master pv]# kubectl create -f k8s_pvc.yaml 

3)查看pvc的状态
volume对应刚刚创建的kod-pv,在yaml文件中不指定那个pv默认随机分配
[root@k8s-master pv]# kubectl get pvc
NAME      STATUS    VOLUME    CAPACITY   ACCESSMODES   AGE
kod-pvc   Bound     kod-pv    10Gi       RWX           14s

4)查看pv的状态
现在pv的状态为bound
[root@k8s-master pv]# kubectl get pv
NAME      CAPACITY   ACCESSMODES   RECLAIMPOLICY   STATUS    CLAIM             REASON    AGE
kod-pv    10Gi       RWX           Recycle         Bound     default/kod-pvc             10m

5)查看pvc的详细
[root@k8s-master pv]# kubectl describe pvc kod-pvc 
Name:		kod-pvc
Namespace:	default
StorageClass:	
Status:		Bound
Volume:		kod-pv			//pv的是谁
Labels:		<none>
Capacity:	10Gi
Access Modes:	RWX
No events.

在这里插入图片描述

2.4.指定pvc使用哪个pv

实现思路,可以用标签进行绑定实现

2.4.1.创建多个pv

1)编写yaml文件
[root@k8s-master pv]# vi k8s_pv.yaml 
apiVersion: v1
kind: PersistentVolume
metadata:
  name: kod-pv01
  labels:
    pv: kod-pv01				//定义pv标签,用于pvc关联
    type: kod-pv01
spec:
  capacity:
    storage: 10Gi
  accessModes:
    - ReadWriteMany
  persistentVolumeReclaimPolicy: Recycle
  nfs:
    path: "/data2/kod_pv01"
    server: 192.168.81.210
    readOnly: false
---
apiVersion: v1
kind: PersistentVolume
metadata:
  name: kod-pv02
  labels:
    pv: kod-pv02				//定义pv标签,用于pvc关联
    type: kod-pv02
spec:
  capacity:
    storage: 10Gi
  accessModes:
    - ReadWriteMany
  persistentVolumeReclaimPolicy: Recycle
  nfs:
    path: "/data2/kod_pv02"
    server: 192.168.81.210
    readOnly: false
 
2)创建
[root@k8s-master pv_kz]# kubectl create -f k8x_pv.yaml 
persistentvolume "kod-pv01" created
persistentvolume "kod-pv02" created

3)查看
[root@k8s-master pv_kz]# kubectl get pv
NAME       CAPACITY   ACCESSMODES   RECLAIMPOLICY   STATUS      CLAIM             REASON    AGE
kod-pv     10Gi       RWX           Recycle         Bound       default/kod-pvc             30m
kod-pv01   10Gi       RWX           Recycle         Available                               8s
kod-pv02   10Gi       RWX           Recycle         Available                               8s

最主要的地方

在这里插入图片描述

2.4.2.创建多个pvc分别指定不同的pv

[root@k8s-master pv_kz]# vim k8x_pvc.yaml 

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: kod-pvc01
spec:
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 5Gi
  selector:
    matchLabels:
      pv: kod-pv01				//定义pv标签,与pv进行关联

---

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: kod-pvc02
spec:
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 5Gi
  selector:
    matchLabels:
      pv: kod-pv02			//定义pv标签,与pv进行关联

2.4.3.验证

会发现pvc01关联pv01,pvc02关联pv02

在这里插入图片描述

6.5.删除pv、pvc

pv
[root@k8s-master pv_kz]# kubectl delete pv kod-pv01 kod-pv02
persistentvolume "kod-pv01" deleted
persistentvolume "kod-pv02" deleted

pvc
[root@k8s-master pv_kz]# kubectl delete pvc kod-pvc01
persistentvolumeclaim "kod-pvc01" deleted

6.6.将pvc挂载到应用上

1)挂载pv
[root@k8s-master kod]# vim kod_deployment.yaml 
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: kod-deploy
spec:
  replicas: 5
  template:
    metadata:
      labels:
        app: kodnginx
    spec:
      containers:
      - name: nginxkod
        image: 192.168.81.240/k8s/kodexplorer:v4
        ports:
        - containerPort: 80
        volumeMounts:				//挂载pv
        - name: kod-data				//挂载资源的名称,随便起
          mountPath: /web/kodexporer/data			//挂载目录
      volumes:
      - name: kod-data				//资源名称和volumemounts中的name对应
        persistentVolumeClaim:
          claimName: kod-pvc01			//pvc的名称

2)更新配置
[root@k8s-master kod]# kubectl apply -f kod_deployment.yaml 
deployment "kod-deploy" configured

3)由于挂载问题导致没权限,进入容器修改权限
[root@k8s-master kod]# kubectl exec -it kod-deploy-2316599975-1z3l3 /bin/bash
[root@kod-deploy-2316599975-1z3l3 web]# chmod -R 777 /web/

[root@k8s-master kod]# kubectl exec -it kod-deploy-2316599975-9cxr1 /bin/bash
[root@kod-deploy-2316599975-9cxr1 kodexporer]# chmod -R 777 /web/

[root@k8s-master kod]# kubectl exec -it kod-deploy-2316599975-cfb1d /bin/bash
[root@kod-deploy-2316599975-cfb1d kodexporer]# chmod -R 777 /web/                     

[root@k8s-master kod]# kubectl exec -it kod-deploy-2316599975-dbhwb /bin/bash
[root@kod-deploy-2316599975-dbhwb kodexporer]# chmod -R 777 /web/

[root@k8s-master kod]# kubectl exec -it kod-deploy-2316599975-hdh2t /bin/bash
[root@kod-deploy-2316599975-hdh2t kodexporer]# chmod -R 777 /web/                     

不使用pv之前,上传的数据,当一个pod死了之后数据也会消失,使用pv之后pod死了数据也不会丢

浏览354 评论0
返回
目录
返回
首页
k8s部署kodexplorer(八) k8s常用资源之svc资源(五)