Kubernetes持久化儲存2——探究實驗

振宇要低調發表於2017-03-28

一、簡介

  本文在“建立PV,建立PVC掛載PV,建立POD掛載PVC”這個環境的基礎上,進行各種刪除實驗,並記錄、分析各資源的狀態。

二、實驗指令碼

  實驗建立了一個PV、一個PVC掛載了PV、一個POD掛載PVC,並編寫了兩個簡單的小指令碼來快速建立和刪除環境。對應的指令碼如下所示:

  需要注意的是在建立PV時,PV並不會去檢查你配置的server是否真的存在;也不會檢查server上是否有一個可用的NFS服務;當然更不會檢查你設定的storage大小是否真有那麼大。個人感覺PV的設定只是一個宣告,系統並不會對此做任何檢查。PVC的掛載也只是根據配額的大小和訪問模式,過濾一下PV,並以最小的代價支援。

[root@k8s-master pv]# cat nfs-pv.yaml 
apiVersion: v1
kind: PersistentVolume
metadata:
  name: pv0001
spec:
  capacity:
    storage: 5Gi 
  accessModes:
    - ReadWriteMany 
  persistentVolumeReclaimPolicy: Recycle
  nfs:
    path: "/data/disk1"
    server: 192.168.20.47 
    readOnly: false
[root@k8s-master pv]# cat pvc.yaml 
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: nfs-pvc
spec:
  accessModes:
  - ReadWriteMany      
  resources:
     requests:
       storage: 1Gi     
[root@k8s-master pv]# cat test-pvc-pod.yaml 
apiVersion: v1
kind: Pod
metadata:
  name: test-nfs-pvc
  labels:
    name: test-nfs-pvc
spec:
  containers:
    - name: test-nfs-pvc
      image: registry:5000/back_demon:1.0  
      ports:
        - name: backdemon
          containerPort: 80
      command:
        - /run.sh
      volumeMounts:
        - name: nfs-vol 
          mountPath: /home/laizy/test/nfs-pvc 
  volumes:
    - name: nfs-vol
      persistentVolumeClaim:
        claimName: nfs-pvc
[root@k8s-master pv]# cat start.sh 
#!/bin/bash
kubectl create -f nfs-pv.yaml
kubectl create -f pvc.yaml
kubectl create -f test-pvc-pod.yaml
[root@k8s-master pv]# cat remove.sh 
#!/bin/bash
kubectl delete pod test-nfs-pvc
kubectl delete persistentvolumeclaim nfs-pvc
kubectl delete persistentvolume pv0001
[root@k8s-master pv]#

三、刪除PV實驗

  建立PV,建立PVC掛載PV,建立POD掛載PVC。在刪除PV後,PVC狀態從Bound變為Lost,Pod中的Volume仍然能用,資料也沒有被刪除。

[root@k8s-master pv]# ./start.sh 
persistentvolume "pv0001" created
persistentvolumeclaim "nfs-pvc" created
pod "test-nfs-pvc" created
[root@k8s-master pv]# kubectl get persistentvolume
NAME   CAPACITY   ACCESSMODES   RECLAIMPOLICY STATUS   CLAIM        REASON    AGE
pv0001   5Gi        RWX           Recycle       Bound   default/nfs-pvc            15s
[root@k8s-master pv]# kubectl get persistentvolumeclaim
NAME    STATUS    VOLUME   CAPACITY   ACCESSMODES   AGE
nfs-pvc   Bound     pv0001    5Gi        RWX           18s
[root@k8s-master pv]# kubectl get pod test-nfs-pvc 
NAME       READY     STATUS    RESTARTS   AGE
test-nfs-pvc   1/1       Running    0          39s
[root@k8s-master pv]# kubectl delete persistentvolume pv0001 
persistentvolume "pv0001" deleted
[root@k8s-master pv]# kubectl get persistentvolume
No resources found.
[root@k8s-master pv]# kubectl get persistentvolumeclaim 
NAME    STATUS    VOLUME    CAPACITY   ACCESSMODES   AGE
nfs-pvc    Lost      pv0001     0                        1m
[root@k8s-master pv]# kubectl get pod test-nfs-pvc 
NAME           READY     STATUS    RESTARTS   AGE
test-nfs-pvc   1/1       Running   0          1m
[root@k8s-master pv]# kubectl exec -ti test-nfs-pvc /bin/bash
[root@test-nfs-pvc /]# cd /home/laizy/test/nfs-pvc/
[root@test-nfs-pvc nfs-pvc]# ls
2.out
[root@test-nfs-pvc nfs-pvc]# exit
exit
[root@k8s-master pv]#

