天天看點

TKE操作指南 - PV,PVC,StoragClass配置使用(十三)

在講解TKE存儲,我們先了解以下幾個概念。

簡介

PersistentVolume(PV):叢集内的存儲資源,例如節點是叢集的資源。PV 獨立于 Pod 的生命周期,根據不同的 StorageClass 類型建立不同類型的 PV。

PersistentVolumeClaim(PVC):叢集内的存儲請求。例如,PV 是 Pod 使用節點資源,PVC 則聲明使用 PV 資源。當 PV 資源不足時,PVC 也可以動态建立 PV。

StorageClass 描述存儲的類型,叢集管理者可以為叢集定義不同的存儲類别。騰訊雲 TKE 服務預設提供塊存儲類型的 StorageClass,通過 StorageClass 配合 PersistentVolumeClaim 可以動态建立需要的存儲資源。

注意事項

  • CBS 盤不支援跨可用區挂載。若挂載 CBS 類型 PV 的 Pod 遷移到其他可用區,将會導緻挂載失敗。
  • TKE 控制台不支援 CBS 盤擴縮容,請自行前往 CBS 控制台執行操作。

配置步驟

1.建立StoragClass

2.建立PVC通過界面挂載

3.建立PVC通過yaml檔案挂載

4.驗證挂載成功

1.建立Storagclass

TKE操作指南 - PV,PVC,StoragClass配置使用(十三)

點選并展開存儲,點選StorageClass,進入 StorageClass 資訊頁面,點選建立。

TKE操作指南 - PV,PVC,StoragClass配置使用(十三)

根據實際需求,設定 StorageClass 參數。關鍵參數資訊如下:

  • 名稱:自定義。(我填寫的是cbs)
  • 計費模式:根據實際需求進行選擇。(我選擇的是按量計費)
  • 可用區:根據實際需求進行設定,預設為 “随機可用區”。(我這裡選擇廣州二區)
  • 雲盤類型:根據實際需求進行選擇。(我選擇的是普通盤)
  • 回收政策:根據實際需求進行選擇。(我選擇的是删除)

單擊【建立StorageClass】,完成建立。

TKE操作指南 - PV,PVC,StoragClass配置使用(十三)

至此,storageclass建立完成!

2.建立PVC通過界面挂載

2.1 建立PVC資料卷

TKE操作指南 - PV,PVC,StoragClass配置使用(十三)

展開存儲,選擇并點選PersitentVolumeClaim,點選建立。

TKE操作指南 - PV,PVC,StoragClass配置使用(十三)

輸入PVC資料卷名稱,輸入容量大小(我這裡填寫10G,用于測試),其他參數預設,點選建立PersitentVolumeClaim

TKE操作指南 - PV,PVC,StoragClass配置使用(十三)

至此PVC建立完成!

2.2 将資料卷挂載至wordpress容器

現在我要将建立10G的PVC卷挂載至wordpress nginx容器的/tmp目錄

界面方式挂載:

TKE操作指南 - PV,PVC,StoragClass配置使用(十三)

3.建立PVC通過yaml檔案挂載

現在我要将建立10G的PVC卷挂載至wordpress nginx容器的/tmp目錄

建立PVC流程,可參考步驟2中的2.1 建立PVC資料卷。

這裡需要登入node節點機器,執行

# kubectl get deployment //檢視deployment任務

# kubectl edit deployment wordpress //編輯deployment wordpress yaml檔案,進行參數添加

添加如下框選處的YAML檔案内容(pvc-data是之前建立PVC的名稱),然後執行:wq儲存退出即可。

TKE操作指南 - PV,PVC,StoragClass配置使用(十三)
TKE操作指南 - PV,PVC,StoragClass配置使用(十三)

修改後的wordpress yaml檔案内容如下:

apiVersion: apps/v1beta2
kind: Deployment
metadata:
  annotations:
    deployment.kubernetes.io/revision: "1"
  generation: 1
  labels:
    k8s-app: wordpress
    qcloud-app: wordpress
  name: wordpress
  namespace: default
spec:
  progressDeadlineSeconds: 600
  replicas: 1
  revisionHistoryLimit: 10
  selector:
    matchLabels:
      k8s-app: wordpress
      qcloud-app: wordpress
  strategy:
    rollingUpdate:
      maxSurge: 25%
      maxUnavailable: 25%
    type: RollingUpdate
  template:
    metadata:
      creationTimestamp: null
      labels:
        k8s-app: wordpress
        qcloud-app: wordpress
    spec:
      containers:
      - env:
        - name: PATH
          value: /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
        image: ccr.ccs.tencentyun.com/liangfeng/test:test-nginx-201908051618-d852fb93feb0e32c386f4facb8550a3e1e679066
        imagePullPolicy: IfNotPresent
        name: nginx
        resources:
          limits:
            cpu: 500m
            memory: 1Gi
          requests:
            cpu: 250m
            memory: 256Mi
        volumeMounts:
        - mountPath: /tmp
          name: data-volume
        terminationMessagePath: /dev/termination-log
        terminationMessagePolicy: File
      - env:
        - name: PATH
          value: /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
        image: ccr.ccs.tencentyun.com/liangfeng/test:test-php-201908051634-7ec4253f2f17431d387aadecbf3d2b79a690681f
        imagePullPolicy: IfNotPresent
        name: php
        resources:
          limits:
            cpu: 500m
            memory: 1Gi
          requests:
            cpu: 250m
            memory: 256Mi
        terminationMessagePath: /dev/termination-log
        terminationMessagePolicy: File
      dnsPolicy: ClusterFirst
      imagePullSecrets:
      - name: qcloudregistrykey
      - name: tencenthubkey
      restartPolicy: Always
      schedulerName: default-scheduler
      securityContext: {}
      terminationGracePeriodSeconds: 30
      volumes:
      - name: data-volume
        persistentVolumeClaim:
          claimName: pvc-data           

複制

4.驗證挂載成功

TKE操作指南 - PV,PVC,StoragClass配置使用(十三)
TKE操作指南 - PV,PVC,StoragClass配置使用(十三)

在pod管理中,點選遠端登入,再登入,此時進入nginx容器控制台,執行df -h,可以看到PVC 10G資料卷已挂載在/tmp目錄,挂載成功!