k8s 的存储PVC与PV

PVC 相关的定义

kubectl explain persistentVolumeClaim
KIND:     PersistentVolumeClaim
VERSION:  v1

DESCRIPTION:
     PersistentVolumeClaim is a user's request for and claim to a persistent
     volume

FIELDS:
   apiVersion	<string>
     APIVersion defines the versioned schema of this representation of an
     object. Servers should convert recognized schemas to the latest internal
     value, and may reject unrecognized values. More info:
     https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources

   kind	<string>
     Kind is a string value representing the REST resource this object
     represents. Servers may infer this from the endpoint the client submits
     requests to. Cannot be updated. In CamelCase. More info:
     https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds

   metadata	<Object>
     Standard object's metadata. More info:
     https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#metadata

   spec	<Object>
     Spec defines the desired characteristics of a volume requested by a pod
     author. More info:
     https://kubernetes.io/docs/concepts/storage/persistent-volumes#persistentvolumeclaims

   status	<Object>
     Status represents the current information/status of a persistent volume
     claim. Read-only. More info:
     https://kubernetes.io/docs/concepts/storage/persistent-volumes#persistentvolumeclaims
# kubectl explain persistentVolumeClaim.spec
KIND:     PersistentVolumeClaim
VERSION:  v1

RESOURCE: spec <Object>

DESCRIPTION:
     Spec defines the desired characteristics of a volume requested by a pod
     author. More info:
     https://kubernetes.io/docs/concepts/storage/persistent-volumes#persistentvolumeclaims

     PersistentVolumeClaimSpec describes the common attributes of storage
     devices and allows a Source for provider-specific attributes

FIELDS:
   accessModes	<[]string>   #访问模型是否支持多人访问
     AccessModes contains the desired access modes the volume should have. More
     info:
     https://kubernetes.io/docs/concepts/storage/persistent-volumes#access-modes-1

   dataSource	<Object>    #是否启动数据卷快照
     This field requires the VolumeSnapshotDataSource alpha feature gate to be
     enabled and currently VolumeSnapshot is the only supported data source. If
     the provisioner can support VolumeSnapshot data source, it will create a
     new volume and data will be restored to the volume at the same time. If the
     provisioner does not support VolumeSnapshot data source, volume will not be
     created and the failure will be reported as an event. In the future, we
     plan to support more data source types and the behavior of the provisioner
     may change.

   resources	<Object>  # 最小资源限制
     Resources represents the minimum resources the volume should have. More
     info:
     https://kubernetes.io/docs/concepts/storage/persistent-volumes#resources

   selector	<Object>  #标签选择器用来选择拥有指定标签的bv 里选取pv
     A label query over volumes to consider for binding.   

   storageClassName	<string>  #存储累名称
     Name of the StorageClass required by the claim. More info:
     https://kubernetes.io/docs/concepts/storage/persistent-volumes#class-1

   volumeMode	<string>  # 后端存储卷模式
     volumeMode defines what type of volume is required by the claim. Value of
     Filesystem is implied when not included in claim spec. This is a beta
     feature.

   volumeName	<string>  # 后端存储卷名称,如果给定就会指定帮在哪个存储卷上
     VolumeName is the binding reference to the PersistentVolume backing this
     claim.

  在NFS 存储创建几个目录

