Kubernets PV PVC詳解

Kubernets PV PVC

1. Volume

https://kubernetes.io/docs/concepts/storage/volumes/
  • Kubernetes中的Volume提供了在容器中掛載外部存儲的能力
  • Pod需要設置捲來源(spec.volume)和掛載點(spec.containers.volumeMounts)兩個信息後才可以使用相應的Volume

1.1 本地卷emptyDir

創建一個空卷,掛載到Pod中的容器。Pod刪除該卷也會被刪除。應用場景:Pod中容器之間數據共享

#(spec.volumes)
volumes:
- name: data
emptyDir: {}

1.2 本地卷hostPath

掛載Node文件系統上文件或者目錄到Pod中的容器。應用場景:Pod中容器需要訪問宿主機文件

[root@k8s-master01 demo2]# cat hostpath.yaml 
apiVersion: v1
kind: Pod
metadata:
name: my-busybox
spec:
containers:
- name: busybox
image: busybox
args:
- /bin/sh
- -c
- sleep 36000
volumeMounts:

- name: data
mountPath: /data
volumes:
- name: data
hostPath:
path: /tmp
type: Directory

Value

Behavior

Empty

string (default) is for backward compatibility, which means that no checks will be performed before mounting the hostPath volume. DirectoryOrCreate If nothing exists at the given path, an empty directory will be created there as needed with permission set to 0755, having the same group and ownership with Kubelet. Directory

A directory must exist at the given path

FileOrCreate

If nothing exists at the given path, an empty file will be created there as needed with permission set to 0644, having the same group and ownership with Kubelet. File

A file must exist at the given path

Socket

A UNIX socket must exist at the given path

CharDevice

A character device must exist at the given path

BlockDevice

A block device must exist at the given path

1.3 網絡卷nfs

nfs網絡卷不像上面emptydir,pod刪除,數據就沒了。我們在192.168.186.143 機器上安裝一個nfs,作為nfs server。

volumes:
- name: wwwroot
nfs:
server: 192.168.186.143
path: /caimengzhi/kubernetes

2. PersistentVolume 靜態

​ 管理存儲和管理計算有著明顯的不同。PersistentVolume子系統給用戶和管理員提供了一套API,從而抽象出存儲是如何提供和消耗的細節。在這裡,我們介紹兩種新的API資源:PersistentVolume(簡稱PV)和PersistentVolumeClaim(簡稱PVC)。

​ PersistentVolume(持久卷,簡稱PV)是集群內,由管理員提供的網絡存儲的一部分。就像集群中的節點一樣,PV也是集群中的一種資源。它也像Volume一樣,是一種volume插件,但是它的生命週期卻是和使用它的Pod相互獨立的。PV這個API對象,捕獲了諸如NFS、ISCSI、或其他雲存儲系統的實現細節。

​ PersistentVolumeClaim(持久卷聲明,簡稱PVC)是用戶的一種存儲請求。它和Pod類似,Pod消耗Node資源,而PVC消耗PV資源。Pod能夠請求特定的資源(如CPU和內存)。PVC能夠請求指定的大小和訪問的模式(可以被映射為一次讀寫或者多次只讀)。PVC允許用戶消耗抽象的存儲資源,用戶也經常需要各種屬性(如性能)的PV。集群管理員需要提供各種各樣、不同大小、不同訪問模式的PV,而不用向用戶暴露這些volume如何實現的細節。因為這種需求,就催生出一種StorageClass資源。

​ StorageClass提供了一種方式,使得管理員能夠描述他提供的存儲的等級。集群管理員可以將不同的等級映射到不同的服務等級、不同的後端策略。

1、創建存儲端

#創建nfs服務端
yum install -y nfs-utils
# mkdir v{1,2,3,4,5}
# ls
v1 v2 v3 v4 v5
#chmod 777 v{1,2,3,4,5}
# pwd
/data/k8s_volumes
# cat /etc/exports
/data/k8s_volumes/v1 172.22.108.0/24(rw,no_root_squash)
/data/k8s_volumes/v2 172.22.108.0/24(rw,no_root_squash)
/data/k8s_volumes/v3 172.22.108.0/24(rw,no_root_squash)
/data/k8s_volumes/v4 172.22.108.0/24(rw,no_root_squash)
/data/k8s_volumes/v5 172.22.108.0/24(rw,no_root_squash)
[k8s_volumes]# exportfs -arv
exporting 172.22.108.0/24:/data/k8s_volumes/v5
exporting 172.22.108.0/24:/data/k8s_volumes/v4
exporting 172.22.108.0/24:/data/k8s_volumes/v3
exporting 172.22.108.0/24:/data/k8s_volumes/v2
exporting 172.22.108.0/24:/data/k8s_volumes/v1

2、創建pv

# vim pv-daemon.yaml
apiVersion: v1
kind: PersistentVolume
metadata:
name: pv001
labels:
name: pv001
spec:
nfs:
path: /data/volumes/v1
server: 10.100.106.15 #nfs server的ip
accessModes: ["ReadWriteOnce","ReadOnlyMany","ReadWriteMany"]
capacity:
storage: 10Gi

3、創建pvc

[root@k8s-master01 demo2]# cat pvc-daemon.yaml 
apiVersion: v1
kind: PersistentVolumeClaim
metadata:

name: mypvc
namespace: default
spec:
accessModes: ["ReadWriteOnce","ReadOnlyMany","ReadWriteMany"]
resources:
requests:
storage: 9Gi
---
apiVersion: v1
kind: Pod
metadata:
name: caimengzhi-pv-pvc-nfs-pod
spec:
containers:
- name: nginx
image: nginx
volumeMounts:
- mountPath: "/usr/share/nginx/html/"
name: wwwroot
volumes:
- name: wwwroot
persistentVolumeClaim:
claimName: mypvc

備註:

雖然我們使用了pvc,其實還是關聯了之前創建的pv,也就是共享了nfs數據一旦數據有變更,其他都跟著變動。

以上是靜態的供給,也就是先創建pv,然後pvc關聯pv,最後容器服務關聯pvc,接下來我們講述另一種pvc自動創建pv。

故障

 Warning FailedMount 89s kubelet, 172.22.108.12 MountVolume.SetUp failed for volume "pv002-prom" : mount failed: exit status 32
Mounting command: systemd-run
Mounting arguments: --description=Kubernetes transient mount for /var/lib/kubelet/pods/97d1800c-c20a-48c2-a005-1352ec2cb598/volumes/kubernetes.io~nfs/pv002-prom --scope -- mount -t nfs 10.100.106.15:/data/k8s_volumes/v2 /var/lib/kubelet/pods/97d1800c-c20a-48c2-a005-1352ec2cb598/volumes/kubernetes.io~nfs/pv002-prom
Output: Running scope as unit run-149025.scope.
mount: wrong fs type, bad option, bad superblock on 10.100.106.15:/data/k8s_volumes/v2,
missing codepage or helper program, or other error
(for several filesystems (e.g. nfs, cifs) you might
need a /sbin/mount.<type> helper program)

In some cases useful info is found in syslog - try
dmesg | tail or so.
出現故障:
yum -y install nfs-utils
/<type>

3. PersistentVolume 動態


分享到:


相關文章: