全部產品
Search
文件中心

Container Service for Kubernetes:記憶體資料卷

更新時間:Feb 28, 2024

ACK提供了記憶體臨時儲存卷,可以給Container Service提供高效能的本地臨時儲存。

背景資訊

  • 記憶體儲存卷是臨時儲存,Pod重啟後資料將會丟失。

  • 本機存放區適合於快取應用情境。

外掛程式部署

記憶體CSI外掛程式由兩個組件:Plugin(掛載記憶體卷)和Provisioner(建立記憶體卷)。

說明

該功能僅支援1.18及以下版本的ACK叢集。

部署CSI Memory Plugin

apiVersion: storage.k8s.io/v1beta1
kind: CSIDriver
metadata:
  name: memplugin.csi.alibabacloud.com
spec:
  attachRequired: false
  podInfoOnMount: true
---
kind: DaemonSet
apiVersion: apps/v1
metadata:
  name: csi-mem-plugin
  namespace: kube-system
spec:
  selector:
    matchLabels:
      app: csi-mem-plugin
  template:
    metadata:
      labels:
        app: csi-mem-plugin
    spec:
      tolerations:
        - operator: Exists
      serviceAccount: admin
      priorityClassName: system-node-critical
      hostNetwork: true
      hostPID: true
      containers:
        - name: driver-registrar
          image: registry.cn-hangzhou.aliyuncs.com/acs/csi-node-driver-registrar:v1.1.0
          imagePullPolicy: Always
          args:
            - "--v=5"
            - "--csi-address=/csi/csi.sock"
            - "--kubelet-registration-path=/var/lib/kubelet/plugins/memplugin.csi.alibabacloud.com/csi.sock"
          env:
            - name: KUBE_NODE_NAME
              valueFrom:
                fieldRef:
                  apiVersion: v1
                  fieldPath: spec.nodeName
          volumeMounts:
            - name: plugin-dir
              mountPath: /csi
            - name: registration-dir
              mountPath: /registration
        - name: csi-memplugin
          securityContext:
            privileged: true
            capabilities:
              add: ["SYS_ADMIN"]
            allowPrivilegeEscalation: true
          image: registry.cn-hangzhou.aliyuncs.com/acs/csi-plugin:v1.14.8.41-9efe2ede-aliyun
          imagePullPolicy: "Always"
          args :
            - "--endpoint=$(CSI_ENDPOINT)"
            - "--v=5"
            - "--nodeid=$(KUBE_NODE_NAME)"
            - "--driver=memplugin.csi.alibabacloud.com"
          env:
            - name: KUBE_NODE_NAME
              valueFrom:
                fieldRef:
                  apiVersion: v1
                  fieldPath: spec.nodeName
            - name: CSI_ENDPOINT
              value: unix://var/lib/kubelet/plugins/memplugin.csi.alibabacloud.com/csi.sock
          volumeMounts:
            - name: pods-mount-dir
              mountPath: /var/lib/kubelet
              mountPropagation: "Bidirectional"
            - mountPath: /dev
              mountPropagation: "HostToContainer"
              name: host-dev
            - mountPath: /var/log/
              name: host-log
      volumes:
        - name: plugin-dir
          hostPath:
            path: /var/lib/kubelet/plugins/memplugin.csi.alibabacloud.com
            type: DirectoryOrCreate
        - name: registration-dir
          hostPath:
            path: /var/lib/kubelet/plugins_registry
            type: DirectoryOrCreate
        - name: pods-mount-dir
          hostPath:
            path: /var/lib/kubelet
            type: Directory
        - name: host-dev
          hostPath:
            path: /dev
        - name: host-log
          hostPath:
            path: /var/log/
  updateStrategy:
    rollingUpdate:
      maxUnavailable: 10%
    type: RollingUpdate

部署CSI Memory Provisioner

kind: Deployment
apiVersion: apps/v1
metadata:
  name: csi-memprovisioner
  namespace: kube-system
spec:
  selector:
    matchLabels:
      app: csi-memprovisioner
  replicas: 2
  template:
    metadata:
      labels:
        app: csi-memprovisioner
    spec:
      tolerations:
      - operator: "Exists"
      affinity:
        nodeAffinity:
          preferredDuringSchedulingIgnoredDuringExecution:
          - weight: 1
            preference:
              matchExpressions:
              - key: node-role.kubernetes.io/master
                operator: Exists
      priorityClassName: system-node-critical
      serviceAccount: admin
      hostNetwork: true
      containers:
        - name: csi-provisioner
          image: registry.cn-hangzhou.aliyuncs.com/acs/csi-provisioner:v1.4.0-aliyun
          args:
            - "--provisioner=memplugin.csi.alibabacloud.com"
            - "--csi-address=$(ADDRESS)"
            - "--feature-gates=Topology=True"
            - "--volume-name-prefix=mem"
            - "--strict-topology=true"
            - "--timeout=150s"
            - "--enable-leader-election=true"
            - "--leader-election-type=leases"
            - "--retry-interval-start=500ms"
            - "--v=5"
          env:
            - name: ADDRESS
              value: /var/lib/kubelet/plugins/memplugin.csi.alibabacloud.com/csi.sock
          imagePullPolicy: "Always"
          volumeMounts:
            - name: socket-dir
              mountPath: /var/lib/kubelet/plugins/memplugin.csi.alibabacloud.com
      volumes:
        - name: socket-dir
          hostPath:
            path: /var/lib/kubelet/plugins/memplugin.csi.alibabacloud.com
            type: DirectoryOrCreate

使用記憶體資料卷樣本

  1. 使用以下範本部署StorageClass。

    apiVersion: storage.k8s.io/v1
    kind: StorageClass
    metadata:
       name: csi-mem
    provisioner: memplugin.csi.alibabacloud.com
    reclaimPolicy: Delete
  2. 使用以下模板建立PVC與應用。

    apiVersion: v1
    kind: PersistentVolumeClaim
    metadata:
      name: mem-pvc
    spec:
      accessModes:
      - ReadWriteOnce
      resources:
        requests:
          storage: 2Gi
      storageClassName: csi-mem
      ---
      apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: deployment-mem
      labels:
        app: nginx
    spec:
      selector:
        matchLabels:
          app: nginx
      template:
        metadata:
          labels:
            app: nginx
        spec:
          containers:
          - name: nginx
            image: nginx:1.7.9
            command: ["sh", "-c"]
            args: ["sleep 10000"]
            volumeMounts:
              - name: mem-pvc
                mountPath: "/data"
          volumes:
            - name: mem-pvc
              persistentVolumeClaim:
                claimName: mem-pvc