[root@ES ~]# cd /data/kubernetes/
[root@ES kubernetes]# mkdir pv{1,2,3,4,5,6} 
[root@ES kubernetes]# ls
html  pv1  pv2  pv3  pv4  pv5  pv6
[root@ES kubernetes]# cat /etc/exports
/data/activemq  192.168.10.31(rw,sync,no_root_squash,no_all_squash)
/data/activemq  192.168.10.4(rw,sync,no_root_squash,no_all_squash)
/data/kubernetes/pv1  192.168.10.0/24(rw,sync,no_root_squash,no_all_squash)
/data/kubernetes/pv2  192.168.10.0/24(rw,sync,no_root_squash,no_all_squash)
/data/kubernetes/pv3  192.168.10.0/24(rw,sync,no_root_squash,no_all_squash)
/data/kubernetes/pv4  192.168.10.0/24(rw,sync,no_root_squash,no_all_squash)
/data/kubernetes/pv5  192.168.10.0/24(rw,sync,no_root_squash,no_all_squash)
/data/kubernetes/pv6  192.168.10.0/24(rw,sync,no_root_squash,no_all_squash)
[root@ES kubernetes]# systemctl restart nfs
测试
[root@node01 ~]# mount -t nfs 192.168.10.16:/data/kubernetes/pv1 /kubernetes/pod  测试完取消挂载

    定义PV;不能定义名称空间因为数据集群级别

[root@master ~]# kubectl explain pv
KIND:     PersistentVolume
VERSION:  v1

DESCRIPTION:
     PersistentVolume (PV) is a storage resource provisioned by an
     administrator. It is analogous to a node. More info:
     https://kubernetes.io/docs/concepts/storage/persistent-volumes

FIELDS:
   apiVersion	<string>
     APIVersion defines the versioned schema of this representation of an
     object. Servers should convert recognized schemas to the latest internal
     value, and may reject unrecognized values. More info:
     https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources

   kind	<string>
     Kind is a string value representing the REST resource this object
     represents. Servers may infer this from the endpoint the client submits
     requests to. Cannot be updated. In CamelCase. More info:
     https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds

   metadata	<Object>
     Standard object's metadata. More info:
     https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#metadata

   spec	<Object>
     Spec defines a specification of a persistent volume owned by the cluster.
     Provisioned by an administrator. More info:
     https://kubernetes.io/docs/concepts/storage/persistent-volumes#persistent-volumes

   status	<Object>
     Status represents the current information/status for the persistent volume.
     Populated by the system. Read-only. More info:
     https://kubernetes.io/docs/concepts/storage/persistent-volumes#persistent-volumes

[root@master ~]# kubectl explain pv.spec
KIND:     PersistentVolume
VERSION:  v1

RESOURCE: spec <Object>

DESCRIPTION:
     Spec defines a specification of a persistent volume owned by the cluster.
     Provisioned by an administrator. More info:
     https://kubernetes.io/docs/concepts/storage/persistent-volumes#persistent-volumes

     PersistentVolumeSpec is the specification of a persistent volume.

FIELDS:
   accessModes	<[]string>   可以写多个;但要看存储设备是否支持
     AccessModes contains all ways the volume can be mounted. More info:
     https://kubernetes.io/docs/concepts/storage/persistent-volumes#access-modes
