Kubernetes-ダイナミックPV&PVC

11907 ワード

目次
PV&PVC基本使用
StorageClass&Provisioner
PVCの拡張
PV&PVC基本使用
KubernetesではPVとPVCの概念は紹介されていません
PVとPVCの使い方は
管理者はまず一連の各種サイズのPVを作成し、クラスタ内のストレージリソースプールを形成する.
ユーザーはpvcを作成するときにサイズを指定する必要があります.この場合、pvプールから適切なサイズを探してpvをpvcにバインドして使用します.
たとえば、kubernetesがpv/pvcを介してglusterfsを使用することを示します.
1.glusterfsストレージボリュームを作成し、Glusterfs上のafr-volumeというvolumeを使用して作成したと仮定します.
[root@k8s-node1 json]# gluster volume info afr-volume
  
Volume Name: afr-volume
Type: Replicate
Volume ID: 94fddfe0-123d-4db0-b55b-a49ac2486b21
Status: Started
Snapshot Count: 0
Number of Bricks: 1 x 2 = 2
Transport-type: tcp
Bricks:
Brick1: k8s-node1:/opt/afr_data
Brick2: k8s-node2:/opt/afr_data
Options Reconfigured:
transport.address-family: inet
nfs.disable: on

2.kubernetesでglusterfsのendpointを作成する
apiVersion: v1
kind: Endpoints
metadata:
  name: glusterfs-endpoint
  namespace: kube-system
subsets:
- addresses:
  - ip: 172.16.9.201
  - ip: 172.16.9.202
  - ip: 172.16.9.203
  ports:
  - port: 1
    protocol: TCP

3.kubernetesでglusterfsサービスを作成する(Endpointに対応するサービスを作成する.サービス名はEndpointと一致する必要がある)
apiVersion: v1
kind: Service
metadata:
  name: glusterfs-service
  namespace: kube-system
spec:
  ports:
  - port: 1
    protocol: TCP
    targetPort: 1
  type: ClusterIP

4.pvの作成
apiVersion: v1
kind: PersistentVolume
metadata:
  name: gluster-dev-volume
spec:
  capacity:
    storage: 8Gi
  accessModes:
    - ReadWriteMany
  glusterfs:
    endpoints: "glusterfs-cluster"
    path: "afr-volume"
    readOnly: false

5.pvcの作成

kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: glusterfs-nginx
spec:
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 8Gi

pvとpvcを見ると、PVがAvailiableからBound状態になっていることがわかります.
 
StorageClass&Provisioner
しかし、実際には、管理者は、ユーザがどのようなサイズのストレージボリュームを必要としているのか分からないし、様々なサイズのPVを事前に作成することもできない.
最良の効果は、ユーザが指定したサイズのpvcを作成すると、同じサイズのpvが自動的に作成され、ユーザのpvcが関連付けられることである.
KubernetesはStorageClassを作成することによってDynamic Provisioningプロパティを使用します.storageclassはglusterfsストレージ、cephfsストレージなど、どのようなストレージプラグインを使用するかを決定するためにprovisionerを必要とします.
kubernetesには組み込まれたprovisionerがたくさんあります.参考は以下の通りです.
https://kubernetes.io/docs/concepts/storage/storage-classes/#provisioner
 
kubernetesにはglusterfsのprovisioner:kubernetesが内蔵されています.io/glusterfs
glusterfsのprovisionerはheketiが提供するrestapiによってpvcを動的に作成し、storageclassは以下の通りである.
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
  name: replicate-2
parameters:
  clusterid: 888490f4545c9a4cc896a6f7485f0362
  resturl: http://192.168.1.1:8087
  volumetype: replicate:2
provisioner: kubernetes.io/glusterfs
reclaimPolicy: Delete
volumeBindingMode: Immediate

storageclassを使用してpvcを作成するには、endpoint-service-pv-pvcを順番に作成する必要はありません.
直接pvcを作成してサイズを指定すればいいです.
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: testpvc
  namespace: kube-system
spec:
  accessModes:
  - ReadWriteMany
  resources:
    requests:
      storage: 20Gi
  storageClassName: replicate-2

Provisionerが関連するリソースを自動的に作成して関連付けていることがわかります
[root@k8s-node1 ~]# kubectl --namespace=kube-system get pvc aistack-influxdb -oyaml
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  annotations:
    pv.kubernetes.io/bind-completed: "yes"
    pv.kubernetes.io/bound-by-controller: "yes"
    volume.beta.kubernetes.io/storage-provisioner: kubernetes.io/glusterfs
  creationTimestamp: "2019-07-02T06:38:13Z"
  finalizers:
  - kubernetes.io/pvc-protection
  name: testpvc
  namespace: kube-system
  resourceVersion: "3192"
  selfLink: /api/v1/namespaces/kube-system/persistentvolumeclaims/testpvc
  uid: f7392a88-9c93-11e9-aa77-88d7f6ae9c94
