k8s--kubernetes--argo----使用動態存儲PVC---基於nfs 的storageclass

PVC簡介

Docker中有 volumes的概念,在Docker中,volume是 對 物理節點服務器node路徑目錄的一種映射,也就是 我們可以把服務器的一個目錄掛載給鏡像使用。

同樣的,k8s創建的pod也可以掛載volume,而且不僅僅支持 pod所在的服務器node的目錄映射,也可以掛載其他網絡存儲的作爲目錄掛載。

k8s支持volumes的類型如下:

1、awsElasticBlockStore
2、azureDisk
3、azureFile
4、cephfs
5、cinder
6、configMap
7、csi
8、downwardAPI
9、emptyDir
10、fc (fibre channel)
11、flexVolume
12、flocker
13、gcePersistentDisk
14、gitRepo (deprecated)
15、glusterfs
16、hostPath
17、iscsi
18、local
19、nfs
20、persistentVolumeClaim
21、projected
22、portworxVolume
23、quobyte
24、rbd
25、scaleIO
26、secret
27、storageos
28、vsphereVolume

相關類型的使用參考 Volumes

PVC是persistentVolumeClaim類型的縮寫。

PVC是把雲集羣的node當成一個資源池,佔用相應的物理空間作爲 網絡的存儲空間,提供給k8s的pod來掛載使用。

更多詳細參考和pvc的使用方法 Persistent Volumes

Storage Classes

持久型的pvc使用的 預先創建好的nfs或者 node的空間,會一直佔用着資源,造成不使用時的浪費。

而且預先創建好的pvc有可能跟pod不是一個 可用區,將造成 跨可用區的流量費用。

k8s針對這種情況 有storageclass的用法,可以讓用戶動態創建pvc。

詳情參考 Storage Classes

使用s3拉取到 動態pvc中,動態創建pvc通過拓撲感知能夠讓pvc跟 pod在同一個可用區中,通過內網交互,省去流量費用。

因爲s3拉取流量是按次數計費,當文件大 次數小時 ,費用可忽略不計。

所以 這種動態pvc方案 可以很好的降低 大參考文件的存儲和交互成本。

k8s中使用

聲明創建storageClassName,這個地方根據我們使用的底層技術不同會有不同,比如使用rbd,ceph,nfs等等

這裏以nfs爲例

創建serviceaccount.yaml

apiVersion: v1
kind: ServiceAccount
metadata:
  name: nfs-client-provisioner

創建nfs-provisioner.yaml

kind: Deployment
apiVersion: extensions/v1beta1
metadata:
  name: nfs-provisioner
spec:
  replicas: 1
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        app: nfs-provisioner
    spec:
      serviceAccount: nfs-client-provisioner  #這個要與剛纔創建的serviceaccount 的名字一致
      containers:
        - name: nfs-provisioner
          image: registry.cn-hangzhou.aliyuncs.com/open-ali/nfs-client-provisioner
          volumeMounts:
            - name: nfs-client-root
              mountPath: /persistentvolumes
          env:
            - name: PROVISIONER_NAME
              value: example.com/nfs #這裏名字自定義,要記住storageclass 後面要用到
            - name: NFS_SERVER
              value: [已配置的NFS系統的IP地址]
            - name: NFS_PATH
              value: [已配置的NFS系統的掛載路徑] 
      volumes:
        - name: nfs-client-root
          nfs:
            server: [已配置的NFS系統的IP地址]
            path: [已配置的NFS系統的掛載路徑] #這裏需要注意,如果用的公有云服務的nfs 或者nas,必須要提前創建好目錄

StorageClass.yaml

kind: StorageClass
apiVersion: storage.k8s.io/v1
metadata:
  name: nfs-name
provisioner: example.com/nfs #這裏的名字要跟之前創建nfs-client deployment裏寫的名字一樣

聲明創建pvc
spark-pvc.yaml

#聲明
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: pvc-name
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 50Gi
  storageClassName: nfs-name

注意這裏ReadWriteOnce表示只允許一個pod以讀寫模式掛載