四、刪除PVC實驗

4.1清空Pod設定

  設定PV時,如果設定了回收策略是“回收”的時候,在刪除PVC時,系統(Controller-Manager)會啟動一個recycler的Pod,用於清理資料卷中的內容。每種資料卷的回收Pod是不同的,都有自己特定的邏輯。本文以NFS為例,給出具體配置及Pod描述如下:

[root@k8s-master ~]# cat /etc/kubernetes/controller-manager   #配置完成後請重啟controller-manager
###
# The following values are used to configure the kubernetes controller-manager

# defaults from config and apiserver should be adequate

# Add your own!
KUBE_CONTROLLER_MANAGER_ARGS="--pv-recycler-pod-template-filepath-nfs=/etc/kubernetes/recycler.yaml"
[root@k8s-master ~]# cat /etc/kubernetes/recycler.yaml 
apiVersion: v1
kind: Pod
metadata:
  name: pv-recycler-
  namespace: default
spec:
  restartPolicy: Never
  volumes:
  - name: vol
    hostPath:
      path: /any/path/it/will/be/replaced
  containers:
  - name: pv-recycler
    image: "docker.io/busybox"
    imagePullPolicy: IfNotPresent
    command: ["/bin/sh", "-c", "test -e /scrub && rm -rf /scrub/..?* /scrub/.[!.]* /scrub/*  && test -z \"$(ls -A /scrub)\" || exit 1"]
    volumeMounts:
    - name: vol
      mountPath: /scrub
[root@k8s-master ~]#

  如果不進行上面的設定的話,預設回收Pod用的image是gcr上的busybox,因為種種原因,在國內是無法下載的(即使你的機器上有gcr的busybox也不可以,還需要設定映象下載策略為IfNotPresent,否則會一直去gcr查詢是否有新版本的映象,這也會導致imagePullError)。所以必須要在Controller-Manager中進行設定,設定成隨便哪個busybox。

4.2刪除實驗

建立PV,建立PVC掛載PV,建立POD掛載PVC。刪除PVC後,PV狀態從Bound變為Available,系統(controller-manager)呼叫持久化儲存清理外掛(recycler-for-pv0001),將PVC對應的PV清空。Pod中的Volume仍然能用,但volume中的資料被刪除了。