ReadWriteOnce –该卷可以通过单个节点以读写方式安装
ReadOnlyMany –该卷可以被许多节点只读安装
ReadWriteMany –该卷可以被许多节点读写安装
简写
RWO-ReadWriteOnce
ROX-ReadOnlyMany
RWX-ReadWriteMany

   awsElasticBlockStore	<Object>
     AWSElasticBlockStore represents an AWS Disk resource that is attached to a
     kubelet's host machine and then exposed to the pod. More info:
     https://kubernetes.io/docs/concepts/storage/volumes#awselasticblockstore

   azureDisk	<Object>
     AzureDisk represents an Azure Data Disk mount on the host and bind mount to
     the pod.

   azureFile	<Object>
     AzureFile represents an Azure File Service mount on the host and bind mount
     to the pod.

   capacity	<map[string]string>   定义空间大小
     A description of the persistent volume's resources and capacity. More info:
     https://kubernetes.io/docs/concepts/storage/persistent-volumes#capacity

   cephfs	<Object>
     CephFS represents a Ceph FS mount on the host that shares a pod's lifetime

   cinder	<Object>
     Cinder represents a cinder volume attached and mounted on kubelets host
     machine. More info: https://examples.k8s.io/mysql-cinder-pd/README.md

   claimRef	<Object>
     ClaimRef is part of a bi-directional binding between PersistentVolume and
     PersistentVolumeClaim. Expected to be non-nil when bound. claim.VolumeName
     is the authoritative bind between PV and PVC. More info:
     https://kubernetes.io/docs/concepts/storage/persistent-volumes#binding

   csi	<Object>
     CSI represents storage that is handled by an external CSI driver (Beta
     feature).

   fc	<Object>
     FC represents a Fibre Channel resource that is attached to a kubelet's host
     machine and then exposed to the pod.

   flexVolume	<Object>
     FlexVolume represents a generic volume resource that is
     provisioned/attached using an exec based plugin.

   flocker	<Object>
     Flocker represents a Flocker volume attached to a kubelet's host machine
     and exposed to the pod for its usage. This depends on the Flocker control
     service being running

   gcePersistentDisk	<Object>
     GCEPersistentDisk represents a GCE Disk resource that is attached to a
     kubelet's host machine and then exposed to the pod. Provisioned by an
     admin. More info:
     https://kubernetes.io/docs/concepts/storage/volumes#gcepersistentdisk

   glusterfs	<Object>
     Glusterfs represents a Glusterfs volume that is attached to a host and
     exposed to the pod. Provisioned by an admin. More info:
     https://examples.k8s.io/volumes/glusterfs/README.md

   hostPath	<Object>
     HostPath represents a directory on the host. Provisioned by a developer or
     tester. This is useful for single-node development and testing only!
     On-host storage is not supported in any way and WILL NOT WORK in a
     multi-node cluster. More info:
     https://kubernetes.io/docs/concepts/storage/volumes#hostpath

   iscsi	<Object>
     ISCSI represents an ISCSI Disk resource that is attached to a kubelet's
     host machine and then exposed to the pod. Provisioned by an admin.

   local	<Object>
     Local represents directly-attached storage with node affinity

   mountOptions	<[]string>
     A list of mount options, e.g. ["ro", "soft"]. Not validated - mount will
     simply fail if one is invalid. More info:
     https://kubernetes.io/docs/concepts/storage/persistent-volumes/#mount-options

   nfs	<Object>
     NFS represents an NFS mount on the host. Provisioned by an admin. More
     info: https://kubernetes.io/docs/concepts/storage/volumes#nfs

   nodeAffinity	<Object>
     NodeAffinity defines constraints that limit what nodes this volume can be
     accessed from. This field influences the scheduling of pods that use this
     volume.

   persistentVolumeReclaimPolicy	<string>
     What happens to a persistent volume when released from its claim. Valid
     options are Retain (default for manually created PersistentVolumes), Delete
     (default for dynamically provisioned PersistentVolumes), and Recycle
     (deprecated). Recycle must be supported by the volume plugin underlying
     this PersistentVolume. More info:
     https://kubernetes.io/docs/concepts/storage/persistent-volumes#reclaiming

   photonPersistentDisk	<Object>
     PhotonPersistentDisk represents a PhotonController persistent disk attached
     and mounted on kubelets host machine

   portworxVolume	<Object>
     PortworxVolume represents a portworx volume attached and mounted on
     kubelets host machine

   quobyte	<Object>
     Quobyte represents a Quobyte mount on the host that shares a pod's lifetime

   rbd	<Object>
     RBD represents a Rados Block Device mount on the host that shares a pod's
     lifetime. More info: https://examples.k8s.io/volumes/rbd/README.md

   scaleIO	<Object>
     ScaleIO represents a ScaleIO persistent volume attached and mounted on
     Kubernetes nodes.

   storageClassName	<string>
     Name of StorageClass to which this persistent volume belongs. Empty value
     means that this volume does not belong to any StorageClass.

   storageos	<Object>
     StorageOS represents a StorageOS volume that is attached to the kubelet's
     host machine and mounted into the pod More info:
     https://examples.k8s.io/volumes/storageos/README.md

   volumeMode	<string>    是可选的API参数。 Filesystem是volumeMode省略参数时使用的默认模式;您可以设置的值volumeMode以Block将卷用作原始块设备。这样的卷作为一个块设备呈现在Pod中,上面没有任何文件系统。此模式对于为Pod提供最快的访问卷的方式很有用,而Pod和卷之间没有任何文件系统层。另一方面,在Pod中运行的应用程序必须知道如何处理原始块设备。
     volumeMode defines if a volume is intended to be used with a formatted
     filesystem or to remain in raw block state. Value of Filesystem is implied
     when not included in spec. This is a beta feature.

   vsphereVolume	<Object>
     VsphereVolume represents a vSphere volume attached and mounted on kubelets
     host machine

  

  编写PV的yaml文件

