全部文档
当前文档

暂无内容

如果没有找到您期望的内容,请尝试其他搜索词

文档中心

在容器环境中部署独立缓存集群

最近更新时间:2025-01-03 15:19:15

KPFS支持构建分布式缓存,但Kubernetes 容器往往是「转瞬即逝」的,会导致缓存组成员不断更替,使得缓存利用率降低。本文介绍如何在Kuberenetes集群中部署独立缓存集群,在Kuberenetes集群中部署一个稳定的缓存集群,以优化该场景下的缓存利用率。您可以参考以下示范,在集群内指定的节点挂载KPFS客户端,形成一个稳定的缓存组。

在开始阅读本文前,您需要先了解分布式缓存的使用流程。

DaemonSet 方式部署

apiVersion: apps/v1
kind: DaemonSet
metadata:
  # 名称、命名空间可自定义
  name: juicefs-cache-group
  namespace: default
spec:
  selector:
    matchLabels:
      app: juicefs-cache-group
      juicefs-role: cache
  updateStrategy:
    type: RollingUpdate
    rollingUpdate:
      maxUnavailable: 1
  template:
    metadata:
      labels:
        app: juicefs-cache-group
        juicefs-role: cache
    spec:
      # 使用 hostNetwork,让 Pod 以固定 IP 运行,避免容器重建更换 IP,导致缓存数据失效
      hostNetwork: true
      containers:
      - name: juicefs-cache
        command:
        - sh
        - -c
        - |
          for keyval in $(echo $ENVS | sed -e 's/": "/=/g' -e 's/{"//g' -e 's/", "/ /g' -e 's/"}//g' ); do
            echo "export $keyval"
            eval export $keyval
          done

          # 认证和挂载,所有环境变量均引用包含着文件系统认证信息的 Kubernetes Secret
          # 参考文档:https://docs.ksyun.com/documents/43899
          /usr/bin/juicefs auth --token=${TOKEN} --access-key=${ACCESS_KEY} --secret-key=${SECRET_KEY} ${VOL_NAME}

          # 由于在容器中常驻,必须用 --foreground 模式运行,其它挂载选项(特别是 --cache-group)按照实际情况调整
          # 参考文档:https://docs.ksyun.com/documents/43914(mount命令部分)
          /usr/bin/juicefs mount $VOL_NAME /mnt/jfs --foreground --cache-dir=/data/jfsCache --cache-size=512000 --cache-group=jfscache
        env:
        # 存放文件系统认证信息的 Secret,必须和该 StatefulSet 在同一个命名空间下
        # 参考文档:https://docs.ksyun.com/documents/43982(创建文件系统认证信息部分)
        - name: VOL_NAME
          valueFrom:
            secretKeyRef:
              key: name
              name: juicefs-secret
        - name: ACCESS_KEY
          valueFrom:
            secretKeyRef:
              key: access-key
              name: juicefs-secret
        - name: SECRET_KEY
          valueFrom:
            secretKeyRef:
              key: secret-key
              name: juicefs-secret
        - name: TOKEN
          valueFrom:
            secretKeyRef:
              key: token
              name: juicefs-secret
        - name: ENVS
          valueFrom:
            secretKeyRef:
              key: envs
              name: juicefs-secret
        # 使用 Mount Pod 的容器镜像
        # 参考文档:https://juicefs.com/docs/zh/csi/guide/custom-image
        image: juicedata/mount:ee-5.0.21-317356c
        lifecycle:
          # 容器退出时卸载文件系统
          preStop:
            exec:
              command:
              - sh
              - -c
              - umount /mnt/jfs
        # 按照实际情况调整资源请求和约束,可以详读https://kubernetes.io/zh-cn/docs/concepts/configuration/manage-resources-containers/
        resources:
          requests:
            cpu: 4
            memory: 10Gi
        # 挂载文件系统必须启用的权限
        securityContext:
          privileged: true
        volumeMounts:
        - mountPath: /data/jfsCache
          name: cache-dir
        - mountPath: /root/.juicefs
          name: jfs-root-dir
      volumes:
      # 调整缓存目录的路径,如有多个缓存目录需要定义多个 volume
      # 参考文档:https://docs.ksyun.com/documents/43911(客户端本地缓存部分)
      - name: cache-dir
        hostPath:
          path: /data/jfsCache
          type: DirectoryOrCreate
      - name: jfs-root-dir
        emptyDir: {}

使用缓存组

上方示范便是在集群中启动了 独立缓存集群,其缓存组名为 jfscache,那么为了让应用程序的客户端使用该缓存集群,需要让他们一并加入这个缓存组,并额外添加 --no-sharing 这个挂载参数,这样一来,应用程序的客户端虽然加入了缓存组,但却不参与缓存数据的构建,避免了客户端频繁创建、销毁所导致的缓存数据不稳定。

以动态配置为例,按照下方示范修改挂载参数即可。

apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
  name: juicefs-sc
provisioner: csi.juicefs.com
parameters:
  csi.storage.k8s.io/provisioner-secret-name: juicefs-secret
  csi.storage.k8s.io/provisioner-secret-namespace: default
  csi.storage.k8s.io/node-publish-secret-name: juicefs-secret
  csi.storage.k8s.io/node-publish-secret-namespace: default
mountOptions:
  ...
  - cache-group=jfscache
  - no-sharing

纯净模式常规模式

纯净模式

点击可全屏预览文档内容