All Products
Search
Document Center

Container Service for Kubernetes:Use memory volumes

Last Updated:Apr 23, 2024

Alibaba Cloud Container Service for Kubernetes (ACK) provides memory volumes for high-performance temporary storage.

Background information

  • Memory volumes are temporary storage medium. Data in memory volumes is lost after a pod that is mounted with the memory volumes restarts.

  • Memory volumes are applicable to scenarios where fast caching is required.

Deploy the CSI plug-in for memory volumes

The CSI plug-in for memory volumes consists of Plugin and Provisioner. Plugin is used to mount memory volumes. Provisioner is used to create memory volumes.

Note

Only ACK clusters that run Kubernetes 1.18 and earlier support this feature.

Deploy CSI-Plugin for memory volumes

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

Deploy CSI-Provisioner for memory volumes

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

Use memory volumes

  1. Use the following template to create a StorageClass:

    apiVersion: storage.k8s.io/v1
    kind: StorageClass
    metadata:
       name: csi-mem
    provisioner: memplugin.csi.alibabacloud.com
    reclaimPolicy: Delete
  2. Use the following template to create a persistent volume claim (PVC) and application:

    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