k8s學習筆記之五:volume,PV ,PVC


一,volume

Ⅰ、emptyDir(pod消失就消失)

簡介:emptyDir Volume是在Pod分配到node時創建的,從他的名稱就能看得出來,它的出事內容為空,並且無需指定宿主機上對應的目錄文件,

因為這是kubernetes自動分配的一個目錄,當Pod從node上移除時,emptyDir中的數據也會被永久刪除emptyDir的用途有:

例子1:單獨在一台容器掛載目錄

apiVersion: v1
kind: Pod
metadata:
  name: pod-demo
spec:
  containers:
  - name: myapp
    image: ikubernetes/myapp:v1
    volumeMounts:
    - name: cache-volume
      mountPath: /cache     //容器內部掛載目錄
  volumes: 
   - name: cache-volume  
     emptyDir: {}  #volume類型,默認不限制使用空間

 例子2、同一個pod下掛載兩個目錄實現文件共享

apiVersion: v1
kind: Pod
metadata:
  name: pod-demo
spec:
  containers:
   - name: myapp
     image: ikubernetes/myapp:v1
     volumeMounts:
       - name: cache-volume
         mountPath: /cache  //掛載目錄
   - name: busybox
     image: busybox:latest
     imagePullPolicy: IfNotPresent
     command: ["/bin/sh","-c","sleep 6000s"]
     volumeMounts:
       - name: cache-volume
         mountPath: /test     //掛載目錄
  volumes:
    - name: cache-volume
      emptyDir: {}

Ⅱ、hostPath(節點級存儲,生命周期和node相同)

簡介:允許掛載 Node 上的文件系統到 Pod 里面去。 如果 Pod 需要使用 Node 上的文件, 可以使用 hostPath

1、創建pod

apiVersion: v1
kind: Pod
metadata:
  name: test-pd
  namespace: wuchang
spec:
  containers:
  - image: ikubernetes/myapp:v1
    name: test-container
    volumeMounts:
    - mountPath: /test-pd
      name: test-volume
  volumes:
  - name: test-volume
    hostPath:
      path: /data

2、在cache目錄下創建兩個文件

 3、查看mnt目錄下,

二、StorageClass

簡介:在一個大規模的Kubernetes集群里,可能有成千上萬個PVC,這就意味着運維人員必須實現創建出這個多個PV,此外,隨着項目的需要,會有新的PVC不斷被提交,那么運維人員就

需要不斷的添加新的,滿足要求的PV,否則新的Pod就會因為PVC綁定不到PV而導致創建失敗.而且通過 PVC 請求到一定的存儲空間也很有可能不足以滿足應用對於存儲設備的各種需求

而且不同的應用程序對於存儲性能的要求可能也不盡相同,比如讀寫速度、並發性能等,為了解決這一問題,Kubernetes 又為我們引入了一個新的資源對象:StorageClass,通過

StorageClass 的定義,管理員可以將存儲資源定義為某種類型的資源,比如快速存儲、慢速存儲等,用戶根據 StorageClass 的描述就可以非常直觀的知道各種存儲資源的具體特性了,

這樣就可以根據應用的特性去申請合適的存儲資源了。

本案例基於StorageClass+NFS進行演示

1、創建rbac授權

[root@k8s-master storageClass]# cat rbac.yaml 
apiVersion: v1
kind: ServiceAccount
metadata:
  name: nfs-client-provisioner

---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
  name: nfs-client-provisioner-clusterrole
rules:
  - apiGroups: [""]
    resources: ["persistentvolumes"]
    verbs: ["get", "list", "watch", "create", "delete"]
  - apiGroups: [""]
    resources: ["persistentvolumeclaims"]
    verbs: ["get", "list", "watch", "update"]
  - apiGroups: ["storage.k8s.io"]
    resources: ["storageclasses"]
    verbs: ["get", "list", "watch"]
  - apiGroups: [""]
    resources: ["events"]
    verbs: ["list", "watch", "create", "update", "patch"]
  - apiGroups: [""]
    resources: ["endpoints"]
    verbs: ["create", "delete", "get", "list", "watch", "patch", "update"]

---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  name: nfs-client-provisioner-clusterrolebinding
subjects:
- kind: ServiceAccount
  name: nfs-client-provisioner
  namespace: default
roleRef:
  kind: ClusterRole
  name: nfs-client-provisioner-clusterrole
  apiGroup: rbac.authorization.k8s.io

2、創建provinsor.yaml

[root@k8s-master storageClass]# cat provisioner.yaml 
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nfs-client-prosioner
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nfs-client-prosioner
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        app: nfs-client-prosioner
    spec:
      serviceAccountName: nfs-client-provisioner
      containers:
      - name: nfs-client-prosioner
        image: registry.cn-hangzhou.aliyuncs.com/rookieops/nfs-client-provisioner:v0.1
        imagePullPolicy: IfNotPresent
        volumeMounts:
        - name: nfs-client-root
          mountPath: /data/pv
        env:
        - name: PROVISIONER_NAME
          value: rookieops/nfs
        - name: NFS_SERVER
          value: 192.168.48.210
        - name: NFS_PATH
          value: /nfsdata/v120
      volumes:
      - name: nfs-client-root
        nfs:
          server: 192.168.48.210
          path: /nfsdata/v120

3、創建stroageclass.yaml

[root@k8s-master storageClass]# cat storageclass.yaml 
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
  name: nfs-client-storageclass