[root@k8s-master pv]# ./start.sh 
persistentvolume "pv0001" created
persistentvolumeclaim "nfs-pvc" created
pod "test-nfs-pvc" created
[root@k8s-master pv]# kubectl get persistentvolume
NAME      CAPACITY   ACCESSMODES   RECLAIMPOLICY   STATUS    CLAIM             REASON    AGE
pv0001    5Gi        RWX           Recycle         Bound     default/nfs-pvc             11s
[root@k8s-master pv]# kubectl get persistentvolumeclaim 
NAME      STATUS    VOLUME    CAPACITY   ACCESSMODES   AGE
nfs-pvc   Bound     pv0001    5Gi        RWX           14s
[root@k8s-master pv]# kubectl get pod
NAME          READY     STATUS    RESTARTS   AGE
test-nfs-pvc      1/1       Running   0          19s
[root@k8s-master pv]# kubectl exec -ti test-nfs-pvc /bin/bash
[root@test-nfs-pvc /]# touch /home/laizy/test/nfs-pvc/1.out
[root@test-nfs-pvc /]# cd /home/laizy/test/nfs-pvc/
[root@test-nfs-pvc nfs-pvc]# ls
1.out
[root@test-nfs-pvc nfs-pvc]# exit
exit
[root@k8s-master pv]# kubectl delete persistentvolumeclaim nfs-pvc 
persistentvolumeclaim "nfs-pvc" deleted
[root@k8s-master pv]# kubectl get pod
NAME                 READY     STATUS              RESTARTS   AGE
recycler-for-pv0001       0/1       ContainerCreating    0          1s
test-nfs-pvc             1/1       Running             0          1m
[root@k8s-master pv]# kubectl get persistentvolume
NAME      CAPACITY   ACCESSMODES   RECLAIMPOLICY   STATUS      CLAIM     REASON    AGE
pv0001    5Gi        RWX           Recycle         Available                       1m
[root@k8s-master pv]# kubectl get persistentvolumeclaim 
No resources found.
[root@k8s-master pv]# kubectl get pod test-nfs-pvc 
NAME           READY     STATUS    RESTARTS   AGE
test-nfs-pvc   1/1       Running   0          2m
[root@k8s-master pv]# kubectl exec -ti test-nfs-pvc /bin/bash
[root@test-nfs-pvc /]# ls /home/laizy/test/nfs-pvc/
[root@test-nfs-pvc /]

五、刪除Pod實驗

  建立PV,建立PVC掛載PV,建立POD掛載PVC。刪除Pod後,PV、PVC狀態沒變,Pod中的Volume對應的NFS資料沒有被刪除。

[root@k8s-master pv]# ./start.sh 
persistentvolume "pv0001" created
persistentvolumeclaim "nfs-pvc" created
pod "test-nfs-pvc" created
[root@k8s-master pv]# kubectl get persistentvolume
NAME  CAPACITY ACCESSMODES   RECLAIMPOLICY   STATUS    CLAIM   REASON    AGE
pv0001 5Gi      RWX           Recycle         Bound     default/nfs-pvc       11s
[root@k8s-master pv]# kubectl get persistentvolumeclaim
NAME      STATUS    VOLUME    CAPACITY   ACCESSMODES   AGE
nfs-pvc   Bound     pv0001    5Gi        RWX           27s
[root@k8s-master pv]# kubectl get pod
NAME         READY     STATUS    RESTARTS   AGE
test-nfs-pvc     1/1       Running   0          36s
[root@k8s-master pv]# kubectl exec -ti test-nfs-pvc /bin/bash
[root@test-nfs-pvc /]# cat /home/laizy/test/nfs-pvc/1.out 
123456
[root@test-nfs-pvc /]# exit
exit
[root@k8s-master pv]# kubectl delete pod test-nfs-pvc 
pod "test-nfs-pvc" deleted
[root@k8s-master pv]# kubectl get persistentvolume
NAME CAPACITY   ACCESSMODES   RECLAIMPOLICY   STATUS    CLAIM  REASON    AGE
pv0001    5Gi   RWX           Recycle         Bound     default/nfs-pvc      8m
[root@k8s-master pv]# kubectl get persistentvolumeclaim
NAME      STATUS    VOLUME    CAPACITY   ACCESSMODES   AGE
nfs-pvc     Bound     pv0001     5Gi        RWX           8m
[root@k8s-master pv]# ssh 192.168.20.47  #登入到遠端NFS伺服器
root@192.168.20.47's password: 
Last failed login: Mon Mar 27 14:37:19 CST 2017 from :0 on :0
There was 1 failed login attempt since the last successful login.
Last login: Mon Mar 20 10:49:18 2017
[root@localhost ~]# cd /data/disk1/1.out
123456

 

相關文章