23,k8s 之PV,PVC

Posted k8s-pod

tags:

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

PersistentVolume:
	PersistenVolume(PV):对存储资源创建和使用的抽象,使得存储作为集群中的资源管理
		静态
		动态
	PersistentVolumeClaim(PVC):让用户不需要关心具体的Volume实现细节
	
搭建nfs:
		客户端和服务端都安装:
			yum install -y nfs-utils
		服务端执行:
			[root@centos7 ~]# cat /etc/exports
			/data/nfs/wwwroot *(rw,no_root_squash)
			[root@centos7 ~]#
			[root@centos7 ~]# systemctl start nfs
			
		

	
	
pv 静态供给:
[root@centos7 ~]# cat pv.yaml 
apiVersion: v1
kind: PersistentVolume
metadata:
  name: my-pv
spec:
  capacity:
    storage: 5Gi
  accessModes:
    - ReadWriteMany
  nfs:
    path: /data/nfs/wwwroot
    server: 192.168.0.14
[root@centos7 ~]# 
[root@centos7 ~]# kubectl apply -f pv.yaml 
persistentvolume/my-pv created
[root@centos7 ~]# 
[root@centos7 ~]# kubectl get pv
NAME    CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS      CLAIM   STORAGECLASS   REASON   AGE
my-pv   5Gi        RWX            Retain           Available                                   8s
[root@centos7 ~]# 

[root@centos7 ~]# cat pod-pvc.yaml 
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx
        volumeMounts:
        - name: wwwroot
          mountPath: /usr/share/nginx/html
        ports:
        - containerPort: 80
      volumes:
      - name: wwwroot
        persistentVolumeClaim:
          claimName: my-pvc

---

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: my-pvc
spec:
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 5Gi
[root@centos7 ~]# 
[root@centos7 ~]# 
[root@centos7 ~]# kubectl apply -f pod-pvc.yaml 
deployment.apps/nginx-deployment created
persistentvolumeclaim/my-pvc unchanged
[root@centos7 ~]# 
[root@centos7 ~]# kubectl get pv,pvc
NAME                     CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS   CLAIM            STORAGECLASS   REASON   AGE
persistentvolume/my-pv   5Gi        RWX            Retain           Bound    default/my-pvc                           3m

NAME                           STATUS   VOLUME   CAPACITY   ACCESS MODES   STORAGECLASS   AGE
persistentvolumeclaim/my-pvc   Bound    my-pv    5Gi        RWX                           2m13s
[root@centos7 ~]# 
[root@centos7 ~]# 
[root@centos7 ~]# kubectl get pods
NAME                               READY   STATUS    RESTARTS   AGE
nginx-deployment-bc96745d9-2mzqx   1/1     Running   0          2m33s
nginx-deployment-bc96745d9-68lmm   1/1     Running   0          2m33s
nginx-deployment-bc96745d9-khlpm   1/1     Running   0          2m33s
[root@centos7 ~]# kubectl exec -it nginx-deployment-bc96745d9-2mzqx bash
root@nginx-deployment-bc96745d9-2mzqx:/# 
root@nginx-deployment-bc96745d9-2mzqx:/# ls /usr/share/nginx/html/
root@nginx-deployment-bc96745d9-2mzqx:/# ls /usr/share/nginx/html/
index.html
root@nginx-deployment-bc96745d9-2mzqx:/# 





pv 动态供给:
	Nfs 不支持动态供给,所以要先安装nfs的插件。
	Dynamic Provisioning机制工作的核心在于StorageClass的API对象。
	StorageClass声明存储插件,用于自动创建PV。



  

以上是关于23,k8s 之PV,PVC的主要内容,如果未能解决你的问题,请参考以下文章

K8S存储 之 PV 与 PVC

K8S存储 之 PV 与 PVC

K8S存储 之 PV 与 PVC

k8s之PV以及PVC

持久化存储之 PV、PVC、StorageClass

k8s之StorageClass