[root@master vml]# cat pv.yaml 
apiVersion: v1
kind: PersistentVolume
metadata: 
  name: pv01
  labels: 
    name: pv01
spec:
  accessModes: 
  - ReadWriteMany
  capacity: 
    storage: 2Gi
  nfs:
   path: /data/kubernetes/pv1
   server: 192.168.10.16
[root@master vml]# kubectl apply -f pv.yaml  
persistentvolume/pv01 created
[root@master vml]# kubectl describe pv pv01  查看pv
Name:            pv01
Labels:          name=pv01
Annotations:     kubectl.kubernetes.io/last-applied-configuration:
                   {"apiVersion":"v1","kind":"PersistentVolume","metadata":{"annotations":{},"labels":{"name":"pv01"},"name":"pv01"},"spec":{"access
Modes":["...Finalizers:      [kubernetes.io/pv-protection]
StorageClass:    
Status:          Available
Claim:           
Reclaim Policy:  Retain
Access Modes:    RWX
VolumeMode:      Filesystem
Capacity:        2Gi
Node Affinity:   <none>
Message:         
Source:
    Type:      NFS (an NFS mount that lasts the lifetime of a pod)
    Server:    192.168.10.16
    Path:      /data/kubernetes/pv1
    ReadOnly:  false
Events:        <none>

  编写PVC与pod的文件

[root@master vml]# cat hostpath.yaml 
apiVersion: v1
kind: PersistentVolumeClaim
metadata: 
  name: pv01
  namespace: default
spec:
  accessModes: 
  - ReadWriteMany
  resources: 
    requests:  最小限制;可以指定义最小限制或做大限制
      storage: 2Gi
    limits:  最大限制
      storage: 2Gi

---
apiVersion: v1
kind: Pod
metadata:
  name: host-1
  namespace: default
  labels:
    app: myapp
    tier: frontend
spec:
  containers:
  - name: myapp
    image: ikubernetes/myapp:v1
    ports:
    - name: http
      containerPort: 80
    - name: https 
      containerPort: 443
    volumeMounts:
    - name: html
      mountPath: /chenxi/cx
  - name: busybox
    image: busybox:latest
    imagePullPolicy: IfNotPresent
    command:
    - "/bin/sh"
    - "-c"
    - "echo 'chenxi1234' >> /cx/html "
    volumeMounts:
    - name: html
      mountPath: /cx
  volumes:
  - name: html
    persistentVolumeClaim:
      claimName: pv01  这边引用名字

  创建后查看NFS

[root@master vml]# kubectl apply -f hostpath.yaml 
persistentvolumeclaim/pv01 created
pod/host-1 created
[root@ES kubernetes]# ls /data/kubernetes/pv2/
html
[root@ES kubernetes]# cat /data/kubernetes/pv2/html 
chenxi1234
chenxi1234
chenxi1234
chenxi1234
chenxi1234
chenxi1234
chenxi1234

  

 

 

posted @ 2020-04-27 10:26  烟雨楼台,行云流水  阅读(613)  评论(0编辑  收藏  举报