ReadWriteOnce —— 該volume只能被單個節點以讀寫的方式映射
ReadOnlyMany —— 該volume可以被多個節點以只讀方式映射
ReadWriteMany —— 該volume只能被多個節點以讀寫的方式映射,這種存儲可以以讀寫的方式被多個Pod共享。不是每一種存儲都支持這三種方式,像共享方式,目前支持的還比較少,比較常用的是NFS。在PVC綁定PV時通常根據兩個條件來綁定,一個是存儲的大小,另一個就是訪問模式。

如果設置的屬性不對,則會報錯

Pod "zzq-1584759188679-exec-1" is invalid: spec.containers[0].volumeMounts[1].mountPath: Invalid value: "/tmp2": must be unique

yaml中使用
spark.yaml

container:
  image: 123.dkr.ecr.cn-northwest-1.amazonaws.com.cn/module
  imagePullPolicy: Always
  volumeMounts:
    - name: volumes-name
      mountPath: /share/tmp/
volumes:
    - name: volumes-name
      persistentVolumeClaim:
        claimName: pvc-name

使用rbac加權限認證的會稍微麻煩些,使用方法參考:

使用rbac的

1,創建serviceaccount.yaml

apiVersion: v1
kind: ServiceAccount
metadata:
  name: nfs-client-provisioner

2,創建nfs客戶端deployment.yaml

kind: Deployment
apiVersion: extensions/v1beta1
metadata:
  name: nfs-client-provisioner
spec:
  replicas: 1
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        app: nfs-client-provisioner
    spec:
      serviceAccount: nfs-client-provisioner
      containers:
        - name: nfs-client-provisioner
          image: quay.io/external_storage/nfs-client-provisioner:latest
          volumeMounts:
            - name: nfs-client-root
              mountPath: /persistentvolumes #這裏不能修改
          env:
            - name: PROVISIONER_NAME
              value: example.cn/nfs #這裏自定義
            - name: NFS_SERVER
              value: 123.23.xxx.xxx    #寫nfs server地址
            - name: NFS_PATH
              value: /localmnt/storage
      volumes:
        - name: nfs-client-root
          nfs:
            server: 123.23.xxx.xxx
            path: /localmnt/storage    #和之前一樣,掛在之前,一定要提前創建,不然k8s 不認識,也不能自動創建

3,創建集羣綁定規則clusterrolebinding.yaml

kind: ClusterRoleBinding
apiVersion: rbac.authorization.k8s.io/v1alpha1
metadata:
  name: run-nfs-client-provisioner
subjects:
  - kind: ServiceAccount
    name: nfs-client-provisioner
roleRef:
  kind: ClusterRole
  name: nfs-client-provisioner-runner
  apiGroup: rbac.authorization.k8s.io

4,創建集羣角色clusterrole.yaml

kind: ClusterRole
apiVersion: rbac.authorization.k8s.io/v1alpha1
metadata:
  name: nfs-client-provisioner-runner
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"]

5,創建storageclass.yaml

apiVersion: storage.k8s.io/v1beta1
kind: StorageClass
metadata:
  name: standard
provisioner: example.cn/nfs #這裏跟之前創建nfs client端裏定義的名字一樣

argo中使用

apiVersion: argoproj.io/v1alpha1
kind: Workflow
metadata:
  generateName: volumes-pvc-
spec:
  entrypoint: volumes-pvc-example
  volumeClaimTemplates:                 # define volume, same syntax as k8s Pod spec
  - metadata:
      name: vpcname                     # name of volume claim
    spec:
      accessModes:
        - ReadWriteOnce
      resources:
        requests:
          storage: 50Gi
      storageClassName: gp2-topology

  templates:
  - name: volumes-pvc-example
    steps:
    - - name: generate
        template: whalesay

  - name: whalesay
    container:
      image: docker/whalesay:latest
      command: [sh, -c]
      args: ["echo generating message in volume; cowsay hello world | tee /mnt/vol/hello_world.txt"]
      # Mount workdir volume at /mnt/vol before invoking docker/whalesay
      volumeMounts:                     # same syntax as k8s Pod spec
      - name: vpcname
        mountPath: /iter

argo中的PVC聲明參考 argo官方文檔的volume章節

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章