spec:
  accessModes:
  - ReadWriteMany
  dataSource: null
  resources:
    requests:
      storage: 20Gi
  storageClassName: replicate-2
  volumeMode: Filesystem
  volumeName: pvc-f7392a88-9c93-11e9-aa77-88d7f6ae9c94
status:
  accessModes:
  - ReadWriteMany
  capacity:
    storage: 20Gi
  phase: Bound

[root@k8s-node1 ~]# kubectl --namespace=kube-system get pv pvc-f7392a88-9c93-11e9-aa77-88d7f6ae9c94 -oyaml
apiVersion: v1
kind: PersistentVolume
metadata:
  annotations:
    Description: 'Gluster-Internal: Dynamically provisioned PV'
    gluster.kubernetes.io/heketi-volume-id: 65314d1b4c5136fef92b7ef1a3725a57
    gluster.org/type: file
    kubernetes.io/createdby: heketi-dynamic-provisioner
    pv.beta.kubernetes.io/gid: "2001"
    pv.kubernetes.io/bound-by-controller: "yes"
    pv.kubernetes.io/provisioned-by: kubernetes.io/glusterfs
  creationTimestamp: "2019-07-02T06:38:25Z"
  finalizers:
  - kubernetes.io/pv-protection
  name: pvc-f7392a88-9c93-11e9-aa77-88d7f6ae9c94
  resourceVersion: "3196"
  selfLink: /api/v1/persistentvolumes/pvc-f7392a88-9c93-11e9-aa77-88d7f6ae9c94
  uid: fe963cc4-9c93-11e9-aa77-88d7f6ae9c94
spec:
  accessModes:
  - ReadWriteMany
  capacity:
    storage: 20Gi
  claimRef:
    apiVersion: v1
    kind: PersistentVolumeClaim
    name: testpvc
    namespace: kube-system
    resourceVersion: "3168"
    uid: f7392a88-9c93-11e9-aa77-88d7f6ae9c94
  glusterfs:
    endpoints: glusterfs-dynamic-f7392a88-9c93-11e9-aa77-88d7f6ae9c94
    endpointsNamespace: kube-system
    path: vol_65314d1b4c5136fef92b7ef1a3725a57
  persistentVolumeReclaimPolicy: Delete
  storageClassName: replicate-2
  volumeMode: Filesystem
status:
  phase: Bound


[root@k8s-node1 ~]# kubectl --namespace=kube-system get endpoints glusterfs-dynamic-f7392a88-9c93-11e9-aa77-88d7f6ae9c94
NAME                                                     ENDPOINTS                       AGE
glusterfs-dynamic-f7392a88-9c93-11e9-aa77-88d7f6ae9c94   172.16.2.100:1,172.16.2.200:1   6d

[root@k8s-node1 ~]# kubectl --namespace=kube-system get services glusterfs-dynamic-f7392a88-9c93-11e9-aa77-88d7f6ae9c94
NAME                                                     TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)   AGE
glusterfs-dynamic-f7392a88-9c93-11e9-aa77-88d7f6ae9c94   ClusterIP   10.10.159.255           1/TCP     6d

 
PVCの拡張
kubernetes 1.11バージョンでpvc作成後の拡張機能のサポートを開始
storageclassの作成
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
  name: glusterfs-sc
parameters:
  clusterid: 075e35a0ce70274b3ba7f158e77edb2c
  resturl: http://172.16.9.201:8087
  volumetype: replicate:3
provisioner: kubernetes.io/glusterfs
reclaimPolicy: Delete
volumeBindingMode: Immediate
allowVolumeExpansion: true  #   

storageclassにallowVolumeExpansionフィールドが追加されていることがわかります
このフィールドを持たないpvc拡張を先にテスト
まず1 Gのpvcを作成します
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  annotations:
    volume.beta.kubernetes.io/storage-provisioner: kubernetes.io/glusterfs
  name: testpvc
spec:
  accessModes:
  - ReadWriteMany
  dataSource: null
  resources:
    requests:
      storage: 1Gi
  storageClassName: replicate-3
  volumeMode: Filesystem

kubectl create -f pvc.yaml
persistentvolumeclaim/testpvc created

PVCをPodにバインド
[root@k8s-node1 test]# cat pod.yaml
apiVersion: v1
kind: Pod
metadata:
  name: testpod
spec:
  containers:
  - image: nginx:latest
    imagePullPolicy: IfNotPresent
    name: nginx
    - mountPath: /root/testpvc
      name: testpvc
  volumes:
  - name: testpvc
    persistentVolumeClaim:
      claimName: testpvc