provisioner: rookieops/nfs

4、查看pod情況

[root@k8s-master storageClass]# kubectl get pods
NAME                                      READY   STATUS    RESTARTS   AGE
nfs-client-provisioner-66d6f8d7b6-sm9m9   1/1     Running   0          117m
pod-demo                                  1/1     Running   0          171m

4、創建測試pod,檢查是否創建成功

 

三、PV,以NFS為例(永久存儲,生命周期與NFS server相同)

Ⅰ、在非K8S機器安裝nfs服務器

[root@localhost etc]# yum -y install nfs*
[root@localhost etc]# mkdir /nfsdata
[root@localhost etc]# chmod 666 /nfsdata/
[root@localhost etc]# cat /etc/exports
/nfsdata *(rw,no_root_squash,no_all_squash,sync)  //任意客戶端可連接
/home/xubu/share/   192.168.1.1(rw,sync,no_root_squash,no_subtree_check) //指定的 [root@localhost etc]# systemctl start nfs [root@localhost etc]# systemctl start rpcbind

Ⅱ、在所有K8S節點安裝nfs客戶端

[root@k8s-master pod]# yum -y install nfs-utils rpcbind
[root@k8s-node1 mnt]# showmount -e 192.168.254.120

報錯:clnt_create: RPC: Port mapper failure - Unable to receive: errno 113 (No route to host)

解決:該問題為nfs服務器未關閉防火牆,將防火牆關閉即可

Ⅲ、測試新建目錄是否能正常掛載

[root@k8s-master pod]# mkdir /test
[root@k8s-master pod]# mount -t nfs 192.168.254.120:/nfsdata /test/

Ⅳ、創建pv

apiVersion: v1
kind: PersistentVolume
metadata:
  name: pv-nfs
spec:
  capacity:
   storage: 1Gi
  accessModes:
   - ReadWriteOnce
  persistentVolumeReclaimPolicy: Retain
  storageClassName: nfs
  nfs:
    path: /nfsdata
    server: 192.168.254.120

① capacity 指定 PV 的容量為 1G。
② accessModes 指定訪問模式為 ReadWriteOnce,支持的訪問模式有:
     ReadWriteOnce – PV 能以 read-write 模式 mount 到單個節點。
     ReadOnlyMany – PV 能以 read-only 模式 mount 到多個節點。
     ReadWriteMany – PV 能以 read-write 模式 mount 到多個節點。
③ persistentVolumeReclaimPolicy 指定當 PV 的回收策略為 Recycle,支持的策略有:
     Retain – 需要管理員手工回收。
     Recycle – 如果pvc刪除了,創建的文件也會刪除。
     Delete – 刪除 Storage Provider 上的對應存儲資源,例如 AWS EBS、GCE PD、Azure Disk、OpenStack Cinder Volume 等。
④ storageClassName 指定 PV 的 class 為 nfs。相當於為 PV 設置了一個分類,PVC 可以指定 class 申請相應 class 的 PV。
⑤ 指定 PV 在 NFS 服務器上對應的目錄

Ⅴ、創建PVC

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: mypvc2    
spec:
  accessModes:
   - ReadWriteOnce    //根據這個方式來選擇綁定的PV
  resources:
    requests:
      storage: 1Gi
  storageClassName: nfs

Ⅵ、創建pod

[root@k8s-master pod]# cat volume-nfs.yaml 
apiVersion: v1
kind: Pod
metadata:
  name: pod-demo
spec:
  containers:
   - name: myapp
     image: ikubernetes/myapp:v1
     volumeMounts:
       - name: cache-volume
         mountPath: /cache
   - name: busybox
     image: busybox:latest
     imagePullPolicy: IfNotPresent
     command: ["/bin/sh","-c","sleep 6000s"]
     volumeMounts:
       - name: cache-volume
         mountPath: /test
  volumes:
    - name: cache-volume    //名稱和volumemounts的名稱一致
      persistentVolumeClaim:
        claimName: mypvc2    //名稱為pvc的名稱

執行創建pod命令

[root@k8s-master pod]# kubectl apply -f volume-nfs.yaml 

Ⅶ、驗證pod里的兩個容器創建文件是否會同步

1、在pod里面創建文件

[root@k8s-master pod]# kubectl exec -it pod-demo -c busybox -- /bin/sh   //進入容器
/ # ls
bin   dev   etc   home  proc  root  run   sys   test  tmp   usr   var
/ # cd test/  進入共享目錄
/test # ls
/test # touch hello   //創建文件hello
/test # echo "1.html">1.html  //創建文件1.html
/test # ls
1.html  hello

2、查看nfs服務器上的目錄,成功

[root@localhost nfs1]# ll
total 4
-rw-r--r--. 1 root root 7 Jul 27 04:02 1.html
-rw-r--r--. 1 root root 0 Jul 27 04:01 hello
[root@localhost nfs1]# pwd
/nfs1
[root@localhost nfs1]# 

Ⅷ、PVC 的回收

當不在需要使用pvc的時候可以進行相應的刪除操作,

1、刪除之前的狀態為bound

2、執行刪除的操作

[root@k8s-master pod]# kubectl delete pvc mypvc2
persistentvolumeclaim "mypvc2" deleted

3、查看文件還是存在

[root@localhost nfs1]# ll
total 4
-rw-r--r--. 1 root root 7 Jul 27 04:02 1.html
-rw-r--r--. 1 root root 0 Jul 27 04:01 hello


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM