文档

内存数据卷

更新时间:

ACK提供了内存临时存储卷,可以给容器服务提供高性能的本地临时存储。

背景信息

  • 内存存储卷是临时存储,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