KubernetesK8S基礎使用方法總結——儲存卷

一、kubernetes儲存卷型別分類

kubernetes儲存端可支援的型別有如下所示:

[root@master1 ~]# kubectl explain pods。spec。volumesKIND: PodVERSION: v1RESOURCE: volumes <[]Object>DESCRIPTION: List of volumes that can be mounted by containers belonging to the pod。 More info: https://kubernetes。io/docs/concepts/storage/volumes Volume represents a named volume in a pod that may be accessed by any container in the pod。FIELDS: awsElasticBlockStore 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 AzureDisk represents an Azure Data Disk mount on the host and bind mount to the pod。 azureFile AzureFile represents an Azure File Service mount on the host and bind mount to the pod。 cephfs CephFS represents a Ceph FS mount on the host that shares a pod’s lifetime cinder Cinder represents a cinder volume attached and mounted on kubelets host machine。 More info: https://examples。k8s。io/mysql-cinder-pd/README。md configMap ConfigMap represents a configMap that should populate this volume csi CSI (Container Storage Interface) represents storage that is handled by an external CSI driver (Alpha feature)。 downwardAPI DownwardAPI represents downward API about the pod that should populate this volume emptyDir EmptyDir represents a temporary directory that shares a pod‘s lifetime。 More info: https://kubernetes。io/docs/concepts/storage/volumes#emptydir fc FC represents a Fibre Channel resource that is attached to a kubelet’s host machine and then exposed to the pod。 flexVolume FlexVolume represents a generic volume resource that is provisioned/attached using an exec based plugin。 flocker Flocker represents a Flocker volume attached to a kubelet‘s host machine。 This depends on the Flocker control service being running gcePersistentDisk GCEPersistentDisk represents a GCE 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#gcepersistentdisk gitRepo GitRepo represents a git repository at a particular revision。 DEPRECATED: GitRepo is deprecated。 To provision a container with a git repo, mount an EmptyDir into an InitContainer that clones the repo using git, then mount the EmptyDir into the Pod‘s container。 glusterfs Glusterfs represents a Glusterfs mount on the host that shares a pod’s lifetime。 More info: https://examples。k8s。io/volumes/glusterfs/README。md hostPath HostPath represents a pre-existing file or directory on the host machine that is directly exposed to the container。 This is generally used for system agents or other privileged things that are allowed to see the host machine。 Most containers will NOT need this。 More info: https://kubernetes。io/docs/concepts/storage/volumes#hostpath iscsi ISCSI represents an ISCSI Disk resource that is attached to a kubelet‘s host machine and then exposed to the pod。 More info: https://examples。k8s。io/volumes/iscsi/README。md name -required- Volume’s name。 Must be a DNS_LABEL and unique within the pod。 More info: https://kubernetes。io/docs/concepts/overview/working-with-objects/names/#names nfs NFS represents an NFS mount on the host that shares a pod‘s lifetime More info: https://kubernetes。io/docs/concepts/storage/volumes#nfs persistentVolumeClaim PersistentVolumeClaimVolumeSource represents a reference to a PersistentVolumeClaim in the same namespace。 More info: https://kubernetes。io/docs/concepts/storage/persistent-volumes#persistentvolumeclaims photonPersistentDisk PhotonPersistentDisk represents a PhotonController persistent disk attached and mounted on kubelets host machine portworxVolume PortworxVolume represents a portworx volume attached and mounted on kubelets host machine projected Items for all in one resources secrets, configmaps, and downward API quobyte Quobyte represents a Quobyte mount on the host that shares a pod’s lifetime rbd 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 ScaleIO represents a ScaleIO persistent volume attached and mounted on Kubernetes nodes。 secret Secret represents a secret that should populate this volume。 More info: https://kubernetes。io/docs/concepts/storage/volumes#secret storageos StorageOS represents a StorageOS volume attached and mounted on Kubernetes nodes。 vsphereVolume VsphereVolume represents a vSphere volume attached and mounted on kubelets host machine

二、儲存卷使用例項

1.emptyDir

自主式pod演示emptyDir儲存卷型別的使用方法如下:

apiVersion: v1kind: Podmetadata: name: pod-vol namespace: default labels: app: myapp tier: frontend annotations: qjbj。com/created-by: “cluster admin”spec: containers: - name: myapp image: ikubernetes/myapp:v1 ports: - name: http containerPort: 80 volumeMounts: - name: html mountPath: /usr/share/nginx/html/ - name: busybox image: busybox:latest imagePullPolicy: IfNotPresent volumeMounts: - name: html mountPath: /data/ command: - “/bin/sh” - “-c” - “while true; do echo $(date) >> /data/index。html; sleep 5; done” volumes: - name: html emptyDir: {}

2.hostPath

hostPath的儲存卷會直接使用node節點的儲存資源來儲存資料,其中type為DirectoryOrCreate表示若沒有此路徑,則自動建立。

apiVersion: v1kind: Podmetadata: name: podvolhostpath namespace: defaultspec: containers: - name: myappvol image: ikubernetes/myapp:v1 volumeMounts: - name: html mountPath: /usr/share/nginx/html/ volumes: - name: html hostPath: path: /data/pod/volume1 type: DirectoryOrCreate

在目錄/data/pod/volume1下建立測試檔案index。html,新增內容如“i am host content。”,測試結果如下:

[root@node1 /]# curl 10。244。2。45i am host content。

3.nfs

nfs伺服器安裝部署nfs參考:linux伺服器安裝部署和使用nfs總結

apiVersion: v1kind: Podmetadata: name: podvolnfs namespace: defaultspec: containers: - name: myappvol image: ikubernetes/myapp:v1 volumeMounts: - name: html mountPath: /usr/share/nginx/html/ volumes: - name: html nfs: path: /data/volumes server: 192。168。222。153

在nfs伺服器共享目錄/data/volumes下建立index。html檔案,新增內容如“i am nfs server。”,用生成的pod資源的IP地址直接訪問,如下:

[root@node1 ~]# curl 10。244。2。47i am nfs server。