[root@k8s-node1 test]# kubectl create -f pod.yaml
pod/testpod created

容器に入ってPVCサイズを見ると、マウントされているディレクトリ/root/testpvcは確かに1 G程度
[root@k8s-node1 test]# kubectl exec -it testpod bash
[root@testpod /]# df -h
Filesystem                                         Size  Used Avail Use% Mounted on
overlay                                            1.1T   29G  1.1T   3% /
tmpfs                                               64M     0   64M   0% /dev
tmpfs                                               32G     0   32G   0% /sys/fs/cgroup
172.16.2.100:vol_088693919fcae3397926ad54f7d77815 1020M   33M  987M   4% /root/testpvc
/dev/mapper/k8s-kubelet                            704G   33M  704G   1% /etc/hosts
/dev/mapper/k8s-docker                             1.1T   29G  1.1T   3% /etc/hostname
shm                                                 64M     0   64M   0% /dev/shm
tmpfs                                               32G   12K   32G   1% /run/secrets/kubernetes.io/serviceaccount
tmpfs                                               32G     0   32G   0% /proc/acpi
tmpfs                                               32G     0   32G   0% /proc/scsi
tmpfs                                               32G     0   32G   0% /sys/firmware

pvcを変更し、サイズを2 Gに変更
[root@k8s-node1 test]# kubectl edit pvc testpvc
...

error: persistentvolumeclaims "testpvc" could not be patched: persistentvolumeclaims "testpvc" is forbidden: only dynamically provisioned pvc can be resized and the storageclass that provisions the pvc must support resize
You can run `kubectl replace -f /tmp/kubectl-edit-vqpx5.yaml` to try this update again.

直接エラーを報告して、修正できないように表示して、動的pvcだけがresizeすることができて、そしてprovisionはresizeをサポートすることができます
現在の環境ではglusterfsを使用しています.storageclassを変更し、allowVolumeExpansionを追加します.true
[root@k8s-node1 test]# kubectl --namespace=kube-system edit storageclasses.storage.k8s.io replicate-2
storageclass.storage.k8s.io/replicate-2 edited

[root@k8s-node1 test]# kubectl --namespace=kube-system get storageclasses.storage.k8s.io replicate-2 -oyaml
allowVolumeExpansion: true
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
  creationTimestamp: "2019-07-02T06:08:00Z"
  name: replicate-2
  resourceVersion: "1261960"
  selfLink: /apis/storage.k8s.io/v1/storageclasses/replicate-2
  uid: bee4b8f2-9c8f-11e9-aa77-88d7f6ae9c94
parameters:
  clusterid: 888490f4545c9a4cc896a6f7485f0362
  resturl: http://172.16.2.100:8087
  volumetype: replicate:2
provisioner: kubernetes.io/glusterfs
reclaimPolicy: Delete
volumeBindingMode: Immediate

storageclassにallowVolumeExpansion:trueフィールドが追加されていることがわかります.pvcを再度変更し、1 Gを2 Gに変更しました.今回はエラーはありません.
[root@k8s-node1 test]# kubectl edit pvc testpvc
persistentvolumeclaim/testpvc edited

pvcを見ると大きさが2 Gになっています
[root@k8s-node1 test]# kubectl get pvc testpvc
NAME      STATUS   VOLUME                                     CAPACITY   ACCESS MODES   STORAGECLASS   AGE
testpvc   Bound    pvc-6880b78a-a450-11e9-aa77-88d7f6ae9c94   2Gi        RWX            replicate-2    13m

コンテナに入って実際のサイズを見ると/root/testpvcディレクトリも2 Gになっていることがわかります
[root@testpod /]# df -h
Filesystem                                         Size  Used Avail Use% Mounted on
overlay                                            1.1T   29G  1.1T   3% /
tmpfs                                               64M     0   64M   0% /dev
tmpfs                                               32G     0   32G   0% /sys/fs/cgroup
172.16.2.100:vol_088693919fcae3397926ad54f7d77815  2.0G   66M  2.0G   4% /root/testpvc
/dev/mapper/k8s-kubelet                            704G   33M  704G   1% /etc/hosts
/dev/mapper/k8s-docker                             1.1T   29G  1.1T   3% /etc/hostname
shm                                                 64M     0   64M   0% /dev/shm
tmpfs                                               32G   12K   32G   1% /run/secrets/kubernetes.io/serviceaccount
tmpfs                                               32G     0   32G   0% /proc/acpi
tmpfs                                               32G     0   32G   0% /proc/scsi
tmpfs                                               32G     0   32G   0% /sys/firmware